8
votes

I have trained a keras sequential model in a linux 64 machine and saved to a .h5 file.

It this PC I can load the model and do predictions without problems.

Now I'm implementing the prediction in a Raspberry Pi 3 that have installed keras, tensorflow, h5py and python3.

when I load the model

from keras.models import load_model
model = load_model('model-0.6358.h5')

, I'm getting:

usr/lib/python3.4/importlib/_bootstrap.py:321: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
return f(*args, **kwds)

/usr/local/lib/python3.4/dist-packages/keras/models.py:291: UserWarning: Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
warnings.warn('Error in loading the saved optimizer '

But... it looks like it predicts right.

How can I avoid that warning message?

2
Yes... it will predict right. The "optimizer" is only necessary for "training", and it's state will only help select the proper learning rate and weight updates. Even with a fresh new optimizer, it will be possible to train if you manually adjust its properties to reasonable amounts. A fresh new optimizer may have a hard time at the beginning of the training, but after a few epochs it will very probably find its way.Daniel Möller
@DanielMöller that make sense, I mistrust when I get one warning message only in one platform, any way I will not train in the RPI. But do you know why I'm getting that message?Mquinteiro
Unfortunately I don't know.... it's probably some kind of incompatibility. In my case it's worse, because I was never able to load a model this way even on the same machine.Daniel Möller
@DanielMöller h5py problem? Keras problem? Did you try to load the file with f = h5py.File("name.h5",'r') for name in f: print name?Mquinteiro
For me it worked when I changed the version of keras to the same which I used to train the model.Shivam K. Thakkar

2 Answers

18
votes

load_model first builds the saved model architecture with its saved weights and then tries to build the saved optimizer with its saved weights.

However, you get an error message because there is a mismatch between the shape of the saved optimizer weights and the shape of the weights that the optimizer is expecting based on the architecture of the loaded model.

I ran into this issue using Keras 2.1.4 when I tried to save and re-load a model that had inner submodels that were set to trainable=False. This information seems not to be preserved when you save the model, so after re-instatiating the inner submodel is set to trainable=True and the optimizer would expect more saved weights than were actually saved. If this might be the problem in your case, I described a workaround in this bug-report:

  1. Set the trainability of all the inner model layers explicitly
  2. Right before saving, the trainability flags of all the layers have to be set to the state that they had at model compile time

If you want to get rid of the warning and you do not need the optimizer after saving anyway, you can also save your model without the optimizer: use model.save(filename, include_optimizer=False)

1
votes

I resolved the issue by adding compile = False in the load_model function.

The references to the official site: https://www.tensorflow.org/api_docs/python/tf/keras/models/load_model?hl=en

Here an example:

model = tf.keras.models.load_model('path/of/your/model', compile = False)