denoise use resnet in matlab

To denoise an image using ResNet in Matlab, you can follow these steps:

  1. Load the noisy image:
main.m
noisy_img = imread('noisy_image.jpg');
39 chars
2 lines
  1. Create a denoising network using ResNet:
main.m
denoising_layers = [imageInputLayer([size(noisy_img,1) size(noisy_img,2) 3])
                    convolution2dLayer(3,64,'Padding','same')
                    reluLayer()
                    residualBlock(64,3)
                    residualBlock(64,3)
                    residualBlock(64,3)
                    convolution2dLayer(3,3,'Padding','same')
                    regressionLayer()];
denoising_net = trainNetwork(noisy_img, noisy_img, denoising_layers);
462 chars
10 lines

Here, residualBlock(64,3) is a custom layer for ResNet architecture, which can be defined as follows:

main.m
function layer = residualBlock(numFilters, numBlocks)
  
    layers = [
        convolution2dLayer(3,numFilters,'Padding','same')
        batchNormalizationLayer
        reluLayer
        convolution2dLayer(3,numFilters,'Padding','same')
        batchNormalizationLayer
    ];

    layer = layerGraph();
    for i = 1:numBlocks
        layer = addLayers(layer,layers);
        layer = connectLayers(layer,"input","conv1");
        if mod(i,2) == 0
            layer = addLayers(layer,batchNormalizationLayer(['bn' num2str(i)]));
            layer = connectLayers(layer,['conv' num2str(i)],['bn' num2str(i)]);
            layer = addLayers(layer,reluLayer(['relu' num2str(i)]));
            layer = connectLayers(layer,['bn' num2str(i)],['relu' num2str(i)]);
        else
            layer = addLayers(layer,reluLayer(['relu' num2str(i)]));
            layer = connectLayers(layer,['conv' num2str(i)],['relu' num2str(i)]);
        end
    end
    layer = addLayers(layer,depthConcatenationLayer(numBlocks));
    for i = 1:numBlocks
        layer = connectLayers(layer,['relu' num2str(i)],['depthcat/in' num2str(i)]);
    end
end

1129 chars
31 lines
  1. Denoise the image using the trained network:
main.m
denoised_img = predict(denoising_net, noisy_img);
50 chars
2 lines

Now, you should have the denoised image in denoised_img variable.

gistlibby LogSnag