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

Reduce inference time #6736

Closed
1 task done
devendraswamy opened this issue Feb 22, 2022 · 17 comments
Closed
1 task done

Reduce inference time #6736

devendraswamy opened this issue Feb 22, 2022 · 17 comments
Labels
question Further information is requested Stale Stale and schedule for closing soon

Comments

@devendraswamy
Copy link

Search before asking

Question

Need a solution/suggestion for reducing the 2sec inference time to less then 1 sec.

Additional

Hi, I developed a 4 models for 4 object detectionsin single inference script and I was successfully inference the 4 models with 2 sec at once. Actually I need a solution for reducing the 2sec inference time to less then 1 sec. I'm trying with torch.set_num_threads( ) and multiprocessing separately but I did not getting results in 1 sec. kindly help me to reduce the inference time(in cpu). Thank you in advance.

@devendraswamy devendraswamy added the question Further information is requested label Feb 22, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Feb 22, 2022

👋 Hello @devendraswamy, 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 [email protected].

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

CI CPU testing

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

@glenn-jocher
Copy link
Member

glenn-jocher commented Feb 22, 2022

@devendraswamy 👋 Hello! Thanks for asking about inference speed issues. YOLOv5 🚀 can be run on CPU (i.e. --device cpu, slow) or GPU if available (i.e. --device 0, faster). You can determine your inference device by viewing the YOLOv5 console output:

detect.py inference

python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images/

detect.py

YOLOv5 PyTorch Hub inference

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Images
dir = 'https://ultralytics.com/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')]  # batch of images

# Inference
results = model(imgs)
results.print()  # or .show(), .save()
# Speed: 631.5ms pre-process, 19.2ms inference, 1.6ms NMS per image at shape (2, 3, 640, 640)

Increase Speeds

If you would like to increase your inference speed some options are:

  • Use batched inference with YOLOv5 PyTorch Hub
  • Reduce --img-size, i.e. 1280 -> 640 -> 320
  • Reduce model size, i.e. YOLOv5x -> YOLOv5l -> YOLOv5m -> YOLOv5s -> YOLOv5n
  • Use half precision FP16 inference with python detect.py --half and python val.py --half
  • Use a faster GPUs, i.e.: P100 -> V100 -> A100
  • Export to ONNX or OpenVINO for up to 3x CPU speedup (CPU Benchmarks)
  • Export to TensorRT for up to 5x GPU speedup
  • Use a free GPU backends with up to 16GB of CUDA memory: Open In Colab Open In Kaggle

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

@devendraswamy
Copy link
Author

devendraswamy commented Feb 22, 2022

@devendraswamy 👋 Hello! Thanks for asking about inference speed issues. YOLOv5 🚀 can be run on CPU (i.e. --device cpu, slow) or GPU if available (i.e. --device 0, faster). You can determine your inference device by viewing the YOLOv5 console output:

detect.py inference

python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images/
detect.py

YOLOv5 PyTorch Hub inference

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Images
dir = 'https://ultralytics.com/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')]  # batch of images

# Inference
results = model(imgs)
results.print()  # or .show(), .save()
# Speed: 631.5ms pre-process, 19.2ms inference, 1.6ms NMS per image at shape (2, 3, 640, 640)

Increase Speeds

If you would like to increase your inference speed some options are:

  • Use batched inference with YOLOv5 PyTorch Hub
  • Reduce --img-size, i.e. 1280 -> 640 -> 320
  • Reduce model size, i.e. YOLOv5x -> YOLOv5l -> YOLOv5m -> YOLOv5s -> YOLOv5n
  • Use half precision FP16 inference with python detect.py --half and python val.py --half
  • Use a faster GPUs, i.e.: P100 -> V100 -> A100
  • Export to ONNX or OpenVINO for up to 3x CPU speedup (CPU Benchmarks)
  • Export to TensorRT for up to 5x GPU speedup
  • Use a free GPU backends with up to 16GB of CUDA memory: Open In Colab Open In Kaggle

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

Thank you for your valuable reply and I am using yolov5s model with 640 image size and pytorch 1.10.1 and also I tried the inference with ONNX model with 640 image size but my inference time is 2sec only. As for now my prediction time is 2 sec for 4 models. if possible please guide me to reduce the inference time to 1sec.

@devendraswamy
Copy link
Author

devendraswamy commented Mar 1, 2022

@devendraswamy 👋 Hello! Thanks for asking about inference speed issues. YOLOv5 🚀 can be run on CPU (i.e. --device cpu, slow) or GPU if available (i.e. --device 0, faster). You can determine your inference device by viewing the YOLOv5 console output:

detect.py inference

python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images/
detect.py ### YOLOv5 PyTorch [Hub](https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading) inference ```python import torch

Model

model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

Images

dir = 'https://ultralytics.com/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')] # batch of images

Inference

results = model(imgs)
results.print() # or .show(), .save()

Speed: 631.5ms pre-process, 19.2ms inference, 1.6ms NMS per image at shape (2, 3, 640, 640)



    
      
    

      
    

    
  
### Increase Speeds
If you would like to increase your inference speed some options are:

* Use batched inference with [YOLOv5 PyTorch Hub](https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading)
* Reduce `--img-size`, i.e. 1280 -> 640 -> 320
* Reduce model size, i.e. YOLOv5x -> YOLOv5l -> YOLOv5m -> YOLOv5s -> YOLOv5n
* Use half precision FP16 inference with `python detect.py --half` and `python val.py --half`
* Use a faster GPUs, i.e.: P100 -> V100 -> A100
* [Export](https://docs.ultralytics.com/yolov5/tutorials/model_export) to ONNX or OpenVINO for up to 3x CPU speedup ([CPU Benchmarks](https://github.com/ultralytics/yolov5/pull/6613))
* [Export](https://docs.ultralytics.com/yolov5/tutorials/model_export) to TensorRT for up to 5x GPU speedup
* Use a free GPU backends with up to 16GB of CUDA memory: [![Open In Colab](https://camo.githubusercontent.com/84f0493939e0c4de4e6dbe113251b4bfb5353e57134ffd9fcab6b8714514d4d1/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667)](https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb) [![Open In Kaggle](https://camo.githubusercontent.com/a08ca511178e691ace596a95d334f73cf4ce06e83a5c4a5169b8bb68cac27bef/68747470733a2f2f6b6167676c652e636f6d2f7374617469632f696d616765732f6f70656e2d696e2d6b6167676c652e737667)](https://www.kaggle.com/models/ultralytics/yolov5)

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

Thank you for your valuable reply and I am using yolov5s model with 640 image size and pytorch 1.10.1 and also I tried the inference with ONNX model with 640 image size but my inference time is 2sec only. As for now my prediction time is 2 sec for 4 models. if possible please guide me to reduce the inference time to 1sec.

Any one please help to reduce the inference time. Thank you.

@DavidBaldsiefen
Copy link
Contributor

@devendraswamy did you attempt using a half-precision model?

If you have a NVIDIA GPU available, the easiest way to speed up inference may be to use TensorRT:

python export.py --weights my-weights.pt --include engine --device 0 then python detect.py --weights my-weights.engine --device 0

@devendraswamy
Copy link
Author

devendraswamy commented Mar 7, 2022 via email

@DavidBaldsiefen
Copy link
Contributor

Is your goal increasing throughput or reducing latency? If it is about throughput, you could try increasing batch size. Latency is more difficult to increase, especially on CPU-bound devices. Do you use a custom trained model?

@devendraswamy
Copy link
Author

devendraswamy commented Mar 7, 2022 via email

@DavidBaldsiefen
Copy link
Contributor

Is there a reason you are using four different models as opposed to one? Do I understand correctly that you try to run inference on a single image with four different models simultaneously?

@devendraswamy
Copy link
Author

Thank you for your support . The hardware will gives four parts of image , i have to detect the objects in that four parts image and gives the results to front end with in 1 sec. currently its taking 2 sec , any suggestions kindly let me know.

@DavidBaldsiefen
Copy link
Contributor

@devendraswamy have you tried passing all four images at once by using batch-size 4?

@devendraswamy
Copy link
Author

@devendraswamy have you tried passing all four images at once by using batch-size 4?

That was not possible because four images are predicted with four different weight files , any other suggestions ,please let me know.

@glenn-jocher
Copy link
Member

@devendraswamy individual models may provide the absolute best mAP, but for most use cases best practice would be to combine your four datasets into one 4-class dataset and train a single model on it. See https://community.ultralytics.com/t/how-to-combine-weights-to-detect-from-multiple-datasets/38/9

@github-actions
Copy link
Contributor

github-actions bot commented Apr 10, 2022

👋 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 ⭐!

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Apr 10, 2022
@devendraswamy
Copy link
Author

But As per Accuracy concern , I need to to train the models individually , Please let me know any another solution for Inference time reduction , I train the all the model with 640 image resolution and I am using onnx models.

Thank you in advance

@PankajBarai
Copy link

My goal is reduce the inference time and I'm using custom model trained weights for inference. Actually it's taking 2 sec for inference the all the four objects with four different weight files at a once. I need to decreases the four models inference time to 1 sec , any help grateful to you.

On Mon, Mar 7, 2022, 18:44 DavidB @.> wrote: Is your goal increasing throughput or reducing latency? If it is about throughput, you could try increasing batch size. Latency is more difficult to increase, especially on CPU-bound devices. Do you use a custom trained model? — Reply to this email directly, view it on GitHub <#6736 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLD6LTBSEFE47MRD45PN73U6X6LFANCNFSM5PANXNMA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you were mentioned.Message ID: @.>

Can you share how u reduce inference time

@glenn-jocher
Copy link
Member

glenn-jocher commented Dec 17, 2022

@PankajBarai 👋 Hello! Thanks for asking about inference speed issues. PyTorch Hub speeds will vary by hardware, software, model, inference settings, etc. Our default example in Colab with a V100 looks like this:

Screen Shot 2022-05-03 at 10 20 39 AM

YOLOv5 🚀 can be run on CPU (i.e. --device cpu, slow) or GPU if available (i.e. --device 0, faster). You can determine your inference device by viewing the YOLOv5 console output:

detect.py inference

python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images/

Screen Shot 2022-05-03 at 2 48 42 PM

YOLOv5 PyTorch Hub inference

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Images
dir = 'https://ultralytics.com/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')]  # batch of images

# Inference
results = model(imgs)
results.print()  # or .show(), .save()
# Speed: 631.5ms pre-process, 19.2ms inference, 1.6ms NMS per image at shape (2, 3, 640, 640)

Increase Speeds

If you would like to increase your inference speed some options are:

  • Use batched inference with YOLOv5 PyTorch Hub
  • Reduce --img-size, i.e. 1280 -> 640 -> 320
  • Reduce model size, i.e. YOLOv5x -> YOLOv5l -> YOLOv5m -> YOLOv5s -> YOLOv5n
  • Use half precision FP16 inference with python detect.py --half and python val.py --half
  • Use a faster GPUs, i.e.: P100 -> V100 -> A100
  • Export to ONNX or OpenVINO for up to 3x CPU speedup (CPU Benchmarks)
  • Export to TensorRT for up to 5x GPU speedup (GPU Benchmarks)
  • Use a free GPU backends with up to 16GB of CUDA memory: Open In Colab Open In Kaggle

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

4 participants