Jonathan.H
Jonathan.H

Reputation: 193

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?

Upvotes: 8

Views: 5244

Answers (3)

kit
kit

Reputation: 1

The way I usually use this is as follows:

def create_model():
    <my model>
    <model.compile>
    return model
checkpoint = keras.callbacks.ModelCheckpoint(filepath, verbose=<val>, monitor=<val>, save_best_only=True, save_weights_only=True)

classifier = create_model()
classifier.fit(<your parameters>)
classifier.evaluate(<your parameters>)

loaded_model = create_model()
loaded_model.load_weights(filepath)
y_pred = loaded.model.<predict_method>(test_set,verbose=<val>)
'''

Upvotes: 0

user9767356
user9767356

Reputation:

If you haven't figured out the answer to this yet, I think I've got it.

I haven't dug into the code to exactly figure out why, but basically the model checkpoint callback can only be loaded with the load_weights() function, which is then used for evaluation.

If you want to save a model that you can load to train again later you need to use model.save and model.load_model. Hopefully helpful to someone who wanders upon this.

Upvotes: 2

Nicole Finnie
Nicole Finnie

Reputation: 1590

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.

Upvotes: 3

Related Questions