-
Notifications
You must be signed in to change notification settings - Fork 539
/
Demo_FDnCNN_Gray_Clip.m
110 lines (86 loc) · 3.53 KB
/
Demo_FDnCNN_Gray_Clip.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
% This is the testing demo of Flexible DnCNN (FDnCNN) for denoising noisy grayscale images corrupted by
% AWGN with clipping setting. The noisy input is 8-bit quantized.
%
% "Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising"
% "FFDNet: Toward a Fast and Flexible Solution for CNN based Image Denoising"
%
% 2018/05
% If you have any question, please feel free to contact with me.
% Kai Zhang (e-mail: [email protected])
%clear; clc;
format compact;
global sigmas; % input noise level or input noise level map
addpath(fullfile('utilities'));
folderModel = 'models';
folderTest = 'testsets';
folderResult= 'results';
imageSets = {'BSD68','Set12'}; % testing datasets
setTestCur = imageSets{2}; % current testing dataset
showResult = 1;
useGPU = 1; % CPU or GPU. For single-threaded (ST) CPU computation, use "matlab -singleCompThread" to start matlab.
pauseTime = 0;
imageNoiseSigma = 50; % image noise level, 25.5 is the default setting of imnoise( ,'gaussian')
inputNoiseSigma = 50; % input noise level
folderResultCur = fullfile(folderResult, [setTestCur,'_Clip_',num2str(imageNoiseSigma),'_',num2str(inputNoiseSigma)]);
if ~isdir(folderResultCur)
mkdir(folderResultCur)
end
load(fullfile('model','FDnCNN_Clip_gray.mat'));
net = vl_simplenn_tidy(net);
% for i = 1:size(net.layers,2)
% net.layers{i}.precious = 1;
% end
if useGPU
net = vl_simplenn_move(net, 'gpu') ;
end
% read images
ext = {'*.jpg','*.png','*.bmp'};
filePaths = [];
for i = 1 : length(ext)
filePaths = cat(1,filePaths, dir(fullfile(folderTest,setTestCur,ext{i})));
end
% PSNR and SSIM
PSNRs = zeros(1,length(filePaths));
SSIMs = zeros(1,length(filePaths));
for i = 1:length(filePaths)
% read images
label = imread(fullfile(folderTest,setTestCur,filePaths(i).name));
[w,h,~]=size(label);
if size(label,3)==3
label = rgb2gray(label);
end
[~,nameCur,extCur] = fileparts(filePaths(i).name);
label = im2single(label);
% add noise
randn('seed',0);
%input = imnoise(label,'gaussian'); % corresponds to imageNoiseSigma = 25.5;
input = imnoise(label,'gaussian',0,(imageNoiseSigma/255)^2);
% tic;
if useGPU
input = gpuArray(input);
end
% set noise level map
sigmas = inputNoiseSigma/255; % see "vl_simplenn.m".
% denoising
res = vl_simplenn(net,input,[],[],'conserveMemory',true,'mode','test');
output = res(end).x;
if useGPU
output = gather(output);
input = gather(input);
end
% toc;
% calculate PSNR, SSIM and save results
[PSNRCur, SSIMCur] = Cal_PSNRSSIM(im2uint8(label),im2uint8(output),0,0);
if showResult
imshow(cat(2,im2uint8(input),im2uint8(label),im2uint8(output)));
title([filePaths(i).name,' ',num2str(PSNRCur,'%2.2f'),'dB',' ',num2str(SSIMCur,'%2.4f')])
%imwrite(im2uint8(output), fullfile(folderResultCur, [nameCur, '_' num2str(imageNoiseSigma,'%02d'),'_' num2str(inputNoiseSigma,'%02d'),'_PSNR_',num2str(PSNRCur*100,'%4.0f'), extCur] ));
%imwrite(im2uint8(input), fullfile(folderResultCur, [nameCur, '_' num2str(imageNoiseSigma,'%02d'),'_' num2str(inputNoiseSigma,'%02d'), extCur] ));
drawnow;
pause(pauseTime)
end
disp([filePaths(i).name,' ',num2str(PSNRCur,'%2.2f'),'dB',' ',num2str(SSIMCur,'%2.4f')])
PSNRs(i) = PSNRCur;
SSIMs(i) = SSIMCur;
end
disp([mean(PSNRs),mean(SSIMs)]);