Esempio n. 1
0
def extract_query(net,
                  dataset,
                  q_idx,
                  scale=800,
                  crop=True,
                  flip=None,
                  rotate=None):
    # Load query image
    img = Image.open(dataset.get_query_filename(q_idx))
    # Crop the query ROI
    if crop:
        img = img.crop(tuple(dataset.get_query_roi(q_idx)))
    # Apply transformations
    img = trf.resize_image(img, scale)
    # Flip
    if flip: img = trf.flip_image(img, flip)
    # Rotation
    if rotate: img = trf.rotate(img, rotate)
    # Convert to Pytorch's tensor and normalize
    I = trf.to_tensor(img)
    I = trf.normalize(
        I, dict(rgb_means=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]))
    I = I.unsqueeze(0)  #.to(device)

    # Forward pass to extract the features
    with torch.no_grad():
        print('Extracting features using input={} pixels...'.format(scale))
        q_feat = net(I).cpu().numpy()

    return q_feat, img
Esempio n. 2
0
def demo(args):
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    # load the Oxford5k database
    dataset = create('Oxford')
    print(dataset)

    # initialize architecture and load weights
    print('Loading the model...')
    model = resnet50_rank_DA().to(device)
    model.eval()
    print('Done\n')

    # load the precomputed dataset features
    d_feats_file = 'data/features/resnet50-rnk-lm-da_ox.npy'
    try:
        d_feats = np.load(d_feats_file)
    except OSError as e:
        print(
            'ERROR: File {} not found. Please follow the instructions to download the pre-computed features.'
            .format(d_feats_file))
        sys.exit()

    # Load the query image
    img = Image.open(dataset.get_query_filename(args.qidx))
    # Crop the query ROI
    img = img.crop(tuple(dataset.get_query_roi(args.qidx)))
    # Apply transformations
    img = trf.resize_image(img, 800)
    I = trf.to_tensor(img)
    I = trf.normalize(
        I, dict(rgb_means=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]))
    I = I.unsqueeze(0).to(device)
    # Forward pass to extract the features
    with torch.no_grad():
        print('Extracting the representation of the query...')
        q_feat = model(I).cpu().numpy()
    print('Done\n')

    # Rank the database and visualize the top-k most similar images in the database
    dataset.vis_top(d_feats,
                    args.qidx,
                    q_feat=q_feat,
                    topk=args.topk,
                    out_image_file='results/out.png')
Esempio n. 3
0
def eval_batch():
    eval_len = len(SET_DICT)
    accuracy = np.zeros(len(args.top_k))
    start_time = datetime.datetime.now()

    for i in xrange(eval_len - args.skip_num):
        im = cv2.imread(SET_DICT[i + args.skip_num]['path'])
        im = T.bgr2rgb(im)
        scale_im = T.pil_resize(Image.fromarray(im), args.base_size)
        normalized_im = T.normalize(np.asarray(scale_im) / 255.0,
                                    mean=PIXEL_MEANS,
                                    std=PIXEL_STDS)
        crop_ims = []
        if args.crop_type == 'center':  # for single crop
            crop_ims.append(
                T.center_crop(normalized_im, crop_size=args.crop_size))
        elif args.crop_type == 'multi':  # for 10 crops
            crop_ims.extend(
                T.mirror_crop(normalized_im, crop_size=args.crop_size))
        else:
            crop_ims.append(normalized_im)

        score_vec = np.zeros(args.class_num, dtype=np.float32)
        iter_num = int(len(crop_ims) / args.batch_size)
        timer_pt1 = datetime.datetime.now()
        for j in xrange(iter_num):
            input_data = np.asarray(
                crop_ims, dtype=np.float32)[j * args.batch_size:(j + 1) *
                                            args.batch_size]
            input_data = input_data.transpose(0, 3, 1, 2)
            input_data = torch.autograd.Variable(
                torch.from_numpy(input_data).cuda(), volatile=True)
            outputs = MODEL(input_data)
            scores = outputs.data.cpu().numpy()
            score_vec += np.sum(scores, axis=0)
        score_index = (-score_vec / len(crop_ims)).argsort()
        timer_pt2 = datetime.datetime.now()

        SET_DICT[i + args.skip_num]['evaluated'] = True
        SET_DICT[i + args.skip_num]['score_vec'] = score_vec / len(crop_ims)

        print 'Testing image: {}/{} {} {}/{} {}s' \
            .format(str(i + 1), str(eval_len - args.skip_num), str(SET_DICT[i + args.skip_num]['path'].split('/')[-1]),
                    str(score_index[0]), str(SET_DICT[i + args.skip_num]['gt']),
                    str((timer_pt2 - timer_pt1).microseconds / 1e6 + (timer_pt2 - timer_pt1).seconds)),

        for j in xrange(len(args.top_k)):
            if SET_DICT[i +
                        args.skip_num]['gt'] in score_index[:args.top_k[j]]:
                accuracy[j] += 1
            tmp_acc = float(accuracy[j]) / float(i + 1)
            if args.top_k[j] == 1:
                print '\ttop_' + str(args.top_k[j]) + ':' + str(tmp_acc),
            else:
                print 'top_' + str(args.top_k[j]) + ':' + str(tmp_acc)
    end_time = datetime.datetime.now()

    w = open(LOG_PTH, 'w')
    s1 = 'Evaluation process ends at: {}. \nTime cost is: {}. '.format(
        str(end_time), str(end_time - start_time))
    s2 = '\nThe model is: {}. \nThe val file is: {}. \n{} images has been tested, crop_type is: {}, base_size is: {}, ' \
         'crop_size is: {}.'.format(args.model_weights, args.val_file, str(eval_len - args.skip_num),
                                    args.crop_type, str(args.base_size), str(args.crop_size))
    s3 = '\nThe PIXEL_MEANS is: ({}, {}, {}), PIXEL_STDS is : ({}, {}, {}).' \
        .format(str(PIXEL_MEANS[0]), str(PIXEL_MEANS[1]), str(PIXEL_MEANS[2]), str(PIXEL_STDS[0]), str(PIXEL_STDS[1]),
                str(PIXEL_STDS[2]))
    s4 = ''
    for i in xrange(len(args.top_k)):
        _acc = float(accuracy[i]) / float(eval_len - args.skip_num)
        s4 += '\nAccuracy of top_{} is: {}; correct num is {}.'.format(
            str(args.top_k[i]), str(_acc), str(int(accuracy[i])))
    print s1, s2, s3, s4
    w.write(s1 + s2 + s3 + s4)
    w.close()

    if args.save_score_vec:
        w = open(LOG_PTH.replace('.txt', 'scorevec.txt'), 'w')
        for i in xrange(eval_len - args.skip_num):
            w.write(SET_DICT[i + args.skip_num]['score_vec'])
        w.close()
    print('DONE!')
Esempio n. 4
0
try:
    d_feats = np.load(d_feats_file)
except OSError as e:
    print(
        'ERROR: File {} not found. Please follow the instructions to download the pre-computed features.'
        .format(d_feats_file))
    sys.exit()

# Load the query image
img = Image.open(dataset.get_query_filename(args.qidx))
# Crop the query ROI
img = img.crop(tuple(dataset.get_query_roi(args.qidx)))
# Apply transformations
img = trf.resize_image(img, 800)
I = trf.to_tensor(img)
I = trf.normalize(
    I, dict(rgb_means=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]))
I = I.unsqueeze(0).to(device)
# Forward pass to extract the features
with torch.no_grad():
    print('Extracting the representation of the query...')
    q_feat = model(I).numpy()
print('Done\n')

# Rank the database and visualize the top-k most similar images in the database
dataset.vis_top(d_feats,
                args.qidx,
                q_feat=q_feat,
                topk=args.topk,
                out_image_file='out.png')