Dr-Swopt hai 1 semana
pai
achega
9caef843c9

BIN=BIN
best.onnx


+ 89 - 0
export_raw_tflite.py

@@ -0,0 +1,89 @@
+"""
+Export best.pt to TFLite with RAW output format (no end-to-end NMS).
+The ultralytics_yolo plugin's JNI postprocessor expects [batch, C+4, N] format
+where rows 0-3 are cx,cy,w,h and rows 4+ are class scores.
+
+This script:
+1. Exports best.pt -> best.onnx (without NMS)
+2. Converts ONNX -> TFLite using onnx_tf
+"""
+import subprocess
+import sys
+import os
+
+def run(cmd):
+    print(f"Running: {cmd}")
+    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
+    print(result.stdout)
+    if result.returncode != 0:
+        print("STDERR:", result.stderr)
+    return result.returncode
+
+# Step 1: Export ONNX without NMS using ultralytics
+print("=== Step 1: Export ONNX (no NMS) ===")
+code = """
+from ultralytics import YOLO
+model = YOLO('best.pt')
+model.export(format='onnx', imgsz=640, simplify=True, nms=False, opset=12)
+print('ONNX exported')
+"""
+result = subprocess.run([sys.executable, '-c', code], capture_output=True, text=True, cwd=os.getcwd())
+print(result.stdout)
+if result.returncode != 0:
+    print("Error:", result.stderr)
+    sys.exit(1)
+
+# Check ONNX output shape
+print("=== Step 2: Verify ONNX output shape ===")
+check_code = """
+import onnx
+m = onnx.load('best.onnx')
+for out in m.graph.output:
+    print('Output:', out.name, [d.dim_value for d in out.type.tensor_type.shape.dim])
+"""
+subprocess.run([sys.executable, '-c', check_code], cwd=os.getcwd())
+
+# Step 3: Convert ONNX to TFLite using onnx2tf with disable_strict_mode
+print("=== Step 3: Convert ONNX -> TFLite ===")
+ret = run(
+    f'python -m onnx2tf -i best.onnx -o best_raw_saved_model '
+    f'--not_use_onnx_optimization '
+    f'--output_tfv1_signaturedefs '
+    f'--non_verbose '
+    f'--disable_strict_mode'
+)
+
+# Step 4: Convert SavedModel -> TFLite (float32)
+print("=== Step 4: Convert SavedModel -> TFLite Float32 ===")
+tflite_code = """
+import tensorflow as tf
+import os
+
+# Find saved_model
+base = 'best_raw_saved_model'
+for root, dirs, files in os.walk(base):
+    if 'saved_model.pb' in files:
+        saved_model_dir = root
+        break
+
+print(f'Using SavedModel: {saved_model_dir}')
+
+converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
+converter.target_spec.supported_types = [tf.float32]
+tflite_model = converter.convert()
+
+with open('best_raw_float32.tflite', 'wb') as f:
+    f.write(tflite_model)
+print('Saved: best_raw_float32.tflite')
+
+# Verify output shape
+interp = tf.lite.Interpreter(model_path='best_raw_float32.tflite')
+interp.allocate_tensors()
+inp = interp.get_input_details()
+out = interp.get_output_details()
+print('Input shape:', inp[0]['shape'])
+for i, o in enumerate(out):
+    print(f'Output[{i}]:', o['shape'], o['dtype'])
+"""
+subprocess.run([sys.executable, '-c', tflite_code], cwd=os.getcwd())
+print("Done. Check best_raw_float32.tflite")

+ 1 - 0
palm_oil_mobile/linux/flutter/generated_plugins.cmake

@@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
 )
 
 list(APPEND FLUTTER_FFI_PLUGIN_LIST
+  tflite_flutter
 )
 
 set(PLUGIN_BUNDLED_LIBRARIES)

+ 56 - 0
palm_oil_mobile/pubspec.lock

@@ -1,6 +1,14 @@
 # Generated by pub
 # See https://dart.dev/tools/pub/glossary#lockfile
 packages:
+  archive:
+    dependency: transitive
+    description:
+      name: archive
+      sha256: a96e8b390886ee8abb49b7bd3ac8df6f451c621619f52a26e815fdcf568959ff
+      url: "https://pub.dev"
+    source: hosted
+    version: "4.0.9"
   async:
     dependency: transitive
     description:
@@ -192,6 +200,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "4.1.2"
+  image:
+    dependency: "direct main"
+    description:
+      name: image
+      sha256: f9881ff4998044947ec38d098bc7c8316ae1186fa786eddffdb867b9bc94dfce
+      url: "https://pub.dev"
+    source: hosted
+    version: "4.8.0"
   image_picker:
     dependency: "direct main"
     description:
@@ -400,6 +416,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.3.0"
+  petitparser:
+    dependency: transitive
+    description:
+      name: petitparser
+      sha256: "91bd59303e9f769f108f8df05e371341b15d59e995e6806aefab827b58336675"
+      url: "https://pub.dev"
+    source: hosted
+    version: "7.0.2"
   platform:
     dependency: transitive
     description:
@@ -416,6 +440,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.1.8"
+  posix:
+    dependency: transitive
+    description:
+      name: posix
+      sha256: "185ef7606574f789b40f289c233efa52e96dead518aed988e040a10737febb07"
+      url: "https://pub.dev"
+    source: hosted
+    version: "6.5.0"
   pub_semver:
     dependency: transitive
     description:
@@ -424,6 +456,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.2.0"
+  quiver:
+    dependency: transitive
+    description:
+      name: quiver
+      sha256: ea0b925899e64ecdfbf9c7becb60d5b50e706ade44a85b2363be2a22d88117d2
+      url: "https://pub.dev"
+    source: hosted
+    version: "3.2.2"
   sky_engine:
     dependency: transitive
     description: flutter
@@ -525,6 +565,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "0.7.10"
+  tflite_flutter:
+    dependency: "direct main"
+    description:
+      name: tflite_flutter
+      sha256: "0bba9040d8decda0960d7abf8eabf32243bf092bc7d0084e8e19681866b0bdbe"
+      url: "https://pub.dev"
+    source: hosted
+    version: "0.12.1"
   typed_data:
     dependency: transitive
     description:
@@ -573,6 +621,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "1.1.0"
+  xml:
+    dependency: transitive
+    description:
+      name: xml
+      sha256: "971043b3a0d3da28727e40ed3e0b5d18b742fa5a68665cca88e74b7876d5e025"
+      url: "https://pub.dev"
+    source: hosted
+    version: "6.6.1"
   yaml:
     dependency: transitive
     description:

+ 2 - 0
palm_oil_mobile/pubspec.yaml

@@ -39,6 +39,8 @@ dependencies:
   path_provider: ^2.1.2
   path: ^1.9.0
   ultralytics_yolo: ^0.2.0
+  tflite_flutter: ^0.12.1
+  image: ^4.8.0
 
 dev_dependencies:
   flutter_test:

+ 1 - 0
palm_oil_mobile/windows/flutter/generated_plugins.cmake

@@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
 )
 
 list(APPEND FLUTTER_FFI_PLUGIN_LIST
+  tflite_flutter
 )
 
 set(PLUGIN_BUNDLED_LIBRARIES)