Closed Yaoxingtian closed 2 years ago
π Hello @Yaoxingtian, 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 Glenn Jocher at glenn.jocher@ultralytics.com.
Python>=3.6.0 with all requirements.txt installed including PyTorch>=1.7. To get started:
$ git clone https://github.com/ultralytics/yolov5
$ cd yolov5
$ pip install -r requirements.txt
YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
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.
@Yaoxingtian batched NMS is used by default in YOLOv5. It has no relation to agnostic, which is an NMS option.
@glenn-jocher I think what @Yaoxingtian is asking is why do you perform batched NMS by shifting all boxes by their class index and max_wh and calling torchvision.ops.nms(). Surely using torchvision.ops.batched_nms() would be faster as it wouldn't attempt to compare boxes that are not the same class?
https://pytorch.org/vision/stable/ops.html#torchvision.ops.batched_nms
@eoinoconn ah I see. I think the process is similar internally with batched_nms, but the last time we profiled this option was slower than the current. If this has changed please let us know though, we'd love to be able to speed this up if possible.
@Yaoxingtian @eoinoconn line-profiling comparison from today shows ops.nms
is faster than ops.batched_nms
. This is consistent with past results.
@glenn-jocher I think what @Yaoxingtian is asking is why do you perform batched NMS by shifting all boxes by their class index and max_wh and calling torchvision.ops.nms(). Surely using torchvision.ops.batched_nms() would be faster as it wouldn't attempt to compare boxes that are not the same class?
https://pytorch.org/vision/stable/ops.html#torchvision.ops.batched_nms
Yes, this is what I want to say. Thanks so much!
@Yaoxingtian @eoinoconn line-profiling comparison from today shows
ops.nms
is faster thanops.batched_nms
. This is consistent with past results.
@glenn-jocher @eoinoconn thanks for your reply ! I've tried to use agnostic and torchvision.ops.batched_nms, and they are both help to eliminate overlap boxes. so can agnostic replaces by batched_nms? @glenn-jocher I see the result you showed, could you tell me what the tool you use to calculate the op time ?
FYI, Previously, in order to export the post-processing part to onnx and torchscript graph, I've implemented a batched_nms
version of YOLOv5, the current code structure will be a little more streamlined, as the price, will reduce part of the functionality. At the moment I can guarantee that the accuracy of the two is basically the same, but I have not done a careful comparison in terms of inference time. I guess that this implementation can be used as a reference.
https://github.com/zhiqwang/yolov5-rt-stack/blob/284e3e2/yolort/models/box_head.py#L371-L385
# Compute conf
# box_conf x class_conf, w/ shape: num_anchors x num_classes
scores = pred_logits[:, 5:] * pred_logits[:, 4:5]
boxes = det_utils.decode_single(pred_logits[:, :4], anchors_tuple)
# remove low scoring boxes
inds, labels = torch.where(scores > self.score_thresh)
boxes, scores = boxes[inds], scores[inds, labels]
# non-maximum suppression, independently done per level
keep = box_ops.batched_nms(boxes, scores, labels, self.nms_thresh)
# keep only topk scoring head_outputs
keep = keep[: self.detections_per_img]
boxes, scores, labels = boxes[keep], scores[keep], labels[keep]
@Yaoxingtian as I said before agnostic has nothing to do with batched vs non-batched, which are just methods of implementing NMS. Agnostic is a setting that allows you to apply NMS treating all boxes as class-agnostic.
Profiling tool is line profiler.
π 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 β!
Bench script for NMS:
from utils.torch_utils import time_sync
# Batched NMS
c = x[:, 5] * (0 if agnostic else 1) # classes
boxes, scores = x[:, :4], x[:, 4] # boxes (offset by class), scores
t = time_sync()
for _ in range(100):
i = torchvision.ops.batched_nms(boxes, scores, c, iou_thres) # NMS
print('torchvision.ops.batched_nms', time_sync() - t)
# NMS
c = x[:, 5:6] * (0 if agnostic else max_wh) # classes
boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores
t = time_sync()
for _ in range(100):
j = torchvision.ops.nms(boxes, scores, iou_thres) # NMS
print('torchvision.ops.nms', time_sync() - t)
assert torch.allclose(i, j), 'allclose failure'
βQuestion agnostic
Additional context