Yana
Yana

Reputation: 975

How to load my tensorflow model with ModelCheckpoint callbacks?

I have trained a model and save the weights using ModelCheckpoint:

checkpoint_callback = ModelCheckpoint(
    filepath = checkpoint_prefix,
    save_weights_only = True,
    save_freq = 'epoch')

During the night while my model was training the electricity went off for some time and my computer turned off. Now I opened my Jupyter notebook and I want to load my model without training it from the beginning. How am I supposed to do this without compiling it again and just using the checkpoints? I also have tensorboard callbacks:

tensorboard_callback = TensorBoard(
    log_dir = 'tensorboard_logs\\'+ model_name,
    histogram_freq = 5,
    write_graph = True,
    update_freq = 'epoch')

Upvotes: 2

Views: 1321

Answers (1)

Thibault Bacqueyrisses
Thibault Bacqueyrisses

Reputation: 2331

Since you only saved the weights of your model, you need to reconstruct the graph and then load your last checkpoint weights on it.

So you have to recreate your model and compile it.
For the next time, if you want to save the complete model, so you don't have to compile it again every time you load it, set save_weights_only to False.
It allow you to load your model with keras.models.load_model() and directly fit it after.

model = Sequential()
model.add() 
...
model.compile()

And then load your weights:

model.load_weights(checkpoint_prefix)

and then you can use it normaly :

model.fit( ... )

Upvotes: 1

Related Questions