Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error loading the saved optimizer. keras python raspberry

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?

like image 289
Mquinteiro Avatar asked Mar 09 '18 13:03

Mquinteiro


2 Answers

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)

like image 169
KiraMichiru Avatar answered Nov 19 '22 23:11

KiraMichiru


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)
like image 45
Francesco Laiti Avatar answered Nov 19 '22 22:11

Francesco Laiti