ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.66k stars 16.11k forks source link

why not use batched_nms to replace agnostic ? thanks so much ! #5261

Closed Yaoxingtian closed 2 years ago

Yaoxingtian commented 2 years ago

❔Question agnostic

Additional context

github-actions[bot] commented 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.

Requirements

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

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 commented 2 years ago

@Yaoxingtian batched NMS is used by default in YOLOv5. It has no relation to agnostic, which is an NMS option.

eoinoconn commented 2 years ago

@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://github.com/ultralytics/yolov5/blob/19c8760caa70f4d04d3fb974d797f9d922bf6eb8/utils/general.py#L689-L691

https://pytorch.org/vision/stable/ops.html#torchvision.ops.batched_nms

glenn-jocher commented 2 years ago

@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.

glenn-jocher commented 2 years ago

@Yaoxingtian @eoinoconn line-profiling comparison from today shows ops.nms is faster than ops.batched_nms. This is consistent with past results.

Screenshot 2021-11-02 at 23 32 57
Yaoxingtian commented 2 years ago

@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://github.com/ultralytics/yolov5/blob/19c8760caa70f4d04d3fb974d797f9d922bf6eb8/utils/general.py#L689-L691

https://pytorch.org/vision/stable/ops.html#torchvision.ops.batched_nms

Yes, this is what I want to say. Thanks so much!

Yaoxingtian commented 2 years ago

@Yaoxingtian @eoinoconn line-profiling comparison from today shows ops.nms is faster than ops.batched_nms. This is consistent with past results.

Screenshot 2021-11-02 at 23 32 57

@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 ?

zhiqwang commented 2 years ago

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]
glenn-jocher commented 2 years ago

@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.

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

glenn-jocher commented 2 years ago

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'