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.

Error loading a saved model?

See original GitHub issue

There is my model:

data_dim = 25*88
timesteps = 40

secen = Sequential()
secen.add(Convolution2D(32, 3, 3, activation='relu', border_mode='same', input_shape=(1, 25, 88)))
secen.add(MaxPooling2D((2, 2)))
secen.add(Dropout(0.25))
secen.add(Convolution2D(32, 3, 3, activation='relu', border_mode='same'))
secen.add(MaxPooling2D((2, 2)))
secen.add(Dropout(0.25))
secen.add(Convolution2D(32, 3, 3, activation='relu', border_mode='same'))
secen.add(MaxPooling2D((2, 2)))
secen.add(Dropout(0.25))
secen.add(Flatten())

secde = Sequential()
secde.add(Convolution2D(32, 3, 3,border_mode='same', input_shape=(1, 25, 11)))
secde.add(Activation('tanh'))
secde.add(UpSampling2D(size=(1, 2)))
secde.add(Convolution2D(32, 3, 3, border_mode='same'))
secde.add(Activation('tanh'))
secde.add(UpSampling2D(size=(1, 2)))
secde.add(Convolution2D(32, 3, 3, border_mode='same'))
secde.add(Activation('tanh'))
secde.add(UpSampling2D(size=(1, 2)))
secde.add(Convolution2D(1, 3, 3, border_mode='same'))
secde.add(Activation('sigmoid'))

music_input = Input(shape=(timesteps, 1, 25, 88))

encoded_frame_sequence = TimeDistributed(secen)(music_input)
encoded_music = LSTM(data_dim/8,return_sequences=True)(encoded_frame_sequence)
encoded_music=Dropout(0.25)(encoded_music)
encoded_music=Reshape((timesteps, 1, 25,11 ))(encoded_music)
decode_music=TimeDistributed(secde)(encoded_music)

model=Model(input=music_input,output=decode_music)

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy')

I fitted the model.However when I load it from a .h5 file,i got an error.

model = load_model('midinet_0110.h5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda2\Lib\site-packages\keras\models.py", line 179, in load_model
    model.optimizer.set_weights(optimizer_weight_values)
  File "C:\Anaconda2\Lib\site-packages\keras\optimizers.py", line 90, in set_weights
    'provided weight shape ' + str(w.shape))
ValueError: Optimizer weight shape (275L, 275L) not compatible with provided weight shape (1056, 275)

I guess it may caused by the complicacy of my model?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
dustinandrewscommented, Jun 19, 2017

Reproduced with Keras 2.0.5 on Tensorflow with the Adam optimizer. I’m using an Inception V4 model.

1reaction
cogitaecommented, Feb 8, 2017

I had same problems with save_model + load_model on trained model with Adam optimizer on Theano. Keras 1.1.1 The problem was that the optimizer weights were not loaded in the same order so there was an assertion on weight shapes. I could solve locally this issue modifying Adam code to set names for each weights it used so that weights matched between save and load. In optimizers.py :

#        self.iterations = K.variable(0)
#TS 20170208 bugfix : save_model + load_model not working after training
        self.iterations = K.variable(0, name='Adam_iter')

#TS 20170208 bugfix : save_model + load_model not working after training
#        ms = [K.zeros(shape) for shape in shapes]
        ms = [K.zeros(shape,name='Adam_p_ms_'+ str(p.name)) for p,shape in zip(params,shapes)]
#        vs = [K.zeros(shape) for shape in shapes]
        vs = [K.zeros(shape,name='Adam_p_vs_'+ str(p.name)) for p,shape in zip(params,shapes)]

I haven’t seen yet if there is a naming convention that should be used and if this is the correct fix, probably all optimizers need it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error loading saved model · Issue #51746 - GitHub
After saving a Quant Aware model to saved_model using model.save(), I get this error while loading it with tf.keras.models.load_model() - ...
Read more >
Error when loading a saved model - TensorFlow Forum
Hello communit, i saved a model which i trained for a deep-q learning project. Tensorflow/keras 2.7.0 on Python 3.9.9 64bit on Windows 10....
Read more >
Unable to load a Keras saved model (Error: unable to open file)
Try like this, model.save("my_model"). Calling model.save('my_model') creates a folder named my_model, containing the following:
Read more >
Error loading saved model - PyTorch Forums
I have a model: cnn=CNN() torch.save(cnn, './model/trained_model.pt') model = torch.load('./model/trained_model.pt') AttributeError: Can't ...
Read more >
Save and load models in Tensorflow - GeeksforGeeks
Save and load models in Tensorflow ... The development of the model can be saved both before and after testing. As a result,...
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