def test_proposal_target(): bbox = torch.tensor( [[115, 131, 613, 513], [230, 53, 460, 506], [643, 133, 697, 272], [706, 158, 800, 257], [300, 400, 500, 600]], device=device) bbox = bbox.t() img_size = (600, 800) feat_size = (37, 50) anchor_creator = region.AnchorCreator(device=torch.device('cuda:1')) anchors = anchor_creator(img_size, feat_size).view(4, -1) in_index = region.find_inside_index(anchors, img_size) rpn_cls_out = torch.rand(1, 18, 37, 50, device=device) rpn_reg_out = torch.rand(1, 36, 37, 50, device=device) props_creator = region.ProposalCreator(6000, 2000, 0.7, 16) rois, scores = props_creator(rpn_cls_out, rpn_reg_out, anchors, img_size) print('number of rois:', rois.shape) print('scores', scores.shape) gt_label = torch.tensor([2, 2, 5, 6, 8], device=device) props_tar_creator = region.ProposalTargetCreator() roi_bbox, roi_label, roi_param = props_tar_creator(rois, bbox, gt_label) print('roi_bbox.shape', roi_bbox.shape) print('roi_label.shape', roi_label.shape) print('roi_param.shape', roi_param.shape) print('roi_label==1:', (roi_label == 1).sum()) print('roi_label==0:', (roi_label == 0).sum()) start = time.time() for i in range(5000): roi_bbox, roi_label, roi_param = props_tar_creator( rois, bbox, gt_label) end = time.time() print('time used: {} secs'.format(end - start))
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(): img_size = (600,800) grid=(37,50) print('Test AnchorCreator'.center(50, '-')) anchor_creator = region.AnchorCreator(device=torch.device('cuda:0')) print('start to create anchors using new method:') anchors = anchor_creator(img_size=(600, 800), grid=(37, 50)) print(anchors.shape) print('first 10 anchors') print(anchors.view(4,9,-1)[:,:,0]) ws = anchors[2] - anchors[0] hs = anchors[3] - anchors[1] old_gen = region.AnchorGenerator(scales=[128, 256, 512], aspect_ratios=[0.5, 1.0, 2.0], allow_cross=True) print('start to create anchors using old method') old_anchors = list(old_gen.anchors(img_size=img_size, grid=grid)) print('number of old anchors:', len(old_anchors)) print('anchor 10:\n', old_anchors[0]) for i in range(10): print('old anchor', i) print_old(old_anchors[i]) print('Next test performance') idx = 0 start = time.time() for h in range(500, 700): for w in range(800, 1000): grid = (int(h/16), int(w/16)) idx += 1 anchors = anchor_creator((h,w), grid) secs = time.time() - start print('create {} set of anchors, used {} seconds or {} mins'\ .format(idx, secs, secs/60)) print('Next test performance(old)') idx = 0 start = time.time() for h in range(500, 700): for w in range(800, 1000): grid = (int(h/16), int(w/16)) idx += 1 anchors = old_gen.anchors((h, w), grid) anchors = list(anchors) secs = time.time() - start print('create {} set of anchors, used {} seconds or {} mins'\ .format(idx, secs, secs/60))
def test_find_inside_index(): img_size = (600, 800) grid = (37, 50) anchor_creator = region.AnchorCreator(device=torch.device('cuda:0')) anchors = anchor_creator(img_size=(600, 800), grid=(37, 50)) anchors = anchors.view(4, -1) inside_idx = region.find_inside_index(anchors, img_size) labels = torch.full((16650, ), -1) print('inside_idx type:', type(inside_idx)) print('inside_idx:\n', inside_idx) labels[inside_idx] = 1 labels[8000:8050] = 0 print('1 labels:', (labels == 1).sum()) print('test random sample') region.random_sample_label(labels, 128, 256) print('1 labels:', (labels == 1).sum()) print('0 labels:', (labels == 0).sum()) print('-1 labels:', (labels == -1).sum())
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))
import sys, os, time import os.path as osp cur_dir = osp.dirname(osp.realpath(__file__)) sys.path.append(osp.join(cur_dir, '..')) from lib import data from lib import config from lib import utils from lib import region import torch img_size = (600, 800) grid = (37, 50) anchor_creator = region.AnchorCreator(device=torch.device('cuda:0')) anchors = anchor_creator(img_size=(600, 800), grid=(37, 50)) print(anchors.shape) anchors = anchors.view(4, -1) device = torch.device('cuda:0') def test_proposal_creator_data(): rpn_cls_out = torch.rand(1, 18, 37, 50, device=device) rpn_reg_out = torch.rand(1, 36, 37, 50, device=device) props_creator = region.ProposalCreator(6000, 50, 0.7, 16) rois, scores = props_creator(rpn_cls_out, rpn_reg_out, anchors, img_size) print(rois.shape) print(rois) print(scores)