ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.37k stars 16.05k forks source link

Add support for MLProgram #13076

Closed Harryjeffs closed 1 month ago

Harryjeffs commented 2 months ago

Search before asking

Description

Currently, converting a YOLOv5 model to CoreML with NMS results in a neural network. However, CoreMLTools v6.0 introduces a more performant and streamlined MLProgram type, used in YOLOv8. Can we backport MLProgram support for YOLOv5 models? This enhancement could provide significant performance improvements.

Use case

With WWDC around the corner and an increased focus on AI/ML, having quicker access to more efficient models can enhance developer and user experience.

Additional

https://apple.github.io/coremltools/docs-guides/source/comparing-ml-programs-and-neural-networks.html

Are you willing to submit a PR?

github-actions[bot] commented 2 months ago

👋 Hello @Harryjeffs, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 2 months ago

Hello,

Thank you for your suggestion and for your willingness to contribute to the YOLOv5 project! The introduction of MLProgram support in CoreMLTools v6.0 indeed presents an exciting opportunity for performance improvements.

To proceed effectively, here are a few steps we can take:

  1. Reproducible Example: If you have a minimum reproducible code example that demonstrates the current conversion process and highlights any limitations or issues, please share it. This will help us understand the context better and ensure we can reproduce and address any potential bugs. You can refer to our minimum reproducible example guide for more details.

  2. Version Check: Ensure you are using the latest versions of torch and the YOLOv5 repository. This helps us confirm that any issues or enhancements are based on the most current codebase. If you haven't updated recently, please do so and try the conversion again.

  3. PR Submission: Since you are interested in submitting a PR, we encourage you to start by forking the repository and creating a new branch for your changes. Once you have a working implementation, you can submit a PR for review. Make sure to include detailed documentation and any necessary tests to validate the new functionality.

Here is a basic outline to get you started with the conversion process using CoreMLTools v6.0:

import coremltools as ct
import torch
from models.common import DetectMultiBackend

# Load YOLOv5 model
model = DetectMultiBackend('yolov5s.pt', device='cpu')

# Convert to CoreML
input_shape = (1, 3, 640, 640)  # Example input shape
dummy_input = torch.zeros(input_shape)
traced_model = torch.jit.trace(model, dummy_input)

# Convert to CoreML with MLProgram
mlmodel = ct.convert(
    traced_model,
    inputs=[ct.TensorType(shape=input_shape)],
    convert_to="mlprogram"
)

# Save the CoreML model
mlmodel.save("yolov5s_mlprogram.mlmodel")

Feel free to adjust the code as needed and share any findings or improvements you make. The YOLO community and the Ultralytics team are here to support you throughout the process.

Looking forward to your contribution!

github-actions[bot] commented 1 month ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐