コード例 #1
0
    ('model={:s}',  'vae'),
    ('z={:02d}',  args.z),
    ('run={:04d}', args.run)
]
model_name = '_'.join([t.format(v) for (t, v) in layout])
pprint(vars(args))
print('Model name:', model_name)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_loader, labeled_subset, _ = ut.get_mnist_data(device, use_test_subset=True)
vae = VAE(z_dim=args.z, name=model_name).to(device)

if args.train:
    writer = ut.prepare_writer(model_name, overwrite_existing=True)
    train(model=vae,
          train_loader=train_loader,
          labeled_subset=labeled_subset,
          device=device,
          tqdm=tqdm.tqdm,
          writer=writer,
          iter_max=args.iter_max,
          iter_save=args.iter_save)
    ut.evaluate_lower_bound(vae, labeled_subset, run_iwae=args.train == 2)
    x = vae.sample_x(100).view(100, 1, 28, 28)
    db.printTensor(x)
    ImUtil.showBatch(x, show=True)
    input('Press key to exit')
else:
    ut.load_model_by_name(vae, global_step=args.iter_max)
    ut.evaluate_lower_bound(vae, labeled_subset, run_iwae=True)
コード例 #2
0
          ('run={:04d}', args.run)]
model_name = '_'.join([t.format(v) for (t, v) in layout])
pprint(vars(args))
print('Model name:', model_name)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_loader, labeled_subset, _ = ut.get_mnist_data(device,
                                                    use_test_subset=True)
vae = VAE(z_dim=args.z, name=model_name).to(device)

if args.train:
    writer = ut.prepare_writer(model_name, overwrite_existing=True)
    train(model=vae,
          train_loader=train_loader,
          labeled_subset=labeled_subset,
          device=device,
          tqdm=tqdm.tqdm,
          writer=writer,
          iter_max=args.iter_max,
          iter_save=args.iter_save)
    ut.evaluate_lower_bound(vae, labeled_subset, run_iwae=args.train == 2)

else:
    ut.load_model_by_name(vae, global_step=args.iter_max)
    # Set run_iwae to false so I could generate the images
    ut.evaluate_lower_bound(vae, labeled_subset, run_iwae=False)

samples = vae.sample_x(200)
img = samples.view(200, 1, 28, 28)
utils.save_image(img, "VAEDigitsGrid.png", nrow=20)
コード例 #3
0
                    default=10000,
                    help="Save model every n iterations")
parser.add_argument('--run',
                    type=int,
                    default=0,
                    help="Run ID. In case you want to run replicates")
args = parser.parse_args()
layout = [('model={:s}', 'vae'), ('z={:02d}', args.z),
          ('run={:04d}', args.run)]
model_name = '_'.join([t.format(v) for (t, v) in layout])
pprint(vars(args))
print('Model name:', model_name)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_loader, labeled_subset, _ = ut.get_mnist_data(device,
                                                    use_test_subset=True)
vae = VAE(z_dim=args.z, name=model_name).to(device)

ut.load_model_by_name(vae, global_step=args.iter_max)
ut.evaluate_lower_bound(vae, labeled_subset, run_iwae=False)
samples = torch.reshape(vae.sample_x(200), (10, 20, 28, 28))
#print(torch.reshape(vae.sample_x(200), (200, 28, 28)))

f, axarr = plt.subplots(10, 20)

for i in range(samples.shape[0]):
    for j in range(samples.shape[1]):
        axarr[i, j].imshow(samples[i, j].detach().numpy())
        axarr[i, j].axis('off')

plt.show()
コード例 #4
0
ファイル: run_vae.py プロジェクト: vukien95/cs236
          ('run={:04d}', args.run)]
model_name = '_'.join([t.format(v) for (t, v) in layout])
pprint(vars(args))
print('Model name:', model_name)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_loader, labeled_subset, _ = ut.get_mnist_data(device,
                                                    use_test_subset=True)
vae = VAE(z_dim=args.z, name=model_name).to(device)

if args.train:
    writer = ut.prepare_writer(model_name, overwrite_existing=True)
    train(model=vae,
          train_loader=train_loader,
          labeled_subset=labeled_subset,
          device=device,
          tqdm=tqdm.tqdm,
          writer=writer,
          iter_max=args.iter_max,
          iter_save=args.iter_save)
    ut.evaluate_lower_bound(vae, labeled_subset, run_iwae=args.train == 2)

else:
    ut.load_model_by_name(vae, global_step=args.iter_max)
    ut.evaluate_lower_bound(vae, labeled_subset, run_iwae=True)

if args.sample:
    with torch.no_grad():
        images = vae.sample_x(200)
        save_image(images.view(200, 1, 28, 28), 'vae_sample.png', nrow=20)