def compute_real_features(num_samples, sess, batch_size, dataset=None, feat_file=None, seed=0, verbose=True, log_dir='./log', root='./dataset'): """ Reads the image data and compute the features for real images. Args: num_samples (int): Number of real images to compute statistics. sess (Session): TensorFlow session to use. dataset (str/Dataset): Dataset to load. batch_size (int): The batch size to feedforward for inference. feat_file (str): The features file to load from if there is already one. verbose (bool): If True, prints progress of computation. log_dir (str): Directory where feature statistics can be stored. Returns: ndarray: Real data features. """ # Create custom feature file name if feat_file is None: feat_dir = os.path.join(log_dir, 'metrics', 'features') if not os.path.exists(feat_dir): os.makedirs(feat_dir) feat_file = os.path.join( feat_dir, "pr_feat_{}_{}k_run_{}.npy".format(dataset, num_samples // 1000, seed)) if feat_file and os.path.exists(feat_file): print("INFO: Loading existing features for real images...") f = np.load(feat_file) real_features = f[:] #f.close() else: # Obtain the numpy format data print("INFO: Obtaining images...") images = get_dataset_images(dataset, num_samples=num_samples, root=root) # Compute the features print("INFO: Computing features for real images...") real_features = inception_utils.get_activations(images=images, sess=sess, batch_size=batch_size, verbose=verbose) if not os.path.exists(feat_file): print("INFO: Saving features for real images...") np.save(feat_file, real_features) return real_features
def compute_fake_features(netG, num_samples, sess, device, seed, batch_size, print_every=20, verbose=True): """ Directly produces the images and convert them into numpy format without saving the images on disk. Args: netG (Module): Torch Module object representing the generator model. num_samples (int): The number of fake images for computing statistics. sess (Session): TensorFlow session to use. device (str): Device identifier to use for computation. seed (int): The random seed to use. batch_size (int): The number of samples per batch for inference. print_every (int): Interval for printing log. verbose (bool): If True, prints progress. Returns: ndarray: Fake data features. """ with torch.no_grad(): # Set model to evaluation mode netG.eval() # Inference variables batch_size = min(num_samples, batch_size) # Collect all samples() images = [] start_time = time.time() for idx in range(num_samples // batch_size): # Collect fake image fake_images = netG.generate_images(num_images=batch_size, device=device).detach().cpu() images.append(fake_images) # Print some statistics if (idx + 1) % print_every == 0: end_time = time.time() print( "INFO: Generated image {}/{} [Random Seed {}] ({:.4f} sec/idx)" .format( (idx + 1) * batch_size, num_samples, seed, (end_time - start_time) / (print_every * batch_size))) start_time = end_time # Produce images in the required (N, H, W, 3) format for FID computation images = torch.cat(images, 0) # Gives (N, 3, H, W) images = _normalize_images(images) # Gives (N, H, W, 3) # Compute the fake features fake_features = inception_utils.get_activations( images=images, sess=sess, batch_size=batch_size, verbose=verbose) return fake_features
def test_get_activations(self): inception_path = './metrics/inception_model' inception_utils.create_inception_graph(inception_path) images = np.ones((4, 32, 32, 3)) with tf.compat.v1.Session() as sess: feat = inception_utils.get_activations(images=images, sess=sess) assert feat.shape == (4, 2048)
def calculate_activation_statistics(images, sess, batch_size=50, verbose=True): """ Calculation of the statistics used by the FID. Args: images (ndarray): Numpy array of shape (N, H, W, 3) and values in the range [0, 255]. sess (Session): TensorFlow session object. batch_size (int): Batch size for inference. verbose (bool): If True, prints out logging information. Returns: ndarray: Mean of inception features from samples. ndarray: Covariance of inception features from samples. """ act = iu.get_activations(images, sess, batch_size, verbose) mu = np.mean(act, axis=0) sigma = np.cov(act, rowvar=False) return mu, sigma