ultralytics / yolov5

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

How to do real-time object detection? #9726

Closed Wuhanhan123 closed 1 year ago

Wuhanhan123 commented 2 years ago

Search before asking

Question

Hi. How to do real-time object detection? Thanks :)

Additional

No response

github-actions[bot] commented 2 years ago

👋 Hello @Wuhanhan123, 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 screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. 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.

glenn-jocher commented 2 years ago

@Wuhanhan123 👋 Hello! Thanks for asking about handling inference results. YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a pure python environment without using detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # yolov5n - yolov5x6 official model
#                                            'custom', 'path/to/best.pt')  # custom model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0]  # im predictions (tensor)

results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

results.pandas().xyxy[0].value_counts('name')  # class counts (pandas)
# person    2
# tie       1

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck 🍀 and let us know if you have any other questions!

github-actions[bot] commented 2 years ago

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

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 YOLOv5 🚀 and Vision AI ⭐!

khelkun commented 1 year ago

I raise up the question after several researches on the subject because I try to find how to achieve the fatest inference object detection with a Tensorflow.js model.

Those benchmarks on the exported variants of YOLOv5 are interesting, except there's none for Tensorflow.js (NaN). Would you have benchmarks about Tensorflow.js?

My experience in running Tensorflow.js inference in my web browser reveal this:

60ms is 16 FPS, 90ms is 11 FPS, how could I get closer to 30 FPS (e.g 33ms)?

glenn-jocher commented 11 months ago

@khelkun thanks for reaching out! Achieving faster inference with a Tensorflow.js model can be an iterative process involving both model architecture and the hardware it runs on. Here are a few optimizations to consider:

  1. Quantization: Reducing model precision which lowers inference time at the cost of accuracy.
  2. Model Pruning: Removing unimportant weights to speed up inference while maintaining accuracy.
  3. Hardware Acceleration: Using WebGL backend for Tensorflow.js which leverages the GPU for improved performance.
  4. Batch Inference: Processing multiple inputs simultaneously can improve throughput.
  5. Model Architecture: Experiment with lighter YOLOv5 variants or customizing model design based on specific requirements.

The community may provide further insights on optimizing Tensorflow.js inference speeds while maintaining model accuracy. All the best with your optimization efforts! 🚀