question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'

See original GitHub issue

AutoKeras version: master KerasTuner version: master TensorFlow version: 2.2.0

I am running a multi label image classifier based on the example code here: https://autokeras.com/tutorial/image_classification/

I was able to get the example code working with these dependency versions.

Now I am trying to modify the code to run on my own dataset. I have loaded in my own images and labels. I have set multi_label=True since my dataset can have more than one label per image.

My process seems to be pretty standard. After loading in the data and labels, I transform them into numpy arrays, then use MultiLabelBinarizer to pre-process, split into test and train sets, and run the image classiifer. Code is here:

data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)

# binarize the labels using scikit-learn's special multi-label
# binarizer implementation
print("[INFO] class labels:")
mlb = MultiLabelBinarizer()
labels = mlb.fit_transform(labels)
# loop over each of the possible class labels and show them
for (i, label) in enumerate(mlb.classes_):
    print("{}. {}".format(i + 1, label))

(x_train, x_test, y_train, y_test) = train_test_split(data, labels, test_size=0.2, random_state=42)
print(x_train.shape)  # (60000, 28, 28)
print(y_train.shape)  # (60000,)
print(y_train[:3])  # array([7, 2, 1], dtype=uint8)

Afterwards, code is straight from the example, except setting multi_label=True:

# Initialize the image classifier.
clf = ak.ImageClassifier(max_trials=3, multi_label=True)  # It tries 10 different models.
# Feed the image classifier with training data.
clf.fit(x_train, y_train, epochs=3)


# Predict with the best model.
predicted_y = clf.predict(x_test)
print(predicted_y)


# Evaluate the best model with testing data.
print(clf.evaluate(x_test, y_test))

The code runs fine for a few trials. Then, I get this error:

KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'
[Warning] Invalid model 0/5

Here is the full stack trace:

Starting new trial
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/graph.py", line 235, in build
    outputs = block.build(hp, inputs=temp_inputs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/heads.py", line 82, in build
    output_node = reduction.SpatialReduction().build(hp, output_node)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/reduction.py", line 106, in build
    default='global_avg')
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 691, in Choice
    return self._retrieve(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 619, in _retrieve
    return self.values[hp.name]
KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'
[Warning] Invalid model 0/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/graph.py", line 235, in build
    outputs = block.build(hp, inputs=temp_inputs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/heads.py", line 82, in build
    output_node = reduction.SpatialReduction().build(hp, output_node)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/reduction.py", line 106, in build
    default='global_avg')
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 691, in Choice
    return self._retrieve(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 619, in _retrieve
    return self.values[hp.name]
KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'
[Warning] Invalid model 1/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/graph.py", line 235, in build
    outputs = block.build(hp, inputs=temp_inputs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/heads.py", line 82, in build
    output_node = reduction.SpatialReduction().build(hp, output_node)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/reduction.py", line 106, in build
    default='global_avg')
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 691, in Choice
    return self._retrieve(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 619, in _retrieve
    return self.values[hp.name]
KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'
[Warning] Invalid model 2/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/graph.py", line 235, in build
    outputs = block.build(hp, inputs=temp_inputs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/heads.py", line 82, in build
    output_node = reduction.SpatialReduction().build(hp, output_node)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/reduction.py", line 106, in build
    default='global_avg')
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 691, in Choice
    return self._retrieve(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 619, in _retrieve
    return self.values[hp.name]
KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'
[Warning] Invalid model 3/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/graph.py", line 235, in build
    outputs = block.build(hp, inputs=temp_inputs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/heads.py", line 82, in build
    output_node = reduction.SpatialReduction().build(hp, output_node)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/reduction.py", line 106, in build
    default='global_avg')
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 691, in Choice
    return self._retrieve(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 619, in _retrieve
    return self.values[hp.name]
KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'
[Warning] Invalid model 4/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/graph.py", line 235, in build
    outputs = block.build(hp, inputs=temp_inputs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/heads.py", line 82, in build
    output_node = reduction.SpatialReduction().build(hp, output_node)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/engine/block.py", line 34, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/blocks/reduction.py", line 106, in build
    default='global_avg')
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 691, in Choice
    return self._retrieve(hp)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hyperparameters.py", line 619, in _retrieve
    return self.values[hp.name]
KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'
[Warning] Invalid model 5/5
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)
    104                 with maybe_distribute(self.distribution_strategy):
--> 105                     model = self.hypermodel.build(hp)
    106             except:

17 frames
KeyError: 'classification_head_1/spatial_reduction_1/reduction_type'

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)
    113                 if i == self._max_fail_streak:
    114                     raise RuntimeError(
--> 115                         'Too many failed attempts to build model.')
    116                 continue
    117 

RuntimeError: Too many failed attempts to build model.

How can I resolve this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dahlocommented, Jul 19, 2020

@castrovictor Sure thing. I’m running using the following Dockerfile:

FROM nvidia/cuda:10.1-cudnn7-devel-ubuntu18.04

# install apt packages
RUN apt update && apt upgrade -y && apt install -y \
        curl \
        git \
        python3 \
        python3-pip \
        vim \
        wget

# install pip packages
RUN pip3 install --no-cache-dir -U pip
RUN pip3 install --no-cache-dir -U setuptools six
RUN pip3 install --no-cache-dir \
        scipy==1.4.1 \
        tensorflow-gpu==2.2.0 \
        git+https://github.com/keras-team/autokeras.git@7db2afd \
        git+https://github.com/keras-team/keras-tuner.git@fa84eb2

# clean the cache
RUN rm -rf /var/lib/apt/lists/* && rm -rf ~/.cache/pip/*

Running pip3 freeze inside the container gives the following:

$ pip3 freeze 
absl-py==0.9.0
asn1crypto==0.24.0
astunparse==1.6.3
autokeras @ git+https://github.com/keras-team/autokeras.git@7db2afde4af83ca113841a979e218d775a04eb40
backcall==0.2.0
cachetools==4.1.1
certifi==2020.6.20
chardet==3.0.4
colorama==0.4.3
cryptography==2.1.4
decorator==4.4.2
future==0.18.2
gast==0.3.3
google-auth==1.19.2
google-auth-oauthlib==0.4.1
google-pasta==0.2.0
grpcio==1.30.0
h5py==2.10.0
idna==2.6
importlib-metadata==1.7.0
ipython==7.16.1
ipython-genutils==0.2.0
jedi==0.17.2
joblib==0.16.0
Keras-Preprocessing==1.1.2
keras-tuner @ git+https://github.com/keras-team/keras-tuner.git@fa84eb29446f6234aea243a73cef85de4bbdb642
keyring==10.6.0
keyrings.alt==3.0
Markdown==3.2.2
numpy==1.19.0
oauthlib==3.1.0
opt-einsum==3.2.1
packaging==20.4
pandas==1.0.5
parso==0.7.0
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.5
protobuf==3.12.2
ptyprocess==0.6.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycrypto==2.6.1
Pygments==2.6.1
pygobject==3.26.1
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2020.1
pyxdg==0.25
requests==2.24.0
requests-oauthlib==1.3.0
rsa==4.6
scikit-learn==0.23.1
scipy==1.4.1
SecretStorage==2.3.1
six==1.15.0
tabulate==0.8.7
tensorboard==2.2.2
tensorboard-plugin-wit==1.7.0
tensorflow==2.2.0
tensorflow-estimator==2.2.0
tensorflow-gpu==2.2.0
termcolor==1.1.0
terminaltables==3.1.0
threadpoolctl==2.1.0
tqdm==4.48.0
traitlets==4.3.3
urllib3==1.25.9
wcwidth==0.2.5
Werkzeug==1.0.1
wrapt==1.12.1
zipp==3.1.0

and python version:

$ python3 --version
Python 3.6.9
0reactions
castrovictorcommented, Jul 19, 2020

@dahlo would you mind posting your configuration? Installing the versions you mention I get this error:

ImportError: dlopen: cannot load any more object with static TLS


Contents of /mnt/sdd/vicastro/envs/ak4/lib/python3.8/site-packages/sklearn/__check_build: pycache setup.py _check_build.cpython-38-x86_64-linux-gnu.so init.py


It seems that scikit-learn has not been built correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python KeyError when using pandas - Stack Overflow
I'm following a tutorial on NLP but have encountered a key error error when trying to group my raw data into good and...
Read more >
What a Python KeyError Usually Means
Python's KeyError exception is a common exception encountered by beginners. Knowing why a KeyError can be raised and some solutions to ...
Read more >
KeyError Pandas – How To Fix - Data Independent
Pandas KeyError - This annoying error means that Pandas can not find your column name in your dataframe. Here's how to fix this...
Read more >
Fixing KeyError Exceptions in Python - YouTube
When you encounter a KeyError, there are a few standard ways to handle it. Depending on your use case, some of these solutions...
Read more >
KeyError Pandas: How To Fix - YouTube
Pandas KeyError is frustrating. This error happens because Pandas cannot find what you're looking ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found