Skip to content
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

Added support for fp16 (half) to export.py #3010

Merged
merged 2 commits into from
May 2, 2021
Merged

Added support for fp16 (half) to export.py #3010

merged 2 commits into from
May 2, 2021

Conversation

hodovo
Copy link
Contributor

@hodovo hodovo commented May 1, 2021

This adds support for the --half argument when exporting a model.

Considerations
I have only tested this with torchscript. It might be a good idea to only support this for torchscript if you know of any problems with ONNX/CoreML and fp16. My focus is on torchscript. I can easily modify this pull request to load the model separately as half for only torchscript and also create a separate img for the trace.

Let me know your thoughts :)

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Added FP16 half-precision export option to YOLOv5 model exporter. 🚀

📊 Key Changes

  • New --half argument to the model export script enabling FP16 half-precision exports. 🎛️
  • Added an assertion to ensure FP16 export is not attempted on CPUs, as it's only compatible with GPUs. 🔒
  • Conditional logic to convert the model and input image to FP16 when the --half flag is used. 🔧

🎯 Purpose & Impact

  • The added --half flag allows users to export their models with reduced precision, which can lead to performance gains on compatible hardware (mostly GPUs) by reducing the computational workload. 📈
  • This update emphasizes efficiency, as using FP16 can decrease model size and increase inference speed, which is beneficial for deployment on edge devices or in environments where resources are limited. 🏎️
  • The assertion prevents users from inadvertently trying to export an FP16 model on unsupported devices like CPUs, avoiding potential errors and confusion. 🚫🖥️

@glenn-jocher
Copy link
Member

@hodovo thanks for the PR! I've minimized the changes a bit but otherwise looks good.

This work conflicts with an earlier PR in progress #2982, so we will merge the earlier PR first hopefully early this week, and then once #2982 is merged we will merge master in this one, resolving conflicts manually.

@hodovo
Copy link
Contributor Author

hodovo commented May 2, 2021

Sounds good! Your changes look much nicer :)

Let me know if you need anything. Thank you.

@glenn-jocher
Copy link
Member

Tested, works well with ONNX (below) and CoreML (not shown).

Screenshot 2021-05-02 at 22 41 19

@glenn-jocher glenn-jocher merged commit e2a80c6 into ultralytics:master May 2, 2021
@glenn-jocher
Copy link
Member

@hodovo PR is merged. Thank you for your contributions!

@hodovo hodovo deleted the fp16-export branch May 2, 2021 22:22
@hodovo
Copy link
Contributor Author

hodovo commented May 2, 2021

My pleasure. Thank you for the awesome platform you have built :)

@xonobo
Copy link

xonobo commented May 3, 2021

Does torchscript work properly in half precision? In this issue it is stated that half precison of convolution is not implemented. A simple testing code for the exported models may be nice for this case.

@hodovo
Copy link
Contributor Author

hodovo commented May 3, 2021

@xonobo Yes it does. That issue is specifically for CPU and not GPU. The implementation in this pull request checks to ensure that the export device is a CUDA device. However, that doesn't enforce how the user utilizes the model afterwards.

KMint1819 pushed a commit to KMint1819/yolov5 that referenced this pull request May 12, 2021
* Added support for fp16 (half) to export.py

* minimize code additions

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
danny-schwartz7 pushed a commit to danny-schwartz7/yolov5 that referenced this pull request May 22, 2021
* Added support for fp16 (half) to export.py

* minimize code additions

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Lechtr pushed a commit to Lechtr/yolov5 that referenced this pull request Jul 20, 2021
* Added support for fp16 (half) to export.py

* minimize code additions

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
(cherry picked from commit e2a80c6)
BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this pull request Aug 26, 2022
* Added support for fp16 (half) to export.py

* minimize code additions

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants