Skip to content

ys31jp/yolov5

 
 

Repository files navigation

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#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#1127

About

YOLOv5 in PyTorch > ONNX > CoreML > iOS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 66.8%
  • Java 30.3%
  • Shell 2.3%
  • Dockerfile 0.6%