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

[Bug]: patchcore pytorch error #1798

Closed
1 task done
jth3galv opened this issue Feb 29, 2024 · 6 comments · Fixed by #1944
Closed
1 task done

[Bug]: patchcore pytorch error #1798

jth3galv opened this issue Feb 29, 2024 · 6 comments · Fixed by #1944
Labels
More Info Requested More information requested from the user

Comments

@jth3galv
Copy link

jth3galv commented Feb 29, 2024

Using pathcore gives the following torch error.:

RuntimeError: expand(torch.FloatTensor{[1, 1, 1, 33, 33]}, size=[1, -1, -1, -1]): the number of sizes provided (4) must be greater or equal to the number of dimensions in the tensor (5)

See the log sections for the full error.

Dataset

Folder

Model

PatchCore

Steps to reproduce the behavior

I am trying to use patchcore as by tutorial. I am using a fresh install of anomalib 1.0.0.dev.
Running the following produce an error.


# Import the datamodule
from anomalib.data import Folder
from anomalib.data.utils import TestSplitMode
torchmetrics.functional.auc = AUROC # needed otherwise i get an import error

# Import the required modules
from anomalib.data import MVTec
from anomalib.models import Patchcore
from anomalib.engine import Engine


# Create the datamodule
datamodule = Folder(
    root='myfolder"',
    normal_dir="OK",
    abnormal_dir="KO",
    image_size=(256, 256),
    test_split_mode=TestSplitMode.SYNTHETIC,
    task="segmentation",
)

# Setup the datamodule

datamodule.setup()

model = Patchcore()

engine = Engine()

OS information

OS information:

  • OS: Ubuntu 22.04
  • Python version: 3.10
  • Anomalib version: '1.0.0dev'
  • PyTorch version: '2.2.1+cu121'
  • CPU

Expected behavior

to run without errors

Screenshots

No response

Pip/GitHub

GitHub

What version/branch did you use?

No response

Configuration YAML

?

Logs

RuntimeError                              Traceback (most recent call last)
Cell In[34], line 7
      3 from anomalib.models import Patchcore
      4 from anomalib.engine import Engine
----> 7 model = Patchcore()
      9 engine = Engine()

File ~/code/anomalib/src/anomalib/models/image/patchcore/lightning_model.py:53, in Patchcore.__init__(self, backbone, layers, pre_trained, coreset_sampling_ratio, num_neighbors)
     43 def __init__(
     44     self,
     45     backbone: str = "wide_resnet50_2",
   (...)
     49     num_neighbors: int = 9,
     50 ) -> None:
     51     super().__init__()
---> 53     self.model: PatchcoreModel = PatchcoreModel(
     54         backbone=backbone,
     55         pre_trained=pre_trained,
     56         layers=layers,
     57         num_neighbors=num_neighbors,
     58     )
     59     self.coreset_sampling_ratio = coreset_sampling_ratio
     60     self.embeddings: list[torch.Tensor] = []

File ~/code/anomalib/src/anomalib/models/image/patchcore/torch_model.py:55, in PatchcoreModel.__init__(self, layers, backbone, pre_trained, num_neighbors)
     49 self.feature_extractor = TimmFeatureExtractor(
     50     backbone=self.backbone,
     51     pre_trained=pre_trained,
     52     layers=self.layers,
     53 )
     54 self.feature_pooler = torch.nn.AvgPool2d(3, 1, 1)
---> 55 self.anomaly_map_generator = AnomalyMapGenerator()
     57 self.register_buffer("memory_bank", torch.Tensor())
     58 self.memory_bank: torch.Tensor

File ~/code/anomalib/src/anomalib/models/image/patchcore/anomaly_map.py:30, in AnomalyMapGenerator.__init__(self, sigma)
     28 super().__init__()
     29 kernel_size = 2 * int(4.0 * sigma + 0.5) + 1
---> 30 self.blur = GaussianBlur2d(kernel_size=(kernel_size, kernel_size), sigma=(sigma, sigma), channels=1)

File ~/code/anomalib/src/anomalib/models/components/filters/blur.py:68, in GaussianBlur2d.__init__(self, sigma, channels, kernel_size, normalize, border_type, padding)
     66     self.kernel = normalize_kernel2d(self.kernel)
     67 self.kernel.unsqueeze_(0).unsqueeze_(0)
---> 68 self.kernel = self.kernel.expand(self.channels, -1, -1, -1)
     69 self.border_type = border_type
     70 self.padding = padding

RuntimeError: expand(torch.FloatTensor{[1, 1, 1, 33, 33]}, size=[1, -1, -1, -1]): the number of sizes provided (4) must be greater or equal to the number of dimensions in the tensor (5)

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jth3galv
Copy link
Author

jth3galv commented Mar 4, 2024

I downgraded torch and torchmetrics and it works without errors. I guess some changes will be needed to make it work with newer versions.

voidmain443 referenced this issue Mar 14, 2024
* Fix onnx export by rewriting GaussianBlur

* Address codacy complaints.

Reame variable to something other than `input`

* Move GaussianBlur2d to anomalib.post_processing

* Move blur to `anomlib.models.components.filters`
@lukasvrabel
Copy link

which versions are you using? I downgraded to torch==2.1.0 and it still gives the same error. I cannot downgrade more, because then it conflicts with torchvision being too low...

@jth3galv
Copy link
Author

I am using:

  • torch version: '2.1.2+cu121'
  • torchmetrics version : '0.10.3'
  • torchvision version: '0.16.2+cu121'

In general I think the quickest approach (this is what I finally did ) to make it work is to have a virtual env and install all the requirements as listed in the requirements files.

@voidmain443
Copy link

voidmain443 commented Mar 28, 2024

I downgraded this package and then it works

I downgraded and. then it works I can't resolve this issue just downgrade this package 0.7.0

@samet-akcay
Copy link
Contributor

This is peculiar. I'm having trouble reproducing this issue on my end

@samet-akcay
Copy link
Contributor

@voidmain443, on a fresh environment, can you try pip install anomalib[full]? This works on our side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
More Info Requested More information requested from the user
Projects
None yet
4 participants