-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_train.m
221 lines (187 loc) · 9.39 KB
/
net_train.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
function info = net_train(varargin)
% -------------------------------------------------------------------------
% Config
% -------------------------------------------------------------------------
addpath(genpath('evaluationCode'));
addpath '/home/amax/ShiHengcan/caffe-2017/matlab' % your caffe path
opts.dataset = 'NYU_Depth_V2' ; % "pascal" or others
opts.imdbDir = './data/NYU_Depth_V2' ;
opts.modelDir = './models' ;
model_weights = fullfile(opts.modelDir,'pspnet50_init.caffemodel') ;
model_solver = fullfile(opts.modelDir,'solver_poly.prototxt') ;
opts.imageSize = [473, 473] ;
opts.numEpochs = 20 ;
opts.miniBatchSize = 1 ;
opts.valEpochs = 1 ; % val in every opts.valEpoch epoches
opts.useGpus = 0 ; % your GPU
opts = vl_argparse(opts, varargin);
% -------------------------------------------------------------------------
% Get data
% -------------------------------------------------------------------------
imdb = load(fullfile(opts.imdbDir, 'imdb.mat')) ;
stats = load(fullfile(opts.imdbDir, 'imdbStats.mat')) ;
train = find(imdb.images.set == 1 & imdb.images.segmentation) ;
val = find(imdb.images.set == 2 & imdb.images.segmentation) ;
opts.rgbMean_train = stats.rgbMean_train ;
opts.rgbMean_val = stats.rgbMean_val ;
opts.numClass = numel(imdb.objectClasses.id) ;
% -------------------------------------------------------------------------
% Set gpu
% -------------------------------------------------------------------------
if ~isempty(opts.useGpus)
caffe.set_mode_gpu();
caffe.set_device(opts.useGpus);
else
caffe.set_mode_cpu();
end
% -------------------------------------------------------------------------
% Initialize model
% -------------------------------------------------------------------------
solver = caffe.Solver(model_solver);
solver.net.copy_from(model_weights);
% -------------------------------------------------------------------------
% Train
% -------------------------------------------------------------------------
for epoch = 1 : opts.numEpochs
% train
if exist('train')
numMinibatches = ceil(numel(train) / opts.miniBatchSize) ;
inputsIndex = randperm(numel(train)) ; % randomlize the training sequence
pAccuracy = zeros(1,numel(train)) ;
pCorrect = zeros(1,numel(train)) ;
pLabeled = zeros(1,numel(train)) ;
areaIntersection = zeros(opts.numClass, numel(train)) ;
areaUnion = zeros(opts.numClass, numel(train)) ;
bicls_pAccuracy = zeros(1,numel(train)) ;
bicls_pCorrect = zeros(1,numel(train)) ;
bicls_pLabeled = zeros(1,numel(train)) ;
for miniBatch = 1 : numMinibatches
fprintf('Epoch%d: train %d/%d', epoch, miniBatch, numMinibatches) ;
% generate minibatch training data
if miniBatch == numMinibatches
batchIdx = inputsIndex((miniBatch-1) * opts.miniBatchSize + 1 : end) ;
numResIdx = opts.miniBatchSize - length(batchIdx) ;
batchIdx = [batchIdx, inputsIndex(1 : numResIdx)] ;
else
batchIdx = inputsIndex((miniBatch-1) * opts.miniBatchSize + 1 : miniBatch * opts.miniBatchSize) ;
end
batch = train(batchIdx) ;
inputs_data = get_batch(imdb, batch, opts) ;
% set inputs
for n = 2:2:length(inputs_data)
solver.net.blobs(solver.net.inputs{n/2}).set_data(inputs_data{n});
end
% one iter of SGD update
time = tic ;
solver.step(1);
time = toc(time) ;
% evaluate the accuracy
prediction = solver.net.blobs('conv6_biclass_interp').get_data() ;
prediction_pos = prediction(:, :, 2:2:end) ;
prediction_neg = prediction(:, :, 1:2:end-1) ;
bicls_pred = (prediction_pos - prediction_neg) > 0 ;
bicls_pred = bicls_pred + 1 ;
bicls_anno = inputs_data{4} + 1 ;
[bicls_pAccuracy(batchIdx), bicls_pCorrect(batchIdx), bicls_pLabeled(batchIdx)] = ...
pixelAccuracy(bicls_pred, bicls_anno) ;
prediction = solver.net.blobs('conv6_interp').get_data() ;
[~, prediction] = max(prediction,[],3 ) ;
prediction = uint8(prediction) ;
anno = inputs_data{6} + 1 ;
if strcmp(opts.dataset, 'pascal')
anno(anno==255) = 0 ;
anno(anno==256) = 0 ;
end
prediction = imresize(prediction, size(anno), 'nearest') ;
[pAccuracy(batchIdx), pCorrect(batchIdx), pLabeled(batchIdx)] = pixelAccuracy(prediction, anno) ;
[areaIntersection(:,batchIdx), areaUnion(:,batchIdx)]=intersectionAndUnion(prediction, anno, opts.numClass);
%print
fprintf('||segAcc: %.3f', pAccuracy(batchIdx)) ;
fprintf('||biclsAcc: %.3f', bicls_pAccuracy(batchIdx)) ;
speed = opts.miniBatchSize / time ;
fprintf('||speed: %.1fHz\n', speed) ;
end
trainMeanPixelAccuracy = sum(pCorrect)/sum(pLabeled);
trainMeanbiclsAccuracy = sum(bicls_pCorrect)/sum(bicls_pLabeled);
IoU = sum(areaIntersection,2)./sum(eps+areaUnion,2);
trainMeanIoU = mean(IoU);
fprintf('for all images ||pixelAccuracy: %.3f', trainMeanPixelAccuracy) ;
fprintf(' ||meanIoU: %.3f\n', trainMeanIoU) ;
end
% val
if exist('val', 'var') && ...
((epoch == opts.numEpochs) || (mod(epoch, opts.valEpochs) == 0) )
numMinibatches = ceil(numel(val) / opts.miniBatchSize) ;
pAccuracy = zeros(1,numel(val)) ;
pCorrect = zeros(1,numel(val)) ;
pLabeled = zeros(1,numel(val)) ;
areaIntersection = zeros(opts.numClass, numel(val)) ;
areaUnion = zeros(opts.numClass, numel(val)) ;
bicls_pAccuracy = zeros(1,numel(val)) ;
bicls_pCorrect = zeros(1,numel(val)) ;
bicls_pLabeled = zeros(1,numel(val)) ;
for miniBatch = 1 : numMinibatches
fprintf('Epoch%d: val %d/%d', epoch, miniBatch, numMinibatches) ;
if miniBatch == numMinibatches
batchIdx = val((miniBatch-1) * opts.miniBatchSize + 1 : end) ;
numResIdx = opts.miniBatchSize - length(batchIdx) ;
batchIdx = [batchIdx, val(1 : numResIdx)] ;
else
batchIdx = val((miniBatch-1) * opts.miniBatchSize + 1 : miniBatch * opts.miniBatchSize) ;
end
batch = batchIdx ;
inputs_data = get_batch(imdb, batch, opts) ;
% set inputs
inputs = {};
for n = 2:2:length(inputs_data)
inputs = cat(2,inputs,inputs_data{n});
end
% forward
time = tic ;
tmp = solver.net.forward(inputs) ;
time = toc(time) ;
% evaluate the accuracy
prediction = solver.net.blobs('conv6_biclass_interp').get_data() ;
prediction_pos = prediction(:, :, 2:2:end) ;
prediction_neg = prediction(:, :, 1:2:end-1) ;
bicls_pred = (prediction_pos - prediction_neg) > 0 ;
bicls_pred = bicls_pred + 1 ;
bicls_anno = inputs_data{4} + 1 ;
[bicls_pAccuracy(batchIdx), bicls_pCorrect(batchIdx), bicls_pLabeled(batchIdx)] = ...
pixelAccuracy(bicls_pred, bicls_anno) ;
prediction = solver.net.blobs('conv6_interp').get_data() ;
[~, prediction] = max(prediction,[],3 ) ;
prediction = uint8(prediction) ;
anno = inputs_data{6} + 1 ;
if strcmp(opts.dataset, 'pascal')
anno(anno==255) = 0 ;
anno(anno==256) = 0 ;
end
prediction = imresize(prediction, size(anno), 'nearest') ;
[pAccuracy(batchIdx), pCorrect(batchIdx), pLabeled(batchIdx)] = pixelAccuracy(prediction, anno) ;
[areaIntersection(:,batchIdx), areaUnion(:,batchIdx)]=intersectionAndUnion(prediction, anno, opts.numClass);
%print
fprintf('||segAcc: %.3f', pAccuracy(batchIdx)) ;
fprintf('||biclsAcc: %.3f', bicls_pAccuracy(batchIdx)) ;
speed = opts.miniBatchSize / time ;
fprintf('||speed: %.1fHz\n', speed) ;
end
valMeanPixelAccuracy = sum(pCorrect)/sum(pLabeled);
valMeanbiclsAccuracy = sum(bicls_pCorrect)/sum(bicls_pLabeled);
IoU = sum(areaIntersection,2)./sum(eps+areaUnion,2);
valMeanIoU = mean(IoU);
fprintf('for all images ||pixelAccuracy: %.3f', valMeanPixelAccuracy) ;
fprintf(' ||meanIoU: %.3f\n', valMeanIoU) ;
end
% save net
netPath = fullfile(opts.modelDir, ['net-epoch-',num2str(epoch),'.caffemodel']) ;
solver.net.save(netPath) ;
statePath = fullfile(opts.modelDir, ['state-epoch-',num2str(epoch),'.mat']) ;
if exist('val', 'var') && ((epoch == opts.numEpochs) || (mod(epoch, opts.valEpochs) == 0) )
save(statePath, 'trainMeanPixelAccuracy', 'trainMeanIoU', 'trainMeanbiclsAccuracy', ...
'valMeanPixelAccuracy', 'valMeanIoU', 'valMeanbiclsAccuracy') ;
else
save(statePath, 'trainMeanPixelAccuracy', 'trainMeanIoU', 'trainMeanbiclsAccuracy') ;
end
end
caffe.reset_all();