예제 #1
0
def eval_loc(cls_logits,
             cls_map,
             img_path,
             label,
             gt_boxes,
             topk=(1, 5),
             threshold=None,
             mode='union',
             iou_th=0.5):
    top_boxes, top_maps, gt_known_box, gt_known_map = get_topk_boxes_hier(
        cls_logits[0],
        cls_map,
        img_path,
        label,
        topk=topk,
        threshold=threshold,
        mode=mode)
    top1_box, top5_boxes = top_boxes

    # update result record
    (locerr_1, locerr_5), top1_wrong_detail = evaluate.locerr(
        (top1_box, top5_boxes),
        label.data.long().numpy(),
        gt_boxes,
        topk=(1, 5),
        iou_th=iou_th)
    locerr_gt_known, _ = evaluate.locerr((gt_known_box, ),
                                         label.data.long().numpy(),
                                         gt_boxes,
                                         topk=(1, ),
                                         iou_th=iou_th)

    return locerr_1, locerr_5, locerr_gt_known[
        0], top_maps, top5_boxes, gt_known_map, top1_wrong_detail
예제 #2
0
def eval_loc_scg(cls_logits,
                 top_cams,
                 gt_known_cams,
                 aff_maps,
                 img_path,
                 label,
                 gt_boxes,
                 topk=(1, 5),
                 threshold=None,
                 mode='union',
                 fg_th=0.1,
                 bg_th=0.01,
                 iou_th=0.5,
                 sc_maps_fo=None):
    top_boxes, top_maps = get_topk_boxes_hier_scg(cls_logits[0],
                                                  top_cams,
                                                  aff_maps,
                                                  img_path,
                                                  topk=topk,
                                                  threshold=threshold,
                                                  mode=mode,
                                                  fg_th=fg_th,
                                                  bg_th=bg_th,
                                                  sc_maps_fo=sc_maps_fo)
    top1_box, top5_boxes = top_boxes

    # update result record
    (locerr_1, locerr_5), top1_wrong_detail = evaluate.locerr(
        (top1_box, top5_boxes),
        label.data.long().numpy(),
        gt_boxes,
        topk=(1, 5),
        iou_th=iou_th)

    gt_known_boxes, gt_known_maps = get_topk_boxes_hier_scg(
        cls_logits[0],
        gt_known_cams,
        aff_maps,
        img_path,
        topk=(1, ),
        threshold=threshold,
        mode=mode,
        gt_labels=label,
        fg_th=fg_th,
        bg_th=bg_th,
        sc_maps_fo=sc_maps_fo)

    # update result record
    locerr_gt_known, _ = evaluate.locerr(gt_known_boxes,
                                         label.data.long().numpy(),
                                         gt_boxes,
                                         topk=(1, ),
                                         iou_th=iou_th)

    return locerr_1, locerr_5, locerr_gt_known[
        0], top_maps, top5_boxes, top1_wrong_detail
예제 #3
0
def val(args):
    with open(args.test_box, 'r') as f:
        gt_boxes = [
            map(float,
                x.strip().split(' ')[2:]) for x in f.readlines()
        ]
    gt_boxes = [(box[0], box[1], box[0] + box[2] - 1, box[1] + box[3] - 1)
                for box in gt_boxes]

    # meters
    top1_clsacc = AverageMeter()
    top1_locerr = AverageMeter()
    top5_clsacc = AverageMeter()
    top5_locerr = AverageMeter()
    top1_clsacc.reset()
    top1_locerr.reset()
    top5_clsacc.reset()
    top5_locerr.reset()

    # get model
    model = get_model(args)
    model.eval()

    # get data
    _, valcls_loader, valloc_loader = data_loader(args, test_path=True)
    assert len(valcls_loader) == len(valloc_loader), \
        'Error! Different size for two dataset: loc({}), cls({})'.format(len(valloc_loader), len(valcls_loader))

    # testing
    DEBUG = True
    if DEBUG:
        # show_idxs = np.arange(20)
        np.random.seed(2333)
        show_idxs = np.arange(len(valcls_loader))
        np.random.shuffle(show_idxs)
        show_idxs = show_idxs[:20]

    # evaluation classification task
    pred_prob1 = []
    pred_prob2 = []
    pred_prob3 = []
    for dat in tqdm(valcls_loader):
        # parse data
        img_path, img, label_in = dat
        if args.tencrop == 'True':
            bs, ncrops, c, h, w = img.size()
            img = img.view(-1, c, h, w)
            label_input = label_in.repeat(10, 1)
            label = label_input.view(-1)
        else:
            label = label_in

        # forward pass
        img, label = img.cuda(), label.cuda()
        img_var, label_var = Variable(img), Variable(label)
        logits = model(img_var)

        # get classification prob
        logits0 = logits[-1]
        logits0 = F.softmax(logits0, dim=1)
        if args.tencrop == 'True':
            logits0 = logits0.view(1, ncrops, -1).mean(1)
        pred_prob3.append(logits0.cpu().data.numpy())

        logits1 = logits[-2]
        logits1 = F.softmax(logits1, dim=1)
        if args.tencrop == 'True':
            logits1 = logits1.view(1, ncrops, -1).mean(1)
        pred_prob2.append(logits1.cpu().data.numpy())

        logits2 = logits[-3]
        logits2 = F.softmax(logits2, dim=1)
        if args.tencrop == 'True':
            logits2 = logits2.view(1, ncrops, -1).mean(1)
        pred_prob1.append(logits2.cpu().data.numpy())
        # update result record
        prec1_1, prec5_1 = evaluate.accuracy(logits0.cpu().data,
                                             label_in.long(),
                                             topk=(1, 5))
        top1_clsacc.update(prec1_1[0].numpy(), img.size()[0])
        top5_clsacc.update(prec5_1[0].numpy(), img.size()[0])

    pred_prob1 = np.concatenate(pred_prob1, axis=0)
    pred_prob2 = np.concatenate(pred_prob2, axis=0)
    pred_prob3 = np.concatenate(pred_prob3, axis=0)
    print('== cls err')
    print('Top1: {:.2f} Top5: {:.2f}\n'.format(100.0 - top1_clsacc.avg,
                                               100.0 - top5_clsacc.avg))

    thresholds = map(float, args.threshold.split(','))
    thresholds = list(thresholds)
    for th in thresholds:
        top1_locerr.reset()
        top5_locerr.reset()
        for idx, dat in tqdm(enumerate(valloc_loader)):
            # parse data
            img_path, img, label = dat

            # forward pass
            img, label = img.cuda(), label.cuda()
            img_var, label_var = Variable(img), Variable(label)
            logits = model(img_var)
            child_map = F.upsample(model.module.get_child_maps(),
                                   size=(28, 28),
                                   mode='bilinear',
                                   align_corners=True)
            child_map = child_map.cpu().data.numpy()
            parent_maps = F.upsample(model.module.get_parent_maps(),
                                     size=(28, 28),
                                     mode='bilinear',
                                     align_corners=True)
            parent_maps = parent_maps.cpu().data.numpy()
            root_maps = model.module.get_root_maps()
            root_maps = root_maps.cpu().data.numpy()
            top_boxes, top_maps = get_topk_boxes_hier(pred_prob3[idx, :],
                                                      pred_prob2[idx, :],
                                                      pred_prob1[idx, :],
                                                      child_map,
                                                      parent_maps,
                                                      root_maps,
                                                      img_path[0],
                                                      args.input_size,
                                                      args.crop_size,
                                                      topk=(1, 5),
                                                      threshold=th,
                                                      mode='union')
            top1_box, top5_boxes = top_boxes

            # update result record
            locerr_1, locerr_5 = evaluate.locerr(
                (top1_box, top5_boxes),
                label.cpu().data.long().numpy(),
                gt_boxes[idx],
                topk=(1, 5))
            top1_locerr.update(locerr_1, img.size()[0])
            top5_locerr.update(locerr_5, img.size()[0])
            if DEBUG:
                if idx in show_idxs:
                    save_im_heatmap_box(
                        img_path[0],
                        top_maps,
                        top5_boxes,
                        '../figs/',
                        gt_label=label.cpu().data.long().numpy(),
                        gt_box=gt_boxes[idx])
        print('=========== threshold: {} ==========='.format(th))
        print('== loc err')
        print('Top1: {:.2f} Top5: {:.2f}\n'.format(top1_locerr.avg,
                                                   top5_locerr.avg))
예제 #4
0
def val(args):

    with open(args.test_box, 'r') as f:
        gt_boxes = [
            map(float,
                x.strip().split(' ')[2:]) for x in f.readlines()
        ]
    gt_boxes = [(box[0], box[1], box[0] + box[2] - 1, box[1] + box[3] - 1)
                for box in gt_boxes]

    # meters
    top1_clsacc = AverageMeter()
    top1_locerr = AverageMeter()
    top5_clsacc = AverageMeter()
    top5_locerr = AverageMeter()
    top1_clsacc.reset()
    top1_locerr.reset()
    top5_clsacc.reset()
    top5_locerr.reset()

    # get model
    model = get_model(args)
    model.eval()

    # get data
    _, valcls_loader, valloc_loader = data_loader(args, test_path=True)
    assert len(valcls_loader) == len(valloc_loader), \
        'Error! Different size for two dataset: loc({}), cls({})'.format(len(valloc_loader), len(valcls_loader))

    # testing
    VISLOC = True
    if VISLOC:
        # show_idxs = np.arange(20)
        np.random.seed(2333)
        show_idxs = np.arange(len(valcls_loader))
        np.random.shuffle(show_idxs)
        show_idxs = show_idxs[:20]

    # evaluation classification task
    pred_prob = []
    for dat in tqdm(valcls_loader):
        # parse data
        img_path, img, label_in = dat
        if args.tencrop == 'True':
            bs, ncrops, c, h, w = img.size()
            img = img.view(-1, c, h, w)
            label_input = label_in.repeat(10, 1)
            label = label_input.view(-1)
        else:
            label = label_in

        # forward pass
        img, label = img.cuda(), label.cuda()
        img_var, label_var = Variable(img), Variable(label)
        logits = model(img_var)

        # get classification prob
        logits0 = logits
        logits0 = F.softmax(logits0, dim=1)
        if args.tencrop == 'True':
            logits0 = logits0.view(1, ncrops, -1).mean(1)
        pred_prob.append(logits0.cpu().data.numpy())

        # update result record
        prec1_1, prec5_1 = evaluate.accuracy(logits0.cpu().data,
                                             label_in.long(),
                                             topk=(1, 5))
        top1_clsacc.update(prec1_1[0].numpy(), img.size()[0])
        top5_clsacc.update(prec5_1[0].numpy(), img.size()[0])

    pred_prob = np.concatenate(pred_prob, axis=0)
    # with open('pred_prob.pkl', 'w') as f:
    #     cPickle.dump(pred_prob, f)
    print('== cls err')
    print('Top1: {:.2f} Top5: {:.2f}\n'.format(100.0 - top1_clsacc.avg,
                                               100.0 - top5_clsacc.avg))

    # with open('pred_prob.pkl', 'r') as f:
    #     pred_prob = cPickle.load(f)
    # evaluation localization task
    thresholds = map(float, args.threshold.split(','))
    thresholds = list(thresholds)
    for th in thresholds:
        top1_locerr.reset()
        top5_locerr.reset()
        for idx, dat in tqdm(enumerate(valloc_loader)):
            # parse data
            img_path, img, label = dat

            # forward pass
            img, label = img.cuda(), label.cuda()
            img_var, label_var = Variable(img), Variable(label)
            logits = model(img_var)

            # get localization boxes
            cam_map = model.module.get_cam_maps()  # not normalized
            cam_map = cam_map.cpu().data.numpy()
            top_boxes, top_maps = get_topk_boxes(pred_prob[idx, :],
                                                 cam_map,
                                                 img_path[0],
                                                 args.input_size,
                                                 args.crop_size,
                                                 topk=(1, 5),
                                                 threshold=th,
                                                 mode='union')
            top1_box, top5_boxes = top_boxes

            # update result record
            locerr_1, locerr_5 = evaluate.locerr(
                (top1_box, top5_boxes),
                label.cpu().data.long().numpy(),
                gt_boxes[idx],
                topk=(1, 5))
            top1_locerr.update(locerr_1, img.size()[0])
            top5_locerr.update(locerr_5, img.size()[0])
            if VISLOC:
                if idx in show_idxs:
                    save_im_heatmap_box(
                        img_path[0],
                        top_maps,
                        top5_boxes,
                        '../figs/',
                        gt_label=label.cpu().data.long().numpy(),
                        gt_box=gt_boxes[idx])
        print('=========== threshold: {} ==========='.format(th))
        print('== loc err')
        print('Top1: {:.2f} Top5: {:.2f}\n'.format(top1_locerr.avg,
                                                   top5_locerr.avg))