Ejemplo n.º 1
0
def main():
    cfg = Config()

    TVT, TMO = set_devices(cfg.sys_device_ids)

    #########
    # Model #
    #########

    model = Model(net=cfg.net, pretrained=False)

    #####################
    # Load Model Weight #
    #####################

    used_file = cfg.model_weight_file or cfg.ckpt_file
    loaded = torch.load(used_file, map_location=(lambda storage, loc: storage))
    if cfg.model_weight_file == '':
        loaded = loaded['state_dicts'][0]
    load_state_dict(model, loaded)
    print('Loaded model weights from {}'.format(used_file))

    model = model.base
    # Set eval mode. Force all BN layers to use global mean and variance, also disable dropout.
    model.eval()
    # Transfer Model to Specified Device.
    TMO([model])

    ###################
    # Extract Feature #
    ###################

    im_dir = osp.expanduser(cfg.image_dir)
    im_paths = get_im_names(im_dir,
                            pattern='*.jpg',
                            return_path=True,
                            return_np=False)

    all_feat = []
    import scipy.io as sci

    for i, im_path in enumerate(im_paths):
        im = np.asarray(Image.open(im_path).convert('RGB'))
        im = pre_process_im(im, cfg)
        im = Variable(TVT(torch.from_numpy(im).float()), volatile=True)
        feat = model(im)
        feat = feat.data.cpu().numpy()
        all_feat.append(feat)
        if (i + 1) % 100 == 0:
            print('{}/{} images done'.format(i, len(im_paths)))
    all_feat = np.concatenate(all_feat)
    print('all_feat.shape:', all_feat.shape)
    # all_feat = normalize(all_feat, axis=1)
    # You can save your im_paths and features, or do anything else ...

    sci.savemat(cfg.saved_feature_mat_file, {'features': all_feat})
Ejemplo n.º 2
0
def main():
    cfg = Config()

    TVT, TMO = set_devices(cfg.sys_device_ids)

    #########
    # Model #
    #########

    model = Model(net=cfg.net, pretrained=False)

    #####################
    # Load Model Weight #
    #####################

    used_file = cfg.model_weight_file or cfg.ckpt_file
    loaded = torch.load(used_file, map_location=(lambda storage, loc: storage))
    if cfg.model_weight_file == '':
        loaded = loaded['state_dicts'][0]
    load_state_dict(model, loaded)
    print('Loaded model weights from {}'.format(used_file))

    model = model.base
    # Set eval mode. Force all BN layers to use global mean and variance, also disable dropout.
    model.eval()
    # Transfer Model to Specified Device.
    TMO([model])

    #######################
    # Extract Image Crops #
    #######################

    all_feat = []

    dataset = BoundingBoxCrops(cfg)
    loader = DataLoader(dataset=dataset,
                        batch_size=cfg.batch_size,
                        shuffle=False,
                        num_workers=cfg.num_workers)

    print('Processing detections')
    t = tqdm(iter(loader), leave=False, total=len(loader))
    for i, data in enumerate(t):
        imgs, bad_boxes = data

        imgs = Variable(TVT(imgs))
        with torch.no_grad():
            feats = model(imgs)
        feats = feats.data.cpu().numpy()
        for j in range(len(bad_boxes)):
            if bad_boxes[j] == True:
                feats[j,:] = np.zeros(1280)
        sci.savemat(cfg.saved_feature_mat_path+'/'+"{:05d}".format(i)+'.mat', {'features':feats})
def main():
    cfg = Config()

    TVT, TMO = set_devices(cfg.sys_device_ids)

    #########
    # Model #
    #########

    model = Model(last_conv_stride=cfg.last_conv_stride)
    # Set eval mode. Force all BN layers to use global mean and variance, also disable dropout.
    model.eval()
    # Transfer Model to Specified Device.
    TMO([model])

    #####################
    # Load Model Weight #
    #####################

    used_file = cfg.model_weight_file or cfg.ckpt_file
    loaded = torch.load(used_file, map_location=(lambda storage, loc: storage))
    if cfg.model_weight_file == '':
        loaded = loaded['state_dicts'][0]
    load_state_dict(model, loaded)
    print('Loaded model weights from {}'.format(used_file))

    ###################
    # Extract Feature #
    ###################

    im_dir = osp.expanduser('~/Dataset/market1501/Market-1501-v15.09.15/query')
    im_paths = get_im_names(im_dir,
                            pattern='*.jpg',
                            return_path=True,
                            return_np=False)

    all_feat = []
    for i, im_path in enumerate(im_paths):
        im = np.asarray(Image.open(im_path).convert('RGB'))
        im = pre_process_im(im, cfg)
        im = Variable(TVT(torch.from_numpy(im).float()), volatile=True)
        feat = model(im)
        feat = feat.data.cpu().numpy()
        all_feat.append(feat)
        if (i + 1) % 100 == 0:
            print('{}/{} images done'.format(i, len(im_paths)))
    all_feat = np.concatenate(all_feat)
    print('all_feat.shape:', all_feat.shape)
    all_feat = normalize(all_feat, axis=1)
def main():
    cfg = Config()

    TVT, TMO = set_devices(cfg.sys_device_ids)

    #########
    # Model #
    #########

    model = Model(last_conv_stride=cfg.last_conv_stride)
    # Set eval mode. Force all BN layers to use global mean and variance, also disable dropout.
    model.eval()
    # Transfer Model to Specified Device.
    TMO([model])

    #####################
    # Load Model Weight #
    #####################

    used_file = cfg.model_weight_file or cfg.ckpt_file
    loaded = torch.load(used_file, map_location=(lambda storage, loc: storage))
    if cfg.model_weight_file == '':
        loaded = loaded['state_dicts'][0]
    load_state_dict(model, loaded)
    print('Loaded model weights from {}'.format(used_file))

    ###################
    # Extract Feature #
    ###################

    im_dir = osp.expanduser('../../5Labeled Data/labeledNew')
    im_paths = get_im_names(im_dir,
                            pattern='*.png',
                            return_path=True,
                            return_np=False)

    currentData = {}
    imagesKnown = set()

    with open('../../7Data Features/dataFeatures.json') as json_file:
        currentData = json.load(json_file)
        for image in currentData["images"]:
            imagesKnown.add(image["imageName"])

    all_feat = []
    for i, im_path in enumerate(im_paths):
        im = np.asarray(Image.open(im_path).convert('RGB'))
        im = pre_process_im(im, cfg)
        im = Variable(TVT(torch.from_numpy(im).float()), volatile=True)
        feat = model(im)
        feat = feat.data.cpu().numpy()
        all_feat.append(feat)
        if (i + 1) % 100 == 0:
            print('{}/{} images done'.format(i, len(im_paths)))
    all_feat = np.concatenate(all_feat)
    print('all_feat.shape:', all_feat.shape)
    all_feat = normalize(all_feat, axis=1)
    # You can save your im_paths and features, or do anything else ...

    for i, im_path in enumerate(im_paths):
        if (im_path in imagesKnown):
            print("image", im_path, "already exists")
            pass
        else:
            currentData['images'].append({
                'imageName': im_path,
                'features': all_feat[i].tolist()
            })
            if (i + 1) % 100 == 0:
                print('{}/{} images saved'.format(i, len(im_paths)))
    with open('../../7Data Features/dataFeatures.json', 'w') as outfile:
        json.dump(currentData, outfile)