Loading ModelCheckpoint in tensorflow 2

Viewed 1962

In keras using tensorflow 1 I could ModelCheckpoint(filepath) and the saved file was a called filepath and then I could call model = load_model(filepath) to load the saved model.

Now the equivalent in tensorflow 2 the ModelCheckpoint creates a directory called filepath and when I follow the documentation here to load the saved model I have to create an empty model then call model.load_weights(filepath). Here is my callback and fit:


filepath = "filepath"
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=filepath, mode='max', monitor='val_accuracy', verbose=2, save_best_only=True)
callbacks_list = [checkpoint]
model.fit(train_dataset, validation_data=y_test_dataset, validation_steps=BATCH_SIZE, callbacks=callbacks_list, epochs=5000, verbose=2, steps_per_epoch=(X_train_deleted_nans.shape[0]//BATCH_SIZE))

Doing model.load_weights(filepath) in another script I get the following error:

OSError: Unable to open file (unable to open file: name = 'filepath', errno = 13, error message = 'Permission denied', flags = 0, o_flags = 0)

I would like to get some help on why I would be getting a permission denied error for a model that I created.

2 Answers

Try checkpointing while including the .hdf5 extension when saving your model weights.

filepath = "filepath/model.hdf5"
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=filepath, mode='max', monitor='val_accuracy', verbose=2, save_best_only=True)

What to do if you've spent much time training your model and you don't want to do it again just to save in HDF5 format?

What you can do:

  1. Create your model from code model = build_super_artificial_intelligence_deep_learning_model()
  2. Save it using tf.keras.models.save_model(model, "/path/to/full_model")
  3. Replace the variables.* files in /path/to/full_model/variables with the files with the corresponding extensions in your checkpoint. Rename the files from the checkpoint to variables.*.
  4. Load the model using trained_model = tf.keras.models.load_model("/path/to/full_model").

(tested with TF2.5)

Related