zhiqwang / yolort

yolort is a runtime stack for yolov5 on specialized accelerators such as tensorrt, libtorch, onnxruntime, tvm and ncnn.
https://zhiqwang.com/yolort
GNU General Public License v3.0
708 stars 153 forks source link
deployment detection graghsurgeon inference jit libtorch lightning ncnn nms onnx onnxruntime pytorch tensorrt torchscript trt tvm visualization yolo yolort yolov5
**YOLOv5 Runtime Stack** ______________________________________________________________________ [Documentation](https://zhiqwang.com/yolort/) • [Installation Instructions](https://zhiqwang.com/yolort/installation.html) • [Deployment](#-deployment) • [Contributing](.github/CONTRIBUTING.md) • [Reporting Issues](https://github.com/zhiqwang/yolort/issues/new?assignees=&labels=&template=bug-report.yml) ______________________________________________________________________ [![Python Version](https://img.shields.io/badge/Python-3.6--3.10-FFD43B?logo=python)](https://pypi.org/project/yolort/) [![PyPI version](https://img.shields.io/pypi/v/yolort?color=4D97FF&logo=PyPI)](https://badge.fury.io/py/yolort) [![PyPI downloads](https://static.pepy.tech/personalized-badge/yolort?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=pypi%20downloads)](https://pepy.tech/project/yolort) [![Github downloads](https://img.shields.io/github/downloads/zhiqwang/yolort/total?label=Model%20downloads&logo=PyTorch&color=FF6F00&logoColor=EE4C2C)](https://github.com/zhiqwang/yolort/releases) [![Slack](https://img.shields.io/badge/Slack%20chat-4A154B?logo=slack&logoColor=white)](https://join.slack.com/t/yolort/shared_invite/zt-mqwc7235-940aAh8IaKYeWclrJx10SA) [![PRs Welcome](https://img.shields.io/badge/PRs%20welcome-792EE5?logo=GitHub-Sponsors&logoColor=#white)](.github/CONTRIBUTING.md) [![CI testing](https://github.com/zhiqwang/yolort/actions/workflows/ci-test.yml/badge.svg)](https://github.com/zhiqwang/yolort/actions/workflows/ci-test.yml) [![Build & deploy docs](https://github.com/zhiqwang/yolort/actions/workflows/gh-pages.yml/badge.svg)](https://github.com/zhiqwang/yolort/tree/gh-pages) [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/zhiqwang/yolort/main.svg)](https://results.pre-commit.ci/latest/github/zhiqwang/yolort/main) [![codecov](https://codecov.io/gh/zhiqwang/yolort/branch/main/graph/badge.svg?token=1GX96EA72Y)](https://codecov.io/gh/zhiqwang/yolort) ______________________________________________________________________

🤗 Introduction

What it is. Yet another implementation of Ultralytics's YOLOv5. yolort aims to make the training and inference of the object detection task integrate more seamlessly together. yolort now adopts the same model structure as the official YOLOv5. The significant difference is that we adopt the dynamic shape mechanism, and within this, we can embed both pre-processing (letterbox) and post-processing (nms) into the model graph, which simplifies the deployment strategy. In this sense, yolort makes it possible to deploy the object detection more easily and friendly on LibTorch, ONNX Runtime, TVM, TensorRT and so on.

About the code. Follow the design principle of detr:

object detection should not be more difficult than classification, and should not require complex libraries for training and inference.

yolort is very simple to implement and experiment with. Do you like the implementation of torchvision's faster-rcnn, retinanet or detr? Do you like yolov5? You'll love yolort!

YOLO inference demo

🆕 What's New

🛠️ Usage

There are no extra compiled components in yolort and package dependencies are minimal, so the code is very simple to use.

Installation and Inference Examples

Loading via torch.hub

The models are also available via torch hub, to load yolov5s with pretrained weights simply do:

model = torch.hub.load("zhiqwang/yolort:main", "yolov5s", pretrained=True)

Loading checkpoint from official yolov5

The following is the interface for loading the checkpoint weights trained with ultralytics/yolov5. Please see our documents on what we share and how we differ from yolov5 for more details.

from yolort.models import YOLOv5

# Download checkpoint from https://github.com/ultralytics/yolov5/releases/download/v6.0/yolov5s.pt
ckpt_path_from_ultralytics = "yolov5s.pt"
model = YOLOv5.load_from_yolov5(ckpt_path_from_ultralytics, score_thresh=0.25)

model.eval()
img_path = "test/assets/bus.jpg"
predictions = model.predict(img_path)

🚀 Deployment

Inference on LibTorch backend

We provide a tutorial to demonstrate how the model is converted into torchscript. And we provide a C++ example of how to do inference with the serialized torchscript model.

Inference on ONNX Runtime backend

We provide a pipeline for deploying yolort with ONNX Runtime.

from yolort.runtime import PredictorORT

# Load the serialized ONNX model
engine_path = "yolov5n6.onnx"
y_runtime = PredictorORT(engine_path, device="cpu")

# Perform inference on an image file
predictions = y_runtime.predict("bus.jpg")

Please check out this tutorial to use yolort's ONNX model conversion and ONNX Runtime inferencing. And you can use the example for ONNX Runtime C++ interface.

Inference on TensorRT backend

The pipeline for TensorRT deployment is also very easy to use.

import torch
from yolort.runtime import PredictorTRT

# Load the serialized TensorRT engine
engine_path = "yolov5n6.engine"
device = torch.device("cuda")
y_runtime = PredictorTRT(engine_path, device=device)

# Perform inference on an image file
predictions = y_runtime.predict("bus.jpg")

Besides, we provide a tutorial detailing yolort's model conversion to TensorRT and the use of the Python interface. Please check this example if you want to use the C++ interface.

🎨 Model Graph Visualization

Now, yolort can draw the model graph directly, checkout our tutorial to see how to use and visualize the model graph.

YOLO model visualize

👋 Contributing

We love your input! Please see our Contributing Guide to get started and for how to help out. Thank you to all our contributors! If you like this project please consider ⭐ this repo, as it is the simplest way to support us.

Contributors

📖 Citing yolort

If you use yolort in your publication, please cite it by using the following BibTeX entry.

@Misc{yolort2021,
  author =       {Zhiqiang Wang and Song Lin and Shiquan Yu and Wei Zeng and Fidan Kharrasov},
  title =        {YOLORT: A runtime stack for object detection on specialized accelerators},
  howpublished = {\url{https://github.com/zhiqwang/yolort}},
  year =         {2021}
}

🎓 Acknowledgement