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




Image denoising with CNN





No account yet ?

Sign up to access all content




Tested in Anaconda and Python 3.7

import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as td
import torchvision as tv
from PIL import Image
import matplotlib.pyplot as plt
import time
 
dataset_root_dir = 'BSDS300/images'
 
class NoisyBSDSDataset(td.Dataset):
 
    def __init__(self, root_dir, mode='train', image_size=(180, 180), sigma=30):
        super(NoisyBSDSDataset, self).__init__()
        self.mode = mode
        self.image_size = image_size
        self.sigma = sigma
        self.images_dir = os.path.join(root_dir, mode)
        self.files = os.listdir(self.images_dir)
 
    def __len__(self):
        return len(self.files)
 
    def __repr__(self):
        return "NoisyBSDSDataset(mode={}, image_size={}, sigma={})". \
            format(self.mode, self.image_size, self.sigma)
 
    def __getitem__(self, idx):
        img_path = os.path.join(self.images_dir, self.files[idx])
        clean = Image.open(img_path).convert('RGB')   
        # random crop
        i = np.random.randint(clean.size[0] - self.image_size[0])
        j = np.random.randint(clean.size[1] - self.image_size[1])
 
        clean = clean.crop([i, j, i+self.image_size[0], j+self.image_size[1]])
        transform = tv.transforms.Compose([
            # convert it to a tensor
            tv.transforms.ToTensor(),
            # normalize it to the range [−1, 1]
            tv.transforms.Normalize((.5, .5, .5), (.5, .5, .5))
            ])
        clean = transform(clean)
 
        noisy = clean + 2 / 255 * self.sigma * torch.randn(clean.shape)
        return noisy, clean
 
def myimshow(image, ax=plt):
    image = image.to('cpu').numpy()
    image = np.moveaxis(image, [0, 1, 2], [2, 0, 1])
    image = (image + 1) / 2
    image[image < 0] = 0
    image[image > 1] = 1
    h = ax.imshow(image)
    ax.axis('off')
    return h
 
train_set = NoisyBSDSDataset(dataset_root_dir)
test_set = NoisyBSDSDataset(dataset_root_dir, mode='test', image_size=(320, 320))
 
input_path = (r'BSDS300/images/test')
 
cpt = 0
 
for root, dirs, files in os.walk(input_path):
    for filename in files:
        x = test_set[cpt]
        fig, axes = plt.subplots(ncols=2)
        myimshow(x[0], ax=axes[0])
        axes[0].set_title('Noisy')
        myimshow(x[1], ax=axes[1])
        axes[1].set_title('Clean')
        # print(f'image size is {x[0].shape}.')
        cpt += 1
 


Image-Denoising-with-Multi-Column-Dilated-CNNs - GitHub



Tensorflow playground

Tensorflow playground

Tensorflow playground

Tensorflow playground

Tensorflow playground

Tensorflow playground

Tensorflow playground

Tensorflow playground

Tensorflow playground




Convolutional Neural Network


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