예제 #1
0
파일: corloc.py 프로젝트: djkim3/UBR
def discovery_object(img, num_prop, edge_iou_th=0.5, nms_iou=0.5, num_refine=1):
    rand_boxes = generate_anchor_boxes(img.shape[0], img.shape[1])

    refined_boxes = ubr.query(img, rand_boxes[:, :4], num_refine)
    iou = jaccard(refined_boxes, refined_boxes)
    degree = (iou.gt(edge_iou_th).float()).sum(1)
    #degree = degree / torch.log(rand_boxes[:, 4])
    #degree = iou.gt(edge_iou_th).float().sum(1)
    #degree = torch.exp(iou).sum(1)
    #degree = iou.sum(1)
    #degree = iou.gt(0.6).float().sum(1) / rand_boxes[:, 4]
    degree = degree * (1 / degree.max())
    ret = []
    while True:
        val, here = degree.max(0)
        if val[0] < 0 or len(ret) == num_prop:
            break
        here = here[0]
        dead_mask = (iou[here, :] > nms_iou)
        degree[dead_mask] = degree[dead_mask] * 0.5
        degree[here] = -1
        #mean_box = torch.mean(refined_boxes[torch.nonzero(dead_mask).squeeze()], 0)

        #ret.append(mean_box.cpu().numpy())
        ret.append(refined_boxes[here, :].cpu().numpy())

    ret = np.array(ret)
    return ret
예제 #2
0
def sample_pos_prop(proposals, gt_boxes, iou_th):
    iou = jaccard(proposals, gt_boxes)
    max_overlap, _ = iou.max(dim=1)
    if max_overlap.gt(iou_th).sum() == 0:
        return None
    keep = torch.nonzero(max_overlap.gt(iou_th)).view(-1)
    proposals = proposals[keep]
    if proposals.size(0) > 150:
        proposals = proposals[torch.LongTensor(
            np.random.choice(proposals.size(0), 150, replace=False))]
    return proposals
예제 #3
0
    def __init__(self,
                 iou_begin=10,
                 iou_end=100,
                 seed_pool_size_per_bag=10000):
        print('Initialize UniformIouBoxGenerator...')
        self._seed_pool_size_per_bag = seed_pool_size_per_bag
        self._delta = torch.zeros((100, seed_pool_size_per_bag, 4))
        self.iou_begin = iou_begin
        self.iou_end = iou_end

        for idx in range(iou_begin, iou_end):
            cnt = 0
            iou_th = idx / 100
            while cnt < seed_pool_size_per_bag:
                pos_th = min((1 / (2 * iou_th)) - 0.5, 0.5)
                scale_th = max(0.7, iou_th)
                dx = torch.FloatTensor(
                    np.random.uniform(-pos_th, pos_th,
                                      seed_pool_size_per_bag * 100))
                dy = torch.FloatTensor(
                    np.random.uniform(-pos_th, pos_th,
                                      seed_pool_size_per_bag * 100))
                dw = torch.FloatTensor(
                    np.random.uniform(np.log(iou_th), -np.log(scale_th),
                                      seed_pool_size_per_bag * 100))
                dh = torch.FloatTensor(
                    np.random.uniform(np.log(iou_th), -np.log(scale_th),
                                      seed_pool_size_per_bag * 100))
                boxes = torch.stack([dx, dy, torch.exp(dw), torch.exp(dh)], 1)
                iou = jaccard(torch.FloatTensor([[-0.5, -0.5, 0.5, 0.5]]),
                              to_point_form(boxes)).squeeze()
                mask = iou.gt(iou_th) * iou.le(iou_th + 0.01)
                dx = dx[mask]
                dy = dy[mask]
                dw = dw[mask]
                dh = dh[mask]

                new_cnt = min(seed_pool_size_per_bag - cnt, mask.sum())
                self._delta[idx, cnt:cnt + new_cnt, 0] = dx[:new_cnt]
                self._delta[idx, cnt:cnt + new_cnt, 1] = dy[:new_cnt]
                self._delta[idx, cnt:cnt + new_cnt, 2] = dw[:new_cnt]
                self._delta[idx, cnt:cnt + new_cnt, 3] = dh[:new_cnt]
                cnt += new_cnt

        print('Complete')
예제 #4
0
    def __init__(self,
                 iou_th,
                 seed_pool_size=100000,
                 pos_th=0.35,
                 scale_min=0.5,
                 scale_max=1.5):
        self._seed_pool_size = seed_pool_size
        self._delta = torch.zeros((seed_pool_size, 4))
        self._iou_th = iou_th

        cnt = 0
        while cnt < seed_pool_size:
            scale_th_min = np.log(scale_min)
            scale_th_max = np.log(scale_max)
            dx = torch.FloatTensor(
                np.random.uniform(-pos_th, pos_th, seed_pool_size * 10))
            dy = torch.FloatTensor(
                np.random.uniform(-pos_th, pos_th, seed_pool_size * 10))
            dw = torch.FloatTensor(
                np.exp(
                    np.random.uniform(scale_th_min, scale_th_max,
                                      seed_pool_size * 10)))
            dh = torch.FloatTensor(
                np.exp(
                    np.random.uniform(scale_th_min, scale_th_max,
                                      seed_pool_size * 10)))

            boxes = torch.stack([dx, dy, dw, dh], 1)
            iou = jaccard(torch.FloatTensor([[-0.5, -0.5, 0.5, 0.5]]),
                          to_point_form(boxes)).squeeze()
            mask = iou.ge(iou_th)
            dx = dx[mask]
            dy = dy[mask]
            dw = dw[mask]
            dh = dh[mask]

            new_cnt = min(seed_pool_size - cnt, mask.sum())
            self._delta[cnt:cnt + new_cnt, 0] = dx[:new_cnt]
            self._delta[cnt:cnt + new_cnt, 1] = dy[:new_cnt]
            self._delta[cnt:cnt + new_cnt, 2] = dw[:new_cnt]
            self._delta[cnt:cnt + new_cnt, 3] = dh[:new_cnt]
            cnt += new_cnt

        print('init NaturalBoxGenerator')
예제 #5
0
    def __init__(self, iou_th, seed_pool_size=100000):
        self._seed_pool_size = seed_pool_size
        self._delta = torch.zeros((seed_pool_size, 4))
        self._iou_th = iou_th

        cnt = 0
        while cnt < seed_pool_size:
            pos_th = min((1 / (2 * iou_th)) - 0.5, 0.5)
            scale_th = max(0.7, iou_th)
            dx = torch.FloatTensor(
                np.random.uniform(-pos_th, pos_th, seed_pool_size * 10))
            dy = torch.FloatTensor(
                np.random.uniform(-pos_th, pos_th, seed_pool_size * 10))
            dw = torch.FloatTensor(
                np.random.uniform(np.log(iou_th), -np.log(scale_th),
                                  seed_pool_size * 10))
            dh = torch.FloatTensor(
                np.random.uniform(np.log(iou_th), -np.log(scale_th),
                                  seed_pool_size * 10))
            boxes = torch.stack([dx, dy, torch.exp(dw), torch.exp(dh)], 1)
            iou = jaccard(torch.FloatTensor([[-0.5, -0.5, 0.5, 0.5]]),
                          to_point_form(boxes)).squeeze()
            mask = iou.ge(iou_th)
            dx = dx[mask]
            dy = dy[mask]
            dw = dw[mask]
            dh = dh[mask]

            new_cnt = min(seed_pool_size - cnt, mask.sum())
            self._delta[cnt:cnt + new_cnt, 0] = dx[:new_cnt]
            self._delta[cnt:cnt + new_cnt, 1] = dy[:new_cnt]
            self._delta[cnt:cnt + new_cnt, 2] = dw[:new_cnt]
            self._delta[cnt:cnt + new_cnt, 3] = dh[:new_cnt]
            cnt += new_cnt

        print('init UniformBoxGenerator')
예제 #6
0
파일: corloc.py 프로젝트: djkim3/UBR
#     print(cor / len(dataset))
#     avg = avg + cor / len(dataset)
# print(avg / 20)


avg = 0
dataset = VOCDiscovery('./data/VOCdevkit2007', [('2007', 'trainval')], None)
cor = np.zeros(20)
tot = np.zeros(20)
for i in range(len(dataset)):
    id, gt = dataset.pull_anno(i)
    labels = gt[:, 4].astype(np.int)

    proposals = loadmat(result_path % id)['proposals'] - 1
    top1 = proposals[0:1]
    iou = jaccard(torch.FloatTensor(top1), torch.FloatTensor(gt[:, :4]))
    max_iou, max_idx = iou.max(1)
    max_iou = max_iou[0]
    max_idx = max_idx[0]
    if max_iou > 0.5:
        cls = labels[max_idx]
        cor[cls] = cor[cls] + 1
    for cls in range(20):
        if cls in labels:
            tot[cls] = tot[cls] + 1

for a in cor:
    print('%.3f' % a, end=' ')
print('')

for a in tot:
예제 #7
0
        if cnt == 0:
            print('@@@@@ no box @@@@@', data_idx)
            output_file.write('@@@@@ no box @@@@@ %d\n' % data_idx)
            continue
        rois = rois[:cnt, :]
        rois = rois.cuda()
        bbox_pred, _ = UBR(im_data, Variable(rois))
        bbox_pred = bbox_pred.data
        refined_boxes = inverse_transform(rois[:, 1:], bbox_pred)
        refined_boxes[:, 0].clamp_(min=0, max=data_width - 1)
        refined_boxes[:, 1].clamp_(min=0, max=data_height - 1)
        refined_boxes[:, 2].clamp_(min=0, max=data_width - 1)
        refined_boxes[:, 3].clamp_(min=0, max=data_height - 1)

        gt_boxes = gt_boxes.cuda()
        base_iou = jaccard(rois[:, 1:], gt_boxes)
        refined_iou = jaccard(refined_boxes, gt_boxes)
        base_max_overlap, base_max_overlap_idx = base_iou.max(1)
        refined_max_overlap, refined_max_overlap_idx = refined_iou.max(1)
        # plt.imshow(raw_img)
        # draw_box(rois[:10, 1:] / im_scale)
        # draw_box(refined_boxes[:10, :] / im_scale, 'yellow')
        # draw_box(gt_boxes / im_scale, 'black')
        # plt.show()
        for from_th in range(10):
            mask1 = base_max_overlap.gt(from_th * 0.1) * base_max_overlap.le(from_th * 0.1 + 0.1)
            after_iou = refined_iou[range(refined_iou.size(0)), base_max_overlap_idx]
            for to_th in range(10):
                mask2 = after_iou.gt(to_th * 0.1) * after_iou.le(to_th * 0.1 + 0.1)
                mask3 = refined_max_overlap.gt(to_th * 0.1) * refined_max_overlap.le(to_th * 0.1 + 0.1)
                fixed_target_result[from_th, to_th] += (mask1 * mask2).sum()
예제 #8
0
    img, gt, h, w, id = dataset[perm[i]]
    result = discovery_object(img, args.K, args.edge_iou, args.nms_iou,
                              args.num_refine)

    #np.save('/home/seungkwan/repo/proposals/VOC07_trainval_ubr64523_%d_%.1f_%.1f_%d/%s' % (args.K, args.edge_iou, args.nms_iou, args.num_refine, id[1]), result)
    #savemat('/home/seungkwan/repo/proposals/VOC07_%s_1/%s' % (args.dataset, id[1]), {'proposals': result + 1}) # this is because matlab use one-based index

    gt = gt[:, :4]
    gt[:, 0] *= w
    gt[:, 2] *= w
    gt[:, 1] *= h
    gt[:, 3] *= h

    gt = torch.FloatTensor(gt)
    result = torch.FloatTensor(result)
    iou = jaccard(result, gt)
    P = P + gt.size(0)
    tp = tp + iou.max(0)[0].gt(0.5).sum().item()

    if i % 10 == 9:
        print(i, time.time() - st, tp / P)
        st = time.time()

    # plt.imshow(img)
    # result[:, 0].clip(5, w - 6)
    # result[:, 2].clip(5, w - 6)
    # result[:, 1].clip(5, h - 6)
    # result[:, 3].clip(5, h - 6)
    #
    # draw_box(result)
    # draw_box(result[0:1, :], 'black')