Source code for model.architectures.pytorch.dncnn

# Copyright or © or Copr. IETR/INSA Rennes (2019)
# 
# Contributors :
#     Eduardo Fernandes-Montesuma eduardo.fernandes-montesuma@insa-rennes.fr (2019)
#     Florian Lemarchand florian.lemarchand@insa-rennes.fr (2019)
# 
# 
# OpenDenoising is a computer program whose purpose is to benchmark image
# restoration algorithms.
# 
# This software is governed by the CeCILL-C license under French law and
# abiding by the rules of distribution of free software. You can  use,
# modify and/ or redistribute the software under the terms of the CeCILL-C
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
# 
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty  and the software's author, the holder of the
# economic rights, and the successive licensors have only  limited
# liability.
# 
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean  that it is complicated to manipulate,  and  that  also
# therefore means  that it is reserved for developers  and  experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
# 
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-C license and that you accept its terms.


import torch
import torch.nn as nn


[docs]class DnCNN(nn.Module):
[docs] def __init__(self, depth=17, n_filters=64, kernel_size=3, n_channels=1): """Pytorch implementation of DnCNN. Implementation followed the original paper [1]_. Authors original code can be found on `their Github Page <https://github.com/cszn/DnCNN/>`_. Notes ----- This implementation is based on the following `Github page <https://github.com/SaoYan/DnCNN-PyTorch>`_. Parameters ---------- depth : int Number of fully convolutional layers in dncnn. In the original paper, the authors have used depth=17 for non- blind denoising and depth=20 for blind denoising. n_filters : int Number of filters on each convolutional layer. kernel_size : int tuple 2D Tuple specifying the size of the kernel window used to compute activations. n_channels : int Number of image channels that the network processes (1 for grayscale, 3 for RGB) References ---------- .. [1] Zhang K, Zuo W, Chen Y, Meng D, Zhang L. Beyond a gaussian denoiser: Residual learning of deep cnn for image denoising. IEEE Transactions on Image Processing. 2017 Example ------- >>> from OpenDenoising.model.architectures.pytorch import DnCNN >>> dncnn_s = DnCNN(depth=17) >>> dncnn_b = DnCNN(depth=20) """ super(DnCNN, self).__init__() layers = [ nn.Conv2d(in_channels=n_channels, out_channels=n_filters, kernel_size=kernel_size, padding=1, bias=False), nn.ReLU(inplace=True) ] for _ in range(depth-2): layers.append(nn.Conv2d(in_channels=n_filters, out_channels=n_filters, kernel_size=kernel_size, padding=1, bias=False)) layers.append(nn.BatchNorm2d(n_filters)) layers.append(nn.ReLU(inplace=True)) layers.append(nn.Conv2d(in_channels=n_filters, out_channels=n_channels, kernel_size=kernel_size, padding=1, bias=False)) self.dncnn = nn.Sequential(*layers)
[docs] def forward(self, x): out = self.dncnn(x) return out