Exemplo n.º 1
0
def calculate_fid(feats_real, feats_gen):
    mu_real = np.mean(feats_real, axis=0)
    sigma_real = np.cov(feats_real, rowvar=False)
    mu_gen = np.mean(feats_gen, axis=0)
    sigma_gen = np.cov(feats_gen, rowvar=False)
    fid = calculate_frechet_distance(mu_real, sigma_real, mu_gen, sigma_gen)
    return fid
Exemplo n.º 2
0
def test_vae_fid(model, args, total_fid_samples):
    dims = 2048
    device = 'cuda'
    num_gpus = args.num_process_per_node * args.num_proc_node
    num_sample_per_gpu = int(np.ceil(total_fid_samples / num_gpus))

    g = create_generator_vae(model, args.batch_size, num_sample_per_gpu)
    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
    model = InceptionV3([block_idx], model_dir=args.fid_dir).to(device)
    m, s = compute_statistics_of_generator(g,
                                           model,
                                           args.batch_size,
                                           dims,
                                           device,
                                           max_samples=num_sample_per_gpu)

    # share m and s
    m = torch.from_numpy(m).cuda()
    s = torch.from_numpy(s).cuda()
    # take average across gpus
    utils.average_tensor(m, args.distributed)
    utils.average_tensor(s, args.distributed)

    # convert m, s
    m = m.cpu().numpy()
    s = s.cpu().numpy()

    # load precomputed m, s
    path = os.path.join(args.fid_dir, args.dataset + '.npz')
    m0, s0 = load_statistics(path)

    fid = calculate_frechet_distance(m0, s0, m, s)
    return fid
Exemplo n.º 3
0
    def score(self, generator, loader):
        """
        Evaluate generator performance computing FID between generator output
        and validation dataset
        :param generator:
        :param loader:
        :return:
        """
        generator.eval()
        act_real = []
        act_fake = []
        for idx, sample in zip(range(self.limit), loader):
            noise = self.noise_sampler.sample_batch(sample[0].shape[0])
            noise = [c.to(self.generator_device) for c in noise]
            # rescale generator output from [-1, 1] to [0, 1]
            G_sample = (generator(*noise) + 1)/2
            # transfer tensor from generator device to device with Inception model
            G_sample = G_sample.to(self.device)
            with torch.cuda.device(self.device.index):
                act_real_batch = get_activations(((sample[0] + 1)/2).cuda(), self.model, batch_size=64, dims=self.dims, cuda=1)
                act_fake_batch = get_activations(G_sample, self.model, batch_size=64, dims=self.dims, cuda=1)
                act_real.append(act_real_batch)
                act_fake.append(act_fake_batch)

        act_real = np.concatenate(act_real)
        act_fake = np.concatenate(act_fake)
        # calculate activation statistics
        m1 = np.mean(act_real, axis=0)
        s1 = np.cov(act_real, rowvar=False)
        m2 = np.mean(act_fake, axis=0)
        s2 = np.cov(act_fake, rowvar=False)
        fid_value = calculate_frechet_distance(m1, s1, m2, s2)
        return fid_value
Exemplo n.º 4
0
 def distance(self, X1, X2):
     assert(X1.shape == X2.shape)
     # Generator outputs images with pixel values in [-1, 1]
     # While inception expect them to be in range [0, 1]
     # Inplace operations are used to save memory usage as X1 and X2 can be very big
     X1 += 1
     X1 /= 2
     X2 += 1
     X2 /= 2
     with torch.cuda.device(self.device.index):
         m1, s1 = calculate_activation_statistics(X1, self.model, batch_size=64, dims=self.dims, cuda=1)
         m2, s2 = calculate_activation_statistics(X2, self.model, batch_size=64, dims=self.dims, cuda=1)
         fid_value = calculate_frechet_distance(m1, s1, m2, s2)
     return fid_value
Exemplo n.º 5
0
                 device,
                 exportCuts=args.fid)

        if args.fid:
            # Compute FID
            distance = []

            for ind, dim in enumerate(["x", "y", "z"]):
                files = [
                    os.path.join("output/epoch/", x)
                    for x in os.listdir("output/epoch")
                    if '{}.png'.format(dim) in x
                ]
                mu, sigma = fid_score.calculate_activation_statistics(
                    files,
                    inceptionModel,
                    batch_size=args.batch_size,
                    cuda=torch.cuda.is_available())
                distance.append(
                    fid_score.calculate_frechet_distance(
                        muTI[ind], sigmaTI[ind], mu, sigma))

        with open("{}.log".format(args.name), "a") as f:
            f.write(
                f"{distance[0]}, {distance[1]}, {distance[2]}, {fake_loss}, {real_loss}\n"
            )

        if epoch % args.checkpoint_freq == 0:
            torch.save(generator.state_dict(),
                       "output/{}_e{}.model".format(args.name, epoch))