文章目录
- NMS代码实现
- 基于im2col的展开Python实现卷积层和池化层
NMS代码实现
import numpy as np
bboxes = np.array([[100, 100, 210, 210, 0.72],
[250, 250, 420, 420, 0.8],
[220, 220, 320, 330, 0.92],
[100, 100, 210, 210, 0.72],
[230, 240, 325, 330, 0.81],
[220, 230, 315, 340, 0.9]])
def nms(iou_thresh=0.5, conf_thr = 0.5):
x1, x2, y1, y2, confidence = bboxes[:,0], bboxes[:,1], bboxes[:,2], bboxes[:,3], bboxes[:,4]
area = (x1-x2)*(y1-y2)
index = confidence.argsort()[::-1]
keep = []
while (len(index)) > 0:
idx_self, idx_other = index[0], index[1:]
if confidence[idx_self] < conf_thr:
break
keep.append(idx_self)
xx1, yy1 = np.maximum(x1[idx_self], x1[idx_other]), np.maximum(y1[idx_self], y1[idx_other])
xx2, yy2 = np.minimum(x2[idx_self], x2[idx_other]), np.maximum(y2[idx_self], y2[idx_other])
w, h = np.maximum(0, xx2-xx1), np.maximum(0, yy2-yy1)
insection = w * h
union = area[idx_self] + area[idx_other] - insection
iou = insection / union
keep_index = np.where(iou <= iou_thresh)[0]
index = index[keep_index + 1]
return np.array(keep)
if __name__ == '__main__':
print(nms())
基于im2col的展开Python实现卷积层和池化层