-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcnn_qp2_mpii.m
220 lines (198 loc) · 8 KB
/
cnn_qp2_mpii.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
function cnn_qp2_mpii(varargin)
global VOCopts
startup;
opts.dataDir = fullfile('data/mpii_human') ;
opts.modelType = 'fcn32s';
opts.networkType = 'dagnn';
opts.weightInitMethod = 'xavierimproved' ;
opts.alpha = 0.5;
[opts, varargin] = vl_argparse(opts, varargin) ;
addpath(opts.dataDir);
opts.expDir = fullfile('models', ['qp2-' opts.modelType]);
[opts, varargin] = vl_argparse(opts, varargin) ;
opts.gpus = [1];
opts.batchSize = 40;
[opts, varargin] = vl_argparse(opts, varargin) ;
opts.numFetchThreads = 4 ;
opts.lite = false ;
opts.imdbPath = fullfile(opts.expDir, 'imdb.mat');
opts.train.momentum = 0.90;
opts.train.batchSize = opts.batchSize;
opts.train.numSubBatches = 1;
opts.train.continue = true ;
opts.train.gpus = opts.gpus ;
opts.train.prefetch = true ;
opts.train.sync = false ;
opts.train.cudnn = true ;
opts.train.expDir = opts.expDir ;
opts.learningRate = 1e-6;
[opts, varargin] = vl_argparse(opts, varargin) ;
opts.train.learningRate = opts.learningRate;
opts.numEpochs = 50;
opts = vl_argparse(opts, varargin);
opts.train.numEpochs = opts.numEpochs;
if ~exist(opts.expDir), mkdir(opts.expDir); end;
switch opts.modelType
case 'fcn32s'
error('no qp2 fcn32s');
case 'fcn16s'
imdbPath = strrep(opts.imdbPath, '16s', '32s');
imdbPath = strrep(imdbPath, 'qp2', 'qp1');
imdb = load(imdbPath);
assert(imdb.labelSize(3) == 16);
fcn32s_dir = strrep(opts.expDir, '16s', '32s');
fcn32s_dir = strrep(fcn32s_dir, 'qp2', 'qp1');
fcn32s_epoch = findLastCheckpoint(30, fcn32s_dir);
fcn32s_model = sprintf('net-epoch-%d.mat', fcn32s_epoch);
fcn32s_model = fullfile(fcn32s_dir, fcn32s_model);
fprintf('Training fcn16s based on %s\n', fcn32s_model);
%
net = initialize_qp2_fcn16s(fcn32s_model, double(imdb.labelSize(3)));
% compute image statistics
imageStatsPath = fullfile(opts.expDir, 'imageStats.mat');
imageStatsPath = strrep(imageStatsPath, '16s', '32s');
imageStatsPath = strrep(imageStatsPath, 'qp2', 'qp1');
case 'fcn8s'
imdbPath = strrep(opts.imdbPath, '8s', '32s');
imdbPath = strrep(imdbPath, 'qp2', 'qp1');
imdb = load(imdbPath);
assert(imdb.labelSize(3) == 16);
fcn16s_dir = strrep(opts.expDir, '8s', '16s');
fcn16s_epoch = findLastCheckpoint(15, fcn16s_dir);
fcn16s_model = sprintf('net-epoch-%d.mat', fcn16s_epoch);
fcn16s_model = fullfile(fcn16s_dir, fcn16s_model);
fprintf('Training fcn8s based on %s\n', fcn16s_model);
%
net = initialize_qp2_fcn8s(fcn16s_model, double(imdb.labelSize(3)));
% compute image statistics
imageStatsPath = fullfile(opts.expDir, 'imageStats.mat');
imageStatsPath = strrep(imageStatsPath, '8s', '32s');
imageStatsPath = strrep(imageStatsPath, 'qp2', 'qp1');
case 'fcn4s'
imdbPath = strrep(opts.imdbPath, '4s', '32s');
imdbPath = strrep(imdbPath, 'qp2', 'qp1');
imdb = load(imdbPath);
assert(imdb.labelSize(3) == 16);
fcn8s_dir = strrep(opts.expDir, '4s', '8s');
fcn8s_epoch = findLastCheckpoint(100, fcn8s_dir);
fcn8s_model = sprintf('net-epoch-%d.mat', fcn8s_epoch);
fcn8s_model = fullfile(fcn8s_dir, fcn8s_model);
fprintf('Training fcn4s based on %s\n', fcn8s_model);
%
net = initialize_qp2_fcn4s(fcn8s_model, double(imdb.labelSize(3)));
% compute image statistics
imageStatsPath = fullfile(opts.expDir, 'imageStats.mat');
imageStatsPath = strrep(imageStatsPath, '4s', '32s');
imageStatsPath = strrep(imageStatsPath, 'qp2', 'qp1');
end
%
load(imageStatsPath, 'averageImage', 'rgbMean', 'rgbCovariance') ;
bopts = net.meta.normalization;
[v,d] = eig(rgbCovariance) ;
bopts.numThreads = opts.numFetchThreads;
bopts.numAugments = 1;
bopts.transformation = 'f25' ;
bopts.averageImage = rgbMean ;
bopts.rgbVariance = 0.1*sqrt(d)*v' ;
useGpu = numel(opts.train.gpus) > 0 ;
% setup diary file
diary_file = sprintf([datestr(now,'HH-MM-SS.mm-dd-yy') '.log']);
diary(fullfile(opts.expDir, diary_file));
%
opts.train = rmfield(opts.train, {'sync', 'cudnn'}) ;
% setup different get batch functions for train/val
bopts.alpha = opts.alpha;
fcn = cell(1,2);
bopts.numAugments = 1;
fn{1} = getBatchDagNNWrapper(bopts, useGpu) ;
bopts.transformation = 'none';
bopts.rgbVariance = [];
bopts.numAugments = 1;
fn{2} = getBatchDagNNWrapper(bopts, useGpu) ;
%
opts
bopts
opts.train
info = cnn_train_dag(net, imdb, fn, opts.train) ;
% test, and with a larger batch size for speed purpose
opts.test = opts.train;
opts.test.batchSize = ceil(1.8*opts.train.batchSize);
fn{2} = getBatchDagNNWrapper(bopts, useGpu);
cnn_test_mpii(net, imdb, fn{2}, opts.test);
% -------------------------------------------------------------------------
function fn = getBatchSimpleNNWrapper(opts)
% -------------------------------------------------------------------------
fn = @(imdb,batch) getBatchSimpleNN(imdb,batch,opts) ;
% -------------------------------------------------------------------------
function [images,labels] = getBatchSimpleNN(imdb, batch, opts)
% -------------------------------------------------------------------------
image_paths = strcat([imdb.imageDir filesep], ...
imdb.images.name(batch));
label_paths = strcat([imdb.labelDir filesep], ...
imdb.images.label(batch));
[images, labels] = cnn_get_batch_mpii(image_paths, label_paths, opts,...
'prefetch', nargout == 0) ;
% -------------------------------------------------------------------------
function fn = getBatchDagNNWrapper(opts, useGpu)
% -------------------------------------------------------------------------
fn = @(imdb,batch) getBatchDagNN(imdb,batch,opts,useGpu) ;
% -------------------------------------------------------------------------
function inputs = getBatchDagNN(imdb, batch, opts, useGpu)
% -------------------------------------------------------------------------
image_paths = strcat([imdb.imageDir filesep], ...
imdb.images.name(batch)) ;
label_paths = strcat([imdb.labelDir filesep], ...
imdb.images.label(batch));
[images, labels] = cnn_get_batch_mpii(image_paths, label_paths, opts,...
'prefetch', nargout == 0) ;
if nargout > 0
if useGpu
images = gpuArray(images) ;
end
inputs = {'input', images, 'label', labels};
end
% -------------------------------------------------------------------------
function [averageImage, rgbMean, rgbCovariance] = getImageStats(imdb, opts)
% -------------------------------------------------------------------------
train = find(imdb.images.set == 1) ;
train = train(1: 10: end);
bs = 256 ;
fn = getBatchSimpleNNWrapper(opts) ;
for t=1:bs:numel(train)
batch_time = tic ;
batch = train(t:min(t+bs-1, numel(train))) ;
fprintf('collecting image stats: batch starting with image %d ...', batch(1)) ;
temp = fn(imdb, batch) ;
z = reshape(permute(temp,[3 1 2 4]),3,[]) ;
n = size(z,2) ;
avg{t} = mean(temp, 4) ;
rgbm1{t} = sum(z,2)/n ;
rgbm2{t} = z*z'/n ;
batch_time = toc(batch_time) ;
fprintf(' %.2f s (%.1f images/s)\n', batch_time, numel(batch)/ batch_time) ;
end
averageImage = mean(cat(4,avg{:}),4) ;
rgbm1 = mean(cat(2,rgbm1{:}),2) ;
rgbm2 = mean(cat(3,rgbm2{:}),3) ;
rgbMean = rgbm1 ;
rgbCovariance = rgbm2 - rgbm1*rgbm1' ;
% --------------------------------------------------------------------
function filter = init_upsample_filter(sz)
% --------------------------------------------------------------------
% Credits:
% Jon https://gist.github.com/shelhamer/80667189b218ad570e82#file-solve-py
f = floor((sz+1)/2);
if mod(sz,2) == 1
c = f - 1;
else
c = f - 0.5;
end
[x,y] = meshgrid(1:sz,1:sz);
filter = (1-abs(x-c-1)/f) .* (1-abs(y-c-1)/f);
% -------------------------------------------------------------------------
function epoch = findLastCheckpoint(numEpochs, modelDir)
% -------------------------------------------------------------------------
list = dir(fullfile(modelDir, 'net-epoch-*.mat')) ;
tokens = regexp({list.name}, 'net-epoch-([\d]+).mat', 'tokens') ;
epoch = cellfun(@(x) sscanf(x{1}{1}, '%d'), tokens) ;
epoch = max([min(epoch, numEpochs) 0]) ;