def process_eval_image( cfg, fname_in, roi, fname_out, spatial_levels, image_helper, model, pca, eval_dataset_name, verbose=False, ): if is_revisited_dataset(eval_dataset_name): img = image_helper.load_and_prepare_revisited_image(fname_in, roi=roi) elif is_instre_dataset(eval_dataset_name): img = image_helper.load_and_prepare_instre_image(fname_in) else: img = image_helper.load_and_prepare_image(fname_in, roi=roi) v = torch.autograd.Variable(img.unsqueeze(0)) vc = v.cuda() # the model output is a list always. activation_map = model(vc)[0].cpu() if verbose: print(f"Eval image raw activation map shape: { activation_map.shape }") # process the features: rmac | l2 norm if cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE == "rmac": descriptors = get_rmac_descriptors( activation_map, spatial_levels, pca=pca, normalize=cfg.IMG_RETRIEVAL.NORMALIZE_FEATURES, ) elif cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE == "gem": descriptors = gem( activation_map, p=cfg.IMG_RETRIEVAL.GEM_POOL_POWER, add_bias=True, ) else: descriptors = activation_map # Optionally l2 normalize the features. if (cfg.IMG_RETRIEVAL.NORMALIZE_FEATURES and cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE != "rmac"): # RMAC performs normalization within the algorithm, hence we skip it here. descriptors = l2n(descriptors, dim=1) # Optionally apply pca. if pca and cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE != "rmac": # RMAC performs pca within the algorithm, hence we skip it here. descriptors = pca.apply(descriptors) if fname_out: save_file(descriptors.data.numpy(), fname_out, verbose=False) return descriptors.data.numpy()
def process_train_image(i, out_dir, verbose=False): if i % LOG_FREQUENCY == 0: logging.info(f"Train Image: {i}"), fname_out = None if out_dir: fname_out = f"{out_dir}/{i}.npy" if fname_out and PathManager.exists(fname_out): feat = load_file(fname_out) train_features.append(feat) else: fname_in = train_dataset.get_filename(i) if is_revisited_dataset(train_dataset_name): img = image_helper.load_and_prepare_revisited_image(fname_in, roi=None) elif is_whiten_dataset(train_dataset_name): img = image_helper.load_and_prepare_whitening_image(fname_in) else: img = image_helper.load_and_prepare_image(fname_in, roi=None) v = torch.autograd.Variable(img.unsqueeze(0)) vc = v.cuda() # the model output is a list always. activation_map = model(vc)[0].cpu() if verbose: print( f"Train Image raw activation map shape: { activation_map.shape }" ) # once we have the features, # we can perform: rmac | gem pooling | l2 norm if cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE == "rmac": descriptors = get_rmac_descriptors( activation_map, spatial_levels, normalize=cfg.IMG_RETRIEVAL.NORMALIZE_FEATURES, ) elif cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE == "gem": descriptors = gem( activation_map, p=cfg.IMG_RETRIEVAL.GEM_POOL_POWER, add_bias=True, ) else: descriptors = activation_map # Optionally l2 normalize the features. if (cfg.IMG_RETRIEVAL.NORMALIZE_FEATURES and cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE != "rmac"): # RMAC performs normalization within the algorithm, hence we skip it here. descriptors = l2n(descriptors, dim=1) if fname_out: save_file(descriptors.data.numpy(), fname_out, verbose=False) train_features.append(descriptors.data.numpy())
def gem_pool_and_save_features(features, p, add_bias, gem_out_fname): if PathManager.exists(gem_out_fname): logging.info("Loading train GeM features...") features = load_file(gem_out_fname) else: logging.info(f"GeM pooling features: {features.shape}") features = l2n(gem(features, p=p, add_bias=True)) save_file(features, gem_out_fname) logging.info(f"Saved GeM features to: {gem_out_fname}") return features