openvinotoolkit / anomalib

An anomaly detection library comprising state-of-the-art algorithms and features such as experiment management, hyper-parameter optimization, and edge inference.
https://anomalib.readthedocs.io/en/latest/
Apache License 2.0
3.4k stars 615 forks source link

input_shape error when using notebook Fastflow demo #2135

Closed lzd-1230 closed 3 weeks ago

lzd-1230 commented 3 weeks ago

When I used notebook demo and replace the datapart to my own (1080, 1920) image

datamodule = Folder(
    root=dataset_root,
    name="phone-half",
    normal_dir="good",
    abnormal_dir="flaw",
    mask_dir="mask/flaw",
    image_size=(1080, 1920),
    train_batch_size=1,
    eval_batch_size=1,
    num_workers=1,
    test_split_mode = TestSplitMode.FROM_DIR,
    task=task,
)
datamodule.setup()

I got error like

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[9], [line 1](vscode-notebook-cell:?execution_count=9&line=1)
----> [1](vscode-notebook-cell:?execution_count=9&line=1) engine.fit(datamodule=datamodule, model=model)

File ~/patchcore-inspection/anomalib/anomalib-src/src/anomalib/engine/engine.py:541, in Engine.fit(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
    [539](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/patchcore-inspection/anomalib/anomalib-src/src/anomalib/engine/engine.py:539)     self.trainer.validate(model, val_dataloaders, datamodule=datamodule, ckpt_path=ckpt_path)
    [540](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/patchcore-inspection/anomalib/anomalib-src/src/anomalib/engine/engine.py:540) else:
--> [541](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/patchcore-inspection/anomalib/anomalib-src/src/anomalib/engine/engine.py:541)     self.trainer.fit(model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)

File ~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py:544, in Trainer.fit(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
    [542](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py:542) self.state.status = TrainerStatus.RUNNING
    [543](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py:543) self.training = True
--> [544](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py:544) call._call_and_handle_interrupt(
    [545](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py:545)     self, self._fit_impl, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path
    [546](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py:546) )

File ~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/call.py:44, in _call_and_handle_interrupt(trainer, trainer_fn, *args, **kwargs)
     [42](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/call.py:42)     if trainer.strategy.launcher is not None:
     [43](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/call.py:43)         return trainer.strategy.launcher.launch(trainer_fn, *args, trainer=trainer, **kwargs)
---> [44](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/call.py:44)     return trainer_fn(*args, **kwargs)
     [46](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/call.py:46) except _TunerExitException:
     [47](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/call.py:47)     _call_teardown_hook(trainer)

File ~/miniconda3/envs/anomalib/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py:580, in Trainer._fit_impl(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
...
   [2571](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/torch/nn/functional.py:2571)         layer_norm, (input, weight, bias), input, normalized_shape, weight=weight, bias=bias, eps=eps
   [2572](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/torch/nn/functional.py:2572)     )
-> [2573](https://vscode-remote+ssh-002dremote-002blsx-002d4090.vscode-resource.vscode-cdn.net/home/lzd/patchcore-inspection/anomalib/~/miniconda3/envs/anomalib/lib/python3.10/site-packages/torch/nn/functional.py:2573) return torch.layer_norm(input, normalized_shape, weight, bias, eps, torch.backends.cudnn.enabled)

RuntimeError: Given normalized_shape=[256, 67, 120], expected input with shape [*, 256, 67, 120], but got input of size[1, 256, 68, 120]

And I don't know how to solve it, I've tested my datamodule in Patchcore model and it works perfectly, but wrong in this notebook using FastFlow model.

after change the image_size = 256 still get error like this.

RuntimeError: Given normalized_shape=[64, 64, 64], expected input with shape [*, 64, 64, 64], but got input of size[1, 64, 64, 114]

I couldn't figure out what's wrong with that. Any advice would be great thanks!

lzd-1230 commented 3 weeks ago

I solve it by adding transform method to Floder() and force data to be square. like, and make the input data to be torch.Size([3, 1920, 1920])

transform_img = transforms.Compose([
    transforms.Resize(resize),  
    transforms.CenterCrop(imagesize),  
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])