carloshvp / yolov5-micro

YOLOv5 optimized for speed even further

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Info

This branch provides detection and Android code complement to branch tf-only-export. models/tf.py uses TF2 API to construct a tf.Keras model according to *.yaml config files and reads weights from *.pt, without using ONNX.

Because this branch persistently rebases to master branch of ultralytics/yolov5, use git pull --rebase or git pull -f instead of git pull.

Usage

1. Git clone yolov5 and checkout tf-android

git clone https://github.com/zldrobit/yolov5.git
cd yolov5
git checkout tf-android

and download pretrained weights from

https://github.com/ultralytics/yolov5.git

2. Install requirements

pip install -r requirements.txt
pip install tensorflow==2.4.1

3. Convert and verify

  • Convert weights to TensorFlow SavedModel, GraphDef and fp16 TFLite model, and verify them with
PYTHONPATH=. python models/tf.py --weights weights/yolov5s.pt --cfg models/yolov5s.yaml --img 320
python3 detect.py --weights weights/yolov5s.pb --img 320
python3 detect.py --weights weights/yolov5s_saved_model/ --img 320
  • Convert weights to int8 TFLite model, and verify it with (Post-Training Quantization needs train or val images from COCO 2017 dataset)
PYTHONPATH=. python3  models/tf.py --weights weights/yolov5s.pt --cfg models/yolov5s.yaml --img 320 --tfl-int8 --source /data/dataset/coco/coco2017/train2017 --ncalib 100
python3 detect.py --weights weights/yolov5s-int8.tflite --img 320 --tfl-int8
  • Convert weights to TensorFlow SavedModel and GraphDef integrated with NMS, and verify them with
PYTHONPATH=. python3  models/tf.py --img 320 --weights weights/yolov5s.pt --cfg models/yolov5s.yaml --tf-nms
python3 detect.py --img 320 --weights weights/yolov5s.pb --no-tf-nms
python3 detect.py --img 320 --weights weights/yolov5s_saved_model --no-tf-nms

4. Put TFLite models in assets folder of Android project, and change

  • inputSize to --img
  • output_width according to new/old inputSize ratio
  • anchors to m.anchor_grid as ultralytics/yolov5#1127 (comment) in android/app/src/main/java/org/tensorflow/lite/examples/detection/tflite/DetectorFactory.java

Then run the program in Android Studio.

If you have further question, plz ask in ultralytics/yolov5#1127

About

YOLOv5 optimized for speed even further

License:GNU General Public License v3.0


Languages

Language:Jupyter Notebook 98.3%Language:Python 1.1%Language:Java 0.5%Language:Shell 0.0%Language:Dockerfile 0.0%