标准NMS(SNMS):
1) 所有检出的output bbox按cls score划分(对于正类进行划分)
2)bbox的cls score做降序排列,得到一个降序的order
3) 从order中top1 cls score 开始,计算该bbox 与 order中其他bbox的IOU,IOU大于阈值(说明为同一bbox),剔除
4)对order中所有的bbox进行上操作
import numpy as np import numpy as np def py_cpu_nms(dets, thresh): """Pure Python NMS baseline.""" x1 = dets[:, 0] #结果:[ 30. 50. 210. 430.] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] areas = (x2 - x1 + 1) * (y2 - y1 + 1) #结果:[ 36381. 36081. -5064. 2511.] # res是按照score降序排序的 res = scores.argsort()[::-1] #结果:[0 1 2 3] # print(res) # print(x1[res[0]]) keep = [] while res.size > 0: keep.append(res[0]) xx1 = np.maximum(x1[res[0]], x1[res[1:]]) #res[1:] 由于每次都要与res[0]进行比较 yy1 = np.maximum(y1[res[0]], y1[res[1:]]) xx2 = np.minimum(x2[res[0]], x2[res[1:]]) yy2 = np.minimum(y2[res[0]], y2[res[1:]]) w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[res[0]] + areas[res[1:]] - inter) # 去除掉与分数重叠度大于设定阈值的anchor box(或滑框) inds = np.where(ovr <= thresh)[0] #结果 训练3次 [1 2]表示,第二行,第三行各一个满足条件 #返回满足条件的行索引(一行有几个满足条件,返回几次) # 真实行数=索引+1 res = res[inds + 1] #结果 训练3次 [2 3],[3],[] print(res) return keep # test if __name__ == "__main__": dets = np.array([[30, 20, 230, 200, 1], [50, 50, 260, 220, 0.9], [210, 30, 420, 5, 0.8], [430, 280, 460, 360, 0.7]]) thresh = 0.1 keep_dets = py_cpu_nms(dets, thresh) print(keep_dets)
举例子:
import numpy as np a = np.array([[0, 1, 2], [0, 2, 4], [0, 9, 6]]) b=np.where(a < 4) print(b)
结果:(array([0, 0, 0, 1, 1, 2], dtype=int64), array([0, 1, 2, 0, 1, 0], dtype=int64))
表示: 满足条件行数列表,满足条件列索引
2、计算IOU # 标注方向 上左下右(逆时针)
def compute_iou(rec1, rec2): # computing area of each rectangles S1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1]) S2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1]) # computing the S S = S1 + S2 # find the each edge of intersect rectangle #并集面积 left = max(rec1[1], rec2[1]) right = min(rec1[3], rec2[3]) top = max(rec1[0], rec2[0]) bottom = min(rec1[2], rec2[2]) w=max(0,right-left) h=max(0,bottom-top) area=w*h return (area/ (S - area)) * 1.0
2.局部感知NMS(LNMS)
解决:文本检测面临的是成千上万个几何体(对与每个框)
提出:基