-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add export and detection for TensorFlow saved_model, graph_def and TFLite #959
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @zldrobit, thank you for submitting a PR! To allow your work to be integrated as seamlessly as possible, we advise you to:
- Verify your PR is up-to-date with origin/master. If your PR is behind origin/master update by running the following, replacing 'feature' with the name of your local branch:
git remote add upstream https://github.com/ultralytics/yolov5.git
git fetch upstream
git checkout feature # <----- replace 'feature' with local branch name
git rebase upstream/master
git push -u origin -f
- Verify all Continuous Integration (CI) checks are passing.
- Reduce changes to the absolute minimum required for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." -Bruce Lee
Very nice! Do you have a way to perform the detection with Java / Android already? This is where the TFLite formats will probably be used the most. |
@zldrobit ,thank you for your pr, i have exported TFLite models successfully. but how can i use the letterbox function in tflite project ? |
@bartvollebregt Thanks :D I am cleaning my Android project code, and I am going to push that code soon. |
@zldrobit Do you also plan to look into an edgetpu implementation? I am having issue with int8 quantization of the keras model |
Some operations used in the model such as resize_nearest_neighbor are not compatible with the edge TPU |
@BernardinD I have never used an Edge TPU and I found it has 4T FLOPs computation power today. @Guilhen yes, resize_nearest_neighbor has 3D input/output limitations. |
@bartvollebregt @lei522 I updated the TF conversion script for GPU delegate compatibility and uploaded Android TFLite demo.
to generate I have tested yolov5s batch-size 1 run time on Android devices: Snapdragon 820: 1.9s on CPU (4threads) and 1.3s on GPU |
@zldrobit i can run the tflite model, but when i run the model with hybrid quantization model or float16 quantization, i get the ValueError:Input array not provided for operation 'reshape'. |
@lei522 I added fp16 and int8 TFLite model export.
This will generate @BernardinD @Guilhen The updated code supports full integer quantization (with --tfl-int8), which can be compiled to Edge TPU format. I have tested int8 quantization model on Android devices. The inference time of fp16 model (batch-size 1) is: The inference time of int8 model (batch-size 1) is: |
Thanks @zldrobit, I was able to run the edgetpu compilation with your current conversion. Although when I try to run the outputted graph I'm given the error Any suggestions? The compilation also only gets 1 operation to get mapped |
@BernardinD plz check your representative image forlder. |
Sure no problem. Here's the log file: And I used the default 100 ncalib out of a folder of 1000 pictures. That should be fine, correct? |
I managed to convert to int8 model with no issue. However, may i know if the same inference code (detect.py) could still be used to inference the int8 tflite model. I tried changing the input type to uint8 and the results seem to be wrong. Thank you and really appreciate it for the conversion scripts. |
The android demo does not seem to be runnable. A bunch of the drawables are missing, and the env package is missing |
@zldrobit hi, thanks for this PR! Export to *.tflite is an amazing contribution, I know a lot of people have been asking for this functionality. I think originally this PR featured only the export code (first 3 or 4 commits), and the accompanying android app was later added in commit 5. For us to merge this into the repo we'd ideally want to strip this down to the minimum export functionality required as you had originally, as supporting and maintaining an android app is beyond the scope of the repo (even though it's an amazing contribution in and of itself). EDIT: maybe the best way to move forward is to submit a new PR featuring only the export, or to reset the head to an earlier commit on the branch, whatever is easiest for you, let me know, thanks! |
@BernardinD sorry for this late reply, I was on vocation. |
@minthawzin1995 int8 quantization does not support some ops in Detect module, some code needs to be added in detect.py . |
@idenc I'll check the android code again, thx for reporting. |
@glenn-jocher I am planning to submit a new PR. |
@zldrobit yes, please submit a new PR with tf.py and associated export code, but without detect.py integration and without the /android folder. This will help us minimize keep code maintenance going forward. |
Export and Detection (/w TFLite int8 inference) functionality and Android project has been integrated in a new branch https://github.com/zldrobit/yolov5/tree/tf-android @minthawzin1995 Plz try the new branch and int8 inference with:
inference time is around 18s on CPU. @idenc Sorry for my unfamiliar with Android Studio. I have added necessary files in the Android project. |
@zldrobit Thank you for the fast response, I have tested the new update and the detection works perfectly now for the int-8 model. |
Hi @zldrobit, I'm getting different results on the android app compared to detect.py. I converted yolov5s model to fp16 using the following command: PYTHONPATH=. python models/tf.py --weights models/yolov5s.pt --cfg models/yolov5s.yaml --img 320 I setup the android app with the fp-16 model but on the app the results are much worse compared to when running inference using detect.py like this: python detect.py --weights models/yolov5s-fp16.tflite --img-size 320 I've verified that the masks and output shapes are correct. I set SAVE_PREVIEW_BITMAP as true and got the images going to inference on the phone and tried them with detect.py and it works well. I've tried with the default coco model as well as a custom model. For the custom model, the results are much worse from the android app. Here are the results on the coco model: Using android app: Using detect.py: Am I missing something? |
@jsn5 I notice that there are difference between above and below pictures. |
The pics look different because for the phone results I had to take a screenshot of the app. Regardless of lighting and contrast, the results seem very different overall. For a custom dataset that I trained yolov5s on, the detection on phone would miss many of the objects while taking the same preview image and running using detect.py is able to detect most of the objects in the scene. I'll try to do some more tests and share it with you. |
@jsn5 @zldrobit the differences may also be due to image transforms in Android. In iOS, there are a few options for how the camera image is transformed into the model input shape. iDetection iOS app currently uses scaleFill, which distorts the aspect ratio slightly, so we have a TODO item to move to scaleFit (same as YOLOv5 PyTorch dataloader), but this is not as simple as it sounds because the bounding box reconstruction is dependent on the exact input transforms, so modifications at the input must be accompanied by modification of the box reconstruction code as well. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Since this PR is closed, any further questions about TensorFlow/TFLite export may be addressed in #1127. |
@glenn-jocher This PR is out-of-date, and its Android project is not compatible with the models exported from #1127. |
@zldrobit ok makes sense! Will close. |
@BernardinD We were able to compile the quantized model without -a and got TPU ops as 1. But when we used -a, got "Internal compiler error. Aborting!". Any suggestion to resolve it?. Thanks! |
|
@NandhiniN85 Could you share more information to inspect the problem, such as the model size, the number of classes, and the network structure (any change to yolov5*.yaml)? |
@zldrobit I have used yolov5s and the number of classes is 1. The only modification made to the config file is the nc=1. The input resolution is 640 x 640. Every image frame has around 100 objects at least, the size of the objects are very small (for the same reason used 640 x 640). Altogether the model has been trained on with 250k objects. |
@NandhiniN85 640x640 may be too large for YOLOv5 EdgeTPU model. |
@zldrobit Thanks for the details! I will try with a smaller resolution and check out the accuracy. Thanks! |
After i converted .pt to .tflite successfully,i runed the command as below: image 1/2 D:\PycharmProjects\yolov5-tf-android\data\images\bus.jpg: 320x320 4 persons, 1 bus, Done. (18.252s) Process finished with exit code 0 |
@ziyuwzf If you run detection on a PC, TFLite could not use GPU. So it runs slower than PyTorch. You could also run your detection on Google Colab to confirm the performance issue. I ran detection with a 20-thread Intel i9-9820X CPU, and the results are |
While exporting the weights getting error: File "models/tf.py", line 453, in |
@rashmi-learning exports are consolidated in export.py now. For TFLite:
|
This PR add models/tf.py to export TensorFlow saved_model, graph_def and TFLite models.
This script support both yolov5 v2 (LeakyReLU activations) and v3 (Hardswish activations) models.
Export TensorFlow and TFLite models using:
and use one of the following command to detect objects:
This PR is tested sucessfully with PyTorchc 1.6, TensorFlow 1.15.3/2.3.0.
However, TFLite export is only supported under TensorFlow 2.3.0.
🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
The PR introduces support for the Android platform to the YOLOv5 model, focusing on setting up the necessary Android project structure.
📊 Key Changes
🎯 Purpose & Impact