How to save model weights in order to use later?

Viewed 438

I am training a model using Mask RCNN; it saves the model after every epoch as mask_rcnn_building_cfg_0001.h5 (number corresponds epoch number). I use pre-trained weights for Transfer Learning, which works fine. But now I want to use my own trained model to further train as it is much more optimized. In codebase, there is this part:

        callbacks = [
        keras.callbacks.TensorBoard(log_dir=self.log_dir,
                                    histogram_freq=0, write_graph=True, write_images=False),
        keras.callbacks.ModelCheckpoint(self.checkpoint_path,
                                        verbose=0, save_weights_only=True),
    ]

So normally I have to be able to call model.load_weights('mask_rcnn_building_cfg_0001.h5', by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])

and use saved model for resuming training, but training does not start. But the same load_weights() function works with the pretrained model downloaded from internet and model continues training.

Now, how should I set model paremeters in order to save weights after each epoch, for using in further trainings as pretrained weights? My best guess is I somehow have to exclue output layer from mask_rcnn_building_cfg_0001.h5, but not sure hw to do that.

1 Answers
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# Create a callback that saves the model's weights
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)

# Loads the weights
model.load_weights(checkpoint_path)

Complete guidelines on save and load model is given in Tensorflow doc. Please find here.

Related