非极大抑制(NMS:Non Maximum Suppression)起到边框(水平框或倾斜框)去重叠的作用,广泛应用于通用目标检测、人脸检测与OCR检测等算法的后处理。简单的Python实现如下:
def standard_nms(S, thres):
order = np.argsort(S[:, -1])[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
ovr = np.array([intersection(S[i], S[t]) for t in order[1:]])
inds = np.where(ovr <= thres)[0]
order = order[inds+1]
return S[keep]
MXNet的MultiBoxDetection涉及的NMS,体现了非常高的计算效率,API调用接口: mx.contrib.ndarray.MultiBoxDetection,CUDA代码链接以及具体解析如下:
https://github.com/apache/incubator-mxnet/blob/master/src/operator/contrib/multibox_detection.cu
// 限定数值取值范围;__device__声明的核函数只能被CUDA核函数调用
template<typename DType>
__device__ void Clip(DType *value, const DType lower, const DType upper) {
if ((*value) < lower) *value = lower;
if ((*value) > upper) *value = upper;
}
// 计算两个水平框的IOU=交集面积/并集面积
template<typename DType>
__device__ void CalculateOverlap(const DType *a, const DType *b, DType *iou) {
DType w = max(DType(0), min(a[2], b[2]) - max(a[0], b[0]));
DType h = max(DType(0), min(a[3], b[3]) - max(a[1], b[1]));
DType i = w * h;
DType u = (a[2] - a[0]) * (