-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test_SDCNN_epoch.m
381 lines (323 loc) · 12.7 KB
/
main_test_SDCNN_epoch.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
% -------------------------------------------------------------------------
% Description:
% Script to evaluate DsCNN on benchmark datasets
%
% Citation:
% Deep Laplacian Pyramid Networks for Fast and Accurate Super-Resolution
% Wei-Sheng Lai, Jia-Bin Huang, Narendra Ahuja, and Ming-Hsuan Yang
% IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2017
%
% Contact:
% Wei-Sheng Lai
% wlai24@ucmerced.edu
% University of California, Merced
% -------------------------------------------------------------------------
clc;close all;clear all;
addpath('utils');
%% testing options
%model_scale = 4; % model upsampling scale
%depth = 2;
gpu = 0; % GPU ID, gpu = 0 for CPU mode
epochNum = 100; % the epoch number needed to be test
dataset = 'AISD-Test51';
% dataset = 'HighRes-Test';
% dataset = 'WHU-RSSD-Train450';
% dataset = 'UCF-OIRDS74';
% dataset = 'UCF-OIRDS-Test10';
%dataset = 'UCF-OIRDS-Train64';
%dataset = 'Test20_ISTD_Adjust';
%dataset = 'Test50_ISTD_Adjust';
% dataset = 'UCF-355';
%dataset = 'UIUC-76';
%dataset = 'Test540_ISTD_Adjust';
%dataset = 'Train100_ISTD_Adjust';
compute_ifc = 0; % IFC calculation is slow, enable when needed
% tic;
%% load model
% initialize opts
opts = init_SDCNN_opts;
% setup model name
for i = 1:length(opts.train_dataset)
opts.data_name = sprintf('%s',opts.train_dataset{i});
end
opts.net_name = sprintf('SDCNN_%s', opts.loss);
% opts.model_name = sprintf('%s_%s_pw%d_lr%s_step%d_drop%s_min%s_bs%d_depth_%d_level1_relu_improve_noselect_res_BN_U_BRC_crop_fuse_Adjust_step_pool', ...
% opts.net_name, ...
% opts.data_name, opts.patch_size, ...
% num2str(opts.lr), opts.lr_step, ...
% num2str(opts.lr_drop), num2str(opts.lr_min), ...
% opts.batch_size,opts.depth);
opts.model_name = sprintf('%s_%s', ...
opts.net_name, ...
opts.data_name);
% setup paths
input_dir = fullfile('datasets', dataset);
output_dir = fullfile('results', dataset, sprintf('%s', opts.model_name));
avg_Total_epoch_deconv = zeros(epochNum,1);
avg_F_epoch_deconv = zeros(epochNum,1);
avg_BER_epoch_deconv = zeros(epochNum,1);
%% 计算每个epoch训练得到模型的测试精度
for k = 100:epochNum
model_filename = fullfile('models', sprintf('%s', opts.model_name),sprintf('net-epoch-%d.mat', k));
fprintf('Load %s\n', model_filename);
net = load(model_filename);
net = dagnn.DagNN.loadobj(net.net);
net.addLayer('Softmax', dagnn.SoftMax(), {'level4_prediction'}, {'level4_prediction_softmax'});
net.mode = 'test' ;
% output_var = 'level4_prediction';
% output_index = net.getVarIndex(output_var);
% net.vars(output_index).precious = 1;
if( gpu ~= 0 )
gpuDevice(gpu)
net.move('gpu');
end
%% setup paths
output_dir_epoch = fullfile('results', dataset, ...
sprintf('%s', opts.model_name),sprintf('%s', 'epoch',num2str(k)));
output_BW_dir_epoch = fullfile('results', dataset, ...
sprintf('%s', opts.model_name),sprintf('%s', 'epoch',num2str(k)),'BW');
if( ~exist(output_dir_epoch, 'dir') )
mkdir(output_dir_epoch);
end
if( ~exist(output_BW_dir_epoch, 'dir') )
mkdir(output_BW_dir_epoch);
end
addpath(genpath('utils'));
%% load image
% WHU-rssd
folderTest_s = fullfile('..\datasets',dataset,'shadow');
% folderTest_m = fullfile('datasets',dataset,'mask_Rename_cut');
folderTest_m = fullfile('..\datasets',dataset,'mask');
% folderTest_m = fullfile('..\datasets',dataset,'mask_Rename');
% folderTest_s = fullfile('datasets',dataset,'shadow_cut'); %%% test dataset
% folderTest_m = fullfile('datasets',dataset,'maskAdjust_cut'); %%% test dataset
%%% read images
ext = {'*.jpg','*.png','*.bmp','*.tif'};
filePaths_s = [];
filePaths_m = [];
for i = 1 : length(ext)
filePaths_s = cat(1,filePaths_s, dir(fullfile(folderTest_s,ext{i})));
filePaths_m = cat(1,filePaths_m, dir(fullfile(folderTest_m,ext{i})));
end
num_img = length(filePaths_m);
%% testing
Accuracy = zeros(num_img,7);
TP_stat = zeros(num_img,1); % true positive, the number of true shadow pixels which are identified correctly
TN_stat = zeros(num_img,1); % true negative, the number of nonshadow pixels which are identified correctly
FP_stat = zeros(num_img,1); % false positive, the number of nonshadow pixels which are identified as true shadow pixels
FN_stat = zeros(num_img,1); % false negative, the number of true shadow pixels which are identified as nonshadow pixels
count_stat = zeros(num_img,1); % sum of the image pixels
Mean_acc = zeros(1,7); % mean value of accuracy
Std_acc = zeros(1,7); % std value of accuracy
threM = zeros(num_img,1);
Name = cell(num_img,1);
tElapsed = zeros(num_img,1);
for i = 1:num_img
%% read images
image_s_name = filePaths_s(i).name;
image_s = double(imread(fullfile(folderTest_s,image_s_name)))/255;
%img_name = img_list{i};
fprintf('Testing SDCNN on %s: %d/%d: %s\n', dataset, i, num_img, image_s_name);
%% Load GT image
image_m_name = filePaths_m(i).name;
image_GT = double(imread(fullfile(folderTest_m,image_m_name)))/255;
image_GT = image_GT(:,:,1);
image_GT(image_GT>0)=1;
[m,n,channel] = size(image_GT);
%
% mask = image_GT;
% mask() =
% %% Compute (H +1)/(I +1) ratio images
% hsi = rgb2hsi(image_s);
% hsi_h = hsi(:,:,1);
% hsi_s = hsi(:,:,2);
% hsi_i = hsi(:,:,3);
% ratio = (hsi_h + 1)./(hsi_i + 1);
%
% %Normalized (H +1)/(I +1) ratio images
% min1 = min(ratio(:));
% max1 = max(ratio(:));
% ratio = (ratio - min1)/(max1 - min1);
%
% %% Resize ratio image
% ratio2 = imresize(ratio, 0.5);
% ratio4 = imresize(ratio, 0.25);
% ratio8 = imresize(ratio, 0.125);
tStart = tic;
%% apply DsCNN
net.mode = 'test' ;
output_var = 'level4_prediction_softmax';
output_index = net.getVarIndex(output_var);
net.vars(output_index).precious = 1;
% convert to GPU
y = single(image_s);
if( gpu )
y = gpuArray(y);
end
% forward
% tic;
inputs = {'input', y};
net.eval(inputs);
% time = toc;
y = gather(net.vars(output_index).value);
output = double(y);
output_1 = output(:,:,1);
output_2 = output(:,:,2); % shadow probability
% y = gather(net.vars(output_index).value);
% output = double(y);
% % 根据图像大小来决定是否使用GPU
% if m>1000
% GPU = 0;
% else
% GPU = 1;
% end
% output = exc_SDCNN(image_s, net, gpu);
%% 二值化
% thre = 0.5;
thre = graythresh(output_2);
threM(i,1) = thre;
output_BW = zeros(size(output_2));
output_BW(output_2>thre)=1;
output_BW(output_2<thre)=0;
tElapsed(i,1) = toc(tStart);
%% save result
% 保存网络直接输出的概率图
output_filename = fullfile(output_dir_epoch, sprintf('%s', image_s_name));
% fprintf('Save %s\n', output_filename);
imwrite(output_2, output_filename);
% 保存二值化结果图
output_BW_filename = fullfile(output_BW_dir_epoch, sprintf('%s', image_s_name));
imwrite(output_BW, output_BW_filename);
%% evaluate
Accuracy(i,:) = accuracy(output_BW, image_GT);
[TP_stat(i,1),TN_stat(i,1),FP_stat(i,1),FN_stat(i,1),count_stat(i,1)] = accuracy_indiv(output_BW, image_GT);
Name{i,1} = filePaths_s(i).name; % 精度矩阵中加入对应影像名称,便于识别
end
%% 统计平均
Mean_acc = mean(Accuracy);
Std_acc = std(Accuracy);
Accuracy(end+1,:) = Mean_acc;
Accuracy(end+1,:) = Std_acc;
avg_pro_s = Accuracy(end-1,1);
avg_pro_n = Accuracy(end-1,2);
avg_user_s = Accuracy(end-1,3);
avg_user_n = Accuracy(end-1,4);
avg_Total = Accuracy(end-1,5);
avg_F = Accuracy(end-1,6);
avg_BER = Accuracy(end-1,7);
% Show
% fprintf('Average producer accuracy of shadow = %f\n', avg_pro_s);
% fprintf('Average producer accuracy of nonshadow = %f\n', avg_pro_n);
% fprintf('Average user accuracy of shadow = %f\n',avg_user_s);
% fprintf('Average user accuracy of nonshadow = %f\n', avg_user_n);
fprintf('Average total accuracy = %f\n', avg_Total);
fprintf('Average F score = %f\n', avg_F);
fprintf('Average BER = %f\n', avg_BER);
% 保存每个epoch的测试精度平均值
avg_Total_epoch_deconv(k,1) = avg_Total;
avg_F_epoch_deconv(k,1) = avg_F;
avg_BER_epoch_deconv(k,1) = avg_BER;
%% Save
% 保存平均精度
filename_acc_avg = fullfile(output_dir_epoch, 'Accuracy_avg.txt');
save_matrix(Accuracy, filename_acc_avg);
% 保存阈值
filename_thre = fullfile(output_dir_epoch, 'Threshold.txt');
save_matrix(threM, filename_thre);
% 保存运行时间
filename_time = fullfile(output_dir_epoch, 'Time.txt');
save_matrix(tElapsed, filename_time);
%保存文件名
%fid=fopen('Tappen_model\Name.txt','w');
fid=fopen(fullfile(output_dir_epoch,'\','Name.txt'),'w');
for i=1:size(Name,1)
a = Name(i);
a = cell2mat(a);
fprintf(fid,'%s\n',a);
end
end
% %% Draw Total Accuracy Curve
% figure;
% hold on;
%
% % Draw the curve
% plot(avg_Total_epoch_deconv,'-r','DisplayName','Deconv and concat With Multi-Attention Step Fusion Structure');
% % plot(PSNRs_mean_noRes,'-b','DisplayName','Without Residual Learning');
%
% % Add Title and Axis Labels
% % title('Total Accuracy');
% xlabel('Epochs'); % 设置坐标轴标签
% ylabel('Total Accuracy');
%
% grid; % 打开绘图网线
%
% %set(gca,'ytick',[]) %删除 当前图 y 轴刻度
% % % axis([0 linelength 0 260]); % 用来设置坐标轴显示的最大值最小值
% % % set(gca,'YTick',[0:50:260]); % 对坐标轴的刻度的分度进行设置;
%
% %加图例
% legend('show');
%
% hold off;
%
% %% Draw F-score Curve
% figure;
% hold on;
%
% % Draw the curve
% plot(avg_F_epoch_deconv,'-r','DisplayName','Deconv and concat With Multi-Attention Step Fusion Structure');
% % plot(SSIMs_mean_noRes,'-b','DisplayName','Without Residual Learning');
%
% % Add Title and Axis Labels
% %title('Profile');
% xlabel('Epochs'); % 设置坐标轴标签
% ylabel('F-score');
%
% grid; % 打开绘图网线
%
% %set(gca,'ytick',[]) %删除 当前图 y 轴刻度
%
% %加图例
% legend('show');
%
% hold off;
%
% %% Draw BER Curve
% figure;
% hold on;
%
% % Draw the curve
% plot(avg_BER_epoch_deconv,'-r','DisplayName','Deconv and concat With Multi-Attention Step Fusion Structure');
% % plot(SSIMs_mean_noRes,'-b','DisplayName','Without Residual Learning');
%
% % Add Title and Axis Labels
% %title('Profile');
% xlabel('Epochs'); % 设置坐标轴标签
% ylabel('BER');
%
% grid; % 打开绘图网线
%
% %set(gca,'ytick',[]) %删除 当前图 y 轴刻度
%
% %加图例
% legend('show');
%
% hold off;
%
%
% %% Save
% % Overall accuracy
% filename_Total_avg_epoch = fullfile(output_dir, 'Total_avg_epoch_deconv_con_attentionD.txt');
% save_matrix(avg_Total_epoch_deconv, filename_Total_avg_epoch);
% save([output_dir,'\','Total_avg_epoch_deconv_con_attentionD.mat'],'avg_Total_epoch_deconv','-v7.3');
%
% % F-score accuracy
% filename_F_avg_epoch = fullfile(output_dir, 'Fscore_avg_epoch_deconv_con_attentionD.txt');
% save_matrix(avg_F_epoch_deconv, filename_F_avg_epoch);
% save([output_dir,'\','Fscore_avg_epoch_deconv_con_attentionD.mat'],'avg_F_epoch_deconv','-v7.3');
%
% % Balance Error Rate (BER)
% filename_BER_avg_epoch = fullfile(output_dir, 'BER_avg_epoch_deconv_con_attentionD.txt');
% save_matrix(avg_BER_epoch_deconv, filename_BER_avg_epoch);
% save([output_dir,'\','BER_avg_epoch_deconv_con_attentionD.mat'],'avg_BER_epoch_deconv','-v7.3');
% toc;