manual_convert_tflite.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import tensorflow as tf
  2. import numpy as np
  3. import os
  4. import cv2
  5. # 1. Path setup
  6. saved_model_path = 'best_saved_model'
  7. # Try to find where saved_model.pb actually is
  8. target_saved_model = saved_model_path
  9. if not os.path.exists(os.path.join(saved_model_path, 'saved_model.pb')):
  10. for root, dirs, files in os.walk(saved_model_path):
  11. if 'saved_model.pb' in files:
  12. target_saved_model = root
  13. break
  14. print(f"Using SavedModel at: {target_saved_model}")
  15. # 2. Representative Dataset
  16. def representative_dataset():
  17. img_dir = 'unified_dataset/images/val'
  18. count = 0
  19. for f in os.listdir(img_dir):
  20. if f.endswith(('.jpg', '.jpeg', '.png')) and count < 50:
  21. img = cv2.imread(os.path.join(img_dir, f))
  22. if img is None: continue
  23. img = cv2.resize(img, (640, 640))
  24. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  25. img = img.astype(np.float32) / 255.0
  26. img = np.expand_dims(img, axis=0)
  27. yield [img]
  28. count += 1
  29. # 3. Converter
  30. converter = tf.lite.TFLiteConverter.from_saved_model(target_saved_model)
  31. converter.optimizations = [tf.lite.Optimize.DEFAULT]
  32. converter.representative_dataset = representative_dataset
  33. converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
  34. converter.inference_input_type = tf.int8
  35. converter.inference_output_type = tf.int8
  36. try:
  37. tflite_model = converter.convert()
  38. output_path = 'best_int8.tflite'
  39. with open(output_path, 'wb') as f:
  40. f.write(tflite_model)
  41. print(f"Success: {output_path} generated.")
  42. except Exception as e:
  43. print(f"Conversion failed: {e}")