Yahya Nik
Yahya Nik

Reputation: 85

Keras: How to save models or weights?

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

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.

Here is my code:

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)

I get the error:

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

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

Upvotes: 2

Views: 7187

Answers (2)

josoler
josoler

Reputation: 1423

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 :

When saving a model's weights, tf.keras defaults to the checkpoint format. Pass save_format='h5' to use HDF5.

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).

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:

Storing just the weights

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)

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

Storing the whole model

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})

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

Upvotes: 5

user11803894
user11803894

Reputation:

keras has a save command. It saves all the details needed to rebuild the model.

(from the keras docs)

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 am identical compiled model
model = load_model('my_model.h5')

Upvotes: 0

Related Questions