keras: load_model fails to load optimizer
Please make sure that the boxes below are checked before you submit your issue. Thank you!
- Check that you are up-to-date with the master branch of Keras. You can update with: pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
- If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with: pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
- Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
Not sure what the specific cause is yet and how to isolate it, but I’m having some issues using the load_model
method. I get the below error complaining about weight shape for the optimizer.
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-7-946052120828> in <module>()
7
8 if args.model_js == "":
----> 9 model = load_model(args.model_h5, custom_layer_dict)
10 else:
11 model = model_from_json(open(args.model_js).read(), custom_layer_dict)
/usr/local/lib/python2.7/dist-packages/keras/models.pyc in load_model(filepath, custom_objects)
165 optimizer_weight_names = [n.decode('utf8') for n in optimizer_weights_group.attrs['weight_names']]
166 optimizer_weight_values = [optimizer_weights_group[n] for n in optimizer_weight_names]
--> 167 model.optimizer.set_weights(optimizer_weight_values)
168 f.close()
169 return model
/usr/local/lib/python2.7/dist-packages/keras/optimizers.pyc in set_weights(self, weights)
95 str(pv.shape) +
96 ' not compatible with '
---> 97 'provided weight shape ' + str(w.shape))
98 weight_value_tuples.append((p, w))
99 K.batch_set_value(weight_value_tuples)
Exception: Optimizer weight shape (11, 1, 188, 208) not compatible with provided weight shape (208, 188, 11, 1)
image ordering does not impact this. The model has some 1D convolutions, fully connected layers, merging, and some custom layers. If I instead use model_from_json
and them call load_weights
then no problem occurs.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 3
- Comments: 20 (5 by maintainers)
I am afraid, this issue is not solved, and should be re-opened, as it still occurs in the latest Keras version.
Closing since the issue is fixed with the current version.
@tstandley https://keras.io/backend/
clear_session() Destroys the current TF graph and creates a new one. Useful to avoid clutter from old models / layers.
I solved the issue by compelling congruency between the versions of Keras installed on my different machines.
It makes sense to think working with the same model across different versions of the library could engender hiccups due to discrepancies in convention employed in said versions.
First I looked up what versions were installed on my different machines (Guide here: https://stackoverflow.com/a/10215100/2661720),
and for the machine on which I was loading the model, I overwrote the existing Keras version with that installed on the model’s parent machine (Guide here: https://stackoverflow.com/a/33812968/2661720).
This was the thought process that sublimated the problem for me.
I solved the issue by making sure that the exact version and subversion of the environment that creates and loads the model are the same. Ex. Im creating and training the model with keras 2.0.3, the VM that loads it has to run keras 2.0.3 as well, 2.0.0 won’t work.
I installed HDFview and opened the hdf5 file and deleted the optimizer part of the file. Now I can load the model and it works like a charm ! However, I think it is a bug and should get fixed.
I solved the problem by using load_weights function is keras.model. My solution is at issue #4044 . Maybe it’s useful for you.