ultralytics icon indicating copy to clipboard operation
ultralytics copied to clipboard

Runnning on Tflite interpreter fails with different values

Open saka3324 opened this issue 2 years ago • 2 comments

Search before asking

  • [X] I have searched the YOLOv8 issues and found no similar bug report.

YOLOv8 Component

Detection

Bug

Converted YOLOv8s-seg.pt to tflite.

It seemes successful. Input and output details are like this. [{'name': 'inputs_0', 'index': 0, 'shape': array([ 1, 640, 640, 3], dtype=int32), 'shape_signature': array([ 1, 640, 640, 3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] [{'name': 'Identity', 'index': 498, 'shape': array([ 1, 160, 160, 32], dtype=int32), 'shape_signature': array([ 1, 160, 160, 32], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'Identity_1', 'index': 631, 'shape': array([ 1, 116, 8400], dtype=int32), 'shape_signature': array([ 1, 116, 8400], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

So I used tflite interpreter to run this model on following code.

  import torch
  import cv2
  from ultralytics.yolo.data.augment import LetterBox
  from ultralytics.yolo.utils import ops
  import copy
  import numpy as np

  def Preprocess(img):
      original = copy.deepcopy(img)
      img = LetterBox(640, True)(image=img)
      img = img[::-1] #BGR2RGB
      img = np.ascontiguousarray(img)
      img = torch.from_numpy(img)
      img = img.float()
      img /= 255
      return img,original
  
  def Postprocess(preds, img, original):
    preds = ops.non_max_suppression(preds,
                                    0.25,
                                    0.7,
                                    agnostic=False,
                                    max_det=100)
    # at this point, preds values are different from Pytorch's

    for i, pred in enumerate(preds):
        shape = orig_img.shape
        pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], shape).round()
    return preds

  
  image = cv2.imread(imgpath)
  image,originalimg = Preprocess(image)
  interpreter = tf.lite.Interpreter(model_path=model)
  interpreter.allocate_tensors()
  interpreter.set_tensor(input_details[0]['index'], img)
  interpreter.invoke()
  output_data = interpreter.get_tensor(output_details[1]['index']) #recieve [   1,  116, 8400] 
  output_data = torch.from_numpy(output_data.astype(np.float32))

  preds = postprocess(output_data,img,originalimg) 

then, preds values are like this.

[tensor([[296.00000,   0.00000, 296.00000,   0.00000,   1.48874,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.46740,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.46368,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.46071,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.46027,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.42773,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.41953,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.40604,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.40517,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.39853,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.39473,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.37746,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.37366,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.36995,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.36336,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.35947,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.35938,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.35844,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.33753,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.33574,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.32853,  95.00000],
        [296.00000, 297.00000, 296.00000, 297.00000,   1.32728,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.32687,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.32153,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.32110,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.32016,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.31914,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.31679,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.31636,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.31204,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.30827,  95.00000],
        [296.00000, 297.00000, 296.00000, 297.00000,   1.30709,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.30572,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.30567,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.30566,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.30418,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.29971,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.29922,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.29898,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.29892,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.29326,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.29032,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.28821,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.28746,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.28691,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.27904,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.27829,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.27676,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.27225,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.27093, 105.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.26700,  95.00000],
        [296.00000,   0.00000, 296.00000,  96.00000,   1.26693,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.26450, 105.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.26336,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.26283, 105.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.26265,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.25472,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.25440,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.25344,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.25097,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.24687,  95.00000],
        [296.00000,   0.00000, 296.00000, 292.00000,   1.24384,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.24273,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.24143,  95.00000],
        [296.00000, 297.00000, 296.00000, 297.00000,   1.24057,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.23750,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.23689, 105.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.23676,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.23587,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.23425,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.23425,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.23404,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.23129,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.23104,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.23092,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.23083,  95.00000],
        [296.00000,   0.00000, 296.00000,  64.00000,   1.23058,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.23005,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.22921,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.22664,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.22435,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.22415,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.22353,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.22146, 105.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.22025,  95.00000],
        [296.00000, 297.00000, 296.00000, 297.00000,   1.21873,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.21826,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.21644,  95.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.21607,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.21417, 105.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.21402,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.21330,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.21250,  95.00000],
        [296.00000, 297.00000, 296.00000, 297.00000,   1.21248, 104.00000],
        [296.00000,   0.00000, 296.00000, 297.00000,   1.21233,  95.00000],
        [296.00000, 297.00000, 296.00000, 297.00000,   1.21095,  95.00000],
        [296.00000, 297.00000, 296.00000, 297.00000,   1.21048,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.20775,  95.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.20686, 105.00000],
        [296.00000,   0.00000, 296.00000,   0.00000,   1.20648,  95.00000]])]

This value is completely different from the original one. I'm not sure if this is caused by my code or converted model. Any advise would be very appriciated.

Thank you.

Environment

YOLOV8 Colaboratory-Python

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • [ ] Yes I'd like to help by submitting a PR!

saka3324 avatar Jan 14 '23 17:01 saka3324

tflite export isn't supported. We'll be working on it soon

AyushExel avatar Jan 14 '23 18:01 AyushExel

Thank you. I'm looking forward to it.

saka3324 avatar Jan 15 '23 03:01 saka3324

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

  • Docs: https://docs.ultralytics.com
  • HUB: https://hub.ultralytics.com
  • Community: https://community.ultralytics.com

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

github-actions[bot] avatar Mar 21 '23 00:03 github-actions[bot]

@saka3324 you're welcome! We appreciate your patience and understanding. If you have any other questions or need further assistance, feel free to ask. We're here to help!

pderrenger avatar Nov 16 '23 03:11 pderrenger