本文介绍了Keras:如何保存模型或权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这个问题似乎很简单.但是请阅读Keras的保存和恢复帮助页面:

I am sorry if this question seems pretty straight forward. But reading the Keras save and restore help page :

https://www.tensorflow.org/beta/tutorials/keras/save_and_restore_models

我不明白如何在训练期间使用"ModelCheckpoint"进行保存.帮助文件提到应该提供3个文件,我只能看到一个文件,MODEL.ckpt.

I do not understand how to use the "ModelCheckpoint" for saving during training. The help file mentions it should give 3 files, I see only one, MODEL.ckpt.

这是我的代码:

checkpoint_dir = FolderName + "/tmp/model.ckpt"
cp_callback = k.callbacks.ModelCheckpoint(checkpoint_dir,verbose=1,save_weights_only=True)
parallel_model.compile(optimizer=tf.keras.optimizers.Adam(lr=learning_rate),loss=my_cost_MSE, metrics=['accuracy])
    parallel _model.fit(image, annotation, epochs=epoch,
    batch_size=batch_size, steps_per_epoch=10,
                                 validation_data=(image_val,annotation_val),validation_steps=num_batch_val,callbacks=callbacks_list)

此外,当我想通过以下方法训练负重时:

Also, when I want to load the weights after training with:

model = k.models.load_model(file_checkpoint)

我得到了错误:

"raise ValueError('Unknown ' + printable_module_name + ':' + object_name)
ValueError: Unknown loss function:my_cost_MSE"

my-cost_MSE是我在培训中使用的成本函数.

my-cost_MSE is my cost function that is used in the training.

推荐答案

首先,您似乎正在使用tf.keras(来自tensorflow)实现,而不是keras(来自keras-team/keras存储库) ).在这种情况下,如 tf.keras指南所述:

First of all, it looks like you are using the tf.keras (from tensorflow) implementation rather than keras (from the keras-team/keras repo). In this case, as stated in the tf.keras guide :

另一方面,请注意,添加回调ModelCheckpoint通常大致等效于在每个时期结束时调用model.save(...),因此这就是为什么您希望保存三个文件的原因(根据检查点格式).

On the other hand, note that adding the callback ModelCheckpoint is, usually, roughly equivalent to call model.save(...) at the end of each epoch, so that's why you should expect three files to be saved (according to the checkpoint format).

之所以不这样做,是因为通过使用选项save_weights_only=True,您仅保存了权重.大致等效于在每个时期结束时将对model.save的调用替换为model.save_weights.因此,唯一要保存的文件是具有权重的文件.

The reason it's not doing so is because, by using the option save_weights_only=True, you are saving just the weights. Roughly equivalent to replace the call to model.save for model.save_weights at the end of each epoch. Hence, the only file that's being saved is the one with the weights.

从这里开始,您可以采用两种不同的方式进行操作:

From here, you can proceed in two different ways:

您需要预先加载模型(例如结构),然后调用model.load_weights而不是keras.models.load_model:

You need your model (the structure, let's say) to be loaded beforehand and then call model.load_weights instead of keras.models.load_model:

model = MyModel(...)  # Your model definition as used in training
model.load_weights(file_checkpoint)

请注意,在这种情况下,自定义定义(my_cost_MSE)不会有问题,因为您只是加载模型权重.

Note that in this case, you won't have problems with custom definitions (my_cost_MSE) since you are just loading model weights.

另一种进行方法是存储整个模型并相应地加载它:

Another way to proceed is to store the whole model and load it accordingly:

cp_callback = k.callbacks.ModelCheckpoint(
    checkpoint_dir,verbose=1,
    save_weights_only=False
)
parallel_model.compile(
    optimizer=tf.keras.optimizers.Adam(lr=learning_rate),
    loss=my_cost_MSE,
    metrics=['accuracy']
)

model.fit(..., callbacks=[cp_callback])

然后您可以通过以下方式加载它:

Then you could load it by:

model = k.models.load_model(file_checkpoint, custom_objects={"my_cost_MSE": my_cost_MSE})

请注意,在后一种情况下,您需要指定custom_objects,因为需要对它的定义来反序列化模型.

Note that in this latter case, you need to specify custom_objects since its definition is needed to deserialize the model.

这篇关于Keras:如何保存模型或权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 01:50
查看更多