export_mobile_robust.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from ultralytics import YOLO
  2. import os
  3. import time
  4. # 1. Load your high-accuracy PC model
  5. model_path = 'best.pt'
  6. if not os.path.exists(model_path):
  7. print(f"Error: {model_path} not found.")
  8. else:
  9. model = YOLO(model_path)
  10. # 2. Export to TFLite with NMS and Quantization
  11. # We use a try-except block to handle potential file locking issues during cleanup
  12. try:
  13. model.export(
  14. format='tflite',
  15. int8=True,
  16. nms=True,
  17. imgsz=640,
  18. data='unified_dataset/data.yaml'
  19. )
  20. print("Export command completed.")
  21. except Exception as e:
  22. print(f"Export encountered an issue but might have finished: {e}")
  23. # Check if the model actually exists
  24. target_path = 'best_saved_model/best_full_integer_quant.tflite'
  25. if os.path.exists(target_path):
  26. print(f"Success: {target_path} generated.")
  27. else:
  28. # Check other possible names
  29. tflite_files = []
  30. if os.path.exists('best_saved_model'):
  31. for f in os.listdir('best_saved_model'):
  32. if f.endswith('.tflite'):
  33. tflite_files.append(f)
  34. if tflite_files:
  35. print(f"TFLite files found: {tflite_files}")
  36. else:
  37. print("Error: No TFLite model found in best_saved_model/")