After updating Keras to 2.2.3 and using Tensorflow version 1.11.0, I am unable to load saved models.
I am running a script on Google Cloud's Debian machine and am able to save and load the model there, but I am facing the error "UnboundLocalError: local variable 'name' referenced before assignment" when calling load_model on my local Ubuntu machine.
Jesus, you hit my production with this release. Rolling back.
The same with TensorFlow 1.10 (Python 3.6).
Linking to #11269
We would like to work on this, but we cannot reproduce the bug. Can anyone give us a script to reproduce?
I'll close this in favor of #11269
@gabrieldemarmiesse I'm not sure it's a good idea to assume it's the same error.
For now I can only provide a traceback, but it seems to explain everything:
model = load_model(model_name)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/usr/local/lib/python3.6/dist-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.6/dist-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.6/dist-packages/keras/engine/sequential.py", line 294, in from_config
model = cls(name=name)
UnboundLocalError: local variable 'name' referenced before assignment
@adampl maybe I was too quick on closing this issue.
You have to understand that it's very difficult to work with a traceback only.
To give you an idea of what we work with, here are all the tests which are run at every commit in keras: https://github.com/keras-team/keras/tree/master/tests
Among those tests are a lot of tests concerning saving and loading models.
So our tests are passing, all green, and users are telling us that saving and loading doesn't work. It means that users are working with use cases which are not in the tests, thus it is non-trivial to reproduce. Without a script to reproduce, there is very little that we can do.
Furthermore, we can put your script in the test, and then we'll make sure at every commit that your script never breaks again :)
Some advices:
I said it's obvious from the traceback, because there's a typical error at keras/engine/sequential.py:294:
@classmethod
def from_config(cls, config, custom_objects=None):
if 'name' in config:
name = config['name']
build_input_shape = config.get('build_input_shape')
layer_configs = config['layers']
model = cls(name=name) # <--- HERE
for conf in layer_configs:
layer = layer_module.deserialize(conf,
custom_objects=custom_objects)
model.add(layer)
if not model.inputs and build_input_shape:
model.build(build_input_shape)
return model
From the check for name I guess this parameter is optional, so it shouldn't be used when it's not defined. The same applies to build_input_shape and layer_configs variables, which would raise similar errors in the next lines. So apparently the tests don't cover configs without a name.
However, I have no idea if the model should have this parameter nor why it doesn't :)
So check for is None should be added for name, build_input_shape and layer_configs variables
Ran into the same issue with my stored model.
We cannot fix a bug without adding a unit test in the same pull request. This is because there might be further bugs down the road, and the name referenced before assignment might be only one of them.
We cannot know if we really fixed the bug until we have a unit test which will tell us that everything is fine and working.
I'll flag this as bug for visibility.
So check for
is Noneshould be added for name, build_input_shape and layer_configs variables
Nope, this check would itself reference the undefined variable, throwing the same error. The correct solution is to assign these variables some default values before if, or to rewrite the code so that nonexistent variables would never be referenced. The first is better IMHO.
I can confirm that this occurs when trying to load in 2.2.3 models saved with an older Keras version (ie. 2.2.0 in my case) on a Sequential model. Could this be related to the following change in 2.2.3?
Modify the return value of
Sequential.get_config(). Previously, the return value was a list of the config dictionaries of the layers of the model. Now, the return value is a dictionary with keyslayers,name, and an optional keybuild_input_shape. The old config is equivalent tonew_config['layers']. This makes the output ofget_configconsistent across all model classes.
If so, considering there might be people with many previously saved models, a method to use these should probably be provided, or the documentation should emphasize the incompatibility.
Here is a bash script you can run in a virtualenv to replicate the issue:
#!/bin/bash
cat > script.py << EOF
import keras
def main():
if keras.__version__ == '2.2.0':
# create model
model = keras.models.Sequential()
model.add(keras.layers.Dense(2, input_shape=(10,))) # just so we can save
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam())
model.save('./model.h5', overwrite=True)
if keras.__version__ == '2.2.3':
model = keras.models.load_model('./model.h5')
if __name__ == '__main__':
main()
EOF
pip install keras==2.2.0
python script.py
pip install keras==2.2.3
python script.py
Can somebody try master and see if it works? I think this issue has been fixed here: #11280
Linking to #11269
We would like to work on this, but we cannot reproduce the bug. Can anyone give us a script to reproduce?
@gabrieldemarmiesse Here it is!
Source Code + Sample data:
https://www.google.com/url?q=http://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiNDc2ODQyOSIsImRlbGl2ZXJ5X2lkIjoiMzgxMTIzNDc2MiIsInVybCI6Imh0dHA6Ly9weWltZy5jby9tYnZkaz9fX3M9Y2d1ZDVodnhuc2I2NGlobnN4d2YifQ&source=gmail&ust=1538709027986000&usg=AFQjCNG8-4xIpXI8DKv5YZQgHiRAl91ENA
Instructions to run it :
https://www.pyimagesearch.com/2018/09/10/keras-tutorial-how-to-get-started-with-keras-deep-learning-and-python/
Error obtained (bug):
(env) (base) C:WindowsSystem32envScripts>python C:UserspuneethDownloadskeras-tutorialkeras-tutorial/predict.py --image C:UserspuneethDownloadskeras-tutorialkeras-tutorial/images/panda.jpg --model C:UserspuneethDownloadskeras-tutorialkeras-tutorial/output/smallvggnet.model --label-bin C:UserspuneethDownloadskeras-tutorialkeras-tutorial/output/smallvggnet_lb.pickle --width 64 --height 64
Using TensorFlow backend.
[INFO] loading network and label binarizer...
Traceback (most recent call last):
File "C:UserspuneethDownloadskeras-tutorialkeras-tutorial/predict.py", line 46, in
model = load_model(args["model"])
File "C:WindowsSystem32envlibsite-packageskerasenginesaving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "C:WindowsSystem32envlibsite-packageskerasenginesaving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:WindowsSystem32envlibsite-packageskerasenginesaving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:WindowsSystem32envlibsite-packageskeraslayers__init__.py", line 55, in deserialize
printable_module_name='layer')
File "C:WindowsSystem32envlibsite-packageskerasutilsgeneric_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "C:WindowsSystem32envlibsite-packageskerasenginesequential.py", line 294, in from_config
model = cls(name=name)
UnboundLocalError: local variable 'name' referenced before assignment
(env) (base) C:WindowsSystem32envScripts>
Keras 2.2.4 is available on pypi. The bug should be fixed. Can anyone confirm?
Keras 2.2.4 is available on pypi. The bug should be fixed. Can anyone confirm?
@gabrieldemarmiesse The bug is FIXED! Thanks... Here's the output
``
(env) (base) C:WindowsSystem32envScripts>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import keras
Using TensorFlow backend.
keras.__version__
'2.2.4'
^Z
(env) (base) C:WindowsSystem32envScripts>python C:UserspuneethDownloadskeras-tutorialkeras-tutorial/predict.py --image C:UserspuneethDownloadskeras-tutorialkeras-tutorial/images/panda.jpg --model C:UserspuneethDownloadskeras-tutorialkeras-tutorial/output/smallvggnet.model --label-bin C:UserspuneethDownloadskeras-tutorialkeras-tutorial/output/smallvggnet_lb.pickle --width 64 --height 64
Using TensorFlow backend.
[INFO] loading network and label binarizer...
2018-10-05 09:16:59.857450: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
C:WindowsSystem32envlibsite-packagessklearnbase.py:251: UserWarning: Trying to unpickle estimator LabelBinarizer from version 0.19.1 when using version 0.20.0. This might lead to breaking code or invalid results. Use at your own risk.
UserWarning)
c:userspuneethdownloadskeras-tutorialkeras-tutorialpredict.py(50)
()
-> preds = model.predict(image)
(Pdb) n
c:userspuneethdownloadskeras-tutorialkeras-tutorialpredict.py(54)()
-> i = preds.argmax(axis=1)[0]
(Pdb)
c:userspuneethdownloadskeras-tutorialkeras-tutorialpredict.py(55)()
-> label = lb.classes_[i]
(Pdb)
c:userspuneethdownloadskeras-tutorialkeras-tutorialpredict.py(58)()
-> text = "{}: {:.2f}%".format(label, preds[0][i] * 100)
(Pdb)
c:userspuneethdownloadskeras-tutorialkeras-tutorialpredict.py(59)()
-> cv2.putText(output, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(Pdb)
c:userspuneethdownloadskeras-tutorialkeras-tutorialpredict.py(60)()
-> (0, 0, 255), 2)
(Pdb)
c:userspuneethdownloadskeras-tutorialkeras-tutorialpredict.py(63)()
-> cv2.imshow("Image", output)
(Pdb)
c:userspuneethdownloadskeras-tutorialkeras-tutorialpredict.py(64)()
-> cv2.waitKey(0)
(Pdb)
``
Thanks everyone!
Most helpful comment
Here is a bash script you can run in a virtualenv to replicate the issue: