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?
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.
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.
Call tf. keras. Model. save to save a model's architecture, weights, and training configuration in a single file/folder .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With