Skip to content

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

Closed
@FedericoDeBona

Description

@FedericoDeBona

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions