Exemple #1
0
def face_sample(real=False):
    import gatedpixelcnn
    z = gatedpixelcnn.facefeatures_sample(real)
    solver = Solver(_cfg(real), try_load_best=True)
    x = solver.sample(z)
    if real:
        fn = "results/vqvaer_face_sample_1.png"
    else:
        fn = "results/vqvae_face_sample_1.png"
    torchvision.utils.save_image(x, fn, range=(-1, 1), normalize=True)
    utils.show_pics(x, normalize=True, range=(-1, 1))
Exemple #2
0
def mnist_sample():
    cfg = Config()
    solver = Solver(cfg, try_load_best=True)
    x = solver.sample((32, 28, 28)).float() / cfg.input_dim
    x = x[:, None, :, :]
    utils.show_pics(x)
Exemple #3
0
def mk_sample():
    sample.fill_(0)
    net.eval()
    for i in range(28):
        for j in range(28):
            out = net(sample)
            probs = F.softmax(out[:, :, i, j], dim=1)
            sample[:, :, i, j] = torch.multinomial(probs, 1).float() / 255.


is_infer = False

if is_infer:
    mk_sample()
    utils.show_pics(sample)

else:
    tr, te = dataloader.load_mnist(select=8)
    epochs = 30
    for epoch in range(last_epoch + 1, epochs + last_epoch + 1):

        # train
        err_tr = []
        time_tr = time.time()
        net.train()
        for i, (input, _) in enumerate(tr):
            input = input.cuda()
            target = (input[:, 0] * 255).long()
            out = net(input)
            loss = F.cross_entropy(out, target)
Exemple #4
0
                data_cpu = d[0]
                data = data_cpu.to(self.device)
                recon_batch, mu, logvar = self.model(data)
                test_loss += loss_function(recon_batch, data, mu,
                                           logvar).item()
                if i == 0:
                    choose_orig = data_cpu
                    choose_recon = recon_batch.view(-1, 3, 64, 64).cpu()
                    choose_z = mu.cpu()

        test_loss /= len(test_loader)
        return test_loss, choose_orig, choose_recon, choose_z

    def sample(self):
        self.model.eval()
        with torch.no_grad():
            sample_z = torch.randn(64, 100)
            z = sample_z.to(self.device)
            sample_data = self.model.decode(z).cpu().view(64, 3, 64, 64)
            return sample_data, sample_z


solver = Solver()

is_infer = False
if is_infer:
    sample_data, _ = solver.sample()
    utils.show_pics(sample_data)
else:
    solver.train(30)
Exemple #5
0
is_infer = False

checkpoint_path = "results/dcgan.pt"
if is_read_checkpoint and os.path.isfile(checkpoint_path):
    checkpoint = torch.load(checkpoint_path)
    netG.load_state_dict(checkpoint['netG'])
    netD.load_state_dict(checkpoint['netD'])
    optimizerG.load_state_dict(checkpoint['optG'])
    optimizerD.load_state_dict(checkpoint['optD'])
    last_epoch = checkpoint['epoch']
    print('Load checkpoint! Last Epoch: %d' % last_epoch)

if is_infer:
    fake = netG(fixed_noise)
    fake = fake.detach().cpu()
    utils.show_pics(fake, normalize=True, range=(-1, 1))
else:
    normalizer = torchvision.transforms.Normalize((0.5, 0.5, 0.5),
                                                  (0.5, 0.5, 0.5))
    train_loader, test_loader = dataloader.load_faces(normalizer)

    # graph,现在tensorboard似乎好像不能支持2个graph
    noise = torch.randn(64, nz, 1, 1, device=device)
    writer.add_graph(netG, noise)
    fixed_images = utils.first_batch(test_loader)
    writer.add_graph(netD, fixed_images.to(device))

    # original img
    img_grid = torchvision.utils.make_grid(fixed_images,
                                           range=(-1, 1),
                                           normalize=True)
Exemple #6
0
def face_reconstruct(real=False):
    solver = Solver(_cfg(real), try_load_best=True)
    tr, te = _load(real)
    x = utils.random_choices(te.dataset, k=32)
    x_tilde = solver.reconstruct(x)
    utils.show_pics(torch.cat([x, x_tilde]), normalize=True, range=(-1, 1))