Replies: 3 comments 2 replies
-
@Narc17, I tried to reproduce this issue, here is my findings: i see that you are trying to create an AUPRO as an image metric. This won't work as it is a pixel-level metric. If you, therefore, remove the image-level metric that you set in Evaluator it should work. Here is the code I tried, which achieved 0.945 AUPRO score. from anomalib.data import MVTec
from anomalib.engine import Engine
from anomalib.metrics import AUPRO, Evaluator
from anomalib.models import Patchcore
evaluator = Evaluator(
test_metrics=[
# NOTE: AUPRO only works with pixel-level metrics.
# AUPRO(fields=["pred_score", "gt_label"], prefix="image_"),
AUPRO(fields=["anomaly_map", "gt_mask"], prefix="pixel_", strict=False),
],
)
datamodule = MVTec()
model = Patchcore(evaluator=evaluator)
engine = Engine()
engine.fit(model=model, datamodule=datamodule)
engine.test(model=model, datamodule=datamodule) Here is the terminal output: ❯ python debug_aupro.py
/home/sa/Projects/anomalib/.venv/lib/python3.11/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)
/home/sa/Projects/anomalib/debug_aupro.py:13: DeprecationWarning: MVTec is deprecated and will be removed in a future version. Please use MVTecAD instead.
datamodule = MVTec()
INFO:anomalib.models.components.base.anomalib_module:Initializing Patchcore model.
/home/sa/Projects/anomalib/.venv/lib/python3.11/site-packages/lightning/pytorch/utilities/parsing.py:209: Attribute 'evaluator' is an instance of `nn.Module` and is already saved during checkpointing. It is recommended to ignore them using `self.save_hyperparameters(ignore=['evaluator'])`.
INFO:timm.models._builder:Loading pretrained weights from Hugging Face hub (timm/wide_resnet50_2.racm_in1k)
INFO:timm.models._hub:[timm/wide_resnet50_2.racm_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 (cuda), 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:You are using a CUDA device ('NVIDIA GeForce RTX 3090') that has Tensor Cores. To properly utilize them, you should set `torch.set_float32_matmul_precision('medium' | 'high')` which will trade-off precision for performance. For more details, read https://pytorch.org/docs/stable/generated/torch.set_float32_matmul_precision.html#torch.set_float32_matmul_precision
Initializing distributed: GLOBAL_RANK: 0, MEMBER: 1/2
/home/sa/.cursor-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/_vendored/force_pydevd.py:18: UserWarning: incompatible copy of pydevd already imported:
/home/sa/Projects/anomalib/.venv/lib/python3.11/site-packages/pydevd_plugins/extensions/pydevd_plugin_omegaconf.py
warnings.warn(msg + ':\n {}'.format('\n '.join(_unvendored)))
/home/sa/Projects/anomalib/.venv/lib/python3.11/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)
/home/sa/Projects/anomalib/debug_aupro.py:13: DeprecationWarning: MVTec is deprecated and will be removed in a future version. Please use MVTecAD instead.
datamodule = MVTec()
INFO:anomalib.models.components.base.anomalib_module:Initializing Patchcore model.
INFO:timm.models._builder:Loading pretrained weights from Hugging Face hub (timm/wide_resnet50_2.racm_in1k)
INFO:timm.models._hub:[timm/wide_resnet50_2.racm_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.
Initializing distributed: GLOBAL_RANK: 1, MEMBER: 2/2
INFO:lightning_fabric.utilities.rank_zero:----------------------------------------------------------------------------------------------------
distributed_backend=nccl
All distributed processes registered. Starting with 2 processes
----------------------------------------------------------------------------------------------------
INFO:anomalib.data.datamodules.image.mvtecad:Found the dataset.
WARNING:anomalib.metrics.evaluator:Number of devices is greater than 1, setting compute_on_cpu to False.
WARNING:anomalib.metrics.evaluator:Number of devices is greater than 1, setting compute_on_cpu to False.
LOCAL_RANK: 1 - CUDA_VISIBLE_DEVICES: [0,1]
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1]
/home/sa/Projects/anomalib/.venv/lib/python3.11/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 | PostProcessor | 0 | train
2 | evaluator | Evaluator | 0 | train
3 | model | PatchcoreModel | 24.9 M | train
----------------------------------------------------------
24.9 M Trainable params
0 Non-trainable params
24.9 M Total params
99.450 Total estimated model params size (MB)
16 Modules in train mode
174 Modules in eval mode
Epoch 0: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:05<00:00, 0.78it/sINFO:anomalib.models.image.patchcore.lightning_model:Aggregating the embedding extracted from the training set. | 0/? [00:00<?, ?it/s]
INFO:anomalib.models.image.patchcore.lightning_model:Applying core-set subsampling to get the embedding.
INFO:anomalib.models.image.patchcore.lightning_model:Aggregating the embedding extracted from the training set.
INFO:anomalib.models.image.patchcore.lightning_model:Applying core-set subsampling to get the embedding.
Selecting Coreset Indices.: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 10752/10752 [00:09<00:00, 1101.15it/s]
Selecting Coreset Indices.: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 10752/10752 [00:10<00:00, 1074.65it/s]
Epoch 0: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:28<00:00, 0.14it/s]INFO:lightning_fabric.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=1` reached.
Epoch 0: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:32<00:00, 0.12it/s]
INFO:anomalib.callbacks.timer:Training took 34.07 seconds
INFO:anomalib.callbacks.timer:Training took 34.11 seconds
INFO:lightning_fabric.utilities.rank_zero:The following callbacks returned in `LightningModule.configure_callbacks` will override existing callbacks passed to Trainer: Evaluator, ImageVisualizer, PostProcessor, PreProcessor
INFO:anomalib.data.datamodules.image.mvtecad:Found the dataset.
WARNING:anomalib.metrics.evaluator:Number of devices is greater than 1, setting compute_on_cpu to False.
WARNING:anomalib.metrics.evaluator:Number of devices is greater than 1, setting compute_on_cpu to False.
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1]
LOCAL_RANK: 1 - CUDA_VISIBLE_DEVICES: [0,1]
/home/sa/Projects/anomalib/.venv/lib/python3.11/site-packages/lightning/pytorch/trainer/connectors/data_connector.py:216: Using `DistributedSampler` with the dataloaders. During `trainer.test()`, it is recommended to use `Trainer(devices=1, num_nodes=1)` to ensure each sample/batch gets evaluated exactly once. Otherwise, multi-device settings use `DistributedSampler` that replicates some samples to make sure all devices have same batch size in case of uneven inputs.
Testing DataLoader 0: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:02<00:00, 0.84it/s]INFO:anomalib.callbacks.timer:Testing took 7.9087910652160645 seconds
Throughput (batch_size=32) : 10.494650739358288 FPS
INFO:anomalib.callbacks.timer:Testing took 7.911909103393555 seconds
Throughput (batch_size=32) : 10.490514857457079 FPS
Testing DataLoader 0: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:04<00:00, 0.50it/s]
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Test metric ┃ DataLoader 0 ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ pixel_AUPRO │ 0.945563793182373 │
└───────────────────────────┴───────────────────────────┘ |
Beta Was this translation helpful? Give feedback.
-
I'm moving this to Q&A now as it does not seem to be an issue. Let's continue the discussion there. |
Beta Was this translation helpful? Give feedback.
-
I found that the issue is in this: ![]() Line 219. I print out and the fpr is in "cpu" but the self.fpr_limit is in "cuda" |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
Greetings!
I faced a tensor device inconsistent issue while applying
AUPRO
inPatchcore
validation/testing. The model training was did utilizecuda
and was succeeded. However, it raised error about two tensors on different devices in validation or testing. This issue only occurs onAUPRO
metric. How can I slove this issue?Thanks!
Dataset
MVTecAD
Model
PatchCore
Steps to reproduce the behavior
Evaluator
withAUPRO
image and pixel level metrics.Evaluator(test_metrics=[AUPRO(fields=["pred_score", "gt_label"], prefix="image_"), AUPRO(fields=["anomaly_map", "gt_mask"], prefix="pixel_", strict=False) ])
Patchcore
with evaluator={evaluator created with step 1}Engine.fit()
andEngine.test()
Engine.test()
OS information
OS information:
Evaluator(test_metrics=[AUPRO(fields=["pred_score", "gt_label"], prefix="image_"), AUPRO(fields=["anomaly_map", "gt_mask"], prefix="pixel_", strict=False) ])
Expected behavior
AUPRO
metric could be handled GPU(cuda) computations.Screenshots
No response
Pip/GitHub
pip
What version/branch did you use?
2.0.0
Configuration YAML
Did not utilize yaml configs.
Logs
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions