Keras: Error loading a saved model?

Created on 12 Jan 2017  路  7Comments  路  Source: keras-team/keras

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?

Most helpful comment

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

All 7 comments

Hi! I encountered a similar problem and got a workaround by saving the model architecture and weights separately, then loading the model from that. Could be a quick fix while this issue is not resolved.

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.

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

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

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.

Still a problem.

Still a problem to me with Keras on Tensorflow 1.8 when trying this tutorial https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/03C_Keras_API.ipynb

This happens when I tried to save and load the Sequential Model in the tutorial. However no error when saving the Functional Model.

Was this page helpful?
0 / 5 - 0 ratings