def test(encoder_model, epoch, x64_test, x16_test): encoder.eval() decoder.eval() z = torch.randn(9, 1, 16, 16).to(device) _, z16, _ = X_l_encoder(encoder_model, (torch.FloatTensor(x16_test[111:112])).to(device)) z16 = (torch.ones([9, 1, 4, 4])).to(device) * z16 z16 = upsample(z16, 16) z_h = torch.cat((z16, z), 1) gen_imgs = decoder(z_h) samples = np.squeeze(gen_imgs.data.cpu().numpy()) plot_generation(samples, epoch, output_dir) real_imgs = x64_test[[10, 30, 50, 100]] real_x16 = x16_test[[10, 30, 50, 100]] _, z16, _ = X_l_encoder(encoder_model, (torch.FloatTensor(real_x16)).to(device)) z16 = upsample(z16, 16) real_imgs = (torch.FloatTensor(real_imgs)).to(device) encoded_imgs, _, _ = encoder(real_imgs) z_h = torch.cat((z16, encoded_imgs), 1) decoded_imgs = decoder(z_h) samples_gen = np.squeeze(decoded_imgs.data.cpu().numpy()) samples_real = np.squeeze(real_imgs.data.cpu().numpy()) samples = np.vstack((samples_real, samples_gen)) plot_reconstruction(samples, epoch, output_dir)
def test(epoch,x_test): encoder.eval() decoder.eval() z = torch.randn(9, 1, 4 ,4).to(device) imgs = decoder(z) samples = np.squeeze(imgs.data.cpu().numpy()) plot_generation(samples,epoch,output_dir,1) real_imgs = x_test[[10,30,50,100]] real_imgs = (torch.FloatTensor(real_imgs)).to(device) encoded_imgs,_,_ = encoder(real_imgs) decoded_imgs = decoder(encoded_imgs) samples_gen = np.squeeze(decoded_imgs.data.cpu().numpy()) samples_real = np.squeeze(real_imgs.data.cpu().numpy()) samples = np.vstack((samples_real,samples_gen)) plot_reconstruction(samples,epoch,output_dir)
# Saving reconstruction losses with open('reconst_losses', 'wb') as f: pickle.dump(rec_losses, f) # Plot reconstruction losses plot.plot_reconst_losses(rec_losses, 'reconst_losses.png') # Saving train total losses with open('train_total_losses', 'wb') as f: pickle.dump(train_total_losses, f) # Saving valid total losses with open('valid_total_losses', 'wb') as f: pickle.dump(valid_total_losses, f) # Plot train and valid total losses plot.plot_tv_losses(train_total_losses, valid_total_losses, 'tv_total_losses.png') plot.plot_reconstruction('rec1.png', model, test_loader) plot.plot_reconstruction('rec2.png', model, test_loader) plot.plot_reconstruction('rec3.png', model, test_loader) plot.plot_reconstruction('rec4.png', model, test_loader) plot.plot_reconstruction('rec5.png', model, test_loader) plot.plot_generation('gen1.png', model) plot.plot_generation('gen2.png', model) plot.plot_generation('gen3.png', model) plot.plot_generation('gen4.png', model) plot.plot_generation('gen5.png', model)