Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeError: Use Class Method for load_from_checkpoint (lightning 2.1.1 onwards) #65

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ venv/
ENV/
env.bak/
venv.bak/
.micromamba

# Spyder project settings
.spyderproject
Expand Down
3 changes: 1 addition & 2 deletions pl_crossvalidate/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class EnsembleLightningModule(LightningModule):

def __init__(self, model: LightningModule, ckpt_paths: List[str]) -> None:
super().__init__()
model_cls = type(model)
self.models = nn.ModuleList([model_cls.load_from_checkpoint(p) for p in ckpt_paths])
self.models = nn.ModuleList([type(model).load_from_checkpoint(p) for p in ckpt_paths])

# We need to set the trainer to something to avoid errors
model._trainer = object()
Expand Down
6 changes: 3 additions & 3 deletions pl_crossvalidate/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,16 @@ def out_of_sample_score(

# temporarily replace the predict_step method with the score method to use the trainer.predict method
_orig_predict_method = model.predict_step
model.predict_step = model.score

# run prection on each fold
outputs = []
for i, ckpt_path in enumerate(ckpt_paths):
self._set_fold_index(i, datamodule=datamodule)
model.load_from_checkpoint(ckpt_path)
model = type(model).load_from_checkpoint(ckpt_path)
model.predict_step = score_method
out = self.predict(model=model, dataloaders=datamodule.test_dataloader())
model.predict_step = _orig_predict_method
outputs.append(torch.cat(out, 0))
model.predict_step = _orig_predict_method

# reorder to match the order of the dataset
test_indices = torch.cat([torch.tensor(test) for _, test in datamodule.splits])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_trainer_initialization(arguments, expected):
@pytest.mark.parametrize("accelerator", ["cpu", "gpu"])
def test_cross_validate(tmp_path, accelerator):
"""Test cross validation finish a basic run."""
if not torch.cuda.is_available() and torch.cuda.device_count() < 1:
if accelerator == "gpu" and not torch.cuda.is_available() and torch.cuda.device_count() < 1:
pytest.skip("test requires cuda support")

model = BoringModel()
Expand Down