-
Notifications
You must be signed in to change notification settings - Fork 133
/
cnn_shape.m
208 lines (186 loc) · 6.96 KB
/
cnn_shape.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
function net = cnn_shape(dataName, varargin)
%CNN_SHAPE Train an MVCNN on a provided dataset
%
% dataName::
% must be name of a folder under data/
% `baseModel`:: 'imagenet-matconvnet-vgg-m'
% learning starting point
% `fromScratch`:: false
% if false, only the last layer is initialized randomly
% if true, all the weight layers are initialized randomly
% `numFetchThreads`::
% #threads for vl_imreadjpeg
% `aug`:: 'none'
% specifies the operations (fliping, perturbation, etc.) used
% to get sub-regions
% `viewpoolPos` :: 'relu5'
% location of the viewpool layer, only used when multiview is true
% `includeVal`:: false
% if true, validation set is also used for training
% `useUprightAssumption`:: true
% if true, 12 views will be used to render meshes,
% otherwise 80 views based on a dodecahedron
%
% `train`
% training parameters:
% `learningRate`:: [0.001*ones(1, 10) 0.0001*ones(1, 10) 0.00001*ones(1,10)]
% learning rate
% `batchSize`: 128
% set to a smaller number on limited memory
% `momentum`:: 0.9
% learning momentum
% `gpus` :: []
% a list of available gpus
%
% Hang Su
opts.networkType = 'simplenn'; % only simplenn is supported currently
opts.baseModel = 'imagenet-matconvnet-vgg-m';
opts.fromScratch = false;
opts.dataRoot = 'data' ;
opts.imageExt = '.jpg';
opts.numFetchThreads = 0 ;
opts.multiview = true;
opts.viewpoolPos = 'relu5';
opts.useUprightAssumption = true;
opts.aug = 'stretch';
opts.pad = 0;
opts.border = 0;
opts.numEpochs = [5 10 20];
opts.includeVal = false;
[opts, varargin] = vl_argparse(opts, varargin) ;
if strcmpi(opts.baseModel(end-3:end),'.mat'),
[~,modelNameStr] = fileparts(opts.baseModel);
opts.baseModel = load(opts.baseModel);
else
modelNameStr = opts.baseModel;
end
if opts.multiview,
opts.expDir = sprintf('%s-ft-%s-%s-%s', ...
modelNameStr, ...
dataName, ...
opts.viewpoolPos, ...
opts.networkType);
else
opts.expDir = sprintf('%s-ft-%s-%s', ...
modelNameStr, ...
dataName, ...
opts.networkType);
end
opts.expDir = fullfile(opts.dataRoot, opts.expDir);
[opts, varargin] = vl_argparse(opts,varargin) ;
opts.train.learningRate = [0.005*ones(1, 5) 0.001*ones(1, 5) 0.0001*ones(1,10) 0.00001*ones(1,10)];
opts.train.momentum = 0.9;
opts.train.batchSize = 256;
opts.train.maxIterPerEpoch = [Inf, Inf];
opts.train.balancingFunction = {[], []};
opts.train.gpus = [];
opts.train = vl_argparse(opts.train, varargin) ;
if ~exist(opts.expDir, 'dir'), vl_xmkdir(opts.expDir) ; end
assert(strcmp(opts.networkType,'simplenn'), 'Only simplenn is supported currently');
% -------------------------------------------------------------------------
% Prepare data
% -------------------------------------------------------------------------
imdb = get_imdb(dataName);
if ~opts.multiview,
nViews = 1;
else
nShapes = length(unique(imdb.images.sid));
nViews = length(imdb.images.id)/nShapes;
end
imdb.meta.nViews = nViews;
opts.train.train = find(imdb.images.set==1);
opts.train.val = find(imdb.images.set==2);
if opts.includeVal,
opts.train.train = [opts.train.train opts.train.val];
opts.train.val = [];
end
opts.train.train = opts.train.train(1:nViews:end);
opts.train.val = opts.train.val(1:nViews:end);
% -------------------------------------------------------------------------
% Prepare model
% -------------------------------------------------------------------------
net = cnn_shape_init(imdb.meta.classes, ...
'base', opts.baseModel, ...
'restart', opts.fromScratch, ...
'nViews', nViews, ...
'viewpoolPos', opts.viewpoolPos, ...
'networkType', opts.networkType);
% -------------------------------------------------------------------------
% Learn
% -------------------------------------------------------------------------
switch opts.networkType
case 'simplenn', trainFn = @cnn_shape_train ;
case 'dagnn', trainFn = @cnn_train_dag ;
end
trainable_layers = find(cellfun(@(l) isfield(l,'weights'),net.layers));
fc_layers = find(cellfun(@(s) numel(s.name)>=2 && strcmp(s.name(1:2),'fc'),net.layers));
fc_layers = intersect(fc_layers, trainable_layers);
lr = cellfun(@(l) l.learningRate, net.layers(trainable_layers),'UniformOutput',false);
layers_for_update = {trainable_layers(end), fc_layers, trainable_layers};
for s=1:numel(opts.numEpochs),
if opts.numEpochs(s)<1, continue; end
for i=1:numel(trainable_layers),
l = trainable_layers(i);
if ismember(l,layers_for_update{s}),
net.layers{l}.learningRate = lr{i};
else
net.layers{l}.learningRate = lr{i}*0;
end
end
net = trainFn(net, imdb, getBatchFn(opts, net.meta), ...
'expDir', opts.expDir, ...
net.meta.trainOpts, ...
opts.train, ...
'numEpochs', sum(opts.numEpochs(1:s))) ;
end
% -------------------------------------------------------------------------
% Deploy
% -------------------------------------------------------------------------
net = cnn_imagenet_deploy(net) ;
modelPath = fullfile(opts.expDir, 'net-deployed.mat');
switch opts.networkType
case 'simplenn'
save(modelPath, '-struct', 'net') ;
case 'dagnn'
net_ = net.saveobj() ;
save(modelPath, '-struct', 'net_') ;
clear net_ ;
end
% -------------------------------------------------------------------------
function fn = getBatchFn(opts, meta)
% -------------------------------------------------------------------------
bopts.numThreads = opts.numFetchThreads ;
bopts.pad = opts.pad;
bopts.border = opts.border ;
bopts.transformation = opts.aug ;
bopts.imageSize = meta.normalization.imageSize ;
bopts.averageImage = meta.normalization.averageImage ;
bopts.rgbVariance = meta.augmentation.rgbVariance ;
% bopts.transformation = meta.augmentation.transformation ;
switch lower(opts.networkType)
case 'simplenn'
fn = @(x,y) getSimpleNNBatch(bopts,x,y) ;
case 'dagnn'
error('dagnn version not yet implemented');
end
% -------------------------------------------------------------------------
function [im,labels] = getSimpleNNBatch(opts, imdb, batch)
% -------------------------------------------------------------------------
if nargout > 1, labels = imdb.images.class(batch); end
isVal = ~isempty(batch) && imdb.images.set(batch(1)) ~= 1 ;
nViews = imdb.meta.nViews;
batch = bsxfun(@plus,repmat(batch(:)',[nViews 1]),(0:nViews-1)');
batch = batch(:)';
images = strcat([imdb.imageDir filesep], imdb.images.name(batch)) ;
if ~isVal, % training
im = cnn_shape_get_batch(images, opts, ...
'prefetch', nargout == 0, ...
'nViews', nViews);
else
im = cnn_shape_get_batch(images, opts, ...
'prefetch', nargout == 0, ...
'nViews', nViews, ...
'transformation', 'none');
end
nAugs = size(im,4)/numel(images);
if nargout > 1, labels = repmat(labels(:)',[1 nAugs]); end