-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathfcnTest.m
234 lines (198 loc) · 6.57 KB
/
fcnTest.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
function info = fcnTest(varargin)
run matconvnet/matlab/vl_setupnn ;
addpath matconvnet/examples ;
% experiment and data paths
opts.expDir = 'data/fcn32s-voc11' ;
opts.dataDir = 'data/voc11' ;
opts.modelPath = 'data/fcn32-voc11/net-epoch-50.mat' ;
opts.modelFamily = 'matconvnet' ;
[opts, varargin] = vl_argparse(opts, varargin) ;
% experiment setup
opts.imdbPath = fullfile(opts.expDir, 'imdb.mat') ;
opts.vocEdition = '11' ;
opts.vocAdditionalSegmentations = true ;
opts.vocAdditionalSegmentationsMergeMode = 2 ;
opts.gpus = [] ;
opts = vl_argparse(opts, varargin) ;
resPath = fullfile(opts.expDir, 'results.mat') ;
if exist(resPath)
info = load(resPath) ;
return ;
end
if ~isempty(opts.gpus)
gpuDevice(opts.gpus(1))
end
% -------------------------------------------------------------------------
% Setup data
% -------------------------------------------------------------------------
% Get PASCAL VOC 11/12 segmentation dataset plus Berkeley's additional
% segmentations
if exist(opts.imdbPath)
imdb = load(opts.imdbPath) ;
else
imdb = vocSetup('dataDir', opts.dataDir, ...
'edition', opts.vocEdition, ...
'includeTest', false, ...
'includeSegmentation', true, ...
'includeDetection', false) ;
if opts.vocAdditionalSegmentations
imdb = vocSetupAdditionalSegmentations(...
imdb, ...
'dataDir', opts.dataDir, ...
'mergeMode', opts.vocAdditionalSegmentationsMergeMode) ;
end
mkdir(opts.expDir) ;
save(opts.imdbPath, '-struct', 'imdb') ;
end
% Get validation subset
val = find(imdb.images.set == 2 & imdb.images.segmentation) ;
% Compare the validation set to the one used in the FCN paper
% valNames = sort(imdb.images.name(val)') ;
% valNames = textread('data/seg11valid.txt', '%s') ;
% valNames_ = textread('data/seg12valid-tvg.txt', '%s') ;
% assert(isequal(valNames, valNames_)) ;
% -------------------------------------------------------------------------
% Setup model
% -------------------------------------------------------------------------
switch opts.modelFamily
case 'matconvnet'
net = load(opts.modelPath) ;
net = dagnn.DagNN.loadobj(net.net) ;
net.mode = 'test' ;
for name = {'objective', 'accuracy'}
net.removeLayer(name) ;
end
net.meta.normalization.averageImage = reshape(net.meta.normalization.rgbMean,1,1,3) ;
predVar = net.getVarIndex('prediction') ;
inputVar = 'input' ;
imageNeedsToBeMultiple = true ;
case 'ModelZoo'
net = dagnn.DagNN.loadobj(load(opts.modelPath)) ;
net.mode = 'test' ;
predVar = net.getVarIndex('upscore') ;
inputVar = 'data' ;
imageNeedsToBeMultiple = false ;
case 'TVG'
net = dagnn.DagNN.loadobj(load(opts.modelPath)) ;
net.mode = 'test' ;
predVar = net.getVarIndex('coarse') ;
inputVar = 'data' ;
imageNeedsToBeMultiple = false ;
end
if ~isempty(opts.gpus)
gpuDevice(opts.gpus(1)) ;
net.move('gpu') ;
end
net.mode = 'test' ;
% -------------------------------------------------------------------------
% Train
% -------------------------------------------------------------------------
numGpus = 0 ;
confusion = zeros(21) ;
for i = 1:numel(val)
imId = val(i) ;
name = imdb.images.name{imId} ;
rgbPath = sprintf(imdb.paths.image, name) ;
labelsPath = sprintf(imdb.paths.classSegmentation, name) ;
% Load an image and gt segmentation
rgb = vl_imreadjpeg({rgbPath}) ;
rgb = rgb{1} ;
anno = imread(labelsPath) ;
lb = single(anno) ;
lb = mod(lb + 1, 256) ; % 0 = ignore, 1 = bkg
% Subtract the mean (color)
im = bsxfun(@minus, single(rgb), net.meta.normalization.averageImage) ;
% Soome networks requires the image to be a multiple of 32 pixels
if imageNeedsToBeMultiple
sz = [size(im,1), size(im,2)] ;
sz_ = round(sz / 32)*32 ;
im_ = imresize(im, sz_) ;
else
im_ = im ;
end
if ~isempty(opts.gpus)
im_ = gpuArray(im_) ;
end
net.eval({inputVar, im_}) ;
scores_ = gather(net.vars(predVar).value) ;
[~,pred_] = max(scores_,[],3) ;
if imageNeedsToBeMultiple
pred = imresize(pred_, sz, 'method', 'nearest') ;
else
pred = pred_ ;
end
% Accumulate errors
ok = lb > 0 ;
confusion = confusion + accumarray([lb(ok),pred(ok)],1,[21 21]) ;
% Plots
if mod(i - 1,30) == 0 || i == numel(val)
clear info ;
[info.iu, info.miu, info.pacc, info.macc] = getAccuracies(confusion) ;
fprintf('IU ') ;
fprintf('%4.1f ', 100 * info.iu) ;
fprintf('\n meanIU: %5.2f pixelAcc: %5.2f, meanAcc: %5.2f\n', ...
100*info.miu, 100*info.pacc, 100*info.macc) ;
figure(1) ; clf;
imagesc(normalizeConfusion(confusion)) ;
axis image ; set(gca,'ydir','normal') ;
colormap(jet) ;
drawnow ;
% Print segmentation
figure(100) ;clf ;
displayImage(rgb/255, lb, pred) ;
drawnow ;
% Save segmentation
imPath = fullfile(opts.expDir, [name '.png']) ;
imwrite(pred,labelColors(),imPath,'png');
end
end
% Save results
save(resPath, '-struct', 'info') ;
% -------------------------------------------------------------------------
function nconfusion = normalizeConfusion(confusion)
% -------------------------------------------------------------------------
% normalize confusion by row (each row contains a gt label)
nconfusion = bsxfun(@rdivide, double(confusion), double(sum(confusion,2))) ;
% -------------------------------------------------------------------------
function [IU, meanIU, pixelAccuracy, meanAccuracy] = getAccuracies(confusion)
% -------------------------------------------------------------------------
pos = sum(confusion,2) ;
res = sum(confusion,1)' ;
tp = diag(confusion) ;
IU = tp ./ max(1, pos + res - tp) ;
meanIU = mean(IU) ;
pixelAccuracy = sum(tp) / max(1,sum(confusion(:))) ;
meanAccuracy = mean(tp ./ max(1, pos)) ;
% -------------------------------------------------------------------------
function displayImage(im, lb, pred)
% -------------------------------------------------------------------------
subplot(2,2,1) ;
image(im) ;
axis image ;
title('source image') ;
subplot(2,2,2) ;
image(uint8(lb-1)) ;
axis image ;
title('ground truth')
cmap = labelColors() ;
subplot(2,2,3) ;
image(uint8(pred-1)) ;
axis image ;
title('predicted') ;
colormap(cmap) ;
% -------------------------------------------------------------------------
function cmap = labelColors()
% -------------------------------------------------------------------------
N=21;
cmap = zeros(N,3);
for i=1:N
id = i-1; r=0;g=0;b=0;
for j=0:7
r = bitor(r, bitshift(bitget(id,1),7 - j));
g = bitor(g, bitshift(bitget(id,2),7 - j));
b = bitor(b, bitshift(bitget(id,3),7 - j));
id = bitshift(id,-3);
end
cmap(i,1)=r; cmap(i,2)=g; cmap(i,3)=b;
end
cmap = cmap / 255;