Keras: Keras load_model fails after training

Created on 19 Feb 2017  路  5Comments  路  Source: keras-team/keras

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.

Thank you!

  • [x] 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

  • [x] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

  • [x] 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

  • [x] 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).

I tried the code given here: https://github.com/fchollet/keras/issues/2295

I am unable to load my model after training it. I am getting the following error:

ValueError: Optimizer weight shape (3, 3, 512, 512) not compatible with provided weight shape (256, 43)

I tried the solution here: https://github.com/fchollet/keras/issues/3964 using HDFView and deleting the optimiser weights and then reloading. But then I get the error:

ValueError: ('shapes (10,4224) and (1128,256) not aligned: 4224 (dim 1) != 1128 (dim 0)', (10, 4224), (1128, 256))
Apply node that caused the error: Dot22(Reshape{2}.0, lstm_2_W_i)
Toposort index: 249
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(10, 4224), (1128, 256)]
Inputs strides: [(16896, 4), (1024, 4)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[Elemwise{Add}[(0, 0)](Dot22.0, InplaceDimShuffle{x,0}.0)]]

I also tried the solution by barrykui here: https://github.com/fchollet/keras/issues/4044 saving the architecture to JSON and saving weights separately and then loading but even that fails.

stale

Most helpful comment

I was able to devise a workaround. I was able to load the trained model using the steps below:

  1. Create the Sequential model.
    Example:
model = Sequential()
model.add(...)
model.add(...)

model.compile(...)
model.fit(...)
  1. After training save only model weights using model.save_weights()
    Example:
    model.save_weights(SaveLocation)
  2. To load the model weights, create the model programmatically just as in step 1 but do not use the model.compile function.
    Example:
model = Sequential()
model.add(...)
model.add(...)
model.load_weights(weightFile)
  1. The weights are now loaded successfully.

All 5 comments

I was able to devise a workaround. I was able to load the trained model using the steps below:

  1. Create the Sequential model.
    Example:
model = Sequential()
model.add(...)
model.add(...)

model.compile(...)
model.fit(...)
  1. After training save only model weights using model.save_weights()
    Example:
    model.save_weights(SaveLocation)
  2. To load the model weights, create the model programmatically just as in step 1 but do not use the model.compile function.
    Example:
model = Sequential()
model.add(...)
model.add(...)
model.load_weights(weightFile)
  1. The weights are now loaded successfully.

I reopened this issue just in case someone else is able to provide a better solution.

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

I observed this issue on Keras 1.2.2 with TensorFlow 0.12.1 backend on Win64 (Anaconda3 + Spyder). The best solution so far has been from @AahanSingh (without needing to upgrade my setup). Other solutions have alluded to naming each layer and retraining before saving model JSON and weights, which for me was not a convenient option. I have redefined the model as per the suggestion - and this seems to be the simplest workaround till date (at least without having to upgrade my setup).

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.

Was this page helpful?
0 / 5 - 0 ratings