def run_check_nms(): #test nms: H, W = 480, 640 num_objects = 4 rois = [] for n in range(num_objects): w = np.random.randint(64, 256) h = np.random.randint(64, 256) x0 = np.random.randint(0, W - w) y0 = np.random.randint(0, H - h) x1 = x0 + w y1 = y0 + h gt = [x0, y0, x1, y1] M = np.random.randint(10, 20) for m in range(M): dw = int(np.random.uniform(0.5, 2) * w) dh = int(np.random.uniform(0.5, 2) * h) dx = int(np.random.uniform(-1, 1) * w * 0.5) dy = int(np.random.uniform(-1, 1) * h * 0.5) xx0 = x0 - dw // 2 + dx yy0 = y0 - dh // 2 + dy xx1 = xx0 + w + dw yy1 = yy0 + h + dh score = np.random.uniform(0.5, 2) rois.append([xx0, yy0, xx1, yy1, score]) pass rois = np.array(rois).astype(np.float32) if 1: keep = gpu_nms(rois, 0.5) print('gpu_nms :', keep) keep = cython_nms(rois, 0.5) print('cython_nms :', keep) #gpu [52, 39, 48, 43, 47, 21, 9, 6, 32, 8, 16, 36, 28, 29, 53, 41] #py [52, 39, 48, 43, 47, 21, 9, 6, 32, 8, 16, 36, 28, 29, 53, 41] #cython [52, 39, 48, 43, 47, 21, 9, 6, 32, 8, 16, 36, 28, 29, 53, 41] if 1: rois = torch.from_numpy(rois).cuda() keep = torch_nms(rois, 0.5) keep = keep.cpu().numpy() print('torch_nms :', keep)
def rpn_nms(cfg, mode, inputs, window, logits_flat, deltas_flat): if mode in [ 'train', ]: nms_pre_score_threshold = cfg.rpn_train_nms_pre_score_threshold nms_overlap_threshold = cfg.rpn_train_nms_overlap_threshold nms_min_size = cfg.rpn_train_nms_min_size elif mode in [ 'eval', 'valid', 'test', ]: nms_pre_score_threshold = cfg.rpn_test_nms_pre_score_threshold nms_overlap_threshold = cfg.rpn_test_nms_overlap_threshold nms_min_size = cfg.rpn_test_nms_min_size if mode in ['eval']: nms_pre_score_threshold = 0.05 # set low numbe r to make roc curve. else: raise ValueError('rpn_nms(): invalid mode = %s?' % mode) logits = logits_flat.data.cpu().numpy() deltas = deltas_flat.data.cpu().numpy() batch_size, _, height, width = inputs.size() num_classes = cfg.num_classes proposals = [] for b in range(batch_size): proposal = [ np.empty((0, 7), np.float32), ] ps = np_softmax(logits[b]) ds = deltas[b] for c in range(1, num_classes): #skip background #num_classes index = np.where(ps[:, c] > nms_pre_score_threshold)[0] if len(index) > 0: p = ps[index, c].reshape(-1, 1) d = ds[index, c] w = window[index] box = rpn_decode(w, d) box = clip_boxes(box, width, height) keep = filter_boxes(box, min_size=nms_min_size) if len(keep) > 0: box = box[keep] p = p[keep] keep = gpu_nms(np.hstack((box, p)), nms_overlap_threshold) prop = np.zeros((len(keep), 7), np.float32) prop[:, 0] = b prop[:, 1:5] = np.around(box[keep], 0) prop[:, 5] = p[keep, 0] prop[:, 6] = c proposal.append(prop) proposal = np.vstack(proposal) proposals.append(proposal) proposals = Variable(torch.from_numpy(np.vstack(proposals))).cuda() return proposals