Skip to content

Commit

Permalink
Fix latest torch and torchvision compatibility (#2548)
Browse files Browse the repository at this point in the history
* disable weights only loading

* add transform wrapper method
  • Loading branch information
djdameln authored Feb 4, 2025
1 parent b37284e commit 800fd07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/anomalib/data/transforms/center_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,17 @@ def _transform(self, inpt: torch.Tensor, params: dict[str, Any]) -> torch.Tensor
"""
del params
return center_crop_image(inpt, output_size=self.size)

def transform(self, inpt: torch.Tensor, params: dict[str, Any]) -> torch.Tensor:
"""Wrapper for self._transform.
This is to ensure compatibility with Torchvision 2.6+, where the `_transform` method was renamed to `transform`.
Args:
inpt (torch.Tensor): Input tensor to transform
params (dict[str, Any]): Transform parameters (unused)
Returns:
torch.Tensor: Center-cropped output tensor
"""
return self._transform(inpt, params)
2 changes: 1 addition & 1 deletion src/anomalib/deploy/inferencers/torch_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _load_checkpoint(self, path: str | Path) -> dict:
msg = f"Unknown PyTorch checkpoint format {path.suffix}. Make sure you save the PyTorch model."
raise ValueError(msg)

return torch.load(path, map_location=self.device)
return torch.load(path, map_location=self.device, weights_only=False)

def load_model(self, path: str | Path) -> nn.Module:
"""Load the PyTorch model.
Expand Down

0 comments on commit 800fd07

Please sign in to comment.