-
Notifications
You must be signed in to change notification settings - Fork 172
/
Demo_inpaint.m
152 lines (106 loc) · 4.05 KB
/
Demo_inpaint.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
%==========================================================================
% This is the testing code of IRCNN for image inpainting.
%
% @inproceedings{zhang2017learning,
% title={Learning Deep CNN Denoiser Prior for Image Restoration},
% author={Zhang, Kai and Zuo, Wangmeng and Gu, Shuhang and Zhang, Lei},
% booktitle={IEEE Conference on Computer Vision and Pattern Recognition},
% year={2017}
% }
%
% If you have any question, please feel free to contact with <Kai Zhang (cskaizhang@gmail.com)>.
%
%
% by Kai Zhang (1/2018)
%==========================================================================
clear; clc;
addpath('utilities');
imageSets = {'Inpaint_set1'}; % testing dataset
setTest = imageSets(1); % select the dataset
useGPU = 1;
folderTest = 'testsets';
folderResult = 'results';
folderModel = 'models';
if ~exist(folderResult,'file')
mkdir(folderResult);
end
setTestCur = cell2mat(setTest(1));
disp('--------------------------------------------');
disp(['----',setTestCur,'-----Image Inpainting-----']);
disp('--------------------------------------------');
folderTestCur = fullfile(folderTest,setTestCur);
% folder to store results
folderResultCur = fullfile(folderResult, ['Inpaint_',setTestCur]);
if ~exist(folderResultCur,'file')
mkdir(folderResultCur);
end
%% read ground truth image and generate input {y, mask}
% ground truth image
Iname = 'butterfly_gray'; % Isigma = 0.5/255; Msigma = 1 or 3; window = 7; for 75%
Iname = 'butterfly_color'; % Isigma = 0.5/255; Msigma = 5; window = 7; for 80%
Iname = '09'; % Isigma = 0.5/255; Msigma = 1 or 3; window = 7; for 50%
pert = 0.5; % 80% pixels are missing
window = 7; % default 10, from [5,30]
label = im2single(imread(fullfile(folderTestCur,[Iname,'.png'])));
[a,b,c] = size(label);
% generate mask
rand('seed',0);
mask = rand(a,b)>=pert;
mask = repmat(mask,[1,1,c]);
% generate input
y = label.*mask;
%% parameter setting in HQS (tune the following parameters to obtain the best results)
%% -------------------important!------------------
% Parameter settings of IRCNN
% (1) image noise level: Isigma
Isigma = 0.5/255; % ****** from interval [1/255, 20/255] ******; e.g., 1/255, 2.55/255, 7/255, 11/255
% (2) noise level of the last denoiser: Msigma
Msigma = 3; % ****** from {1 3 5 7 9 11 13 15} ******
%--------------------------------------------------------
%% load denoisers
if c==1
load(fullfile(folderModel,'modelgray.mat'));
elseif c==3
load(fullfile(folderModel,'modelcolor.mat'));
end
%% default parameter setting in HQS
totalIter = 30; % default 30
lamda = (Isigma^2)/3; % default 3, ****** from {1 2 3 4} ******
modelSigma1 = 49; % default 49
modelSigmaS = logspace(log10(modelSigma1),log10(Msigma),totalIter);
rho = Isigma^2/((modelSigma1/255)^2);
ns = min(25,max(ceil(modelSigmaS/2),1));
ns = [ns(1)-1,ns];
z = shepard_initialize(y, mask, window);
if useGPU
z = gpuArray(z);
y = gpuArray(y);
end
for itern = 1:totalIter
% step 1
rho = lamda*255^2/(modelSigmaS(itern)^2);
z = (y+rho*z)./(mask+rho);
if ns(itern+1)~=ns(itern)
[net] = loadmodel(modelSigmaS(itern),CNNdenoiser);
net = vl_simplenn_tidy(net);
if useGPU
net = vl_simplenn_move(net, 'gpu');
end
end
% step 2
res = vl_simplenn(net, z,[],[],'conserveMemory',true,'mode','test');
residual = res(end).x;
z = z - residual;
% imshow(z)
% title(int2str(itern))
% drawnow;
end
if useGPU
output = im2uint8(gather(z));
y = im2uint8(gather(y));
end
[PSNR_Cur,SSIM_Cur] = Cal_PSNRSSIM(label*255,output,0,0);
imshow(cat(2,y,output));
disp([PSNR_Cur,SSIM_Cur]);
imwrite(y,fullfile(folderResultCur,[Iname,'_',int2str(pert*100),'_masked.png']));
imwrite(output,fullfile(folderResultCur,[Iname,'_',int2str(pert*100),'_ircnn.png']));