【发布时间】:2019-03-16 04:03:42
【问题描述】:
我通过callbacks.ModelCheckpoint() 使用 HDF5 文件自动保存了我的模型。
# 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])
当我加载模型时,发生了错误。
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'
如果我使用参数 'compile=False' 加载模型,它可以正常工作。
我知道在keras中保存模型的正常方法是:
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')
顺便说一句,当我用 Tensorflow Lite 转换这个模型时,也发生了这个错误。 但我不知道我的模型有什么问题。 有人有想法吗?
【问题讨论】:
-
函数
load_model()可以加载funcsave_model()保存的模型。在callbacks类中,模型由model.save()保存。这些方式有什么区别?如何加载第二种方式保存的模型? -
您是否使用相同的 Keras 版本来保存和加载模型?
-
@MatiasValdenegro 我在 Windows 10 和 Ubuntu 16.04 平台上都使用相同的版本:2.2.2,这个问题发生在 Windows 10,在 Ubuntu 16.04 中运行良好。
标签: keras hdf5 tensorflow-lite