anomalib
anomalib copied to clipboard
[Bug]: IndexError when running PaDiM Example
Describe the bug
I am copy pasting the code from examples/api/03_models/padim.py and trying to run it. When fitting the model, there is an IndexError somewhere in the implementation of engine.fit().
Dataset
MVTec
Model
PADiM
Steps to reproduce the behavior
Steps to reproduce the behavior.
pip install anomalibanomalib install- run the following code
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
"""Example showing how to use the Padim model.
PaDiM (Patch Distribution Modeling) is a model that uses pretrained CNN features
and multivariate Gaussian modeling for anomaly detection.
"""
from anomalib.data import MVTec
from anomalib.engine import Engine
from anomalib.models import Padim
if __name__ == '__main__':
# 1. Basic Usage
# Initialize with default settings
model = Padim()
# 2. Custom Configuration
# Configure model parameters
model = Padim(
backbone="resnet18", # Feature extraction backbone
layers=["layer1", "layer2", "layer3"], # Layers to extract features from
pre_trained=True, # Use pretrained weights
n_features=100, # Number of features to retain
)
# 3. Training Pipeline
# Set up the complete training pipeline
datamodule = MVTec(
root="./datasets/MVTec",
category="bottle",
train_batch_size=32,
eval_batch_size=32, # Important for feature extraction
)
# Initialize training engine with specific settings
engine = Engine(
max_epochs=1, # PaDiM needs only one epoch
accelerator="auto", # Automatically detect GPU/CPU
devices=1, # Number of devices to use
)
# Train the model
engine.fit(
model=model,
datamodule=datamodule,
)
OS information
OS information:
- OS: macOS 14.6.1
- Python version: 3.12.7
- Anomalib version: 2.0.0b2
- PyTorch version: 2.6.0
Expected behavior
I expect it to fit the model and exit. There is no prediction step here, so I'm not expecting much other than some terminal messages providing some details about fitting.
Screenshots
No response
Pip/GitHub
pip
What version/branch did you use?
24.2
Configuration YAML
n/a
Logs
$ python3 padim.py
-----------------------------------------------------------------------------------------------------------
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
INFO:anomalib.models.components.base.anomalib_module:Initializing Padim model.
INFO:timm.models._builder:Loading pretrained weights from Hugging Face hub (timm/resnet18.a1_in1k)
INFO:timm.models._hub:[timm/resnet18.a1_in1k] Safe alternative available for 'pytorch_model.bin' (as 'model.safetensors'). Loading weights using safetensors.
INFO:timm.models._builder:Missing keys (fc.weight, fc.bias) discovered while loading pretrained weights. This is expected if model is being adapted.
INFO:anomalib.models.components.base.anomalib_module:Initializing Padim model.
INFO:timm.models._builder:Loading pretrained weights from Hugging Face hub (timm/resnet18.a1_in1k)
INFO:timm.models._hub:[timm/resnet18.a1_in1k] Safe alternative available for 'pytorch_model.bin' (as 'model.safetensors'). Loading weights using safetensors.
INFO:timm.models._builder:Missing keys (fc.weight, fc.bias) discovered while loading pretrained weights. This is expected if model is being adapted.
GPU available: True (mps), used: True
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
`Trainer(val_check_interval=1.0)` was configured so validation will run at the end of the training epoch..
INFO:anomalib.data.datamodules.image.mvtec:Found the dataset.
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/core/optimizer.py:183: `LightningModule.configure_optimizers` returned `None`, this fit will run with no optimizer
| Name | Type | Params | Mode
-----------------------------------------------------------------
0 | pre_processor | PreProcessor | 0 | train
1 | post_processor | OneClassPostProcessor | 0 | train
2 | evaluator | Evaluator | 0 | train
3 | model | PadimModel | 2.8 M | train
-----------------------------------------------------------------
2.8 M Trainable params
0 Non-trainable params
2.8 M Total params
11.131 Total estimated model params size (MB)
19 Modules in train mode
69 Modules in eval mode
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/connectors/data_connector.py:420: Consider setting `persistent_workers=True` in 'train_dataloader' to speed up the dataloader worker initialization.
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/connectors/data_connector.py:420: Consider setting `persistent_workers=True` in 'val_dataloader' to speed up the dataloader worker initialization.
Epoch 0: 0%| | 0/7 [00:00<?, ?it/s]/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
Epoch 0: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:04<00:00, 1.54it/sINFO:anomalib.models.image.padim.lightning_model:Aggregating the embedding extracted from the training set. | 0/? [00:00<?, ?it/s]
INFO:anomalib.models.image.padim.lightning_model:Fitting a Gaussian to the embedding collected from the training set.
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
Traceback (most recent call last):████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 5.53it/s]
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/src/padim.py", line 45, in <module>
engine.fit(
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/anomalib/engine/engine.py", line 416, in fit
self.trainer.fit(model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/trainer.py", line 539, in fit
call._call_and_handle_interrupt(
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/call.py", line 47, in _call_and_handle_interrupt
return trainer_fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/trainer.py", line 575, in _fit_impl
self._run(model, ckpt_path=ckpt_path)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/trainer.py", line 982, in _run
results = self._run_stage()
^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/trainer.py", line 1026, in _run_stage
self.fit_loop.run()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/fit_loop.py", line 216, in run
self.advance()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/fit_loop.py", line 455, in advance
self.epoch_loop.run(self._data_fetcher)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/training_epoch_loop.py", line 151, in run
self.on_advance_end(data_fetcher)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/training_epoch_loop.py", line 370, in on_advance_end
self.val_loop.run()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/utilities.py", line 179, in _decorator
return loop_run(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/evaluation_loop.py", line 151, in run
return self.on_run_end()
^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/evaluation_loop.py", line 291, in on_run_end
self._on_evaluation_epoch_end()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/evaluation_loop.py", line 370, in _on_evaluation_epoch_end
call._call_callback_hooks(trainer, hook_name)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/call.py", line 222, in _call_callback_hooks
fn(trainer, trainer.lightning_module, *args, **kwargs)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/anomalib/post_processing/one_class.py", line 108, in on_validation_epoch_end
self._pixel_threshold.compute()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/torchmetrics/metric.py", line 700, in wrapped_func
value = _squeeze_if_scalar(compute(*args, **kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/anomalib/metrics/threshold/f1_adaptive_threshold.py", line 113, in compute
self.value = thresholds[torch.argmax(f1_score)]
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
IndexError: index 4938361 is out of bounds for dimension 0 with size 4938361
$
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Hi! I cannot reproduce the error with the latest version of anomalib. Please try to use the development install and check if your problem still persists: https://github.com/openvinotoolkit/anomalib?tab=readme-ov-file#-development-install
I tried using the development install, and the same issue arose.
Pip List:
Package Version Editable project location
---------------------------------- -------------- --------------------------------------------------------------
about-time 4.2.1
absl-py 2.1.0
aiofiles 23.2.1
aiohappyeyeballs 2.4.6
aiohttp 3.11.12
aiosignal 1.3.2
alembic 1.14.1
alive-progress 3.2.0
annotated-types 0.7.0
anomalib 2.0.0b2 /Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/anomalib
antlr4-python3-runtime 4.9.3
anyio 4.8.0
appnope 0.1.4
argon2-cffi 23.1.0
argon2-cffi-bindings 21.2.0
arrow 1.3.0
asttokens 3.0.0
async-lru 2.0.4
attrs 25.1.0
autograd 1.7.0
av 14.1.0
babel 2.17.0
beautifulsoup4 4.13.3
bleach 6.2.0
blinker 1.9.0
cachetools 5.5.1
certifi 2025.1.31
cffi 1.17.1
charset-normalizer 3.4.1
click 8.1.8
cloudpickle 3.1.1
cma 3.2.2
comet_ml 3.49.0
comm 0.2.2
configobj 5.0.9
contourpy 1.3.1
cycler 0.12.1
databricks-sdk 0.43.0
debugpy 1.8.12
decorator 5.1.1
defusedxml 0.7.1
Deprecated 1.2.18
dill 0.3.9
distro 1.9.0
docker 7.1.0
docker-pycreds 0.4.0
docstring_parser 0.16
dulwich 0.22.7
einops 0.8.1
everett 3.1.0
executing 2.2.0
fastapi 0.115.8
fastjsonschema 2.21.1
ffmpy 0.5.0
filelock 3.17.0
Flask 3.1.0
fonttools 4.56.0
fqdn 1.5.1
FrEIA 0.2
frozenlist 1.5.0
fsspec 2025.2.0
ftfy 6.3.1
gitdb 4.0.12
GitPython 3.1.44
google-auth 2.38.0
gradio 5.15.0
gradio_client 1.7.0
grapheme 0.6.0
graphene 3.4.3
graphql-core 3.2.6
graphql-relay 3.2.0
grpcio 1.70.0
gunicorn 23.0.0
h11 0.14.0
httpcore 1.0.7
httpx 0.28.1
huggingface-hub 0.28.1
idna 3.10
imageio 2.37.0
importlib_metadata 8.5.0
importlib_resources 6.5.2
ipykernel 6.29.5
ipython 8.32.0
ipywidgets 8.1.5
isoduration 20.11.0
itsdangerous 2.2.0
jedi 0.19.2
Jinja2 3.1.5
jiter 0.8.2
joblib 1.4.2
json5 0.10.0
jsonargparse 4.36.0
jsonpointer 3.0.0
jsonschema 4.23.0
jsonschema-specifications 2024.10.1
jstyleson 0.0.2
jupyter_client 8.6.3
jupyter_core 5.7.2
jupyter-events 0.12.0
jupyter-lsp 2.2.5
jupyter_server 2.15.0
jupyter_server_terminals 0.5.3
jupyterlab 4.3.5
jupyterlab_pygments 0.3.0
jupyterlab_server 2.27.3
jupyterlab_widgets 3.0.13
kiwisolver 1.4.8
kornia 0.8.0
kornia_rs 0.1.8
lazy_loader 0.4
lightning 2.5.0.post0
lightning-utilities 0.12.0
Mako 1.3.9
Markdown 3.7
markdown-it-py 3.0.0
MarkupSafe 2.1.5
matplotlib 3.10.0
matplotlib-inline 0.1.7
mdurl 0.1.2
mistune 3.1.1
mlflow 2.20.1
mlflow-skinny 2.20.1
mpmath 1.3.0
multidict 6.1.0
natsort 8.4.0
nbclient 0.10.2
nbconvert 7.16.6
nbformat 5.10.4
nest-asyncio 1.6.0
networkx 3.3
ninja 1.11.1.3
nncf 2.15.0
notebook 7.3.2
notebook_shim 0.2.4
numpy 2.1.1
ollama 0.4.7
omegaconf 2.3.0
onnx 1.17.0
open-clip-torch 2.24.0
openai 1.61.1
opencv-python 4.11.0.86
opentelemetry-api 1.30.0
opentelemetry-sdk 1.30.0
opentelemetry-semantic-conventions 0.51b0
openvino 2025.0.0
openvino-telemetry 2025.0.0
orjson 3.10.15
overrides 7.7.0
packaging 24.2
pandas 2.2.3
pandocfilters 1.5.1
parso 0.8.4
pexpect 4.9.0
pillow 11.1.0
pip 25.0
platformdirs 4.3.6
prometheus_client 0.21.1
prompt_toolkit 3.0.50
propcache 0.2.1
protobuf 5.29.3
psutil 6.1.1
ptyprocess 0.7.0
pure_eval 0.2.3
py-cpuinfo 9.0.0
pyarrow 18.1.0
pyasn1 0.6.1
pyasn1_modules 0.4.1
pycparser 2.22
pydantic 2.10.6
pydantic_core 2.27.2
pydot 2.0.0
pydub 0.25.1
Pygments 2.19.1
pymoo 0.6.1.3
pyparsing 3.2.1
python-box 6.1.0
python-dateutil 2.9.0.post0
python-dotenv 1.0.1
python-json-logger 3.2.1
python-multipart 0.0.20
pytorch-lightning 2.5.0.post0
pytz 2025.1
PyYAML 6.0.2
pyzmq 26.2.1
referencing 0.36.2
regex 2024.11.6
requests 2.32.3
requests-toolbelt 1.0.0
rfc3339-validator 0.1.4
rfc3986-validator 0.1.1
rich 13.9.4
rich-argparse 1.7.0
rpds-py 0.22.3
rsa 4.9
ruff 0.9.6
safehttpx 0.1.6
safetensors 0.5.2
scikit-image 0.25.1
scikit-learn 1.6.1
scipy 1.15.1
seaborn 0.13.2
semantic-version 2.10.0
Send2Trash 1.8.3
sentencepiece 0.2.0
sentry-sdk 2.20.0
setproctitle 1.3.4
setuptools 75.8.0
shellingham 1.5.4
simplejson 3.19.3
six 1.17.0
smmap 5.0.2
sniffio 1.3.1
soupsieve 2.6
SQLAlchemy 2.0.38
sqlparse 0.5.3
stack-data 0.6.3
starlette 0.45.3
sympy 1.13.1
tabulate 0.9.0
tensorboard 2.18.0
tensorboard-data-server 0.7.2
terminado 0.18.1
threadpoolctl 3.5.0
tifffile 2025.1.10
timm 1.0.14
tinycss2 1.4.0
tokenizers 0.21.0
tomlkit 0.13.2
torch 2.6.0
torchmetrics 1.6.1
torchvision 0.21.0
tornado 6.4.2
tqdm 4.67.1
traitlets 5.14.3
transformers 4.48.3
typer 0.15.1
types-python-dateutil 2.9.0.20241206
typeshed_client 2.7.0
typing_extensions 4.12.2
tzdata 2025.1
ultralytics 8.3.74
ultralytics-thop 2.0.14
uri-template 1.3.0
urllib3 2.3.0
uvicorn 0.34.0
wandb 0.19.6
wcwidth 0.2.13
webcolors 24.11.1
webencodings 0.5.1
websocket-client 1.8.0
websockets 14.2
Werkzeug 3.1.3
widgetsnbextension 4.0.13
wrapt 1.17.2
wurlitzer 3.1.1
yarl 1.18.3
zipp 3.21.0
Error Logs:
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
INFO:anomalib.models.components.base.anomalib_module:Initializing Padim model.
INFO:timm.models._builder:Loading pretrained weights from Hugging Face hub (timm/resnet18.a1_in1k)
INFO:timm.models._hub:[timm/resnet18.a1_in1k] Safe alternative available for 'pytorch_model.bin' (as 'model.safetensors'). Loading weights using safetensors.
INFO:timm.models._builder:Missing keys (fc.weight, fc.bias) discovered while loading pretrained weights. This is expected if model is being adapted.
INFO:anomalib.models.components.base.anomalib_module:Initializing Padim model.
INFO:timm.models._builder:Loading pretrained weights from Hugging Face hub (timm/resnet18.a1_in1k)
INFO:timm.models._hub:[timm/resnet18.a1_in1k] Safe alternative available for 'pytorch_model.bin' (as 'model.safetensors'). Loading weights using safetensors.
INFO:timm.models._builder:Missing keys (fc.weight, fc.bias) discovered while loading pretrained weights. This is expected if model is being adapted.
INFO:lightning_fabric.utilities.rank_zero:GPU available: True (mps), used: True
INFO:lightning_fabric.utilities.rank_zero:TPU available: False, using: 0 TPU cores
INFO:lightning_fabric.utilities.rank_zero:HPU available: False, using: 0 HPUs
INFO:lightning_fabric.utilities.rank_zero:`Trainer(val_check_interval=1.0)` was configured so validation will run at the end of the training epoch..
INFO:anomalib.data.datamodules.image.mvtec:Found the dataset.
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/core/optimizer.py:183: `LightningModule.configure_optimizers` returned `None`, this fit will run with no optimizer
| Name | Type | Params | Mode
-----------------------------------------------------------------
0 | pre_processor | PreProcessor | 0 | train
1 | post_processor | OneClassPostProcessor | 0 | train
2 | evaluator | Evaluator | 0 | train
3 | model | PadimModel | 2.8 M | train
-----------------------------------------------------------------
2.8 M Trainable params
0 Non-trainable params
2.8 M Total params
11.131 Total estimated model params size (MB)
19 Modules in train mode
69 Modules in eval mode
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/connectors/data_connector.py:420: Consider setting `persistent_workers=True` in 'train_dataloader' to speed up the dataloader worker initialization.
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/connectors/data_connector.py:420: Consider setting `persistent_workers=True` in 'val_dataloader' to speed up the dataloader worker initialization.
Epoch 0: 0%| | 0/7 [00:00<?, ?it/s]<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
Epoch 0: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:05<00:00, 1.25it/sINFO:anomalib.models.image.padim.lightning_model:Aggregating the embedding extracted from the training set. | 0/? [00:00<?, ?it/s]
INFO:anomalib.models.image.padim.lightning_model:Fitting a Gaussian to the embedding collected from the training set.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
<frozen importlib.util>:207: DeprecationWarning: The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. Please replace `openvino.runtime` with `openvino`.
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning)
Traceback (most recent call last):█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 5.63it/s]
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/src/test.py", line 51, in <module>
engine.fit(
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/anomalib/src/anomalib/engine/engine.py", line 416, in fit
self.trainer.fit(model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/trainer.py", line 539, in fit
call._call_and_handle_interrupt(
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/call.py", line 47, in _call_and_handle_interrupt
return trainer_fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/trainer.py", line 575, in _fit_impl
self._run(model, ckpt_path=ckpt_path)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/trainer.py", line 982, in _run
results = self._run_stage()
^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/trainer.py", line 1026, in _run_stage
self.fit_loop.run()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/fit_loop.py", line 216, in run
self.advance()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/fit_loop.py", line 455, in advance
self.epoch_loop.run(self._data_fetcher)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/training_epoch_loop.py", line 151, in run
self.on_advance_end(data_fetcher)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/training_epoch_loop.py", line 370, in on_advance_end
self.val_loop.run()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/utilities.py", line 179, in _decorator
return loop_run(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/evaluation_loop.py", line 151, in run
return self.on_run_end()
^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/evaluation_loop.py", line 291, in on_run_end
self._on_evaluation_epoch_end()
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/loops/evaluation_loop.py", line 370, in _on_evaluation_epoch_end
call._call_callback_hooks(trainer, hook_name)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/lightning/pytorch/trainer/call.py", line 222, in _call_callback_hooks
fn(trainer, trainer.lightning_module, *args, **kwargs)
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/anomalib/src/anomalib/post_processing/one_class.py", line 121, in on_validation_epoch_end
self._pixel_threshold = self._pixel_threshold_metric.compute()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/venv/lib/python3.12/site-packages/torchmetrics/metric.py", line 700, in wrapped_func
value = _squeeze_if_scalar(compute(*args, **kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/anomalib/src/anomalib/metrics/base.py", line 191, in compute
return super().compute() # type: ignore[misc]
^^^^^^^^^^^^^^^^^
File "/Users/johnzeng/Code/DEC/Two-Stage-Doubt-Confirmation/anomalib/src/anomalib/metrics/threshold/f1_adaptive_threshold.py", line 98, in compute
return thresholds if thresholds.dim() == 0 else thresholds[torch.argmax(f1_score)]
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
IndexError: index 4919211 is out of bounds for dimension 0 with size 4919211```
I'm also encountering the same IndexError, using anomalib 2.0.0b2 with a Folder datamodule configured to use only the 'good' images of the hazelnut-mvtech dataset.
But the problem is not reproducible 100% of the time. I was able to train a Patchcore model twice, but all other attempts for Patchcore, Padim or EfficientAd failed.
I also have this issue.
self.value = thresholds[torch.argmax(f1_score)].
I guess it is because thresholds are always having 1 value less than f1_score, I fixed it with self.value = thresholds[torch.argmax(f1_score) - 1].
@zhygallo @JLZ22
If you’re running into issues , you might want to try AnomaVision 🚀
🔥 Production-Ready Deep Learning Library for Anomaly Detection Optimized for both research and deployment, with blazing-fast inference, easy ONNX export, and a flexible, modern API. Works great for manufacturing, quality control, and research scenarios.
It’s stable, actively maintained, and designed to just work.