Skip to content

Int8 Quantized model same output on different inputs #23330

@xyrod6

Description

@xyrod6

System Information

OpenCV python version: 4.7.0.72
Operating System / Platform: Windows 10
Python version: 3.10.6

Detailed description

I am trying to run an int8 quantized model exported to ONNX with opencv backend.

The problem is with the output of the model. Running inference on different images gives the same output tensor.

I tried the inference with pytorch (quantized .pth model) and with openvino (same onnx file) and they give good results.

OpenCV output:
opencv_output_tensor
OpenVino output:
openvino_out_tensor

Steps to reproduce

Note: This is not a fully trained model, so it should not return the correct label!

The model:
int8_quantized.zip
Test images:
test.zip

import openvino.runtime as vino
import cv2
import argparse
import numpy as np
import random
import time

def postprocess(output_blob,_labels):
        _labels=['beige', 'black', 'blue', 'brown', 'golden', 'green', 'grey', 'red', 'silver', 'white']
        batched_class_id_list = []
        for o in output_blob:
            #o=softmax(o)
            class_id_list = o.argsort()[::-1][:1]
            batched_class_id_list.append(class_id_list)
        if len(_labels) > 0:
            batched_predicted_labels = []
            for class_id_list in batched_class_id_list:
                predicted_labels = []
                for class_id in class_id_list:
                    predicted_labels.append(_labels[class_id])
                batched_predicted_labels.append(predicted_labels)
            return batched_predicted_labels
        else:
            return batched_class_id_list

ie = vino.Core()
model = ie.read_model(r"model_path.onnx")
cv_model = cv2.dnn.readNet(r"model_path.onnx")

compiled_model = ie.compile_model(model=model, device_name="CPU")
input_layer = compiled_model.input(0)
img_size = (112, 112) 

src_img = cv2.imread(r"img_path.jpg")
src_size = src_img.shape[:2]

img = cv2.resize(src_img,img_size, interpolation=cv2.INTER_LINEAR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype(dtype=np.float32)
img /= 255.0
input_image = np.expand_dims(img.transpose(2, 0, 1), 0)
cv_model.setInput(input_image)
cv_outputs = cv_model.forward()
cv_pred=postprocess(cv_outputs,None)
pred = compiled_model([input_image])

print("OPENCV OUTPUT TENSOR:")
print(cv_outputs)
print("OPENVINO OUTPUT TENSOR:")
print(pred)
tmp_out=pred[compiled_model.output(0)]

#print(postprocess(tmp_out,None))



BOTH_OUTPUT

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions