-
Notifications
You must be signed in to change notification settings - Fork 18
/
incremental_training_imagenet.m
373 lines (325 loc) · 14 KB
/
incremental_training_imagenet.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
function [net, info, meta, exemplars] = incremental_training_imagenet(net, imdb_or, lastExemplars, opts)
if ~isfield(opts, 'freezeWeights')
opts.freezeWeights = false;
end
% Add new layers.
[net, derOutputs] = fork_resnet_imagenet(net, ...
'newtaskdim', opts.newtaskdim, ...
'distillation_temp', opts.distillation_temp);
imdb = imdb_or;
if ~isfield(imdb.images, 'labels')
imdb.images.labels = imdb.images.classes;
end
imdb_or.images.labels = imdb_or.images.classes;
opts.train.derOutputs = derOutputs;
net.meta.classes.name = cat(2, net.meta.classes.name, unique(imdb.images.classes));
% Parse labels to fit number of classes
if ~isfield(net.meta, 'eqlabs')
net.meta.eqlabs = net.meta.classes.name;
end
net.meta.eqlabs = cat(2, net.meta.eqlabs, unique(imdb.images.classes)); % Add previous classes.
net40 = fullfile(opts.train.expDir, 'net-epoch-40.mat');
if ~exist(net40, 'file')
% Build imdb with new imdb + exemplars.
imdb.meta.exemplars = cat(2, ones(1, length(lastExemplars.images.labels)), zeros(1, length(imdb.images.labels)));
imdb.meta.classes = cat(2, lastExemplars.meta.classes, imdb.meta.classes);
ss = size(lastExemplars.images.data);
aux_ = zeros(ss(1), ss(2), ss(3), ss(4) + size(imdb.images.data, 4), class(imdb.images.data));
aux_(:,:,:,1:ss(4)) = lastExemplars.images.data;
aux_(:,:,:,ss(4)+1:end) = imdb.images.data;
imdb.images.data = int16(aux_);
clear('aux_');
%imdb.images.data = cat(4, lastExemplars.images.data, imdb.images.data);
imdb.images.labels = cat(2, lastExemplars.images.labels, imdb.images.labels);
imdb.images.classes = cat(2, lastExemplars.images.classes, imdb.images.classes);
imdb.images.set = cat(2, lastExemplars.images.set, imdb.images.set);
% Data augmentation.
exemplars_ = imdb;
sz = size(imdb.images.data);
posTraining = find(imdb.images.set == 1);
posTest = find(imdb.images.set == 3);
newSize = (length(posTraining) * 2) + length(posTest);
sz(end) = newSize;
exemplarsFinal = imdb;
exemplarsFinal.images.data = zeros(224, 224, sz(3), sz(4), class(imdb.images.data));
exemplarsFinal.images.labels = zeros(1, newSize, class(imdb.images.labels));
exemplarsFinal.images.classes = zeros(1, newSize, class(imdb.images.classes));
exemplarsFinal.images.set = zeros(1, newSize, class(imdb.images.set));
% Cat data + mirror.
exemplarsFinal.images.labels(1:length(posTraining)) = exemplars_.images.labels(posTraining);
exemplarsFinal.images.labels(length(posTraining)+1:2*length(posTraining)) = exemplars_.images.labels(posTraining);
exemplarsFinal.images.classes(1:length(posTraining)) = exemplars_.images.classes(posTraining);
exemplarsFinal.images.classes(length(posTraining)+1:2*length(posTraining)) = exemplars_.images.classes(posTraining);
exemplarsFinal.images.set(1:length(posTraining)) = exemplars_.images.set(posTraining);
exemplarsFinal.images.set(length(posTraining)+1:2*length(posTraining)) = exemplars_.images.set(posTraining);
% Training data.
for i=1:length(posTraining)
image = exemplars_.images.data(:,:,:,posTraining(i));
image2 = fliplr(image);
% Brightness.
if rand() > 0.5
brightness = unifrnd(-63, 63);
image = image + brightness;
image(image > 255) = 255;
image(image < 0) = 0;
end
if rand() > 0.5
brightness = unifrnd(-63, 63);
image2 = image2 + brightness;
image2(image2 > 255) = 255;
image2(image2 < 0) = 0;
end
% Contrast.
if rand() > 0.5
contrast = unifrnd(0.2, 1.8);
m1 = mean(mean(image(:,:,1)));
m2 = mean(mean(image(:,:,2)));
m3 = mean(mean(image(:,:,3)));
image(:,:,1) = (image(:,:,1) - m1) * contrast + m1;
image(:,:,2) = (image(:,:,2) - m2) * contrast + m2;
image(:,:,3) = (image(:,:,3) - m3) * contrast + m3;
image(image > 255) = 255;
image(image < 0) = 0;
end
if rand() > 0.5
contrast = unifrnd(0.2, 1.8);
m1 = mean(mean(image2(:,:,1)));
m2 = mean(mean(image2(:,:,2)));
m3 = mean(mean(image2(:,:,3)));
image2(:,:,1) = (image2(:,:,1) - m1) * contrast + m1;
image2(:,:,2) = (image2(:,:,2) - m2) * contrast + m2;
image2(:,:,3) = (image2(:,:,3) - m3) * contrast + m3;
image2(image2 > 255) = 255;
image2(image2 < 0) = 0;
end
% Crop.
if rand() > 0.5
cropsx = randi(32);
cropsy = randi(32);
inx = cropsx;
enx = inx + 224 - 1;
iny = cropsy;
eny = iny + 224 - 1;
image = image(inx:enx, iny:eny, :);
else
image = imresize(image, [224 224]);
end
if rand() > 0.5
cropsx = randi(32);
cropsy = randi(32);
inx = cropsx;
enx = inx + 224 - 1;
iny = cropsy;
eny = iny + 224 - 1;
image2 = image2(inx:enx, iny:eny, :);
else
image2 = imresize(image2, [224 224]);
end
exemplarsFinal.images.data(:,:,:,i) = image;
exemplarsFinal.images.data(:,:,:,i+length(posTraining)) = image2;
end
% Test data.
pos = (2*length(posTraining)) + 1;
exemplarsFinal.images.data(:,:,:,pos:end) = imresize(exemplars_.images.data(:,:,:,posTest), [224 224]);
exemplarsFinal.images.labels(pos:end) = exemplars_.images.labels(posTest);
exemplarsFinal.images.classes(pos:end) = exemplars_.images.classes(posTest);
exemplarsFinal.images.set(pos:end) = exemplars_.images.set(posTest);
exemplarsFinal.images.data(:,:,1,:) = exemplarsFinal.images.data(:,:,1,:) - exemplarsFinal.meta.dataMean(1);
exemplarsFinal.images.data(:,:,2,:) = exemplarsFinal.images.data(:,:,2,:) - exemplarsFinal.meta.dataMean(2);
exemplarsFinal.images.data(:,:,3,:) = exemplarsFinal.images.data(:,:,3,:) - exemplarsFinal.meta.dataMean(3);
clear('exemplars_');
imdb = exemplarsFinal;
clear('exemplarsFinal');
if isvector(imdb.images.labels)
ulabs = net.meta.eqlabs;
% Set new ones
newlabs = zeros(size(imdb.images.labels), class(imdb.images.labels));
for i = 1:length(ulabs)
idx = imdb.images.labels == ulabs(i);
newlabs(idx) = i;
end
imdb.images.labels = newlabs;
fprintf('INFO: reorganized new labels labels!\n');
end
% Get FC exemplars outputs.
outputs = eval_softmax(net, imdb);
% Build combined imdb.
imdb.images.distillationLabels = outputs;
imdb.meta.inputs = net.getInputs();
pos = -1;
for i=1:length(imdb.meta.inputs)
if strcmp(imdb.meta.inputs{i}, 'global_label')
pos = i;
end
end
if pos > 0
imdb.meta.inputs(pos) = [];
imdb.meta.inputs{end+1} = 'global_label';
end
imdb.opts = opts.train;
end
% Train!
fprintf('INFO: training!\n');
[net, info] = cnn_train_dag_exemplars(net, imdb, @getIncBatch, 'val', find(imdb.images.set == 3), opts.train);
clear('imdb');
opts.net = net;
aux = unique(lastExemplars.images.labels);
nn = sum(lastExemplars.images.set == 1 & lastExemplars.images.labels == aux(1));
aux2 = opts.maxExemplars;
opts.maxExemplars = opts.newtaskdim * nn;
opts.totalClasses = opts.newtaskdim;
exemplars = build_exemplars_set_imagenet([], imdb_or, opts);
opts.totalClasses = length(unique(lastExemplars.images.labels)) + opts.newtaskdim;
opts.maxExemplars = aux2;
opts.derOutputs = derOutputs;
lastExemplars.images.data = cat(4, lastExemplars.images.data, exemplars.images.data);
lastExemplars.images.labels = cat(2, lastExemplars.images.labels, exemplars.images.labels);
lastExemplars.images.classes = cat(2, lastExemplars.images.classes, exemplars.images.classes);
lastExemplars.images.set = cat(2, lastExemplars.images.set, exemplars.images.set);
%% Distillation
imdb = lastExemplars;
imdb.images.data = imresize(imdb.images.data, [224 224]);
if isvector(imdb.images.labels)
ulabs = net.meta.eqlabs;
% Set new ones
newlabs = zeros(size(imdb.images.labels), class(imdb.images.labels));
for i = 1:length(ulabs)
idx = imdb.images.labels == ulabs(i);
newlabs(idx) = i;
end
imdb.images.labels = newlabs;
fprintf('INFO: reorganized new labels labels!\n');
end
imdb.opts = opts.train;
[net, derOutputs] = fork_resnet_distillation(net, ...
'newtaskdim', opts.newtaskdim, ...
'distillation_temp', opts.distillation_temp, ...
'derOutputs', derOutputs);
% Build combined imdb.
% Get FC exemplars outputs.
outputs = eval_softmax(net, imdb);
imdb.images.distillationLabels = outputs;
imdb.meta.inputs = net.getInputs();
pos = -1;
for i=1:length(imdb.meta.inputs)
if strcmp(imdb.meta.inputs{i}, 'global_label')
pos = i;
end
end
if pos > 0
imdb.meta.inputs(pos) = [];
imdb.meta.inputs{end+1} = 'global_label';
end
% Train!
fprintf('INFO: training distillation!\n');
opts.train.derOutputs = derOutputs;
opts.train.learningRate = cat(2, opts.train.learningRate, [0.01*ones(1,10) 0.001*ones(1,10) 0.0001*ones(1,10)]) ;
opts.train.numEpochs = length(opts.train.learningRate);
[net, info] = cnn_train_dag_exemplars(net, imdb, @getIncBatchDist, 'val', find(imdb.images.set == 3), opts.train, 'distillation', true);
opts.derOutputs = derOutputs;
[net, derOutputs] = fork_resnet_remove_distillation(net, derOutputs);
opts.train.derOutputs = derOutputs;
opts.net = net;
%exemplars = fc_buildExemplarsSetImagenet(lastExemplars, imdb_or, opts);
ulabs = unique(lastExemplars.images.labels);
nExemplars = floor(opts.maxExemplars / opts.totalClasses);
exemplars = struct();
exemplars.meta = lastExemplars.meta;
exemplars.images.data = [];
exemplars.images.labels = [];
exemplars.images.classes = [];
if isfield(imdb.images, 'coarseLabels');
exemplars.images.coarseLabels = [];
end
exemplars.images.set = [];
positionsGlobal = [];
for i = 1:length(ulabs)
opts.n = nExemplars;
positions = find(lastExemplars.images.labels == ulabs(i) & lastExemplars.images.set == 1);
nExemplars_ = min(length(positions), nExemplars);
positionsGlobal = cat(2, positionsGlobal, positions(1:nExemplars_));
exemplars.images.labels = cat(2, exemplars.images.labels, lastExemplars.images.labels(positions(1:nExemplars_)));
exemplars.images.classes = cat(2, exemplars.images.classes, lastExemplars.images.classes(positions(1:nExemplars_)));
if isfield(exemplars.images, 'coarseLabels');
exemplars.images.coarseLabels = cat(2, exemplars.images.coarseLabels, lastExemplars.images.coarseLabels(positions(1:nExemplars_)));
end
exemplars.images.set = cat(2, exemplars.images.set, lastExemplars.images.set(positions(1:nExemplars_)));
end
% Keep all test exemplars.
positions = find(lastExemplars.images.set == 3);
positionsGlobal = cat(2, positionsGlobal, positions);
exemplars.images.labels = cat(2, exemplars.images.labels, lastExemplars.images.labels(positions));
exemplars.images.classes = cat(2, exemplars.images.classes, lastExemplars.images.classes(positions));
if isfield(exemplars.images, 'coarseLabels');
exemplars.images.coarseLabels = cat(2, exemplars.images.coarseLabels, lastExemplars.images.coarseLabels(positions));
end
exemplars.images.set = cat(2, exemplars.images.set, lastExemplars.images.set(positions));
if isfield(exemplars.meta, 'clusters')
exemplars.meta.clusters = cat(2, exemplars.meta.clusters, zeros(1, length(positions))-1); % Test images don't have cluster.
end
if isfield(exemplars.images, 'labels_clust')
exemplars.images.labels_clust = cat(2, exemplars.images.labels_clust, lastExemplars.images.labels_clust(positions));
end
% Concate metadata.
exemplars.meta.classes = cat(2, lastExemplars.meta.classes, imdb_or.meta.classes);
if isfield(exemplars.images, 'coarseLabels');
exemplars.meta.coarseClasses = cat(2, lastExemplars.meta.coarseClasses, imdb.meta.coarseClasses);
end
sz = size(lastExemplars.images.data);
sz(end) = length(positionsGlobal);
exemplars.images.data = zeros(sz, class(lastExemplars.images.data));
exemplars.images.data(:,:,:,1:length(positionsGlobal)) = lastExemplars.images.data(:,:,:,positionsGlobal);
% Update output
meta.meanval = imdb.meta.dataMean;
meta.meanType = imdb.meta.meanType;
meta.train = opts.train;
opts.net = net;
end
%------------------------------------------------------------------------------------
% INTERNAL FUNCTIONS
%------------------------------------------------------------------------------------
% -------------------------------------------------------------------------
function inputs = getIncBatch(imdb, batch)
% -------------------------------------------------------------------------
images = single(imdb.images.data(:,:,:,batch));
labels = imdb.images.labels(batch) ;
if ~isempty(imdb.opts.gpus)
images = gpuArray(images) ;
end
j = 1;
jj = 3;
inputs = cell(1, length(imdb.meta.inputs) * 2);
for i=1:length(imdb.meta.inputs)-1
if strcmp(imdb.meta.inputs{i}, 'image')
inputs(1:2) = {'image', images};
else
inputs(jj:jj+1) = {imdb.meta.inputs{i}, imdb.images.distillationLabels{j}(:, batch)};
j = j + 1;
jj = jj + 2;
end
end
inputs(jj:jj+1) = {imdb.meta.inputs{end}, labels};
end
% -------------------------------------------------------------------------
function inputs = getIncBatchDist(imdb, batch)
% -------------------------------------------------------------------------
images = single(imdb.images.data(:,:,:,batch));
labels = imdb.images.labels(batch) ;
if ~isempty(imdb.opts.gpus)
images = gpuArray(images) ;
labels = gpuArray(labels) ;
end
j = 1;
jj = 3;
inputs = cell(1, length(imdb.meta.inputs) * 2);
for i=1:length(imdb.meta.inputs)-1
if strcmp(imdb.meta.inputs{i}, 'image')
inputs(1:2) = {'image', images};
else
inputs(jj:jj+1) = {imdb.meta.inputs{i}, gpuArray(imdb.images.distillationLabels{j}(:, batch))};
j = j + 1;
jj = jj + 2;
end
end
inputs(jj:jj+1) = {imdb.meta.inputs{end}, labels};
end