def test(load_model_weight=False):
        if load_model_weight:
            if cfg.model_weight_file != '':
                map_location = (lambda storage, loc: storage)
                sd = torch.load(cfg.model_weight_file,
                                map_location=map_location)
                load_state_dict(model, sd)
                print('Loaded model weights from {}'.format(
                    cfg.model_weight_file))
            else:
                load_ckpt(modules_optims, cfg.ckpt_file)

        for test_set, name in zip(test_sets, test_set_names):
            test_set.set_feat_func(ExtractFeature(model_w, TVT))
            print('\n=========> Test on dataset: {} <=========\n'.format(name))
            test_feat, test_ids, test_cams, test_im_names, test_marks = test_set.extract_feat(
                normalize_feat=cfg.normalize_feature, verbose=True)
            write_my_csv('test_features.csv', test_feat)
            write_my_csv('test_ids.csv', test_ids)
            write_my_csv('test_cams.csv', test_cams)
            write_my_cev('test_image_names.csv', test_in_names)
        for train_set, name in zip(train_sets, train_set_names):
            train_set.set_feat_func(ExtractFeature(model_w, TVT))
            print('\n=========> Test on dataset: {} <=========\n'.format(name))
            train_feat, train_im_names, train_labels = train_set.extract_feat(
                normalize_feat=cfg.normalize_feature, verbose=True)
            write_my_csv('train_features.csv', train_feat)
            write_my_csv('train_labels.csv', train_labels)
            write_my_csv('train_image_names.csv', train_im_names)
Пример #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 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})
Пример #3
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)
Пример #5
0
    def test(load_model_weight=False):
        if load_model_weight:
            if cfg.model_weight_file != '':
                map_location = (lambda storage, loc: storage)
                sd = torch.load(cfg.model_weight_file,
                                map_location=map_location)
                load_state_dict(model, sd)
                print('Loaded model weights from {}'.format(
                    cfg.model_weight_file))
            else:
                load_ckpt(modules_optims, cfg.ckpt_file)

        for test_set, name in zip(test_sets, test_set_names):
            test_set.set_feat_func(ExtractFeature(model_w, TVT))
            print('\n=========> Test on dataset: {} <=========\n'.format(name))
            test_set.eval(normalize_feat=cfg.normalize_feature, verbose=True)
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)
Пример #7
0
def main():
  cfg = Config()

  # Redirect logs to both console and file.
  if cfg.log_to_file:
    ReDirectSTD(cfg.stdout_file, 'stdout', False)
    ReDirectSTD(cfg.stderr_file, 'stderr', False)

  TVT, TMO = set_devices(cfg.sys_device_ids)

  # Dump the configurations to log.
  import pprint
  print('-' * 60)
  print('cfg.__dict__')
  pprint.pprint(cfg.__dict__)
  print('-' * 60)

  ###########
  # Dataset #
  ###########

  test_set = create_dataset(**cfg.test_set_kwargs)

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

  model = Model(last_conv_stride=cfg.last_conv_stride)
  # Model wrapper
  model_w = DataParallel(model)

  # May Transfer Model to Specified Device.
  TMO([model])

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

  # To first load weights to CPU
  map_location = (lambda storage, loc: storage)
  used_file = cfg.model_weight_file or cfg.ckpt_file
  loaded = torch.load(used_file, map_location=map_location)
  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 #
  ###################

  test_set.set_feat_func(ExtractFeature(model_w, TVT))

  with measure_time('Extracting feature...', verbose=True):
    feat, ids, cams, im_names, marks = test_set.extract_feat(True, verbose=True)

  #######################
  # Select Query Images #
  #######################

  # Fix some query images, so that the visualization for different models can
  # be compared.

  # Sort in the order of image names
  inds = np.argsort(im_names)
  feat, ids, cams, im_names, marks = \
    feat[inds], ids[inds], cams[inds], im_names[inds], marks[inds]

  # query, gallery index mask
  is_q = marks == 0
  is_g = marks == 1

  prng = np.random.RandomState(1)
  # selected query indices
  sel_q_inds = prng.permutation(range(np.sum(is_q)))[:cfg.num_queries]

  q_ids = ids[is_q][sel_q_inds]
  q_cams = cams[is_q][sel_q_inds]
  q_feat = feat[is_q][sel_q_inds]
  q_im_names = im_names[is_q][sel_q_inds]

  ####################
  # Compute Distance #
  ####################

  # query-gallery distance
  q_g_dist = compute_dist(q_feat, feat[is_g], type='euclidean')

  ###########################
  # Save Rank List as Image #
  ###########################

  q_im_paths = [ospj(test_set.im_dir, n) for n in q_im_names]
  save_paths = [ospj(cfg.exp_dir, 'rank_lists', n) for n in q_im_names]
  g_im_paths = [ospj(test_set.im_dir, n) for n in im_names[is_g]]

  for dist_vec, q_id, q_cam, q_im_path, save_path in zip(
      q_g_dist, q_ids, q_cams, q_im_paths, save_paths):

    rank_list, same_id = get_rank_list(
      dist_vec, q_id, q_cam, ids[is_g], cams[is_g], cfg.rank_list_size)

    save_rank_list_to_im(rank_list, same_id, q_im_path, g_im_paths, save_path)
def main():
  cfg = Config()

  # Redirect logs to both console and file.
  if cfg.log_to_file:
    ReDirectSTD(cfg.stdout_file, 'stdout', False)
    ReDirectSTD(cfg.stderr_file, 'stderr', False)

  TVT, TMO = set_devices(cfg.sys_device_ids)

  # Dump the configurations to log.
  import pprint
  print('-' * 60)
  print('cfg.__dict__')
  pprint.pprint(cfg.__dict__)
  print('-' * 60)

  ###########
  # Dataset #
  ###########

  test_set = create_dataset(**cfg.test_set_kwargs)

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

  model = Model(last_conv_stride=cfg.last_conv_stride)
  # Model wrapper
  model_w = DataParallel(model)

  # May Transfer Model to Specified Device.
  TMO([model])

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

  # To first load weights to CPU
  map_location = (lambda storage, loc: storage)
  used_file = cfg.model_weight_file or cfg.ckpt_file
  loaded = torch.load(used_file, map_location=map_location)
  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 #
  ###################

  test_set.set_feat_func(ExtractFeature(model_w, TVT))

  with measure_time('Extracting feature...', verbose=True):
    feat, ids, cams, im_names, marks = test_set.extract_feat(True, verbose=True)

  #######################
  # Select Query Images #
  #######################

  # Fix some query images, so that the visualization for different models can
  # be compared.

  # Sort in the order of image names
  inds = np.argsort(im_names)
  feat, ids, cams, im_names, marks = \
    feat[inds], ids[inds], cams[inds], im_names[inds], marks[inds]

  # query, gallery index mask
  is_q = marks == 0
  is_g = marks == 1

  prng = np.random.RandomState(1)
  # selected query indices
  sel_q_inds = prng.permutation(range(np.sum(is_q)))[:cfg.num_queries]

  q_ids = ids[is_q][sel_q_inds]
  q_cams = cams[is_q][sel_q_inds]
  q_feat = feat[is_q][sel_q_inds]
  q_im_names = im_names[is_q][sel_q_inds]

  ####################
  # Compute Distance #
  ####################

  # query-gallery distance

  f= open("../../4Scripts/Data Labeling Process/imagesDesc2.txt","w+")

  #for controlIndex in range(len(im_names[is_q])):
  for controlIndex in range(0,6):
     f.write(im_names[is_q][controlIndex] + "|" + "|".join(list(map(str, feat[is_q][controlIndex])))+"\n")

  #for controlIndex in range(len(im_names[is_g])):
  for controlIndex in range(0,6):
      f.write(im_names[is_g][controlIndex] + "|" + "|".join(list(map(str, feat[is_g][controlIndex])))+"\n")

  f.close()