Jelajahi Sumber

adidtional set up for tfllite conversion from best.pt

Dr-Swopt 2 minggu lalu
induk
melakukan
0089edcf9b
7 mengubah file dengan 107 tambahan dan 0 penghapusan
  1. TEMPAT SAMPAH
      best.onnx
  2. TEMPAT SAMPAH
      calibration_image_sample_data_20x128x128x3_float32.npy
  3. TEMPAT SAMPAH
      export_error_full.txt
  4. TEMPAT SAMPAH
      export_log.txt
  5. 18 0
      export_mobile_debug.py
  6. 41 0
      export_mobile_robust.py
  7. 48 0
      manual_convert_tflite.py

TEMPAT SAMPAH
best.onnx


TEMPAT SAMPAH
calibration_image_sample_data_20x128x128x3_float32.npy


TEMPAT SAMPAH
export_error_full.txt


TEMPAT SAMPAH
export_log.txt


+ 18 - 0
export_mobile_debug.py

@@ -0,0 +1,18 @@
+from ultralytics import YOLO
+import os
+
+# 1. Load your high-accuracy PC model
+model_path = 'best.pt'
+if not os.path.exists(model_path):
+    print(f"Error: {model_path} not found.")
+else:
+    model = YOLO(model_path) 
+
+    # Removing int8=True for debugging
+    model.export(
+        format='tflite', 
+        nms=True, 
+        imgsz=640
+    )
+
+    print("Mobile assets attempt finished.")

+ 41 - 0
export_mobile_robust.py

@@ -0,0 +1,41 @@
+from ultralytics import YOLO
+import os
+import time
+
+# 1. Load your high-accuracy PC model
+model_path = 'best.pt'
+if not os.path.exists(model_path):
+    print(f"Error: {model_path} not found.")
+else:
+    model = YOLO(model_path) 
+
+    # 2. Export to TFLite with NMS and Quantization
+    # We use a try-except block to handle potential file locking issues during cleanup
+    try:
+        model.export(
+            format='tflite', 
+            int8=True, 
+            nms=True, 
+            imgsz=640,
+            data='unified_dataset/data.yaml'
+        )
+        print("Export command completed.")
+    except Exception as e:
+        print(f"Export encountered an issue but might have finished: {e}")
+
+    # Check if the model actually exists
+    target_path = 'best_saved_model/best_full_integer_quant.tflite'
+    if os.path.exists(target_path):
+        print(f"Success: {target_path} generated.")
+    else:
+        # Check other possible names
+        tflite_files = []
+        if os.path.exists('best_saved_model'):
+            for f in os.listdir('best_saved_model'):
+                if f.endswith('.tflite'):
+                    tflite_files.append(f)
+        
+        if tflite_files:
+            print(f"TFLite files found: {tflite_files}")
+        else:
+            print("Error: No TFLite model found in best_saved_model/")

+ 48 - 0
manual_convert_tflite.py

@@ -0,0 +1,48 @@
+import tensorflow as tf
+import numpy as np
+import os
+import cv2
+
+# 1. Path setup
+saved_model_path = 'best_saved_model'
+# Try to find where saved_model.pb actually is
+target_saved_model = saved_model_path
+if not os.path.exists(os.path.join(saved_model_path, 'saved_model.pb')):
+    for root, dirs, files in os.walk(saved_model_path):
+        if 'saved_model.pb' in files:
+            target_saved_model = root
+            break
+
+print(f"Using SavedModel at: {target_saved_model}")
+
+# 2. Representative Dataset
+def representative_dataset():
+    img_dir = 'unified_dataset/images/val'
+    count = 0
+    for f in os.listdir(img_dir):
+        if f.endswith(('.jpg', '.jpeg', '.png')) and count < 50:
+            img = cv2.imread(os.path.join(img_dir, f))
+            if img is None: continue
+            img = cv2.resize(img, (640, 640))
+            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
+            img = img.astype(np.float32) / 255.0
+            img = np.expand_dims(img, axis=0)
+            yield [img]
+            count += 1
+
+# 3. Converter
+converter = tf.lite.TFLiteConverter.from_saved_model(target_saved_model)
+converter.optimizations = [tf.lite.Optimize.DEFAULT]
+converter.representative_dataset = representative_dataset
+converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
+converter.inference_input_type = tf.int8
+converter.inference_output_type = tf.int8
+
+try:
+    tflite_model = converter.convert()
+    output_path = 'best_int8.tflite'
+    with open(output_path, 'wb') as f:
+        f.write(tflite_model)
+    print(f"Success: {output_path} generated.")
+except Exception as e:
+    print(f"Conversion failed: {e}")