yolov5 icon indicating copy to clipboard operation
yolov5 copied to clipboard

Batch Inferencing

Open Sanath1998 opened this issue 3 years ago • 6 comments

Search before asking

  • [X] I have searched the YOLOv5 issues and discussions and found no similar questions.

Question

Hi @glenn-jocher,

How can I do batch inferencing instead of sequential inferencing using repository. Any solid documentation or any useful findings will be helpful for me.

There are two scripts for inferencing one is detect.py (Qualitative Inference) and val.py (Quantitative Inference). Could you please explain me on how to do batch inference on any of the above mentioned scripts.

Additional

No response

Sanath1998 avatar Dec 01 '22 08:12 Sanath1998

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

glenn-jocher avatar Dec 01 '22 20:12 glenn-jocher

Hi @glenn-jocher ,

I was able to do sequential inference using detect.py script and batch inferencing using val.py script. One strange thing observed is that, in batch inferencing the time taken to complete inference per batch at the initial stage is less compared to in the middle batch as well as last batch. Theoretically explained as below.

If x is the initial time, 5 is the batch size for 20 images. So there will be 4 batches to complete inference on 20 images. Please refer to table below. Time Taken Per Batch Size Batch Size


               x                                               1st Batch
               x+2                                           2nd Batch
               x+3                                           3rd Batch
               x                                               4th Batch
               

Observation here is that, in the middle batches the time taken is quite more that initial and final batches.

@glenn-jocher Could you please explain on this so that it would be very beneficial. Thankss : )

Sanath1998 avatar Dec 02 '22 08:12 Sanath1998

@Sanath1998 val.py sorts a dataset by aspect ratio and runs rectangular inference. The initial and final batches will have low/high aspect ratios while the middle of the dataset is more square (more FLOPs).

glenn-jocher avatar Dec 02 '22 21:12 glenn-jocher

Hi @glenn-jocher ,

Actually I have one question. If we convert pytorch models to TensorRT models with specific batch size say 4, then we get TensorRT model with Batch Size 4.

Can't we do batch inference for any other batch size where we feed in Batch Size 4 model as an input. I'm not able to do inference for the same. In other words how can we unify the tensorRT inference supporting any batch size input?

Sanath1998 avatar Dec 06 '22 08:12 Sanath1998

@Sanath1998 TRT exports with --dynamic allow for any batch size up to --batch-size

glenn-jocher avatar Dec 06 '22 17:12 glenn-jocher

👋 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:

  • Wiki – https://github.com/ultralytics/yolov5/wiki
  • Tutorials – https://docs.ultralytics.com/yolov5
  • Docs – https://docs.ultralytics.com

Access additional Ultralytics ⚡ resources:

  • Ultralytics HUB – https://ultralytics.com/hub
  • Vision API – https://ultralytics.com/yolov5
  • About Us – https://ultralytics.com/about
  • Join Our Team – https://ultralytics.com/work
  • Contact Us – https://ultralytics.com/contact

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[bot] avatar Jan 07 '23 00:01 github-actions[bot]