def calcoverlaps(BBGT, bb): overlaps = [] for index, GT in enumerate(BBGT): overlap = polyiou.iou_poly(polyiou.VectorDouble(BBGT[index]), polyiou.VectorDouble(bb)) overlaps.append(overlap) return overlaps
def obb_iou(self, dt, gt): ''' gt is a list of GT category_id = 1 of img_id = 1 dt is a list of candidates category_id = 1 of img_id = 1 ''' if len(gt) == 0: return [] total_iou_array = [] for bb in dt: bb = np.array(bb) # bb_xmin = np.min(bb[0::2]) # bb_ymin = np.min(bb[1::2]) # bb_xmax = np.max(bb[0::2]) # bb_ymax = np.max(bb[1::2]) # # 1. calculate the overlaps between hbbs, if the iou between hbbs are 0, the iou between obbs are 0, too. BBGT = np.array(gt) # BBGT_xmin = np.min(BBGT[:, 0::2], axis=1) # BBGT_ymin = np.min(BBGT[:, 1::2], axis=1) # BBGT_xmax = np.max(BBGT[:, 0::2], axis=1) # BBGT_ymax = np.max(BBGT[:, 1::2], axis=1) # ixmin = np.maximum(BBGT_xmin, bb_xmin) # iymin = np.maximum(BBGT_ymin, bb_ymin) # ixmax = np.minimum(BBGT_xmax, bb_xmax) # iymax = np.minimum(BBGT_ymax, bb_ymax) # iw = np.maximum(ixmax - ixmin + 1., 0.) # ih = np.maximum(iymax - iymin + 1., 0.) # inters = iw * ih # # union # uni = ((bb_xmax - bb_xmin + 1.) * (bb_ymax - bb_ymin + 1.) + # (BBGT_xmax - BBGT_xmin + 1.) * # (BBGT_ymax - BBGT_ymin + 1.) - inters) # overlaps = inters / uni # candidate_iou_list = overlaps candidate_iou_list = [] for bbgt in BBGT: bbgt = bbgt.astype(float) bb = bb.astype(float) overlap = polyiou.iou_poly(polyiou.VectorDouble(bbgt), polyiou.VectorDouble(bb)) candidate_iou_list.append(overlap) if total_iou_array == []: total_iou_array = np.array([candidate_iou_list]) else: total_iou_array = np.vstack( (total_iou_array, candidate_iou_list) ) return total_iou_array
def _calculate_ious_one_batch(self, pred_bboxes, target_bboxes): """ 根据pred_bboxes: (num_obj, 8)和target_bboxes: (num_obj, 8), 计算ious: (num_obj, )的list """ num_obj = pred_bboxes.shape[0] ious_one_batch = [] for i in range(num_obj): iou = polyiou.iou_poly(polyiou.VectorDouble(pred_bboxes[i]), polyiou.VectorDouble(target_bboxes[i])) ious_one_batch.append(iou) return ious_one_batch
def py_cpu_nms_poly(dets, thresh): # 多边形的计算 """ 根据阈值 计算nms :param dets: 多条检测记录 :param thresh: nms 阈值 :return: """ scores = dets[:, 8] # 第9个字段是分数 polys = [] areas = [] for i in range(len(dets)): tm_polygon = polyiou.VectorDouble([dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4], dets[i][5], dets[i][6], dets[i][7]]) polys.append(tm_polygon) order = scores.argsort()[::-1] # 从大到小的sorce的下标 keep = [] # nms算法 while order.size > 0: ovr = [] i = order[0] keep.append(i) for j in range(order.size - 1): iou = polyiou.iou_poly(polys[i], polys[order[j + 1]]) ovr.append(iou) ovr = np.array(ovr) inds = np.where(ovr <= thresh)[0] order = order[inds + 1] return keep # 保留的下标
def py_cpu_nms_poly(dets, thresh): scores = dets[:, 8] polys = [] areas = [] for i in range(len(dets)): tm_polygon = polyiou.VectorDouble([dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4], dets[i][5], dets[i][6], dets[i][7]]) polys.append(tm_polygon) order = scores.argsort()[::-1] keep = [] while order.size > 0: ovr = [] i = order[0] keep.append(i) for j in range(order.size - 1): iou = polyiou.iou_poly(polys[i], polys[order[j + 1]]) ovr.append(iou) ovr = np.array(ovr) # print('ovr: ', ovr) # print('thresh: ', thresh) try: if math.isnan(ovr[0]): pdb.set_trace() except: pass inds = np.where(ovr <= thresh)[0] # print('inds: ', inds) order = order[inds + 1] return keep
def py_cpu_nms_poly(dets, thresh): scores = dets[:, 8] polys = [] areas = [] for i in range(len(dets)): tm_polygon = polyiou.VectorDouble([ dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4], dets[i][5], dets[i][6], dets[i][7] ]) polys.append(tm_polygon) order = scores.argsort()[::-1] keep = [] while order.size > 0: ovr = [] i = order[0] keep.append(i) for j in range(order.size - 1): iou_test = polyiou.iou_poly(polys[i], polys[order[j + 1]]) ovr.append(iou_test.iou) ovr = np.array(ovr) inds = np.where(ovr <= thresh)[0] order = order[inds + 1] return keep
def py_cpu_nms_poly(dets, thresh): scores = dets[:, 8] polys = [] areas = [] for i in range(len(dets)): tm_polygon = polyiou.VectorDouble([dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4], dets[i][5], dets[i][6], dets[i][7]]) polys.append(tm_polygon) order = scores.argsort()[::-1] keep = [] while order.size > 0: ovr = [] i = order[0] keep.append(i) for j in range(order.size - 1): iou = polyiou.iou_poly(polys[i], polys[order[j + 1]]) ovr.append(iou) if (iou != iou): print('poly i:', polys[i], 'polys j + 1:', polys[j + 1]) print('det i:', dets[i], 'dets j + 1:', dets[order[j + 1]]) pdb.set_trace() if (np.sum(ovr != ovr) > 0): print('before') pdb.set_trace() ovr2 = np.array(ovr) if (np.sum(ovr2 != ovr2) > 0): print('after') pdb.set_trace() inds = np.where(ovr2 <= thresh)[0] order = order[inds + 1] return keep
def py_cpu_nms_poly(dets, thresh): """ 任意四点poly nms.取出nms后的边框的索引 @param dets: shape(detection_num, [poly, confidence1]) 原始图像中的检测出的目标数量 @param thresh: @return: keep: 经nms后的目标边框的索引 """ scores = dets[:, 8] polys = [] areas = [] for i in range(len(dets)): tm_polygon = polyiou.VectorDouble([ dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4], dets[i][5], dets[i][6], dets[i][7] ]) polys.append(tm_polygon) # argsort将元素小到大排列 返回索引值 [::-1]即从后向前取元素 order = scores.argsort()[::-1] # 取出元素的索引值 顺序为从大到小 keep = [] while order.size > 0: ovr = [] i = order[0] # 取出当前剩余置信度最大的目标边框的索引 keep.append(i) for j in range(order.size - 1): # 求出置信度最大poly与其他所有poly的IoU iou = polyiou.iou_poly(polys[i], polys[order[j + 1]]) ovr.append(iou) ovr = np.array(ovr) inds = np.where(ovr <= thresh)[0] # 找出iou小于阈值的索引 order = order[inds + 1] return keep
def py_cpu_nms_poly(dets, thresh): if dets.shape[0] == 0: return [] scores = dets[:, 8] polys = [] areas = [] for i in range(len(dets)): tm_polygon = polyiou.VectorDouble([ dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4], dets[i][5], dets[i][6], dets[i][7] ]) polys.append(tm_polygon) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) if order.size == 1: # in case of np.where warning return keep ovr = [] for _j in range(1, order.size): j = order[_j] iou = polyiou.iou_poly(polys[i], polys[j]) ovr.append(iou) if np.isnan(iou): # BUG: when the poly points are collinear, the calculation will get nan print dets[i, :], dets[j, :] ovr = np.array(ovr) #np.seterr(all='raise') inds = np.where(ovr <= thresh)[0] order = order[inds + 1] return keep
def calcoverlaps(BBGT, bb): overlaps = [] for index, GT in enumerate(BBGT): # gtpoly = shgeo.Polygon([(BBGT[index, 0], BBGT[index, 1]), # (BBGT[index, 2], BBGT[index,3]), # (BBGT[index, 4], BBGT[index, 5]), # (BBGT[index, 6], BBGT[index, 7])]) # detpoly = shgeo.Polygon([(bb[0], bb[1]), # (bb[2], bb[3]), # (bb[4], bb[5]), # (bb[6], bb[7])]) # overlap = calc_iou(gtpoly, detpoly) overlap = polyiou.iou_poly(polyiou.VectorDouble(BBGT[index]), polyiou.VectorDouble(bb)) # overlap = polygon_iou(BBGT[index], bb) overlaps.append(overlap) return overlaps
def nms_overlap_all(bboxes, nms_thresh): score = bboxes[:, -1] order = score.argsort()[::-1] # reverse vector out_boxes = np.zeros(bboxes.shape[0]) for i in range(bboxes.shape[0]): _i = order[i] if out_boxes[_i] == 1: continue for j in range(i + 1, bboxes.shape[0]): _j = order[j] if out_boxes[_j] == 1: continue overlap = polyiou.iou_poly(polyiou.VectorDouble(bboxes[_i][0:8]), polyiou.VectorDouble(bboxes[_j][0:8])) if overlap > nms_thresh: # if polygon_iou(bboxes[_i][0:8], bboxes[_j][0:8]) > nms_thresh: # print(box_i, box_j, bbox_iou(box_i, box_j, x1y1x2y2=False)) out_boxes[_j] = 1 return np.where(out_boxes == 0)[0]
def py_cpu_nms_poly_fast(dets, thresh): # TODO: test it try: obbs = dets[:, 0:-1] except: print('fail index') pdb.set_trace() x1 = np.min(obbs[:, 0::2], axis=1) y1 = np.min(obbs[:, 1::2], axis=1) x2 = np.max(obbs[:, 0::2], axis=1) y2 = np.max(obbs[:, 1::2], axis=1) scores = dets[:, 8] areas = (x2 - x1 + 1) * (y2 - y1 + 1) polys = [] for i in range(len(dets)): tm_polygon = polyiou.VectorDouble([ dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4], dets[i][5], dets[i][6], dets[i][7] ]) polys.append(tm_polygon) order = scores.argsort()[::-1] keep = [] while order.size > 0: ovr = [] i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) w = np.maximum(0.0, xx2 - xx1) h = np.maximum(0.0, yy2 - yy1) hbb_inter = w * h hbb_ovr = hbb_inter / (areas[i] + areas[order[1:]] - hbb_inter) h_inds = np.where(hbb_ovr > 0)[0] tmp_order = order[h_inds + 1] for j in range(tmp_order.size): iou = polyiou.iou_poly(polys[i], polys[tmp_order[j]]) hbb_ovr[h_inds[j]] = iou try: if math.isnan(ovr[0]): pdb.set_trace() except: pass inds = np.where(hbb_ovr <= thresh)[0] order = order[inds + 1] return keep
def nms(dets, thresh): """ greedily select boxes with high confidence and overlap with current maximum <= thresh rule out overlap >= thresh :param dets: [[x1, y1, x2, y2 score]] :param thresh: retain overlap < thresh :return: indexes to keep """ ''' if dets.shape[0] == 0: return [] #x1 = dets[:, 0] #y1 = dets[:, 1] #x2 = dets[:, 2] #y2 = dets[:, 3] ex_x = np.vstack((dets[:, 0], dets[:, 2], dets[:, 4], dets[:, 6])) ex_y = np.vstack((dets[:, 1], dets[:, 3], dets[:, 5], dets[:, 7])) x1 = np.amin(ex_x, axis=0) y1 = np.amin(ex_y, axis=0) x2 = np.amax(ex_x, axis=0) y2 = np.amax(ex_y, axis=0) scores = dets[:, 8] areas = (x2 - x1 + 1) * (y2 - y1 + 1) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[i] + areas[order[1:]] - inter) inds = np.where(ovr <= thresh)[0] order = order[inds + 1] return keep ''' if dets.shape[0] == 0: return [] scores = dets[:, 8] polys = [] areas = [] for i in range(len(dets)): tm_polygon = polyiou.VectorDouble([ dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4], dets[i][5], dets[i][6], dets[i][7] ]) polys.append(tm_polygon) order = scores.argsort()[::-1] keep = [] while order.size > 0: ovr = [] i = order[0] keep.append(i) for j in range(order.size - 1): iou = polyiou.iou_poly(polys[i], polys[order[j + 1]]) ovr.append(iou) ovr = np.array(ovr) #print 'ovr is',ovr inds = np.where(ovr <= thresh)[0] #print 'inds is',inds order = order[inds + 1] #print 'order is',order return keep
def py_cpu_nms_poly_fast(dets, thresh): x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] # obbs = dets[:, 0:-1] # x1 = np.min(obbs[:, 0::2], axis=1) # y1 = np.min(obbs[:, 1::2], axis=1) # x2 = np.max(obbs[:, 0::2], axis=1) # y2 = np.max(obbs[:, 1::2], axis=1) # scores = dets[:, 8] areas = (x2 - x1 + 1) * (y2 - y1 + 1) polys = [] for i in range(len(dets)): # tm_polygon = polyiou.VectorDouble([dets[i][0], dets[i][1], # dets[i][2], dets[i][3], # dets[i][4], dets[i][5], # dets[i][6], dets[i][7]]) tm_polygon = polyiou.VectorDouble([x1[i], y1[i], x2[i], y1[i], x2[i], y2[i], x1[i], y2[i]]) polys.append(tm_polygon) order = scores.argsort()[::-1] keep = [] while order.size > 0: ovr = [] i = order[0] keep.append(i) # if order.size == 0: # break xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) # w = np.maximum(0.0, xx2 - xx1 + 1) # h = np.maximum(0.0, yy2 - yy1 + 1) w = np.maximum(0.0, xx2 - xx1) h = np.maximum(0.0, yy2 - yy1) hbb_inter = w * h hbb_ovr = hbb_inter / (areas[i] + areas[order[1:]] - hbb_inter) # h_keep_inds = np.where(hbb_ovr == 0)[0] h_inds = np.where(hbb_ovr > 0)[0] tmp_order = order[h_inds + 1] for j in range(tmp_order.size): iou = polyiou.iou_poly(polys[i], polys[tmp_order[j]]) hbb_ovr[h_inds[j]] = iou # ovr.append(iou) # ovr_index.append(tmp_order[j]) # ovr = np.array(ovr) # ovr_index = np.array(ovr_index) # print('ovr: ', ovr) # print('thresh: ', thresh) try: if math.isnan(ovr[0]): pdb.set_trace() except: pass inds = np.where(hbb_ovr <= thresh)[0] # order_obb = ovr_index[inds] # print('inds: ', inds) # order_hbb = order[h_keep_inds + 1] order = order[inds + 1] # pdb.set_trace() # order = np.concatenate((order_obb, order_hbb), axis=0).astype(np.int) return keep