How to load and use model checkpoint (.ckpt)?

Hello,

I trained a model with Pytorch Lighntning and now have a .ckpt file for the checkpoint. I would like to load this checkpoint to be able to see the kind of output it generates. How can this be done ?

Thanks !

The way I do it is as follows. This method is especially useful if the hyperparameters with which you generated the checkpoint file were not saved in the checkpoint file for some reason.

model = my_model(layers=3, drop_rate=0)
trainer = pl.Trainer()
chk_path = "/path_to_checkpoint/my_checkpoint_file.ckpt"
model2 = my_model.load_from_checkpoint(chk_path, layers=3, drop_rate=0)
results = trainer.test(model=model2, datamodule=my_datamodule, verbose=True)

You can save the hyperparameters in the checkpoint file using self.save_hyperparameters() while defining your model as described here Hyperparameters — PyTorch Lightning 1.1.6 documentation. In that case, you don’t need to provide hyperparameters to load_from_checkpoint, like so,

model = my_model(layers=3, drop_rate=0)
trainer = pl.Trainer()
chk_path = "/path_to_checkpoint/my_checkpoint_file.ckpt"
model2 = my_model.load_from_checkpoint(chk_path)
results = trainer.test(model=model2, datamodule=my_datamodule, verbose=True)
1 Like

you can use

model = ModelClass.load_from_checkpoint(ckpt_file_path)
1 Like