Main Content

Code Generation for Denoising Deep Neural Network

This example shows how to generate plain CUDA® MEX from MATLAB® code and denoise grayscale images by using the denoising convolutional neural network (DnCNN [1]). The pretrained denoising network estimates the noise in a noisy image and then removes it, resulting in a clearer, denoised image.

Third-Party Prerequisites

This example generates CUDA MEX and requires CUDA® enabled NVIDIA® GPU and compatible driver.

Verify GPU Environment

Use the coder.checkGpuInstall function to verify that the compilers and libraries necessary for running this example are set up correctly.

envCfg = coder.gpuEnvConfig('host');
envCfg.DeepLibTarget = 'none';
envCfg.DeepCodegen = 1;
envCfg.Quiet = 1;
coder.checkGpuInstall(envCfg);

Load Noisy Image

Load a noisy grayscale image into the workspace and display the image.

noisyI = imread('noisy_cameraman.png');
imshow(noisyI);
title('Noisy Image');

Figure contains an axes object. The hidden axes object with title Noisy Image contains an object of type image.

Get Pretrained Denoising Network

Call the getDenoisingNetwork helper function to get a pretrained image denoising deep neural network.

net = getDenoisingNetwork;

The getDenoisingNetwork function returns a pretrained dlnetwork object. The pretrained denoising deep neural network detects additive white Gaussian noise (AWGN) of unknown levels. The network is a feed-forward denoising convolutional network that implements a residual learning technique to predict a residual image. In other words, the pretrained network computes the difference between a noisy image and the latent clean image.

The network contains 58 layers. To display an interactive visualization of the deep learning network architecture, use the analyzeNetwork (Deep Learning Toolbox) function.

analyzeNetwork(net);

The denoisenet_predict Function

The denoisenet_predict entry-point function takes a noisy image input and returns a denoised image by using a pretrained denoising network.

The function loads the dlnetwork object returned by getDenoisingNetwork into a persistent variable dlnet and reuses the persistent object on subsequent prediction calls. The predict method returns an estimate of the noise in the input image by using the pretrained denoising image. Then a denoised image is obtained by subtracting the noise from the original image.

type denoisenet_predict
function I = denoisenet_predict(in)
%#codegen
% Copyright 2018-2024 The MathWorks, Inc.

persistent dlnet;


if isempty(dlnet)   
    dlnet = coder.loadDeepLearningNetwork('getDenoisingNetwork', 'DnCNN');
end

% Use the network to predict the noise in the input image
dlIn = dlarray(in, 'SSC');
res = extractdata(predict(dlnet, dlIn));


% Once the noise is estimated, we subtract the noise from the original
% image to obtain a denoised image.

I = in - res;
  

Run MEX Code Generation

To generate CUDA code for the denoisenet_predict.m entry-point function, create a GPU code configuration object for a MEX target and set the target language to C++. Use the coder.DeepLearningConfig function to create a deep learning configuration object and set target library to none. Assign the deep learning configuration object to the DeepLearningConfig property of cfg. Run the codegen command specifying an input size of 256-by-256. This value corresponds to the size of the noisy image that you intend to denoise.

cfg = coder.gpuConfig('mex');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('none');
codegen -config cfg denoisenet_predict -args {ones(256,256,'single')} -report
Code generation successful: View report

Run Generated MEX

The denoising network is trained on input images having an input range [0,1]. Use the im2single (Image Processing Toolbox) function on noisyI to rescale the values from [0,255] to [0,1]. After rescaling, use denoisenet_predict_predict to obtain the denoised image denoisedI.

denoisedI = denoisenet_predict_mex(im2single(noisyI));

View Denoised Image

Display the original noisy image and the denoised image side by side.

imshowpair(noisyI,denoisedI,'montage');
title('Noisy Image (left) and Denoised Image (right)');

Figure contains an axes object. The hidden axes object with title Noisy Image (left) and Denoised Image (right) contains an object of type image.

References

[1] Zhang, K., W. Zuo, Y. Chen, D. Meng, and L. Zhang. "Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising." IEEE Transactions on Image Processing. Vol. 26, Number 7, Feb. 2017, pp. 3142-3155.

See Also

Functions

Objects

Related Topics