Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




Denoising autoencoders





No account yet ?

Sign up to access all content




The denoising autoencoder recovers denoised images from the noisy input images.

It makes use of the fact that higher-level feature representations of the image are relatively stable and robust to input corruption.

During learning, the aim is to reduce the loss of regression between the pixels of the original non-noisy images and those of the denoised images produced by the auto-encoder.



Free image provided by pexel.com





Source : https://iq.opengenus.org/autoencoder/



Tested in Anaconda and Python 3.7

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
 
data = input_data.read_data_sets("./mnist/", one_hot=True)
 
# Print shapes of data
print("Training X: ", data.train.images.shape)
print("Training Y: ", data.train.labels.shape)
print("Test X: ", data.test.images.shape)
print("Test Y: ", data.test.labels.shape)
 
def gaussian_additive_noise(x, std):
    return x + tf.random_normal(shape=tf.shape(x), dtype=tf.float32, mean=0.0, stddev=std)
 
imgs = tf.placeholder(tf.float32, shape=[None, 28*28], name="Input")
 
noise = gaussian_additive_noise(imgs, 0.1)
corrupted_imgs_test = noise.eval(session=tf.Session(), feed_dict={imgs: data.test.images})
 
def plot_mnist(imgs, lbls):
    classes = np.argmax(lbls, 1)
    for i in range(10):
        ids = (classes == i)
        images = imgs[ids][0:10]
        for j in range(3):   
            plt.subplot(5, 10, i + j*10 + 1)
            plt.imshow(images[j].reshape(28, 28), cmap='gray')
            if j == 0:
                plt.title(i)
            plt.axis('off')
    plt.show()
 
def autoencoder(dims=[28*28, 512, 256, 128, 64, 32], std=0.01):
    x = tf.placeholder(tf.float32, shape=[None, dims[0]], name="Input")
    cur = gaussian_additive_noise(x, 0.1)
    Ws = []
    bs = []
    # encoder
    for i, n_out in enumerate(dims[1:]):
        n_inp = int(cur.get_shape()[1])
        W = tf.Variable(tf.random_normal(shape=[n_inp, n_out], mean=0.0, stddev=std, dtype=tf.float32))
        b = tf.Variable(tf.random_normal(shape=[n_out], mean=0.0, stddev=std, dtype=tf.float32))
        Ws.append(W)
        bs.append(b)
        out = tf.nn.tanh(cur @ W + b)
        cur = out
    z = cur
    Ws.reverse()
    bs.reverse()
    # decoder
    for i, n_out in enumerate(dims[:-1][::-1]):
        W = tf.transpose(Ws[i])
        b = tf.Variable(tf.random_normal(shape=[n_out], mean=0.0, stddev=std, dtype=tf.float32))
        out = tf.nn.tanh(cur @ W + b)
        cur = out
    y = cur
    loss = tf.reduce_mean(tf.square(y - x))
    return (x, z, y, loss)
 
lr = 0.001
batch_size = 64
n_epochs = 50
n_batchs = data.train.num_examples // batch_size
 
x, z, y, loss = autoencoder(dims=[28*28, 512, 256, 64], std=0.01)
optimizer = tf.train.AdamOptimizer(lr).minimize(loss)
 
S = tf.Session()
S.run(tf.global_variables_initializer())
 
for i_epoch in range(1, n_epochs+1):
    loss_avg = 0.0
    for i_batch in range(1, n_batchs+1):
        b, _ = data.train.next_batch(batch_size)
        _, loss_val = S.run([optimizer, loss], feed_dict={x: b})
        loss_avg = (loss_val / batch_size)
    print(i_epoch, loss_avg)
    loss_avg = 0.0
 
n_samples = 10
reconstructed = S.run([y], feed_dict={x: corrupted_imgs_test})
 
reconstructed = reconstructed[0]
 
print("\t\t Original Images")
plot_mnist(data.test.images, data.test.labels)
print("\t\t Corrupted Images")
plot_mnist(corrupted_imgs_test, data.test.labels)
print("\t\t Reconstructed Images")
plot_mnist(reconstructed, data.test.labels)
 

GitHub



1 0.0006565095973201096
2 0.00045537366531789303
3 0.0003761366824619472
4 0.0002928035974036902
5 0.00032174811349250376
6 0.0002502257702872157
7 0.0002597917919047177
8 0.00020498571393545717
9 0.00021150620887055993
10 0.00019171871826983988
11 0.0001849753753049299
12 0.00018133854609914124
13 0.00017549424956087023
14 0.00019229580357205123
15 0.00016984686953946948
16 0.00019006276852451265
17 0.00017804895469453186
18 0.00017640826990827918
19 0.00018708533025346696
20 0.00016665128350723535
21 0.00018265729886479676
22 0.00018318100774195045
23 0.00016922973736654967
24 0.00018003385048359632
25 0.0001619419635972008
26 0.00016326784680131823
27 0.00017064779240172356
28 0.00016627645527478307
29 0.0001589412277098745
30 0.0001638741377973929
31 0.0001635970693314448
32 0.00015667248226236552
33 0.00016474079166073352
34 0.00016038685862440616
35 0.00016990529547911137
36 0.0001556825591251254
37 0.00014981665299274027
38 0.00015005131717771292
39 0.00015416524547617882
40 0.00015204529336187989
41 0.00015972289838828146
42 0.0001641279086470604
43 0.00016873727145139128
44 0.00015184780932031572
45 0.0001458231417927891
46 0.0001415067963534966
47 0.00015890247595962137
48 0.00016173098993021995
49 0.00014274028944782913
50 0.0001381593756377697



Original image

Free image provided by pexel.com


Corrupted Image

Free image provided by pexel.com


Reconstructed image

Free image provided by pexel.com




Implementing a deep convolution autoencoder for image denoising.



Tested in Anaconda and Python 3.7

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
 
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Model
 
 
def preprocess(array):
    """
    Normalizes the supplied array and reshapes it into the appropriate format.
    """
 
    array = array.astype("float32") / 255.0
    array = np.reshape(array, (len(array), 28, 28, 1))
    return array
 
 
def noise(array):
    """
    Adds random noise to each image in the supplied array.
    """
 
    noise_factor = 0.4
    noisy_array = array + noise_factor * np.random.normal(
        loc=0.0, scale=1.0, size=array.shape
    )
 
    return np.clip(noisy_array, 0.0, 1.0)
 
 
def display(array1, array2):
    """
    Displays ten random images from each one of the supplied arrays.
    """
 
    n = 10
 
    indices = np.random.randint(len(array1), size=n)
    images1 = array1[indices, :]
    images2 = array2[indices, :]
 
    plt.figure(figsize=(20, 4))
    for i, (image1, image2) in enumerate(zip(images1, images2)):
        ax = plt.subplot(2, n, i + 1)
        plt.imshow(image1.reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
 
        ax = plt.subplot(2, n, i + 1 + n)
        plt.imshow(image2.reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
 
    plt.show()
 
#Prepare the data
 
# Since we only need images from the dataset to encode and decode, we
# won't use the labels.
(train_data, _), (test_data, _) = mnist.load_data()
 
# Normalize and reshape the data
train_data = preprocess(train_data)
test_data = preprocess(test_data)
 
# Create a copy of the data with added noise
noisy_train_data = noise(train_data)
noisy_test_data = noise(test_data)
 
# Display the train data and a version of it with added noise
display(train_data, noisy_train_data)
 
#Build the autoencoder
 
input = layers.Input(shape=(28, 28, 1))
 
# Encoder
x = layers.Conv2D(32, (3, 3), activation="relu", padding="same")(input)
x = layers.MaxPooling2D((2, 2), padding="same")(x)
x = layers.Conv2D(32, (3, 3), activation="relu", padding="same")(x)
x = layers.MaxPooling2D((2, 2), padding="same")(x)
 
# Decoder
x = layers.Conv2DTranspose(32, (3, 3), strides=2, activation="relu", padding="same")(x)
x = layers.Conv2DTranspose(32, (3, 3), strides=2, activation="relu", padding="same")(x)
x = layers.Conv2D(1, (3, 3), activation="sigmoid", padding="same")(x)
 
# Autoencoder
autoencoder = Model(input, x)
autoencoder.compile(optimizer="adam", loss="binary_crossentropy")
autoencoder.summary()
 
autoencoder.fit(
    x=train_data,
    y=train_data,
    epochs=50,
    batch_size=128,
    shuffle=True,
    validation_data=(test_data, test_data),
)
 
predictions = autoencoder.predict(test_data)
display(test_data, predictions)
 
autoencoder.fit(
    x=noisy_train_data,
    y=train_data,
    epochs=100,
    batch_size=128,
    shuffle=True,
    validation_data=(noisy_test_data, test_data),
)
 
predictions = autoencoder.predict(noisy_test_data)
display(noisy_test_data, predictions)
 


Source : https://keras.io/examples/vision/autoencoder/



Free image provided by pexel.com


Free image provided by pexel.com


Free image provided by pexel.com




Image Denoising Autoencoder



Tested in Anaconda and Python 3.7

import torch
from torchvision import datasets
from torchvision import transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
 
tensor_transform = transforms.ToTensor()
 
dataset = datasets.MNIST(root = "./data",
                         train = True,
                         download = True,
                         transform = tensor_transform)
 
train_loader = torch.utils.data.DataLoader(dataset = dataset,
                                     batch_size = 100,
                                     shuffle = True)
dataset2 = datasets.MNIST(root = "./data",
                         train = False,
                         download = True,
                         transform = tensor_transform)
test_loader = torch.utils.data.DataLoader(dataset = dataset2,
                                     batch_size = 100,
                                     shuffle = True)
 
dataiter = iter(train_loader)
images,labels = dataiter.next()
print(torch.min(images),torch.max(images))
 
class Autoencoder(nn.Module):
  def __init__(self):
    super().__init__()
    self.encoder= nn.Sequential(
        nn.Conv2d(1,16,3,stride=2,padding=1), #[(inputsize+2*padding-filter_size)/stride] + 1
        nn.ReLU(),
        nn.Conv2d(16,32,3,stride=2,padding=1),
        nn.ReLU(),
        nn.Conv2d(32,64,5),
        nn.ReLU()        
    )
    self.decoder=nn.Sequential(
        nn.ConvTranspose2d(64,32,5),
        nn.ReLU(),
        nn.ConvTranspose2d(32,16,3,stride=2,padding=1,output_padding=1),
        nn.ReLU(),
        nn.ConvTranspose2d(16,1,3,stride=2,padding=1,output_padding=1), #(inputsize-1)*stride + kernal_size + output_padding - 2*padding
        nn.Sigmoid()
 
    )
  def forward(self,x):
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return decoded
 
model = Autoencoder()
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(),lr=0.001)
 
def add_noise(img):
  noise_factor = 0.5
  noise_img = img + torch.randn_like(img)*noise_factor
  noise_img = torch.clip(noise_img,0.,1.)
  return noise_img
 
losses = []
l = len(train_loader)
running_loss =0 
for epoch in range(5):
  for (img,_) in train_loader:
    noisy_img = add_noise(img)
    reconstruction = model(noisy_img)
    loss = loss_function(reconstruction,img)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    running_loss += loss.item()
  losses.append(running_loss/l)
  print(f"Epoch : {epoch+1}, loss : {losses[epoch]} ")
  running_loss=0
plt.ylabel("Loss")
plt.xlabel("Epoch")
plt.plot(losses)
 
outputs = {}
img, _ = list(test_loader)[-3] 
out = model(img)
outputs["original_img"] = img
outputs['img'] = add_noise(img)
outputs['out'] = out
 
counter = 1
print("Original Images")
for j in range(6):
  val= outputs['original_img']
  plt.subplot(1,6,counter)
  plt.imshow(val[j].reshape(28,28),cmap='gray')
  counter += 1
plt.show()
print("Noisy Images")
for i in range(6):
  val = outputs['img']
  plt.subplot(1, 6, i+1)
 
 
  plt.imshow(val[i].reshape(28, 28), cmap='gray')
  counter += 1
plt.show()
val = outputs['out'].detach().numpy()
print("Reconstructed Images")
for i in range(6):
  plt.subplot(1, 6, i+1)
  plt.imshow(val[i].reshape(28, 28), cmap='gray')
  counter += 1
plt.show()
 


Image_Denoising_Autoencoder - GitHub



Original image

Free image provided by pexel.com

Noisy Images

Free image provided by pexel.com

Reconstructed Image

Free image provided by pexel.com




Image Denoising Autoencoder CNN



Tested in Anaconda and Python 3.7

import torch
from torchvision import datasets
from torchvision import transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torchvision
 
transform = transforms.ToTensor()
 
dataset = datasets.FashionMNIST(root = "./data",
                         train = True,
                         download = True,
                         transform = transform)
 
train_loader = torch.utils.data.DataLoader(dataset = dataset,
                                     batch_size = 100,
                                     shuffle = True)
dataset2 = datasets.FashionMNIST(root = "./data",
                         train = False,
                         download = True,
                         transform = transform)
test_loader = torch.utils.data.DataLoader(dataset = dataset2,
                                     batch_size = 100,
                                     shuffle = True)
 
def imshow(img):
  img = img/2 + 0.5
  npimg = img.numpy()
  plt.imshow(np.transpose(npimg,(1,2,0)))
  plt.show()
dataiter = iter(train_loader)
images,labels = dataiter.next()
 
imshow(torchvision.utils.make_grid(images))
 
dataiter = iter(train_loader)
images,labels = dataiter.next()
print(torch.min(images),torch.max(images))
 
class Autoencoder(nn.Module):
  def __init__(self):
    super().__init__()
    self.encoder= nn.Sequential(
        nn.Conv2d(1,16,3,stride=2,padding=1),
        nn.ReLU(),
        nn.Conv2d(16,32,3,stride=2,padding=1),
        nn.ReLU(),
        nn.Conv2d(32,64,5),
        nn.ReLU()        
    )
    self.decoder=nn.Sequential(
        nn.ConvTranspose2d(64,32,5),
        nn.ReLU(),
        nn.ConvTranspose2d(32,16,3,stride=2,padding=1,output_padding=1),
        nn.ReLU(),
        nn.ConvTranspose2d(16,1,3,stride=2,padding=1,output_padding=1),
        nn.Sigmoid()
 
    )
  def forward(self,x):
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return decoded
 
model = Autoencoder()
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(),lr=0.001,weight_decay=1e-5)
 
def add_noise(img):
  noise_factor = 0.3
  noise_img = img + torch.randn_like(img)*noise_factor
  noise_img = torch.clip(noise_img,0.,1.)
  return noise_img
 
running_loss = 0
losses = []
l = len(train_loader)
for epoch in range(5):
  for (img,_) in train_loader:
    noisy_img = add_noise(img)
    reconstruction = model(noisy_img)
    loss = loss_function(reconstruction,img)
    optimizer.zero_grad()
    loss.backward()
    running_loss += loss.item()
    optimizer.step()
  losses.append(running_loss/l)
  print(f"Epoch : {epoch+1}, loss : {losses[epoch]:.5f} ")
  running_loss = 0
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.plot(losses)
 
outputs = {}
img, _ = list(test_loader)[-3] 
out = model(img)
plt.figure(figsize=(14, 4))
outputs['img'] = add_noise(img)
outputs['out'] = out
outputs['Original_img'] = img
for i in range(6):
	val = outputs['Original_img']
	plt.subplot(1,6,i+1)
	plt.title("Original")
	plt.imshow(val[i].reshape(28,28),cmap='gray')
plt.show()
plt.figure(figsize=(14, 4))
counter = 1
for i in range(6):
	val = outputs['img']
	plt.subplot(1, 6, i+1)
	plt.title("Noisy")
	plt.imshow(val[i].reshape(28, 28), cmap='gray')
	counter += 1
plt.show()
plt.figure(figsize=(14, 4))
val = outputs['out'].detach().numpy()
for i in range(6):
	plt.subplot(1, 6, i+1)
	plt.title("Reconstructed")
	plt.imshow(val[i].reshape(28, 28), cmap='gray')
	# plt.figure(figsize=(18, 5))
	counter += 1
 
plt.show()
 


Image_Denoising_Autoencoder - GitHub



Free image provided by pexel.com

Free image provided by pexel.com

Free image provided by pexel.com

Free image provided by pexel.com

Free image provided by pexel.com




Linear autoencoder

Convolutional autoencoder

Bilinear Upsampling

Bilinear Downsampling


Deep learning

Machine learning












Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !

Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License