79197348

Date: 2024-11-17 14:03:21
Score: 2
Natty:
Report link

Thank you, you are amazing and your post save my day, I apply quantization on YOLO model, the code:

#https://github.com/ultralytics/ultralytics/issues/6676
#https://github.com/onnx/onnx-tensorflow
#https://github.com/onnx/onnx-tensorflow/blob/main/example/onnx_to_tf.py
#https://docs.ultralytics.com/modes/export/#usage-examples
#https://coral.ai/docs/edgetpu/models-intro/#compatibility-overview

import tensorflow as tf
import numpy as np
import os 
import cv2

def representative_data_gen():
  image_paths = "/..../images/"
  op_ = 0

  for image_path in os.listdir(image_paths):
    op_ = op_ + 1 
    image = cv2.imread("/.../images/"+image_path)    
    print("------111-------", image.shape)  
    image = cv2.resize(image, (640, 640))
    print("------222-------", image.shape)  
    image = np.array(image).astype(np.float32) / 255.0 
    print("------333-------", image.shape)  
    image = np.transpose(image, (2, 0, 1))  # (H, W, C) -> (C, H, W)
    print("------444-------", image.shape)  
    image = np.expand_dims(image, axis=0)  # Add batch dimension

    print("------555-------", image.shape)  
    if op_ == 100:
      break
  
    print(len(image), len(image[0]), len(image[0][0]), len(image[0][0][0]))
    yield [image] #[np.array(image[0][0], dtype=np.float32)]# [image]
    print(" what next ")
   


# ... (Load your TensorFlow model)
converter = tf.lite.TFLiteConverter.from_saved_model('/..../teston')

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8  
converter.inference_output_type = tf.int8  
converter.representative_dataset = representative_data_gen  # Provide a data generator function

tflite_quant_model = converter.convert()

# Save the quantized model
with open('/..../teston/quantized_model2.tflite', 'wb') as f:
    f.write(tflite_quant_model)
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Blacklisted phrase (2): save my day
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: AMER ALAHMAD