def calculate_fid_given_paths(paths, batch_size, device, dims, dataset_size=-1, save1=None, save2=None): """Calculates the FID of two paths""" for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) model.eval() m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims, device, dataset_size) m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims, device, dataset_size) fid_value = calculate_frechet_distance(m1, s1, m2, s2) # Save dataset stats into file if save1 is not None: _save_dataset_stat(save1, m1, s1) if save2 is not None: _save_dataset_stat(save2, m2, s2) return fid_value
def calculate_fig_given_arrays(X, Y, batch_size, cuda, dims): block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]) if cuda: model.cuda() m1, s1 = _compute_statistics_from_array(X, model, batch_size, dims, cuda) m2, s2 = _compute_statistics_from_array(Y, model, batch_size, dims, cuda) fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value
def calcola_statistiche_del_path(path, batch_size, dims, device): if not os.path.exists(path): raise RuntimeError('Invalid path: %s' % path) from pytorch_fid.inception import InceptionV3 block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) m, s = fid._compute_statistics_of_path(path, model, batch_size, dims, device) return m, s
def calculate_fid_given_file_lists(file_list_1, file_list_2, batch_size, device, dims): """Calculates the FID of two file lists""" block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) m1, s1 = calculate_activation_statistics(file_list_1, model, batch_size, dims, device) m2, s2 = calculate_activation_statistics(file_list_2, model, batch_size, dims, device) fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value
def calculate_fid_given_paths(paths, batch_size, device, dims, num_workers=8, val=None): """Calculates the FID of two paths""" for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) m1, s1 = compute_statistics_of_path(paths[0], model, batch_size, dims, device, num_workers) if val is None else val m2, s2 = compute_statistics_of_path(paths[1], model, batch_size, dims, device, num_workers) fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value, [m1, s1]
def calculate_fid_given_images(img_dat1, img_dat2, batch_size, device, dims=2048): """Calculates the FID of two paths""" block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) m1, s1 = _compute_statistics_of_imgs(img_dat1, model, batch_size, dims, device) m2, s2 = _compute_statistics_of_imgs(img_dat2, model, batch_size, dims, device) fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value
def fid(forged_images: torch.Tensor, reference_images: torch.Tensor, feature_dimensionality: int, device: torch.device): inception = InceptionV3( [InceptionV3.BLOCK_INDEX_BY_DIM[feature_dimensionality]]).to(device) inception.eval() forged_pred = np.empty((forged_images.shape[0], feature_dimensionality)) reference_pred = np.empty( (reference_images.shape[0], feature_dimensionality)) for i in range(forged_images.shape[0]): forged_image = forged_images[i:i + 1] forged_image = forged_image.to(device=device, dtype=torch.float) forged_image /= 255 reference_image = reference_images[i:i + 1] reference_image = reference_image.to(device=device, dtype=torch.float) reference_image /= 255 with torch.no_grad(): pred = inception(forged_image)[0] if pred.size(2) != 1 or pred.size(3) != 1: pred = nn.functional.adaptive_avg_pool2d(pred, output_size=(1, 1)) forged_pred[i:i + 1] = pred.cpu().data.numpy().reshape(1, -1) with torch.no_grad(): pred = inception(reference_image)[0] if pred.size(2) != 1 or pred.size(3) != 1: pred = nn.functional.adaptive_avg_pool2d(pred, output_size=(1, 1)) reference_pred[i:i + 1] = pred.cpu().data.numpy().reshape(1, -1) mu_forged = np.mean(forged_pred, axis=0) sigma_forged = np.cov(forged_pred, rowvar=False) mu_reference = np.mean(reference_pred, axis=0) sigma_reference = np.cov(reference_pred, rowvar=False) return calculate_frechet_distance(mu1=mu_forged, sigma1=sigma_forged, mu2=mu_reference, sigma2=sigma_reference)
def calculate_fid_given_paths(paths, batch_size, cuda, dims): """Calculates the FID of two paths""" for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]) if cuda: model.cuda() m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims, cuda) m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims, cuda) fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value
def calculate_fid_given_paths(paths, batch_size, device, dims): """Calculates the FID of two paths""" for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims, device) m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims, device) time_start = time.time() fid_value = calculate_frechet_distance(m1, s1, m2, s2) print('FID time: ', time.time() - time_start) return fid_value
def __call__(self, reference_dataloader, generated_dataloader) -> float: ''' Computes the FVD between the reference and the generated observations :param reference_dataloader: dataloader for reference observations :param generated_dataloader: dataloader for generated observations :return: The FVD between the two distributions ''' # Uses the default block size block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048] model = InceptionV3([block_idx]).cuda() m1, s1 = self.calculate_activation_statistics(reference_dataloader, model) m2, s2 = self.calculate_activation_statistics(generated_dataloader, model) fid_value = self.calculate_frechet_distance(m1, s1, m2, s2) return float(fid_value)
def calculate_fid_given_paths(paths, batch_size, device, dims, glob_re): """Calculates the FID of two paths""" if glob_re is None: glob_re = [None, None] for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) m1, s1 = compute_statistics_of_path(paths[0], model, batch_size, dims, device, glob_re[0]) m2, s2 = compute_statistics_of_path(paths[1], model, batch_size, dims, device, glob_re[1]) fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value
def calculate_fid_given_paths(paths, batch_size, dims): # """Calculates the FID of two paths""" # for p in paths: # if not os.path.exists(p): # raise RuntimeError('Invalid path: %s' % p) os.environ["CUDA_VISIBLE_DEVICES"] = "0" device = torch.device('cuda' if (torch.cuda.is_available()) else 'cpu') block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) m1, s1 = compute_statistics_of_path(paths[0], model, batch_size, dims, device) m2, s2 = compute_statistics_of_path(paths[1], model, batch_size, dims, device) fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value
def calculate_fid_given_paths(paths, batch_size, device, dims): """Calculates the FID of two paths""" for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) a1, m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims, device) a2, m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims, device) fid_value = calculate_frechet_distance(m1, s1, m2, s2) nearest_k = 5 prdc_value = compute_prdc(real_features=a1, fake_features=a2, nearest_k=nearest_k) return fid_value, prdc_value
def __init__( self, device, dims=DIMS, num_samples=-1, verbose=True, ): self.dims = dims self.data_path = None self.gen_name = None self.num_samples = num_samples self.cuda = device.type == 'cuda' if verbose: print('Loading inception model') block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] self.inception = InceptionV3([block_idx]).to(device) #self.inception = torch.nn.Module() if verbose: print('Finished loading inception')
def calculate_fid_given_paths(paths, batch_size, device, dims, num_workers=8): """Calculates the FID of two paths""" for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) m1, s1 = compute_statistics_of_path(paths[0], model, batch_size, dims, device, num_workers) if not os.path.exists('celebA_m2.npy'): print("npy doesnt exists!") m2, s2 = compute_statistics_of_path(paths[1], model, batch_size, dims, device, num_workers) np.save('celebA_m2.npy', m2) np.save('celebA_s2.npy', s2) else: m2 = np.load('celebA_m2.npy') s2 = np.load('celebA_s2.npy') fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value
def get_inception_model(dims=2048): block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] return InceptionV3([block_idx])
metavar='number', type=int, default=-1, help='Number of samples from generator', dest='samples') args = parser.parse_args() # Set device cuda = torch.cuda.is_available() device = torch.device(f"cuda:{args.gpu}" if cuda else "cpu") print(f'Using device: {device}') print('Loading inception model') block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[args.dims] inception = InceptionV3([block_idx]).to(device) print('Finished loading inception') with torch.cuda.device(device) if cuda else nullcontext: # Load params from text file models = get_models(args.hparams, device, load_discr=False) print('Entering Hyperparameter Loop') for h, gen, _ in models: fid = get_fid_score(gen, h.name, h.dataroot,
def define_inception(device, dims): block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) return model