We can now denoise using the denoising autoencoder in the following steps:
- Define the to_img() and plot_sample_img() functions to convert a PyTorch tensor to an image from the lfw dataset (each grayscale image has a size of 50 x 37) and to save the image to disk, respectively:
def to_img(x): x = x.view(x.size(0), 1, 50, 37) return xdef plot_sample_img(img, name): img = img.view(1, 50, 37) save_image(img, './sample_{}.png'.format(name))
- Define the add_noise() function to add random Gaussian noise to an image:
def add_noise(img): noise = torch.randn(img.size()) * 0.2 noisy_img = img + noise return noisy_img
- Implement the min_max_normalization() and tensor_round() preprocessing functions to normalize and round a tensor, ...