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 import numpy as np boxes = np.random.rand(4, 4).astype('float32') boxes[:, 2] = boxes[:, 0] + boxes[:, 2] boxes[:, 3] = boxes[:, 1] + boxes[:, 3] # [[0.06287421 0.5809351 0.3443958 0.8713329 ] # [0.0749094 0.9713205 0.99241287 1.2799143 ] # [0.46246734 0.6753201 1.346266 1.3821303 ] # [0.8984796 0.5619834 1.1254641 1.0201943 ]] out = paddle.vision.ops.nms(paddle.to_tensor(boxes), 0.1) # [0, 1, 3, 0] scores = np.random.rand(4).astype('float32') # [0.98015213 0.3156527 0.8199343 0.874901 ] categories = [0, 1, 2, 3] category_idxs = np.random.choice(categories, 4) # [2 0 0 3] out = paddle.vision.ops.nms(paddle.to_tensor(boxes), 0.1, paddle.to_tensor(scores), paddle.to_tensor(category_idxs), categories, 4) # [0, 3, 2]