How to do it...

We can now denoise using the denoising autoencoder in the following steps:

  1. 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))
  1. 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
  1. Implement the min_max_normalization() and tensor_round() preprocessing functions to normalize and round a tensor, ...

Get Python Image Processing Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.