问题描述:
ultralytics发布的yolov5 6.0版本中已经加入了export.py文件可以将训练好的模型.pt转换为.tflite格式的轻量化模型部署在Android端,但是注意6.0版本当前代码只能转换5.0版本的。
我在使用6.0版本的export.py代码时报错 name 'SPPF' is not defined,找了半天报错的位置才发现,6.0版本模型文件中model/commen.py文件夹中多了一个SPPF模块来代替5.0版本中的SPP模块,当然旧的SPP模块依旧还在,而在将模型转换为tensorflow模型时,export.py(还没随之更新)只能识别SPP模块,而不能识别SPPF模块导致的。
解释:下面这行代码就是export.py调用的,他并没有考虑SPPF这个模块。
解决方法:
将5.0版本的模型配置文件yolov5s.yaml移到6.0版本中的model文件夹下,训练一版模型后,然后实际上利用6.0版本的export.py来生成5.0版本的模型对应的.tflite文件。
接下来就参照这些部署就好了:
; 2. Convert and verify
- Convert weights to fp16 TFLite model, and verify it with
python export.py --weights yolov5s.pt --include tflite --img 320
python detect.py --weights yolov5s-fp16.tflite --img 320
or
- Convert weights to int8 TFLite model, and verify it with
python export.py --weights yolov5s.pt --include tflite --int8 --img 320 --data data/coco128.yaml
python detect.py --weights yolov5s-int8.tflite --img 320
Note that:
-
int8 quantization needs dataset images to calibrate weights and activations, and the default COCO128 dataset is downloaded automatically.
-
Change
--img
to the input resolution of your model, if it isn't 320.
3. Clone this repo (tf-android branch) for Android app
git clone https://github.com/zldrobit/yolov5.git yolov5-android
4. Put TFLite models in assets
folder of Android project, and change
inputSize
to--img
output_width
according to new/oldinputSize
ratioanchors
tom.anchor_grid
as https://github.com/ultralytics/yolov5/pull/1127#issuecomment-714651073
in android/app/src/main/java/org/tensorflow/lite/examples/detection/tflite/DetectorFactory.javalabelFilename
according to the classes of the model
in https://github.com/zldrobit/yolov5/blob/522d65e848d3e5a378eb0f29a9fbb204221400e8/android/app/src/main/java/org/tensorflow/lite/examples/detection/tflite/DetectorFactory.java#L19-L48.
Then run the program in Android Studio.
参照博客:
Original: https://blog.csdn.net/qq_41799012/article/details/122036777
Author: qq_41799012
Title: 怎样将YOLOv5部署到移动端 报错:name ‘SPPF‘ is not defined