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]: the tiling does not work with the pytorch inference #2328

Open
1 task done
nickjyj opened this issue Sep 30, 2024 · 1 comment
Open
1 task done

[Bug]: the tiling does not work with the pytorch inference #2328

nickjyj opened this issue Sep 30, 2024 · 1 comment

Comments

@nickjyj
Copy link

nickjyj commented Sep 30, 2024

Describe the bug

I have successfully trained a Padim model using the images size of 256x256 and the tiling size of 224x224. Then, I converted it to a pytorch model. When I started inference using pytorch inferencer, it throwed an error:

 Traceback (most recent call last):
   File "/app/infer.py", line 8, in <module>
     out = model1.forward(input.cuda())
   File "/app/anomalib/src/anomalib/deploy/inferencers/torch_inferencer.py", line 223, in forward
     return self.model(image)
   File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1527, in _wrapped_call_impl
     return self._call_impl(*args, **kwargs)
   File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1536, in _call_impl
     return forward_call(*args, **kwargs)
   File "/app/anomalib/src/anomalib/deploy/export.py", line 86, in forward
     return self.model(batch)
   File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1527, in _wrapped_call_impl
     return self._call_impl(*args, **kwargs)
   File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1536, in _call_impl
     return forward_call(*args, **kwargs)
   File "/app/anomalib/src/anomalib/models/image/padim/torch_model.py", line 147, in forward
     output = self.anomaly_map_generator(
   File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1527, in _wrapped_call_impl
     return self._call_impl(*args, **kwargs)
   File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1536, in _call_impl
     return forward_call(*args, **kwargs)
   File "/app/anomalib/src/anomalib/models/image/padim/anomaly_map.py", line 133, in forward
     return self.compute_anomaly_map(embedding, mean, inv_covariance, image_size=image_size)
   File "/app/anomalib/src/anomalib/models/image/padim/anomaly_map.py", line 101, in compute_anomaly_map
     score_map = self.compute_distance(
   File "/app/anomalib/src/anomalib/models/image/padim/anomaly_map.py", line 45, in compute_distance
     delta = (embedding - mean).permute(2, 0, 1)
 RuntimeError: The size of tensor a (4096) must match the size of tensor b (65536) at non-singleton dimension 2

It seems that the exported models, neither ckpt nor pt file contains the tiling information, which leads to the shape mismatch.

Dataset

Folder

Model

PADiM

Steps to reproduce the behavior

  1. train a Padim model using any datasets with the tiling size of 224x224 and the image size of 256x256
  2. convert to a pytorch model using anomalib cli: anomalib export --model Padim --export_type torch --ckpt_path /app/out/weights/lightning/model.ckpt --default_root_dir /app/out
  3. run the script provided below:
import torch
from anomalib.deploy.inferencers.torch_inferencer import TorchInferencer

# change the path to your model
model_path = '/app/out/weights/torch/model.pt'

model1 = TorchInferencer(model_path)
input = torch.rand(1,3,512,512)
out = model1.forward(input.cuda())

OS information

the latest commit in the main branch

Expected behavior

it should run without errors

Screenshots

No response

Pip/GitHub

GitHub

What version/branch did you use?

main

Configuration YAML

n/a

Logs

as described above

Code of Conduct

  • I agree to follow this project's Code of Conduct
@blaz-r
Copy link
Contributor

blaz-r commented Oct 1, 2024

It is highly possible that the Tiler is not exported in the checkpoint. I'll verify this tomorrow and think about a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants