Skip to content

[Bug]: Lightning vs exported models have huge pred_score gap #2686

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

Open
1 task done
FedericoDeBona opened this issue May 5, 2025 · 1 comment
Open
1 task done

Comments

@FedericoDeBona
Copy link

Describe the bug

Train a model and inference with engine.predict() gives the correct pred_score, then exporting models in torch, openvino and ONNX, all three give the same wrong pred_score (always higher).
I expect results of the exported models to be equal to the lightning one.

Train code

augmentations = transforms.Compose([transforms.Resize((256, 256))])
datamodule = Folder(
	name="test",
	root=f"/home/rdvisiofy/biiond-worker/local_dataset/64",
	normal_dir="good",
	abnormal_dir="defected",
	augmentations=augmentations
)
model = Patchcore()
engine = Engine()
engine.fit(datamodule=datamodule, model=model)
#Run inference
da = PredictDataset(path = "/home/rdvisiofy/biiond-worker/local_dataset/64/good/01JT3F31K0BWQ1DH2T2RBZBVV7.jpeg")
da = DataLoader(da, collate_fn=da.collate_fn)
pred = engine.predict(dataloaders=da)
#OUTPUT: pred_score 0.2846

Inference code

image = cv2.imread("/home/rdvisiofy/biiond-worker/local_dataset/64/good/01JT3F31K0BWQ1DH2T2RBZBVV7.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (256,256))

# OPENVINO
pred = OpenVINOInferencer(path="/home/rdvisiofy/biiond-worker/deleteme/results/Patchcore/test/latest/weights/openvino/model.bin", device="CPU") .predict(image)
#OUTPUT: pred_score 0.4146552

pred = TorchInferencer("/home/rdvisiofy/biiond-worker/deleteme/results/Patchcore/test/latest/weights/torch/model.pt").predict(image)
#OUTPUT:pred_score 0.4146552

# ONNX
image = image.astype(np.float32)/255.0
image = np.transpose(image, (2, 0, 1))
image = np.expand_dims(image, axis=0)
ort_session = ort.InferenceSession("/home/rdvisiofy/biiond-worker/deleteme/results/Patchcore/test/latest/weights/onnx/model.onnx")
outputs = ort_session.run(None, {"input": image})
print("ONNX", outputs)
#OUTPUT: pred_score  0.41465414

Dataset

Other (please specify in the text field below)

Model

PatchCore

Steps to reproduce the behavior

see above

OS information

OS information:

  • Python version: 3.10.0
  • Anomalib version: 2.0.0, via pip
  • PyTorch version: 2.6.0
  • GPU models and configuration: NVIDIA GeForce RTX 4090
  • Any other relevant information: I'm using a custom dataset

Expected behavior

I expect results of the exported models to be equal to the lightning one.

Screenshots

No response

Pip/GitHub

pip

What version/branch did you use?

2.0.0

Configuration YAML

.

Logs

.

Code of Conduct

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

thanks for reporting this @FedericoDeBona. We'll investigate this

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