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
def get_training_pair_images(file, centre_fraction, acceleration): """ :param file: The training image :param centre_fraction: randomly generated centre fraction :param acceleration: randomly generated :return: true gold standard and the fft of the masked k-space image """ hf = h5py.File(file) volume_kspace = hf['kspace'][()] volume_image = hf['reconstruction_esc'][()] mask_func = MaskFunc(center_fractions=[centre_fraction], accelerations=[acceleration ]) # Create the mask function object volume_kspace_tensor = T.to_tensor(volume_kspace) masked_kspace, mask = T.apply_mask(volume_kspace_tensor, mask_func) ##masked_kspace_np=masked_kspace.numpy().reshape(masked_kspace.shape) recon_image = T.ifft2(masked_kspace) # complex image recon_image_abs = T.complex_abs( masked_kspace) # compute absolute value to get a real image #recon_image_rss= T.root_sum_of_square(recon_image_abs,dim=0) # compute absolute rss return np.expand_dims(volume_image, 3), recon_image_abs.numpy()
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')
def get_training_pair(file, centre_fraction, acceleration): """ :param file: The training image :param centre_fraction: randomly generated centre fraction :param acceleration: randomly generated :return: """ hf = h5py.File(file) volume_kspace = hf['kspace'][()] volume_image = hf['reconstruction_esc'][()] mask_func = MaskFunc(center_fractions=[centre_fraction], accelerations=[acceleration ]) # Create the mask function object volume_kspace_tensor = T.to_tensor(volume_kspace) masked_kspace, mask = T.apply_mask(volume_kspace_tensor, mask_func) masked_kspace_np = masked_kspace.numpy().reshape(masked_kspace.shape) return np.expand_dims(volume_image, 3), masked_kspace_np
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).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')