anomalib icon indicating copy to clipboard operation
anomalib copied to clipboard

🐞 Padim - Difference in Engine prediction vs ONNX or Torch prediction

Open mbignotti opened this issue 3 months ago • 7 comments

Describe the bug

Hi, I am sorry to bother you, but I can't get an exported ONNX model to work at inference time as inside the engine.predict() method. In particular, when I use the engine.fit(model, datamodule) method, followed by engine.predict(model, datamodule) I get a reasonable result, that seems correct. When I try to use the exported ONNX model, or even the same torch based model without using the engine and the datamodule (a single forward pass), I get a different result that seems just wrong.

I have searched into the code base, but I did not manage to understand what is it that the Engine is doing differently.

I have attached a reproducible example based on the MVTechAD dataset. In the experiment.ipynb notebook you can see that the ONNX and the pytorch model return the same result. The inference on the same image with the engine, however, yields a different result.

Note: In order to re-run the notebook it is necessary to re-download the dataset and to change the paths to the images inside the code.

Dataset

MVTecAD

Model

PADiM

Steps to reproduce the behavior

  1. Unzip the file
  2. Run uv sync and uv lock to install the virtual env
  3. Check the experiment.ipynb notebook

sample.zip

OS information

OS information:

  • OS: Ubuntu 24.04
  • Python version: 3.12.0
  • Anomalib version: 2.1.0
  • PyTorch version: 2.8.0
  • CUDA/cuDNN version: 12.8
  • GPU models and configuration: GeForce RTX 3070

Expected behavior

ONNX and Torch models should yield the same output as engine.predict(), provided the input image is pre-processed correctly.

Screenshots

Image Image

Pip/GitHub

pip

What version/branch did you use?

2.1.0

Configuration YAML

None

Logs

None

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

mbignotti avatar Sep 08 '25 15:09 mbignotti

Thanks for submitting this issue! It has been added to our triage queue. A maintainer will review it shortly.

github-actions[bot] avatar Sep 08 '25 15:09 github-actions[bot]

@mbignotti

I faced the same problem before. The mismatch doesn’t actually come from the ONNX export — ONNX and PyTorch give the same raw results. The difference is that engine.predict() in anomalib adds extra preprocessing and postprocessing steps (normalization, resizing, Gaussian blur smoothing, etc.). That’s why the output from the engine looks different from running a single forward or the exported model directly.

If you want a cleaner setup, you can check out my implementation here: AnomaVision. I exported models into different formats (Torch, ONNX, etc.) and they all give consistent results across inference, without hidden differences. In my benchmarks, it actually outperformed anomalib.

Here’s a demo video: https://youtu.be/6x19w0XtNPk.

DeepKnowledge1 avatar Sep 13 '25 19:09 DeepKnowledge1

Hi @DeepKnowledge1, Thank you for your comment! However, I think that this should have been solved with the Anomalib 2.0 release. As described in the docs, both pre and post processing should now be included in the onnx graph. I also confirmed this while debugging the pytorch model. Hence, I don't think that's the issue, unless there are other callbacks called by the engine that are doing some more pre-processing. I would rather wait for @ashwinvaidya17 or some other maintainer to answer on this.

Thanks again!

mbignotti avatar Sep 14 '25 13:09 mbignotti

Thought this has been addressed with https://github.com/open-edge-platform/anomalib/pull/2843. We'll double check whether this issue still persists

samet-akcay avatar Sep 14 '25 15:09 samet-akcay

Hi! I was wondering if you got the chance to take a look at the issue. I tested with version 2.2.0 by using the same code attached, and the problem seem to still persist.

Thanks again!

mbignotti avatar Nov 07 '25 10:11 mbignotti

@mbignotti : thanks for your detailed bug report! @samet-akcay I got the same problem when I tried to use torch inferencer (Fastflow model - Anomalib v2.2.0). Seems like the bug is still there!

tailyer avatar Nov 20 '25 11:11 tailyer

same problem (Dinomaly model - Anomalib v2.2.0 )when export to OpenVINO model

InvalidSyntaxxx avatar Nov 28 '25 09:11 InvalidSyntaxxx