Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in load a model saved by callbakcs.ModelCheckpoint() in Keras

I saved my model automatically by callbacks.ModelCheckpoint() with a HDF5 file.

# Checkpoint In the /output folder
filepath = "./model/mnist-cnn-best.hd5"

# Keep only a single checkpoint, the best over test accuracy.
checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', 
                                             verbose=1, save_best_only=True,
                                             mode='max')

# Train
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test),
          callbacks=[checkpoint])

When I load a model, an error occured.

  model = keras.models.load_model("./mnist-cnn-best.hd5")

  File "D:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\saving.py", line 251, in load_model
    training_config['weighted_metrics'])
KeyError: 'weighted_metrics'

If I load model with param 'compile=False', it works correctly.

I know the normal way to save model in keras is:

from keras.models import load_model

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
del model  # deletes the existing model

# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')

By the way, this error also happened when me convert this model by Tensorflow Lite. But I don't know what's wrong with my model. Does anyone has an idea?

like image 352
Jonathan.H Avatar asked Oct 11 '18 06:10

Jonathan.H


People also ask

What is keras callback ModelCheckpoint used for?

ModelCheckpoint callback is used in conjunction with training using model. fit() to save a model or weights (in a checkpoint file) at some interval, so the model or weights can be loaded later to continue the training from the state saved.

How do I use callbacks in Tensorflow?

Callbacks can be passed to keras methods such as fit , evaluate , and predict in order to hook into the various stages of the model training and inference lifecycle. To create a custom callback, subclass keras. callbacks. Callback and override the method associated with the stage of interest.

How do you save a keras model?

Call tf. keras. Model. save to save a model's architecture, weights, and training configuration in a single file/folder .


1 Answers

I hit a similar problem that yields the same error message, but the cause might be different than yours:

Code: (Tensorflow 1.11 and tf.keras.version: 2.1.6-tf)

 if load_model_path.endswith('.h5'):
        model = tf.keras.models.load_model(load_model_path)

Error message:

  File "...../lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 251, in load_model
    training_config['weighted_metrics'])
KeyError: 'weighted_metrics'

And I found out it's because the model was saved in an older Keras version. I had to comment out the code related to weighted_metrics to be able to load the model. However, it's just a workaround before I can find a sustainable solution to the mismatching problem. Interestingly, @fchollet just added weighted_metrics to the latest Keras version recently (Oct 2018).
https://github.com/keras-team/keras/blob/master/keras/engine/saving.py#L136 I hope this will help the people who hit the same problem as I did.

like image 134
Nicole Finnie Avatar answered Nov 20 '22 10:11

Nicole Finnie