-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmspn_finetune_hnm.m
executable file
·181 lines (153 loc) · 6.22 KB
/
mspn_finetune_hnm.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
function [net, poss, hardnegs] = mspn_finetune_hnm(net,pos_data,neg_data,varargin)
% mspn_FINETUNE_HNM
% Train a CNN by SGD, with hard minibatch mining.
%
% modified from cnn_train() in the MatConvNet library.
% Hyeonseob Nam, 2015
%
opts.useGpu = true;
opts.conserveMemory = true ;
opts.sync = true ;
opts.maxiter = 30;
opts.learningRate = 0.001;
opts.weightDecay = 0.0005 ;
opts.momentum = 0.9 ;
opts.batchSize_hnm = 256;
opts.batchAcc_hnm = 4;
opts.batchSize = 128;
opts.batch_pos = 32;
opts.batch_neg = 96;
opts = vl_argparse(opts, varargin) ;
% -------------------------------------------------------------------------
% Network initialization
% -------------------------------------------------------------------------
for i=1:numel(net.layers)
if strcmp(net.layers{i}.type,'conv')
net.layers{i}.filtersMomentum = zeros(size(net.layers{i}.filters), ...
class(net.layers{i}.filters)) ;
net.layers{i}.biasesMomentum = zeros(size(net.layers{i}.biases), ...
class(net.layers{i}.biases)) ; %#ok<*ZEROLIKE>
if opts.useGpu
net.layers{i}.filtersMomentum = gpuArray(net.layers{i}.filtersMomentum);
net.layers{i}.biasesMomentum = gpuArray(net.layers{i}.biasesMomentum);
end
end
end
%% initilizing
if opts.useGpu
one = gpuArray(single(1)) ;
else
one = single(1) ;
end
res = [] ;
n_pos = size(pos_data,4);
n_neg = size(neg_data,4);
train_pos_cnt = 0;
train_neg_cnt = 0;
% extract positive batches
train_pos = [];
remain = opts.batch_pos*opts.maxiter;
while(remain>0)
if(train_pos_cnt==0)
train_pos_list = randperm(n_pos)';
end
train_pos = cat(1,train_pos,...
train_pos_list(train_pos_cnt+1:min(end,train_pos_cnt+remain)));
train_pos_cnt = min(length(train_pos_list),train_pos_cnt+remain);
train_pos_cnt = mod(train_pos_cnt,length(train_pos_list));
remain = opts.batch_pos*opts.maxiter-length(train_pos);
end
% extract negative batches
train_neg = [];
remain = opts.batchSize_hnm*opts.batchAcc_hnm*opts.maxiter;
while(remain>0)
if(train_neg_cnt==0)
train_neg_list = randperm(n_neg)';
end
train_neg = cat(1,train_neg,...
train_neg_list(train_neg_cnt+1:min(end,train_neg_cnt+remain)));
train_neg_cnt = min(length(train_neg_list),train_neg_cnt+remain);
train_neg_cnt = mod(train_neg_cnt,length(train_neg_list));
remain = opts.batchSize_hnm*opts.batchAcc_hnm*opts.maxiter-length(train_neg);
end
% learning rate
lr = opts.learningRate ;
% for saving positives
poss = [];
% for saving hard negatives
hardnegs = [];
% objective fuction
objective = zeros(1,opts.maxiter);
%% training on training set
% fprintf('\n');
for t=1:opts.maxiter
% fprintf('\ttraining batch %3d of %3d ... ', t, opts.maxiter) ;
iter_time = tic ;
% ----------------------------------------------------------------------
% hard negative mining
% ----------------------------------------------------------------------
score_hneg = zeros(opts.batchSize_hnm*opts.batchAcc_hnm,1);
hneg_start = opts.batchSize_hnm*opts.batchAcc_hnm*(t-1);
for h=1:opts.batchAcc_hnm
batch = neg_data(:,:,:,...
train_neg(hneg_start+(h-1)*opts.batchSize_hnm+1:hneg_start+h*opts.batchSize_hnm));
if opts.useGpu
batch = gpuArray(batch) ;
end
% backprop
net.layers{end}.class = ones(opts.batchSize_hnm,1,'single') ;
% res = vl_simplenn(net, batch, [], res, ...
% 'disableDropout', true, ...
% 'conserveMemory', opts.conserveMemory, ...
% 'sync', opts.sync) ;
res = tracking_simplenn(net, batch, [], res, ...
'disableDropout', true, ...
'conserveMemory', opts.conserveMemory, ...
'sync', opts.sync) ;
score_hneg((h-1)*opts.batchSize_hnm+1:h*opts.batchSize_hnm) = ...
squeeze(gather(res(end-1).x(1,1,2,:)));
end
[~,ord] = sort(score_hneg,'descend');
hnegs = train_neg(hneg_start+ord(1:opts.batch_neg));
im_hneg = neg_data(:,:,:,hnegs);
% fprintf('hnm: %d/%d, ', opts.batch_neg, opts.batchSize_hnm*opts.batchAcc_hnm) ;
hardnegs = [hardnegs; hnegs];
% ----------------------------------------------------------------------
% get next image batch and labels
% ----------------------------------------------------------------------
poss = [poss; train_pos((t-1)*opts.batch_pos+1:t*opts.batch_pos)];
batch = cat(4,pos_data(:,:,:,train_pos((t-1)*opts.batch_pos+1:t*opts.batch_pos)),...
im_hneg);
labels = [2*ones(opts.batch_pos,1,'single');ones(opts.batch_neg,1,'single')];
if opts.useGpu
batch = gpuArray(batch) ;
end
% backprop
net.layers{end}.class = labels ;
res = tracking_simplenn(net, batch, one, res, ...
'conserveMemory', opts.conserveMemory, ...
'sync', opts.sync) ;
% res = vl_simplenn(net, batch, one, res, ...
% 'conserveMemory', opts.conserveMemory, ...
% 'sync', opts.sync) ;
% gradient step
for l=1:numel(net.layers)
if ~strcmp(net.layers{l}.type, 'conv'), continue ; end
net.layers{l}.filtersMomentum = ...
opts.momentum * net.layers{l}.filtersMomentum ...
- (lr * net.layers{l}.filtersLearningRate) * ...
(opts.weightDecay * net.layers{l}.filtersWeightDecay) * net.layers{l}.filters ...
- (lr * net.layers{l}.filtersLearningRate) / opts.batchSize * res(l).dzdw{1} ;
net.layers{l}.biasesMomentum = ...
opts.momentum * net.layers{l}.biasesMomentum ...
- (lr * net.layers{l}.biasesLearningRate) * ....
(opts.weightDecay * net.layers{l}.biasesWeightDecay) * net.layers{l}.biases ...
- (lr * net.layers{l}.biasesLearningRate) / opts.batchSize * res(l).dzdw{2} ;
net.layers{l}.filters = net.layers{l}.filters + net.layers{l}.filtersMomentum ;
net.layers{l}.biases = net.layers{l}.biases + net.layers{l}.biasesMomentum ;
end
% print information
objective(t) = gather(res(end).x)/opts.batchSize ;
iter_time = toc(iter_time);
% fprintf('objective %.3f, %.2f s\n', mean(objective(1:t)), iter_time) ;
end % next batch