def sample(epoch): print('sampling') fgen.eval() n = 256 taus = [0.7, 0.8, 0.9, 1.0] for t in taus: z = torch.randn(n, 3, imageSize, imageSize).to(device) z = z * t img, _ = fgen.decode(z) img = postprocess(img, n_bits) image_file = 'sample{}.t{:.1f}.png'.format(epoch, t) save_image(img, os.path.join(result_path, image_file), nrow=16)
def reconstruct(epoch): print('reconstruct') fgen.eval() n = 16 np.random.shuffle(test_index) img, _ = get_batch(test_data, test_index[:n]) img = preprocess(img.to(device), n_bits) z, _ = fgen.encode(img) img_recon, _ = fgen.decode(z) abs_err = img_recon.add(img * -1).abs() print('Err: {:.4f}, {:.4f}'.format(abs_err.max().item(), abs_err.mean().item())) img = postprocess(img, n_bits) img_recon = postprocess(img_recon, n_bits) comparison = torch.cat([img, img_recon], dim=0).cpu() reorder_index = torch.from_numpy(np.array([[i + j * n for j in range(2)] for i in range(n)])).view(-1) comparison = comparison[reorder_index] image_file = 'reconstruct{}.png'.format(epoch) save_image(comparison, os.path.join(result_path, image_file), nrow=16)