Esempio n. 1
0
def save_inception_score(gan, i_iter):
    print("Evaluating...")
    num_images_to_eval = 50000
    eval_images = []
    num_batches = num_images_to_eval // 100 + 1
    print("Calculating Inception Score. Sampling {} images...".format(num_images_to_eval))
    np.random.seed(0)

    gan.netG.eval()

    for _ in range(num_batches):
        index = np.arange(100) * 10 + np.tile(np.arange(10), 10)
        images = gan.gen_fake_data(100, opt.nz).data.cpu().numpy()
        images = images.reshape((-1, 3, 32, 32))
        images = np.rollaxis(images, 1, 4)
        images = images[index]
        eval_images.append(images)

    gan.netG.train()

    np.random.seed()
    eval_images = np.vstack(eval_images)
    
    eval_images = eval_images[:num_images_to_eval]
    eval_images = np.clip((eval_images + 1.0) * 127.5, 0.0, 255.0).astype(np.uint8)
    # Calc Inception score
    eval_images = list(eval_images)
    inception_score_mean, inception_score_std = get_inception_score(eval_images)
    print("Inception Score: Mean = {} \tStd = {}.".format(inception_score_mean, inception_score_std))

    log.add('inception_score', dict(mean=inception_score_mean, std=inception_score_std), i_iter)
Esempio n. 2
0
def save_samples(gan, i_iter):
    if 'noise' not in save_samples.__dict__:
        save_samples.noise = Variable(gan.gen_latent_noise(64, opt.nz))

    if not os.path.exists(opt.path + 'tmp/'):
        os.makedirs(opt.path + 'tmp/')

    fake = gan.gen_fake_data(64, opt.nz, noise=save_samples.noise)
    # fake = next(data_iter)

    fake_01 = (fake[0].data.cpu() + 1.0) * 0.5

    save_image(fake_01, opt.path + 'tmp/' + '{:0>5}.jpeg'.format(i_iter))
Esempio n. 3
0
def save_samples(gan, i_iter):
    gan.netG.eval()

    if 'noise' not in save_samples.__dict__:
        save_samples.noise = Variable(gan.gen_latent_noise(64, opt.nz))

    if not os.path.exists(opt.path + 'tmp/'):
        os.makedirs(opt.path + 'tmp/')

    fake = gan.gen_fake_data(64, opt.nz, noise=save_samples.noise)
    # fake = next(data_iter)
    # print(fake.min(), fake.max())

    fake = fake.view(-1, 3, 32, 32)

    fake_01 = (fake.data.cpu() + 1.0) * 0.5
    # print(fake_01.min(), fake_01.max())

    save_image(fake_01, opt.path + 'tmp/' + '{:0>5}.jpeg'.format(i_iter))
    # alkjfd
    gan.netG.train()
Esempio n. 4
0
def save_samples(gan, i_iter):
    gan.netG.eval()

    if 'noise' not in save_samples.__dict__:
        save_samples.noise = Variable(gan.gen_latent_noise(64, opt.nz))

    if not os.path.exists(opt.path + 'tmp/'):
        os.makedirs(opt.path + 'tmp/')

    fake = gan.gen_fake_data(64, opt.nz, noise=save_samples.noise)
    # fake = next(data_iter)

    fake_list = []

    for i in range(len(fake)):
        for j in range(41):
            img = torch.stack([fake[i, 0, :, :], fake[i, j + 1, :, :]], dim=0)
            fake_list.append(img)

    fake = torch.FloatTensor(len(fake_list), 3, 48, 80)
    fake_tensor = torch.stack(fake_list, dim=0)
    # print(type(fake_tensor))
    # print(type(fake))
    fake[:, :2, :, :] = fake_tensor.data
    fake[:, 2, :, :] = -1

    # fake = next(data_iter)
    # print(fake.min(), fake.max())

    # fake = fake.view(-1, 3, 32, 32)

    fake_01 = (fake.cpu() + 1.0) * 0.5
    # print(fake_01.min(), fake_01.max())

    save_image(fake_01,
               opt.path + 'tmp/' + '{:0>5}.png'.format(i_iter),
               nrow=41)
    gan.netG.train()