def test(): for i in range(1000): a = torch.rand(4, 1000, device='cuda:0') b = torch.rand(4, 1000, device='cuda:0') ele = utils.elem_iou(a, b) tab = utils.calc_iou(a, b) if (ele != tab.diag()).any(): print('WARNING, found diff') print(ele.tolist()) print(tab.tolist())
def test_iou_performance(): # "for VOC 5000 images, we only need to do calc_iou 5000 * 16 * 2" anchor_creator = region.AnchorCreator(device=torch.device('cuda:0')) anchors = anchor_creator((600, 800), (37, 50)).view(4, -1) bbox = anchors[:, :5] print(anchors.shape) print(bbox.shape) start = time.time() for i in range(80000*2): iou_tab = utils.calc_iou(anchors, bbox) print(time.time() - start)
def test_anchor_target_creator(): bbox = torch.tensor([[115, 131, 613, 513], [230, 53, 460, 506], [643, 133, 697, 272], [706, 158, 800, 257]], device=torch.device('cuda:0')) bbox = bbox.t() img_size = (600, 800) feat_size = (37, 50) anchor_creator = region.AnchorCreator(device=torch.device('cuda:0')) anchors = anchor_creator(img_size, feat_size).view(4, -1) in_index = region.find_inside_index(anchors, img_size) in_anchors = anchors[:, in_index] anchor_tar_creator = region.AnchorTargetCreator() print('in_anchors.shape', in_anchors.shape) labels, param, bbox_labels = anchor_tar_creator(img_size, feat_size, in_anchors, bbox) print('1 labels:', (labels == 1).sum()) print('0 labels:', (labels == 0).sum()) print('-1 labels:', (labels == -1).sum()) pos_index = utils.index_of(labels == 1) pos_anchors = in_anchors[:, pos_index[0]] pos_bbox_labels = bbox_labels[:, pos_index[0]] print('pos_anchors:', pos_anchors, pos_anchors.shape) print('pos_bbox_labels:', pos_bbox_labels, pos_bbox_labels.shape) ious = utils.calc_iou(pos_anchors, pos_bbox_labels) print('check ious:', ious.tolist()) print('check ious diag:', ious.diag()) print('Next test performance') img_size2 = (605, 805) start = time.time() for i in range(5000): if i % 2 == 0: labels, param, bbox_labels = anchor_tar_creator( img_size, feat_size, in_anchors, bbox) else: labels, param, bbox_labels = anchor_tar_creator( img_size2, feat_size, in_anchors, bbox) used = time.time() - start print('used {} secs'.format(used))
def test_iou(): bbox = torch.tensor([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], dtype=torch.float32) a = torch.tensor([[1],[4],[7],[10]], dtype=torch.float32) b = torch.tensor([[2],[5],[8],[11]], dtype=torch.float32) print(utils.calc_iou(bbox, bbox))
) X, Y, img_data, debug_img, debug_num_pos = next(data_gen_train) loss_rpn = model_rpn.train_on_batch(X, Y) P_rpn = model_rpn.predict_on_batch(X) R = utils.rpn_to_roi(P_rpn[0], P_rpn[1], C, K.image_dim_ordering(), use_regr=True, overlap_thresh=0.7, max_boxes=300) # note: calc_iou converts from (x1,y1,x2,y2) to (x,y,w,h) format X2, Y1, Y2, IouS = utils.calc_iou(R, img_data, C, class_mapping) if X2 is None: rpn_accuracy_rpn_monitor.append(0) rpn_accuracy_for_epoch.append(0) continue neg_samples = np.where(Y1[0, :, -1] == 1) pos_samples = np.where(Y1[0, :, -1] == 0) if len(neg_samples) > 0: neg_samples = neg_samples[0] else: neg_samples = [] if len(pos_samples) > 0: