environment
window 10 64bit
yolov5 v6.2
torch1.7.1+cuda101
tensorflow-gpu 2.9.1
foreword
In the previous article, with the help of NCNN, run YOLOv5 target detection on Android and yolov5 target detection on Android. Using the torchscript method, we used ncnn
and torchscript
methods respectively to YOLOv5
deploy android
to the mobile phone. In this article we will use another way tflite
to deploy, so use whichever you like.
Specific steps
The latest v6.2 version code is used here, and the official yolov5s.pt
model . To use your own data set, you need to train yourself first. For the training steps, you can refer to the link at the end of the article.
The model conversion process also tensorflow
requires the environment, install it
pip instal tensorflow-gpu
Then you can use export.py
the script to convert, the command is as follows
python export.py --weights yolov5s.pt --include tflite --img 416
The parameter weights
specifies .pt
the model, --include
the parameter specifies the target model to be converted, and --img
the parameter specifies the image size
It can be seen that the converted model is yolov5s-fp16.tflite
, at the same time, under the same directory, there is also a folder yolov5s_saved_model
, which contains .pb
the file , which is protobuf
the file. Here is a detail, that is, .pt
the file is first converted to .pb
and then converted .tflite
to .
Next, use the script detect.py
to verify it, using .tflite
the model generated above
python detect.py --weights yolov5s-fp16.tflite --source data/images/bus.jpg --img 416
There is no problem with detection
With the model file, then you can come to android
the end , I uploaded the sample code github
to , you can directlyclone
git clone https://github.com/xugaoxiang/yolov5_android_tflite.git
What needs to be replaced yolov5_android_tflite/app/src/main/assets
are 2 files under the folder, class.txt
andyolov5s-fp16.tflite
After compiling and installing it on the mobile phone, you can start the camera-based target detection
References
Running YOLOv5 object detection on Android with NCNN
Windows 10 install cuda and cudnn
YOLOv5 model training
YOLOv5 version 5.0
Solution to Android studio gradle build failure