-
Notifications
You must be signed in to change notification settings - Fork 0
/
IFCN_COCOStuff.m
213 lines (172 loc) · 7.29 KB
/
IFCN_COCOStuff.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
function IFCN_COCOStuff(varargin)
%FNCTRAIN Train FCN model using MatConvNet
run ~/codes/matconvnet-1.0-beta16/matlab/vl_setupnn ;
addpath ~/codes/matconvnet-1.0-beta16/examples ;
addpath(genpath('~/codes/toolbox'));
%--------------------------------------------------------------------------
% Set the parameters
opts.modelType = 'fcn8s';
opts.imageSize = 512;
opts.layers = 6;
opts.kerSize = 5; % must be odd number
opts.stream = 'all';
opts.recursive = false;
opts.mode = 'val';
opts.resLayer = 101;
opts.newLr = 3; % the times of lr for skipNetwork and contextNetwork
%--------------------------------------------------------------------------
% -------------------------------------------------------------------------
% Almost fixed
opts.dataset = 'Context';
opts.dataDir = '/home/bshuai/datasets/COCO-Stuff/';
opts.readFromDisk = true;
opts.nh = 512;
opts.rareExponnetial = 2;
opts.nClass = 171;
%--------------------------------------------------------------------------
vgg = ~strncmpi(opts.modelType, 'res', 3);
if vgg
opts.sourceModelPath = '../imagenet/imagenet-vgg-verydeep-16.mat' ;
% Path Setting
networkFolder = sprintf('batch-i%s-%dx%d-%dlayers-1e-3-higher%d', ...'
opts.modelType, opts.kerSize, opts.kerSize, opts.layers, opts.newLr);
else
opts.sourceModelPath = sprintf('../imagenet/imagenet-resnet-%d-dag.mat', opts.resLayer);
% Path Setting
networkFolder = sprintf('%s-%d-DAG-RNN-%d-%dlayers-1e-3-higher%d', ...'
opts.modelType, opts.nh, opts.resLayer, opts.layers, opts.newLr);
end
opts.expDir = sprintf('COCOStuff_%d_Val/%s', ...
opts.imageSize, networkFolder);
[opts, varargin] = vl_argparse(opts, varargin) ;
% experiment setup
opts.imdbPath = fullfile(opts.dataDir, 'imdb-train-val-disk.mat') ;
opts.numFetchThreads = 1 ; % not used yet
% training options (SGD)
opts.train.batchSize = 10;
opts.train.numSubBatches = 1 ;
opts.train.continue = true ;
opts.train.gpus = [2];
opts.train.prefetch = true ;
opts.train.expDir = opts.expDir ;
opts.train.learningRate = 1e-3*[ones(1, 15) 0.1*ones(1,5) 0.01*ones(1,2)] ;
opts.train.numEpochs = numel(opts.train.learningRate) ;
opts = vl_argparse(opts, varargin) ;
% -------------------------------------------------------------------------
% Setup data
% -------------------------------------------------------------------------
if exist(opts.imdbPath)
imdb = load(opts.imdbPath) ;
else
imdb = getCOCOStuffImdb(opts) ;
mkdir(opts.expDir) ;
save(opts.imdbPath, '-struct', 'imdb', '-v7.3') ;
end
% Get training and test/validation subsets
train = find(imdb.images.set == 1 ) ;
val = find(imdb.images.set == 2) ;
% -------------------------------------------------------------------------
% Setup model
% -------------------------------------------------------------------------
% Get initial model from VGG-VD-16
if vgg
net = fcnInitializeNetwork('sourceModelPath', opts.sourceModelPath,...
'kerSize', opts.kerSize, 'layers', opts.layers,...
'nh', opts.nh,'nClass', opts.nClass, 'recursive', opts.recursive, ...
'newLr', opts.newLr) ;
if any(strcmp(opts.modelType, {'fcn16s', 'fcn8s', 'fcn4s'}))
% upgrade model to FCN16s
net = fcnInitializeNetwork16s(net, 'rnn', false,...
'nh', opts.nh, 'nClass', opts.nClass, 'newLr', opts.newLr) ;
end
if any(strcmp(opts.modelType, {'fcn8s', 'fcn4s'}))
% upgrade model fto FCN8s
net = fcnInitializeNetwork8s(net, 'rnn', false, ...
'nh', opts.nh, 'nClass', opts.nClass, 'newLr', opts.newLr) ;
end
if strcmp(opts.modelType, 'fcn4s')
% upgrade model fto FCN8s
net = fcnInitializeNetwork4s(net, 'rnn', false, ...
'nh', opts.nh, 'nClass', opts.nClass, 'newLr', opts.newLr) ;
end
else
net = fcnInitializeResNetwork('sourceModelPath', opts.sourceModelPath,...
'rnn', opts.rnn, 'kerSize', opts.kerSize, 'layers', opts.layers,...
'nh', opts.nh,'nClass', opts.nClass, 'recursive', opts.recursive, ...
'resLayer', opts.resLayer, 'newLr', opts.newLr);
if any(strcmp(opts.modelType, {'res16s', 'res8s', 'res4s'}))
% upgrade model to Res16s
net = fcnInitializeResNetwork16s(net, 'rnn', false,...
'nh', opts.nh, 'nClass', opts.nClass, ...
'resLayer', opts.resLayer, 'newLr', opts.newLr) ;
end
if any(strcmp(opts.modelType, {'res8s', 'res4s'}))
% upgrade model to Res16s
net = fcnInitializeResNetwork8s(net, 'rnn', false,...
'nh', opts.nh, 'nClass', opts.nClass, ...
'resLayer', opts.resLayer, 'newLr', opts.newLr) ;
end
if any(strcmp(opts.modelType, {'res4s'}))
% upgrade model to Res16s
net = fcnInitializeResNetwork4s(net, 'rnn', false,...
'nh', opts.nh, 'nClass', opts.nClass, ...
'resLayer', opts.resLayer, 'newLr', opts.newLr) ;
end
end
% -------------------------------------------------------------------------
% Train
% -------------------------------------------------------------------------
% Setup data fetching options
bopts.numThreads = opts.numFetchThreads ;
bopts.labelStride = 2 ;
bopts.labelOffset = 1 ;
bopts.classWeights = getWeight(imdb.classFrequency, opts.rareExponnetial);
bopts.useGpu = numel(opts.train.gpus) > 0 ;
bopts.readFromDisk = opts.readFromDisk;
bopts.imageSize = opts.imageSize;
bopts.vgg = vgg;
bopts.stream = opts.stream;
bopts.dataset = opts.dataset;
% -------------------------------------------------------------------------
% Network setting
% -------------------------------------------------------------------------
% Launch SGD
if strcmp(opts.mode, 'val')
trainSamples = train;
bnSamples = val;
bnSuffix = 'val';
elseif strcmp(opts.mode, 'test')
trainSamples = [train val];
bnSamples = test;
bnSuffix = 'test';
end
fprintf('---------------------------------------------------\n')
fprintf('Please check the network setting... \n')
fprintf('Network Type: %s \n', opts.modelType);
fprintf('Context Network: Kernel Size %d x %d, %d layers \n', opts.kerSize, ...
opts.kerSize, opts.layers);
fprintf('Batch size: %d \n', opts.train.batchSize);
fprintf('Image Size: %d \n', opts.imageSize);
fprintf('Starting learning rate %8.2e\n', opts.train.learningRate(1));
fprintf('New learning rates for ContextNetwork and SkipNetwork: %d \n', opts.newLr);
fprintf('Rare exponential constant %.1f\n', opts.rareExponnetial);
fprintf('In %s Mode: %d training examples, %d bn samples, with %s bn Suffix \n', ...
opts.mode, numel(trainSamples), numel(bnSamples), bnSuffix);
fprintf('---------------------------------------------------\n')
info = fcn_train_dag(net, imdb, getBatchWrapper(bopts), opts.train, ...
'train', trainSamples, ...
'val', val) ;
% update BN statistics
load(fullfile(opts.expDir, 'net-epoch-22.mat'), 'net');
net = dagnn.DagNN.loadobj(net) ;
updateBN(net, imdb, getBatchWrapper(bopts), opts.train, ...
'train', bnSamples, 'mode', bnSuffix);
% -------------------------------------------------------------------------
function fn = getBatchWrapper(opts)
% -------------------------------------------------------------------------
fn = @(imdb,batch) getBatch(imdb,batch,opts,'prefetch',nargout==0) ;
function classWeight = getWeight(h, k)
h = h(2:end);
h = h / sum(h);
classWeight = (k).^(max(0,ceil((log10(0.004 ./ h)))));
% classWeight = ones(1, numel(h));