Can I load my own weights?

Full code source:

#Download COCO pre-trained weights
!wget --quiet https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5
!ls -lh mask_rcnn_coco.h5

COCO_WEIGHTS_PATH = mask_rcnn_coco.h5


model.load_weights(COCO_MODEL_PATH, by_name=True,
                       exclude=[mrcnn_class_logits, mrcnn_bbox_fc,
                                mrcnn_bbox, mrcnn_mask])
elif init_with == last:
    # Load the last model you trained and continue training
    model.load_weights(model.find_last()[1], by_name=True) 

Can I load my own *.h5 file? For example: I interrupted my kernel after 5 epochs. Can I load my last epoch? Can You explain it for me?

It will be continue a process learning?

Topic faster-rcnn deep-learning

Category Data Science


Yes you can load your own weights, by changing the COCO_WEIGHTS_PATH path in the code to your own. Before that, to save the trained model, you can either train it for a number of epochs, then manually save it using

model.save("mymodel.h5")

You can also use Keras's ModelCheckpoint callback

from keras.callbacks import ModelCheckpoint
model_checkpoint_callback = ModelCheckpoint(
    filepath="mymodel.h5",
    save_weights_only=True,
    save_best_only=True,
    save_freq="epoch")

And add it to callbacks in model.train()

model.train(
          # ... other parameters
          callbacks=[model_checkpoint_callback]
)

Can I load my own "*.h5" file?

Yes, you can load your own .h5 file. But to load weights into a model, you need to have a model architecture defined for it. If the weight dimensions or layer sizes does not match, it will throw an error, of course.

But instead of saving weights, if you save the model by using model.save_model, it does have architecture of the model saved allowing you to re-create the model, weights of the model, training configuration (loss, optimizer), state of the optimizer, allowing you to resume training. But this does not load hyperparameters that are present for that epoch like decay in learning rate etc. You have to write a custom callback for this.

Can I load my last epoch?

Yes, if specified. Keras does not automatically save the model for every epoch or anything until you ask it to. So if you had interrupted something, you cant resume it from there. You have to write a custom callback to make it happen. For details on what are callbacks. For details on how to save the model after every epoch, check out this answer.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.