The classmethod `.load_from_checkpoint` cannot be called on an instance. Please call it on the class type and make sure the return value is used.
Bug description
I've been using pytorch_lightning for quite a while. Recently, I've started using newly proposed imports such as:
import lightning as L
pytorch-lightning==2.0.9.post0 torch==2.1.0
What version are you seeing the problem on?
#v2.0, master
How to reproduce the bug
import lightning as L
# it causes an error
LightningCloudSegNet(L.LightningModule):
def __init__(self, cfg: dict, fold=-1):
super().__init__()
self.cfg = cfg
self.fold = fold
self.model = self._init_model()
self.loss_fn = criterion
self.total_training_steps = None
self.save_hyperparameters(self.cfg)
self.dice_metric = Dice()
self.f1_metric = F1Score(task='binary')
self.val_step_logits = []
self.val_step_masks = []
model = LightningCloudSegNet(cfg=cfg, fold=fold)
ckpt_path = 'saved_models/exp15/exp15_1_last.ckpt'
model.load_from_checkpoint(ckpt_path)
import pytorch-lightning as pl
# it loads the weights
class LightningCloudSegNet(pl.LightningModule):
def __init__(self, cfg: dict, fold=-1):
super().__init__()
self.cfg = cfg
self.fold = fold
self.model = self._init_model()
self.loss_fn = criterion
self.total_training_steps = None
self.save_hyperparameters(self.cfg)
self.dice_metric = Dice()
self.f1_metric = F1Score(task='binary')
self.val_step_logits = []
self.val_step_masks = []
model = LightningCloudSegNet(cfg=cfg, fold=fold)
ckpt_path = 'saved_models/exp15/exp15_1_last.ckpt'
model.load_from_checkpoint(ckpt_path)
Error messages and logs
`Traceback (most recent call last):
File "/home/lasupreme/miniconda3/envs/torchenv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3505, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-f32518f9cfc3>", line 1, in <module>
model.load_from_checkpoint(ckpt_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lasupreme/miniconda3/envs/torchenv/lib/python3.11/site-packages/lightning/pytorch/utilities/model_helpers.py", line 93, in __get__
raise TypeError(
TypeError: The classmethod `LightningCloudSegNet.load_from_checkpoint` cannot be called on an instance. Please call it on the class type and make sure the return value is used.`
Environment
Current environment
#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow):
#- PyTorch Lightning Version (e.g., 1.5.0): 2.0.9.post0
#- Lightning App Version (e.g., 0.5.2):
#- PyTorch Version (e.g., 2.0): 2.1.0
#- Python version (e.g., 3.9): 3.11
#- OS (e.g., Linux): Ubuntu 22.04
#- CUDA/cuDNN version:12.2
#- GPU models and configuration:Nvidia Amper
#- How you installed Lightning(`conda`, `pip`, source):pip
#- Running environment of LightningApp (e.g. local, cloud):local
More info
No response
Hey @SergeySakharovskiy This isn't a bug in Lightning. The error message is actually created by Lightning if you look at the stack trace. It is to inform the user that they should not call the classmethod on an instance. It says at the end:
Please call it on the class type and make sure the return value is used.
Which means in your code you want to do
ckpt_path = 'saved_models/exp15/exp15_1_last.ckpt'
model = LightningCloudSegNet.load_from_checkpoint(ckpt_path, cfg=cfg, fold=fold)
For reference, see the original issue #18169 and PR that did this improvement: https://github.com/Lightning-AI/lightning/pull/18432
config = "config/vae_config/kl-f4.yaml"
path = "lightning_logs/version_0/checkpoints/epoch=0-step=961.ckpt"
# Load the YAML file
with open(config, 'r') as file:
config = yaml.safe_load(file)
# Load model
autoencoder = AutoEncoderKL.load_from_checkpoint(
checkpoint_path=path,
ddconfig=config['model']['params']['ddconfig'],
embed_dim=config['model']['embed_dim'],
monitor="val/total_loss",
# lossconfig=config['model']['params']['lossconfig']
lossconfig=None,
)
# model = AutoEncoderKL.load_from_checkpoint(checkpoint_path=path,
# )
print(autoencoder)
Yaa, that's worked