Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't load_model with error “Optimizer weight shape (256, 32) not compatible with provided weight shape (4,)” #4044

Closed
kuixu opened this issue Oct 13, 2016 · 28 comments

Comments

@kuixu
Copy link

kuixu commented Oct 13, 2016

I have 3 trained model file.

  1. left_branch.h5
  2. right_branch.h5
  3. concat.h5

The model concat.h5 is fine-tuned by concatenating from the two pre-trained model as the initial model(left_branch.h5, right_branch.h5).
While left_branch.h5 and right_branch.h5 model file can be load by function keras.models.load_model(),
but I load the trained concat.h5 formatted model file, I get the error blew.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 167, in load_model
    model.optimizer.set_weights(optimizer_weight_values)
  File "/usr/local/lib/python2.7/dist-packages/keras/optimizers.py", line 97, in set_weights
    'provided weight shape ' + str(w.shape))
Exception: Optimizer weight shape (256, 32) not compatible with provided weight shape (4,)
@kuixu kuixu changed the title Can't load_model Can't load_model with Optimizer weight shape (256, 32) not compatible with provided weight shape (4,) Oct 13, 2016
@kuixu kuixu changed the title Can't load_model with Optimizer weight shape (256, 32) not compatible with provided weight shape (4,) Can't load_model with “Optimizer weight shape (256, 32) not compatible with provided weight shape (4,)” Oct 13, 2016
@kuixu kuixu changed the title Can't load_model with “Optimizer weight shape (256, 32) not compatible with provided weight shape (4,)” Can't load_model with error “Optimizer weight shape (256, 32) not compatible with provided weight shape (4,)” Oct 13, 2016
@xisnu
Copy link

xisnu commented Oct 13, 2016

Similar problem is mentioned in issue #3964. I tried hadikazemi solution. It worked for me.

@kuixu
Copy link
Author

kuixu commented Oct 19, 2016

@xisnu Thanks for your advice! I trained many models and saved into many files, so it is laborious to delete the optimiser_weight manualy which advised by issue #3964.
Finally, I solved it by a using load_weights, as follows:

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model into h5 file")

# later...

# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

# evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
score = loaded_model.evaluate(X, Y, verbose=0)

@Viachaslau-Patsepnia
Copy link

Viachaslau-Patsepnia commented Oct 28, 2016

In my case, the problem was that on one machine everything was fine, on another I got "Optimizer weight shape ... not compatible with provided weight shape ..." exception.

The problem turned out to be in Keras installed using different methods on different machines.

Although it had the same version 1.1.0 on both the machine that built the model and the machine that loaded the model, the files in /usr/local/lib/python2.7/dist-packages/keras/ were different (in particular, /usr/local/lib/python2.7/dist-packages/keras/models.py).

Uninstalling and re-installing Keras on the problematic machine, solved the error, since now files in /usr/local/lib/python2.7/dist-packages/keras/ on both the machine that built the model and the machine that loaded the model were identical (to what I had on the machine that built the model).

@mmishuro
Copy link

Confirming this issue with loading models stored after train. Issue is with weights shapes; it helps to remove optimizer from the model file. Then it loads successfully. I updated Keras to recent 1.1.0, and use Python 2.7.12 from Anaconda 4.0.0 but it does not help. I also found that if you load model in Spyder 2 times, then second load if fine. Seems to be really cool if the issue is fixed. Thanks

@yukoba
Copy link
Contributor

yukoba commented Nov 8, 2016

@fsonntag
I think the problem comes from 028aae1

If you do not use weights.sort(key=lambda x: x.name),
weights order will be different from when it is saved.
weights.sort(key=lambda x: x.auto_name) should not be used.

All the weights should have proper names and their order should be stable.

@fsonntag
Copy link
Contributor

fsonntag commented Nov 8, 2016

Sorry, I don't really get your point. So you're saying, name should always be set in order to solve this issue? Since Theano doesn't set name, I introduced sorting by auto_name. Alternatively one can copy the auto_name attributes to name.

@yukoba
Copy link
Contributor

yukoba commented Nov 8, 2016

Since Theano doesn't set name

@fsonntag
Is it correct???
I think Theano has both name and auto_name.
I believe you do not need to use auto_name.

https://github.com/fchollet/keras/blob/master/keras/backend/theano_backend.py#L67
https://github.com/Theano/Theano/blob/master/theano/gof/graph.py#L387

@fsonntag
Copy link
Contributor

fsonntag commented Nov 8, 2016

Yes, Theano variables have a name property, but it is set to None when trying to order the weights. That was part of the fix in 028aae1. As name was None under Theano, the sorting didn't work. Under Python 2, no error was raised. It was just ignored and not sorted. Python 3 raised an error, that's why I discovered that issue.

@yukoba
Copy link
Contributor

yukoba commented Nov 8, 2016

Yes, Theano variables have a name property, but it is set to None when trying the weights.

Do you mean creating a variable without a name?
I think all the variables in keras.layers classes assign names.

Maybe you should change it to weights.sort(key=lambda x: x.name if x.name else x.auto_name).
I checked that it solves the load_model() problem.

@yenzhen
Copy link

yenzhen commented Nov 10, 2016

I still face the same problem when I load_model()
I've upgraded the latest version of keras
Is this issue solved?
2016-11-10 9 29 13

@yukoba
Copy link
Contributor

yukoba commented Nov 11, 2016

You have to save by the latest git version and load_model() by the latest git version.
Did you do that?
It is impossible to load Keras 1.1.0 and 1.1.1 saved model.

@yenzhen
Copy link

yenzhen commented Nov 11, 2016

I fix it by cloning the repo and reinstall keras
(I use pip to uninstall and install again, but it does't work until I install keras by cloning the repo)
Thanks!

@zawecha1
Copy link

zawecha1 commented Dec 8, 2016

我模型算完了,就不重装了。
修改 /usr/local/lib/python2.7/site-packages/keras/optimizers.py
行 line 92 add
amap={}
for w in weights:
amap[str(w.shape)]=w
for pv, p, w in zip(param_values, params, weights):
w=amap[str(pv.shape)]
if pv.shape != w.shape:
print(p,w)
raise Exception('Optimizer weight shape ' +
str(pv.shape) +
' not compatible with '
'provided weight shape ' + str(w.shape))
weight_value_tuples.append((p, w))
有点坑,要print model.summary() 中的,"Param #" 列不一样才行。

@dteoh
Copy link

dteoh commented Dec 12, 2016

I am using Keras 1.1.1 and am having the same problem. Deleting the optimizer weights as a workaround works for me.

Just in case someone needs to do the same, here's the code:

import h5py
f = h5py.File('model_file.h5', 'r+')
del f['optimizer_weights']
f.close()

@jrosebr1
Copy link

jrosebr1 commented Feb 10, 2017

I can confirm the same issue. Deleting the optimizer_weights as suggested by @dteoh works if your model is already trained.

However, I'm working on a project where I train for N number of epochs, stop training, adjust learning rate, then re-load and continue training the model.

In this case, deleting my optimizer_weights would lead to the error:

AttributeError: 'Model' object has no attribute 'optimizer'

This error makes sense since the optimizer attribute has been deleted. But as it currently stands, I can't figure out how to:

  1. Train a (non-Sequential) model.
  2. Serialize it.
  3. Load it from disk.
  4. Adjust learning rate.
  5. Continue training.

Without the error others in this thread have mentioned.

EDIT: Spun up a new virtual environment with Keras > 1.2 installed and was able to resume training.

@WarpTime
Copy link

Another solution is to name each layer in the model.
This helps if you're using checkpointing or something similar, where you cannot save the weights separately.
For ex: model.add(LSTM(10, name='lstm1'))
This worked for me in keras 1.1.1 and theano 0.8.2

@xisnu
Copy link

xisnu commented Feb 24, 2017

@WarpTime Have you tested it for a model with CTC loss? Because that has been a problem for me.

@WarpTime
Copy link

@xisnu No. I have only tested it on crossentropy.

@datumbox
Copy link
Contributor

datumbox commented Mar 7, 2017

Sample problem here using SGD and categorical_crossentropy.

@WarpTime
Copy link

WarpTime commented Mar 8, 2017

Something like this....

model = Sequential()
model.add(LSTM(100, return_sequences=True, input_shape=(timesteps, data_dim), name='input'))
model.add(Dropout(0.5, name='dr1'))
model.add(LSTM(100, name='ls1'))
model.add(Dropout(0.5, name='dr2'))
model.add(Dense(100, name='d1'))
model.add(Dense(nb_classes, activation='softmax', name='output'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])

@MartinThoma
Copy link
Contributor

MartinThoma commented Mar 22, 2017

After updating from Keras 1.X to Keras 2.0.1, I have the same problem:

  File "./ensemble.py", line 56, in <module>
    models = [load_model(model_path) for model_path in model_names]
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 272, in load_model
    model.optimizer.set_weights(optimizer_weight_values)
  File "/usr/local/lib/python2.7/dist-packages/keras/optimizers.py", line 79, in set_weights
    'provided weight shape ' + str(w.shape))
ValueError: Optimizer weight shape (32,) not compatible with provided weight shape (3, 3, 3, 32)

Deleting the optimizer_weights group (dataset? object? directory? what is the right term?) from the HDF5 file with hdf5view fixed it as @dteoh suggested.

I updated all my models with

#!/usr/bin/env python

"""Make keras 1.x models usable in keras 2.x."""

import glob
import h5py

model_files = sorted(glob.glob('*.h5'))
for model_file in model_files:
    print("Update '{}'".format(model_file))
    with h5py.File(model_file, 'a') as f:
        if 'optimizer_weights' in f.keys():
            del f['optimizer_weights']

Now it is working again.

Also: Is it possible to save the model without optimizer parameters?

gagnonlg added a commit to gagnonlg/explore-ml that referenced this issue May 4, 2017
The code was crashing when loading back a model saved with ModelCheckpoint.
This is a bug in keras 1.1.1: keras-team/keras#4044
@pengpaiSH
Copy link

The following codes really worked! Thank you!

    with h5py.File(model_file, 'a') as f:
        if 'optimizer_weights' in f.keys():
            del f['optimizer_weights']

@oarriaga
Copy link

oarriaga commented Jul 4, 2017

I had the same problem and what it seemed to work for me was to set the compile flag to False in the load_model function. Afterwards one can compile the model with the previously used optimizer.

model = load_model('my_model.hdf5', compile=False)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

@kaulanishasj
Copy link

in case you are having issue in deleting the keys from the model, check this out:

https://stackoverflow.com/questions/44236522/keyerror-couldnt-delete-link-cant-delete-self

@datumbox
Copy link
Contributor

datumbox commented Sep 6, 2017

It seems that the problem of loading optimizer weights is patched in Keras 2.0.8. The patch does not address the reason why the weights have different size but at least you no longer get an exception.

@stale
Copy link

stale bot commented Dec 5, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

@stale stale bot added the stale label Dec 5, 2017
@zhudaoruyi
Copy link

@oarriaga It works for me, thank you !

@wt-huang
Copy link

wt-huang commented Nov 2, 2018

Closing as this is resolved

@wt-huang wt-huang closed this as completed Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests