nms

paddle.vision.ops. nms ( boxes, iou_threshold=0.3, scores=None, category_idxs=None, categories=None, top_k=None ) [source]

This operator implements non-maximum suppression. Non-maximum suppression (NMS) is used to select one bounding box out of many overlapping bounding boxes in object detection. Boxes with IoU > iou_threshold will be considered as overlapping boxes, just one with highest score can be kept. Here IoU is Intersection Over Union, which can be computed by:

\[IoU = \frac{intersection\_area(box1, box2)}{union\_area(box1, box2)}\]

If scores are provided, input boxes will be sorted by their scores firstly.

If category_idxs and categories are provided, NMS will be performed with a batched style, which means NMS will be applied to each category respectively and results of each category will be concated and sorted by scores.

If K is provided, only the first k elements will be returned. Otherwise, all box indices sorted by scores will be returned.

Parameters
  • boxes (Tensor) – The input boxes data to be computed, it’s a 2D-Tensor with the shape of [num_boxes, 4]. The data type is float32 or float64. Given as [[x1, y1, x2, y2], …], (x1, y1) is the top left coordinates, and (x2, y2) is the bottom right coordinates. Their relation should be 0 <= x1 < x2 && 0 <= y1 < y2.

  • iou_threshold (float32, optional) – IoU threshold for determine overlapping boxes. Default value: 0.3.

  • scores (Tensor, optional) – Scores corresponding to boxes, it’s a 1D-Tensor with shape of [num_boxes]. The data type is float32 or float64. Default: None.

  • category_idxs (Tensor, optional) – Category indices corresponding to boxes. it’s a 1D-Tensor with shape of [num_boxes]. The data type is int64. Default: None.

  • categories (List, optional) – A list of unique id of all categories. The data type is int64. Default: None.

  • top_k (int64, optional) – The top K boxes who has higher score and kept by NMS preds to consider. top_k should be smaller equal than num_boxes. Default: None.

Returns

1D-Tensor with the shape of [num_boxes]. Indices of boxes kept by NMS.

Return type

Tensor

Examples

import paddle

boxes = paddle.rand([4, 4]).astype('float32')
boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
boxes[:, 3] = boxes[:, 1] + boxes[:, 3]
print(boxes)
# Tensor(shape=[4, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [[0.64811575, 0.89756244, 0.86473107, 1.48552322],
#         [0.48085716, 0.84799081, 0.54517937, 0.86396021],
#         [0.62646860, 0.72901905, 1.17392159, 1.69691563],
#         [0.89729202, 0.46281594, 1.88733089, 0.98588502]])

out = paddle.vision.ops.nms(boxes, 0.1)
print(out)
# Tensor(shape=[3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
#        [0, 1, 3])

scores = paddle.to_tensor([0.6, 0.7, 0.4, 0.233])

categories = [0, 1, 2, 3]
category_idxs = paddle.to_tensor([2, 0, 0, 3], dtype="int64")

out = paddle.vision.ops.nms(boxes,
                            0.1,
                            paddle.to_tensor(scores),
                            paddle.to_tensor(category_idxs),
                            categories,
                            4)
print(out)
# Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True,
#        [1, 0, 2, 3])