def show_images(images, title=None, path=None): images = utils.make_grid(images) show_image(images[0], title, path)
reconstruction = model(images) # print(test_set_reconstruction.shape) recon_image_ = reconstruction.view(reconstruction.shape[0], 1, 28, 28) images = images.view(images.shape[0], 1, 28, 28) if r % 100 == 0: # show_images(images, 'original') # show_images(recon_image_, 'test_set_reconstruction') grid1 = torchvision.utils.make_grid(images) writer.add_image('orig images', grid1, 0) grid2 = torchvision.utils.make_grid(recon_image_) writer.add_image('recon images', grid2) writer.close() ## maybe we just store the test_set_reconstruction ## maybe we just store the test_set_reconstruction images = utils.make_grid(images) recon_image_ = utils.make_grid(recon_image_) plt.imshow(images[0], cmap='gray') plt.title('Original from epoch {}'.format(epoch + 1)) plt.savefig( 'reconstruction_during_training/originals_epoch_{}_example_{}' .format(epoch + 1, r)) plt.imshow(recon_image_[0], cmap='gray') plt.title('Reconstruction from epoch {}'.format(epoch + 1)) plt.savefig( 'reconstruction_during_training/reconstruction_epoch_{}_example_{}' .format(epoch + 1, r)) model.eval() ## we want also to sample something from the model during training rendom_samples = model.sample(N_SAMPLE)
test_loss_all, test_loss_localization, test_loss_cover = \ localization_loss(pred_label, cropout_label, test_hidden, test_cover, beta=1) # diff_S, diff_C = np.abs(np.array(test_output.data[0]) - np.array(test_secret.data[0])), np.abs(np.array(test_hidden.data[0]) - np.array(test_cover.data[0])) # print (diff_S, diff_C) if idx in [1, 2, 3, 4]: print('Training: Batch {0}/{1}. Total Loss {2:.4f}, Localization Loss {3:.4f}, Cover Loss {3:.4f} '.format( idx + 1, len(train_loader), test_loss_all.data, test_loss_localization.data, test_loss_cover.data)) # Creates img tensor # imgs = [test_secret.data, test_cover.data, test_hidden.data, test_output.data] # 隐藏图像 宿主图像 输出图像 提取得到的图像 imgs = [test_cover.data, test_hidden.data] imgs_tsor = torch.cat(imgs, 0) # Prints Images imshow(utils.make_grid(imgs_tsor), idx + 1, learning_rate=learning_rate, beta=beta) # target_tensor = torch.tensor((pred_label.reshape(1,14,14).detach().cpu().numpy()*255).astype(np.uint8)).to(device) # imshow(target_tensor, idx+1, learning_rate=learning_rate, beta=beta) print('Expected:') print(pred_label.data) print('Real:') print(cropout_label.data) test_losses.append(test_loss_all.data.cpu().numpy()) mean_test_loss = np.mean(test_losses) print('Average loss on test set: {:.2f}'.format(mean_test_loss))
test_hidden, pred_label, cropout_label = net(test_secret, test_cover) # MSE标签距离 loss test_loss_all, test_loss_localization, test_loss_cover = \ localization_loss(pred_label, cropout_label, test_hidden, test_cover, beta=1) # diff_S, diff_C = np.abs(np.array(test_output.data[0]) - np.array(test_secret.data[0])), np.abs(np.array(test_hidden.data[0]) - np.array(test_cover.data[0])) # print (diff_S, diff_C) if idx in [1, 2, 3, 4]: print( 'Training: Batch {0}/{1}. Total Loss {2:.4f}, Localization Loss {3:.4f}, Cover Loss {3:.4f} ' .format(idx + 1, len(train_loader), test_loss_all.data, test_loss_localization.data, test_loss_cover.data)) # Creates img tensor # imgs = [test_secret.data, test_cover.data, test_hidden.data, test_output.data] # 隐藏图像 宿主图像 输出图像 提取得到的图像 imgs = [test_secret, test_hidden, pred_label] imgs_tsor = torch.cat(imgs, 0) # Prints Images imshow(utils.make_grid(imgs_tsor), idx + 1, learning_rate=learning_rate, beta=beta) test_losses.append(test_loss_all.data.cpu().numpy()) mean_test_loss = np.mean(test_losses) print('Average loss on test set: {:.2f}'.format(mean_test_loss))