openvinotoolkit / openvino

OpenVINO™ is an open-source toolkit for optimizing and deploying AI inference
https://docs.openvino.ai
Apache License 2.0
6.72k stars 2.16k forks source link

[Bug]: When loading and inferring a quantized tflite model converted to an openvino model, the results are incorrect. #24045

Open kabirz opened 4 months ago

kabirz commented 4 months ago

OpenVINO Version

2024.0.0

Operating System

Ubuntu 20.04 (LTS)

Device used for inference

CPU

Framework

None

Model used

No response

Issue description

When loading and inferring a quantized tflite model converted to an openvino model, the results are incorrect. the code:

import tflite_runtime.interpreter as tflite
import cv2
import openvino as ov
from darknet import Darknet
from pprint import pprint

core = ov.Core()
dn = Darknet()

# ov model
model = ov.convert_model('face.tflite')
compiled_model = core.compile_model(model=model, device_name='CPU')
# tflite model
interpreter = tflite.Interpreter(model_path='face.tflite')
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
pprint(input_details)
pprint(output_details)
# input image
img = cv2.imread('face_00003.jpg')
input_img = dn.preprocess(img, (56, 56), input_details[0]['quantization'], input_details[0]['dtype'])

# ov model invoke
out1 = compiled_model([input_img])[0]

# tflite invoke
interpreter.set_tensor(input_details[0]['index'], input_img)
interpreter.invoke()
out2 = interpreter.get_tensor(output_details[0]['index'])

pprint(out1)
pprint(out2)

output:

[{'dtype': <class 'numpy.int8'>,
  'index': 0,
  'name': 'Input_int8',
  'quantization': (0.003921568859368563, -128),
  'quantization_parameters': {'quantized_dimension': 0,
                              'scales': array([0.00392157], dtype=float32),
                              'zero_points': array([-128], dtype=int32)},
  'shape': array([ 1, 56, 56,  3], dtype=int32),
  'shape_signature': array([ 1, 56, 56,  3], dtype=int32),
  'sparsity_parameters': {}}]
[{'dtype': <class 'numpy.int8'>,
  'index': 100,
  'name': 'Identity_int8',
  'quantization': (0.14218327403068542, -15),
  'quantization_parameters': {'quantized_dimension': 0,
                              'scales': array([0.14218327], dtype=float32),
                              'zero_points': array([-15], dtype=int32)},
  'shape': array([ 1,  7,  7, 18], dtype=int32),
  'shape_signature': array([ 1,  7,  7, 18], dtype=int32),
  'sparsity_parameters': {}}]
array([[[[-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127]],

        [[-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127]],

        [[-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127]],

        [[-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127]],

        [[-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127]],

        [[-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127]],

        [[-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127],
         [-128, -128, -128, -128, -128,  127, -128, -128, -128, -128,
          -128,  127, -128, -128, -128, -128, -128,  127]]]], dtype=int8)
array([[[[  -9,  -17,  -14,  -18,  -53,   72,   -3,  -12,  -13,  -17,
           -61,   61,   -4,  -12,  -15,  -18,  -76,   50],
         [ -22,  -13,  -14,  -18,  -44,   65,  -18,  -10,  -13,  -18,
           -56,   57,  -15,  -10,  -15,  -19,  -67,   46],
         [ -21,  -15,  -14,  -18,  -48,   63,  -23,  -11,  -13,  -18,
           -58,   54,  -18,   -9,  -14,  -19,  -78,   41],
         [ -21,  -10,  -14,  -20,  -69,   73,  -19,   -6,  -13,  -18,
           -89,   60,  -16,   -6,  -14,  -16,  -87,   52],
         [ -15,  -10,  -15,  -18,  -77,   70,  -16,   -6,  -14,  -17,
           -89,   58,  -20,   -6,  -14,  -17,  -94,   47],
         [   1,  -11,  -14,  -17,  -45,   61,    1,   -6,  -14,  -17,
           -50,   48,   -9,   -6,  -15,  -18,  -73,   37],
         [ -24,  -10,  -13,  -17,  -28,   62,  -29,   -7,  -14,  -18,
           -34,   51,  -23,   -5,  -16,  -18,  -73,   34]],

        [[ -12,  -13,  -14,  -17,  -66,   82,   -1,  -10,  -14,  -16,
           -69,   77,    0,  -10,  -15,  -17,  -70,   67],
         [ -17,  -16,  -14,  -18,  -60,   75,  -21,  -13,  -13,  -16,
           -61,   74,  -16,  -12,  -15,  -17,  -52,   65],
         [ -11,  -14,  -15,  -17,  -79,   71,  -11,  -12,  -15,  -16,
           -93,   65,   -7,   -5,  -13,  -15,  -73,   61],
         [ -14,   -8,  -13,  -18,  -67,   89,   -9,    0,  -12,  -18,
           -78,   92,   -8,   11,  -14,  -16,  -65,   81],
         [ -24,   -9,  -14,  -17,  -88,   93,  -30,   -5,  -13,  -16,
           -90,   98,  -28,    6,  -14,  -15,  -71,   82],
         [  -3,  -18,  -15,  -16,  -69,   77,    2,  -14,  -16,  -15,
           -69,   72,  -12,   -5,  -15,  -15,  -67,   62],
         [ -22,  -18,  -14,  -16,  -43,   74,  -28,  -10,  -14,  -15,
           -36,   74,  -23,   -5,  -17,  -16,  -47,   57]],

        [[ -11,  -16,  -15,  -16,  -74,   75,   -3,  -17,  -15,  -15,
           -75,   67,    0,  -18,  -15,  -15,  -71,   60],
         [ -14,  -17,  -14,  -17,  -65,   63,  -18,  -17,  -13,  -16,
           -61,   61,  -15,  -19,  -15,  -16,  -50,   54],
         [  -4,  -12,  -15,  -14,  -73,   70,   -2,  -13,  -15,  -15,
           -77,   62,   -3,  -14,  -14,  -15,  -78,   53],
         [ -19,  -18,  -15,  -17,  -75,   98,   -4,   -5,  -12,  -13,
           -48,  116,   -5,   -6,  -15,  -15,   13,  109],
         [ -11,  -10,  -15,  -15,  -88,  100,  -32,   -8,  -12,  -14,
           -63,  109,  -40,   -8,  -16,  -16,  -37,   88],
         [  -6,  -13,  -15,  -15,  -62,   64,   -1,  -13,  -16,  -14,
           -59,   59,  -11,  -16,  -14,  -14,  -46,   51],
         [ -18,  -21,  -15,  -16,  -46,   67,  -26,  -17,  -15,  -14,
           -34,   65,  -26,  -19,  -18,  -15,  -35,   52]],

        [[ -15,   -5,  -16,  -17,  -62,   74,   -9,   -8,  -15,  -16,
           -80,   60,   -4,  -15,  -14,  -16,  -83,   51],
         [ -11,   -9,  -14,  -18,  -68,   60,  -15,  -11,  -13,  -17,
           -66,   53,  -16,  -17,  -15,  -17,  -65,   44],
         [ -11,  -17,  -16,  -15,  -81,   77,   -6,  -22,  -16,  -15,
           -93,   66,   -2,  -23,  -13,  -14,  -81,   58],
         [ -12,  -15,  -12,  -17,  -70,   95,   -8,  -28,  -12,  -15,
           -48,  104,   -6,  -34,  -15,  -15,  -24,   92],
         [  -9,  -20,  -14,  -15,  -91,  102,  -26,  -30,  -13,  -16,
           -88,  103,  -36,  -31,  -13,  -16,  -71,   83],
         [  -9,  -15,  -13,  -17,  -41,   54,   -5,  -18,  -13,  -17,
           -45,   47,   -9,  -23,  -14,  -17,  -54,   37],
         [ -19,  -12,  -14,  -17,  -37,   52,  -26,  -19,  -14,  -16,
           -36,   45,  -27,  -25,  -16,  -17,  -53,   31]],

        [[ -20,  -18,  -16,  -15,  -47,   64,  -16,  -14,  -16,  -15,
           -61,   53,   -8,  -12,  -16,  -16,  -72,   41],
         [  -9,  -15,  -14,  -18,  -60,   60,  -13,  -17,  -14,  -17,
           -65,   49,  -15,  -16,  -15,  -16,  -71,   40],
         [ -11,  -22,  -15,  -17,  -54,   68,  -12,  -24,  -15,  -17,
           -77,   52,  -10,  -21,  -12,  -15,  -81,   43],
         [ -13,  -17,  -11,  -18,  -75,   81,  -11,  -28,  -13,  -19,
           -74,   79,  -13,  -30,  -15,  -17, -101,   60],
         [ -11,  -20,  -14,  -17,  -75,   92,  -19,  -24,  -14,  -18,
           -99,   79,  -25,  -20,  -12,  -15, -105,   60],
         [ -17,  -14,  -13,  -17,  -50,   60,  -12,  -19,  -13,  -18,
           -52,   55,  -11,  -18,  -14,  -17,  -69,   42],
         [ -16,  -25,  -15,  -18,  -47,   74,  -22,  -20,  -14,  -16,
           -65,   66,  -27,  -14,  -14,  -16,  -68,   50]],

        [[ -21,  -13,  -17,  -14,  -50,   59,  -16,  -15,  -17,  -14,
           -60,   45,   -9,  -19,  -17,  -15,  -77,   31],
         [ -19,  -17,  -15,  -16,  -57,   60,  -16,  -16,  -15,  -16,
           -64,   49,  -13,  -19,  -15,  -16,  -76,   38],
         [ -16,  -17,  -14,  -18,  -69,   72,  -15,  -17,  -15,  -17,
           -83,   58,  -16,  -20,  -14,  -17,  -98,   45],
         [ -13,  -18,  -13,  -18,  -72,   64,  -13,  -18,  -14,  -18,
           -80,   53,  -14,  -19,  -14,  -17, -102,   38],
         [ -11,  -14,  -14,  -18,  -74,   73,   -9,  -10,  -14,  -17,
           -80,   59,  -15,  -17,  -14,  -17,  -90,   47],
         [ -16,  -24,  -14,  -16,  -59,   58,  -14,  -16,  -13,  -16,
           -55,   53,  -15,  -18,  -16,  -17,  -60,   43],
         [ -18,  -16,  -15,  -16,  -60,   63,  -23,  -14,  -15,  -15,
           -58,   54,  -29,  -21,  -16,  -16,  -69,   39]],

        [[ -20,  -22,  -16,  -16,  -58,   67,  -17,  -24,  -16,  -16,
           -78,   51,  -13,  -22,  -16,  -16,  -98,   34],
         [ -18,  -20,  -15,  -16,  -60,   64,  -15,  -24,  -15,  -17,
           -69,   50,  -13,  -24,  -15,  -17,  -86,   37],
         [ -16,  -16,  -15,  -16,  -72,   62,  -16,  -22,  -15,  -17,
           -77,   48,  -15,  -24,  -15,  -17, -102,   34],
         [ -15,  -18,  -15,  -16,  -71,   63,  -14,  -20,  -14,  -17,
           -78,   48,  -16,  -24,  -15,  -17,  -94,   36],
         [ -14,  -19,  -14,  -16,  -70,   64,  -11,  -25,  -14,  -18,
           -74,   52,  -14,  -27,  -15,  -18, -103,   37],
         [ -17,  -20,  -14,  -16,  -69,   66,  -17,  -25,  -14,  -17,
           -66,   59,  -16,  -27,  -15,  -16,  -80,   45],
         [ -18,  -19,  -15,  -16,  -68,   67,  -21,  -22,  -15,  -16,
           -78,   53,  -24,  -23,  -15,  -16,  -95,   37]]]], dtype=int8)

Step-by-step reproduction

No response

Relevant log output

No response

Issue submission checklist

andrei-kochin commented 4 months ago

@kabirz thank you fore reachin OpenVINO! This issue looks similar to what we have observed internally

@gkrivor is working on it. We'll update you with the progress once we have some progress on the fix.

gkrivor commented 1 month ago

@kabirz could you re-check with latest master?