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 https://pytorch-lightning.readthedocs.io/en/stable/hyperparameters.html. 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)
3 Likes

you can use

model = ModelClass.load_from_checkpoint(ckpt_file_path)
2 Likes

Hi, 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. trainer.fit() creates a model checkpoint file but unable to load the checkpoint file. While I try to load this checkpoint file, I get an error saying

RuntimeError: Error(s) in loading state_dict for Autoencoder

Could anyone please advice if I am missing something here. Thanks.