コード例 #1
0
def generate_images(forwardNet,
                    vaeNet,
                    embeddingNet,
                    valloader,
                    name="evaluate"):
    dataloader_iterator = iter(valloader)
    with torch.no_grad():
        img, target = next(dataloader_iterator)
        img = img.to(device)
        target = target.to(device)
        img = img[:args.val_num]
        target = target[:args.val_num]

        embed_mu, embed_var = embeddingNet(target)
        eps = torch.randn_like(embed_mu)
        embed_std = torch.exp(0.5 * embed_var)
        z = eps * embed_std + embed_mu
        predict_img = vaeNet.sample(z)
        predict_spectrum = forwardNet(predict_img)

        img_result = torch.cat([img, predict_img], dim=0)
        save_binary_img(img_result.data,
                        os.path.join(args.checkpoint, f"{name}_imge.png"),
                        nrow=args.val_num)
        plot_amplitude([target, predict_spectrum], ['input', 'generated'],
                       save_path=os.path.join(args.checkpoint,
                                              f"{name}_spectrum.png"))
コード例 #2
0
def sample_images(net, name="val"):
    with torch.no_grad():
        z = torch.randn(args.val_num, args.latent_dim)
        z = z.to(device)
        sampled = net.sample(z)
        save_binary_img(sampled.data,
                        os.path.join(args.checkpoint, f"{name}.png"),
                        nrow=args.val_num)
コード例 #3
0
def generate_images(net, valloader, name="val"):
    dataloader_iterator = iter(valloader)
    with torch.no_grad():
        img, spe = next(dataloader_iterator)
        img = img.to(device)
        recons = net.generate(img)
        result = torch.cat([img, recons], dim=0)
        save_binary_img(result.data,
                        os.path.join(args.checkpoint, f"{name}.png"),
                        nrow=args.val_num)
コード例 #4
0
def sample_images_random(net, name="rand_sample"):
    with torch.no_grad():
        z = torch.randn(args.val_num, args.latent_dim)
        z = z.to(device)
        results = net.module.sample(z)
        image = results["image"]
        specturm = results["specturm"]
        save_binary_img(image.data,
                        os.path.join(args.checkpoint, f"{name}_image.png"),
                        nrow=args.val_num)
        plot_spectrum([specturm], ['spectrum'],
                      os.path.join(args.checkpoint, f"{name}_spectrum.png"))
コード例 #5
0
def generate_images(net, valloader, name="val"):
    dataloader_iterator = iter(valloader)
    with torch.no_grad():
        img, target = next(dataloader_iterator)
        img = img.to(device)
        target = target.to(device)
        out = net.module.generate(img)
        recons = out["recons"]
        predict_spectrum = out["predict_spectrum"]
        result = torch.cat([img, recons], dim=0)
        save_binary_img(result.data,
                        os.path.join(args.checkpoint, f"{name}_image.png"),
                        nrow=args.val_num)
        plot_amplitude([target, predict_spectrum], ['target', 'predict'],
                       os.path.join(args.checkpoint, f"{name}_amplitude.png"))
コード例 #6
0
def sample_images_spectrum(net, name="rand_sample"):
    dataloader_iterator = iter(valloader)
    with torch.no_grad():
        img, target = next(dataloader_iterator)
        img = img.to(device)
        target = target.to(device)
        z = torch.randn(args.val_num, args.latent_dim)
        z = z.to(device)
        results = net.module.sample(z, target)
        image = results["image"]
        specturm = results["specturm"]
        result_img = torch.cat([img, image], dim=0)
        save_binary_img(result_img.data,
                        os.path.join(args.checkpoint, f"{name}_image.png"),
                        nrow=args.val_num)
        plot_spectrum([target, specturm], ['target', 'spectrum'],
                      os.path.join(args.checkpoint, f"{name}_spectrum.png"))