Skip to content

The inference results using ONNX differ when loading images with cv2 and PIL.Image #162

Description

@TruongNoDame

I used the following image for inference and received two different results.

Image

With cv2:

image = cv2.imread("path/to/image")
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (128, 32), interpolation=cv2.INTER_CUBIC)
image_tensor = torch.from_numpy(image_resized).float()
image_tensor = image_tensor.permute(2, 0, 1)
image_tensor = (image_tensor / 255.0 - 0.5) / 0.5
image = image_tensor.unsqueeze(0).cuda()
image = to_numpy(image)
outputs = ort_sess.run(None, {input_tensor.name: image})
logits = torch.from_numpy(outputs[0])
output = logits.softmax(-1)
pred, conf_scores = token_decoder.decode(output)

The result is: ['NORTIS']

With PIL.Image:

target_transform =  transforms.Compose([
            transforms.Resize((32, 128), transforms.InterpolationMode.BICUBIC),
            transforms.ToTensor(),
            transforms.Normalize(0.5, 0.5)
        ])
image = Image.open("path/to/image").convert("RGB")
image = target_transform(image).unsqueeze(0).cuda()
image = to_numpy(image)
outputs = ort_sess.run(None, {input_tensor.name: image})
logits = torch.from_numpy(outputs[0])
output = logits.softmax(-1)
pred, conf_scores = token_decoder.decode(output)

The result is: ['SALMON']

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions