UnboundLocalError: local variable ‘name’ referenced before assignment

UnboundLocalError: local variable ‘name’ referenced before assignment

When I was loading a saved keras model, I was getting an error: UnboundLocalError: local variable ‘name’ referenced before assignment. I had saved a model in Keras using:

classifier.save(save_name)

when I was trying to load the model, using:

classifier = keras.models.load_model('keras_v2_model.h5')

I was getting error:

Using TensorFlow backend.
Traceback (most recent call last):
File "predict_single.py", line 12, in <module>
classifier = keras.models.load_model('keras_v2_model.h5')
File "/home/azam/.virtualenvs/py3/lib/python3.5/site-packages/keras/engine/saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "/home/azam/.virtualenvs/py3/lib/python3.5/site-packages/keras/engine/saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/azam/.virtualenvs/py3/lib/python3.5/site-packages/keras/engine/saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/home/azam/.virtualenvs/py3/lib/python3.5/site-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/home/azam/.virtualenvs/py3/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/home/azam/.virtualenvs/py3/lib/python3.5/site-packages/keras/engine/sequential.py", line 294, in from_config
model = cls(name=name)
UnboundLocalError: local variable 'name' referenced before assignment

I was using keras version 2.2.3 (latest), and I was also getting same output for all images, my code to predict the classifier on a test image is:

import keras.models
from keras.models import Model
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
from keras.preprocessing import image
import tensorflow as tfwith tf.device('/cpu:0'):
classifier = keras.models.load_model('keras_v2_model.h5')

test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)

result = classifier.predict(test_image)
print(result)
if result[0][0] == 1:

prediction = 'dog'

else:

prediction = 'cat'

print(prediction)

no matter what image I feed to the network to predict from a saved model, I was always getting [[1.]] output.

Advertisements

Solution:

change Keras version. I downgraded to 2.2.2 using:

pip install keras==2.2.2


Collecting keras==2.2.2
Downloading https://files.pythonhosted.org/packages/34/7d/b1dedde8af99bd82f20ed7e9697aac0597de3049b1f786aa2aac3b9bd4da/Keras-2.2.2-py2.py3-none-any.whl (299kB)
100% |████████████████████████████████| 307kB 6.0MB/s
Requirement already satisfied: h5py in /home/azam/.virtualenvs/py3/lib/python3.5/site-packages (from keras==2.2.2) (2.8.0)
Collecting keras-applications==1.0.4 (from keras==2.2.2)
Downloading https://files.pythonhosted.org/packages/54/90/8f327deaa37a71caddb59b7b4aaa9d4b3e90c0e76f8c2d1572005278ddc5/Keras_Applications-1.0.4-py2.py3-none-any.whl (43kB)
100% |████████████████████████████████| 51kB 4.2MB/s
Requirement already satisfied: scipy>=0.14 in /home/azam/.virtualenvs/py3/lib/python3.5/site-packages (from keras==2.2.2) (1.1.0)
Requirement already satisfied: six>=1.9.0 in /home/azam/.virtualenvs/py3/lib/python3.5/site-packages (from keras==2.2.2) (1.11.0)
Requirement already satisfied: pyyaml in /home/azam/.virtualenvs/py3/lib/python3.5/site-packages (from keras==2.2.2) (3.13)
Requirement already satisfied: numpy>=1.9.1 in /home/azam/.virtualenvs/py3/lib/python3.5/site-packages (from keras==2.2.2) (1.15.2)
Collecting keras-preprocessing==1.0.2 (from keras==2.2.2)
Downloading https://files.pythonhosted.org/packages/71/26/1e778ebd737032749824d5cba7dbd3b0cf9234b87ab5ec79f5f0403ca7e9/Keras_Preprocessing-1.0.2-py2.py3-none-any.whl
tensorflow 1.11.0rc1 has requirement keras-applications>=1.0.6, but you'll have keras-applications 1.0.4 which is incompatible.
tensorflow 1.11.0rc1 has requirement keras-preprocessing>=1.0.5, but you'll have keras-preprocessing 1.0.2 which is incompatible.
Installing collected packages: keras-applications, keras-preprocessing, keras
Found existing installation: Keras-Applications 1.0.6
Uninstalling Keras-Applications-1.0.6:
Successfully uninstalled Keras-Applications-1.0.6
Found existing installation: Keras-Preprocessing 1.0.5
Uninstalling Keras-Preprocessing-1.0.5:
Successfully uninstalled Keras-Preprocessing-1.0.5
Found existing installation: Keras 2.2.3
Uninstalling Keras-2.2.3:
Successfully uninstalled Keras-2.2.3
Successfully installed keras-2.2.2 keras-applications-1.0.4 keras-preprocessing-1.0.2

 

Now after executing:

python predict_single.py

Using TensorFlow backend.

[[0.]]
cat

 

 

Related Links:

Leave a Reply

Your email address will not be published.