megengine.functional.nn.nms

nms(boxes, scores, iou_thresh, max_output=None)[源代码]

Performs non-maximum suppression (NMS) on the boxes according to their intersection-over-union(IoU).

参数
  • boxes (Tensor) – tensor of shape (N, 4); the boxes to perform nms on; each box is expected to be in (x1, y1, x2, y2) format.

  • iou_thresh (float) – IoU threshold for overlapping.

  • scores (Tensor) – tensor of shape (N,), the score of boxes.

  • max_output (Optional[int]) – the maximum number of boxes to keep; it is optional if this operator is not traced otherwise it required to be specified; if it is not specified, all boxes are kept.

返回类型

Tensor

返回

indices of the elements that have been kept by NMS, sorted by scores.

注解

max_output should be specified and should have valid positive value under tracing.

实际案例

import numpy as np
from megengine import tensor
import megengine.functional as F

x = np.zeros((100,4))
np.random.seed(42)
x[:,:2] = np.random.rand(100,2)*20
x[:,2:] = np.random.rand(100,2)*20 + 100
scores = tensor(np.random.rand(100))
inp = tensor(x)
result = F.vision.nms(inp, scores, iou_thresh=0.7)
print(result.numpy())

Outputs:

[75 69]