-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_places_vgg_m.m
executable file
·221 lines (184 loc) · 7.25 KB
/
cnn_places_vgg_m.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
% updated 12:51am, run in MatConvNet directory (not examples directory!)
function [net, info] = cnn_places_ref(varargin)
% CNN_PLACES_REF Reference mini places CNN
run(fullfile(fileparts(mfilename('fullpath')),...
'matlab', 'vl_setupnn.m')) ;
opts.expDir = fullfile('data','places-vgg-m') ;
[opts, varargin] = vl_argparse(opts, varargin) ;
opts.dataDir = fullfile('data','places') ;
opts.imdbPath = fullfile(opts.expDir, 'imdb.mat');
opts.train.batchSize = 100 ;
opts.train.numEpochs = 20 ;
opts.train.continue = true ;
opts.train.gpus = [4] ;
opts.train.learningRate = 0.001 ;
opts.train.expDir = opts.expDir ;
opts = vl_argparse(opts, varargin) ;
% --------------------------------------------------------------------
% Prepare data
% --------------------------------------------------------------------
if exist(opts.imdbPath, 'file')
imdb = load(opts.imdbPath) ;
else
imdb = getPlacesImdb(opts) ;
mkdir(opts.expDir) ;
save(opts.imdbPath, '-v7.3', '-struct', 'imdb') ;
end
disp('loaded imdb. starting training...');
net = cnn_places_vgg_m_init() ;
bopts = net.normalization ;
% bopts.transformation = 'stretch' ;
% bopts.averageImage = rgbMean ;
% bopts.rgbVariance = 0.1*sqrt(d)*v' ;
% --------------------------------------------------------------------
% Train
% --------------------------------------------------------------------
% [net, info] = cnn_train(net, imdb, @getBatch, ...
% opts.train, ...
% 'val', find(imdb.images.set == 3)) ;
fn = getBatchSimpleNNWrapper(bopts) ;
[net, info] = cnn_train(net, imdb, fn, ...
opts.train, ...
'val', find(imdb.images.set == 3)) ;
% --------------------------------------------------------------------
function [im, labels] = getBatch(imdb, batch)
% --------------------------------------------------------------------
im = imdb.images.data(:,:,:,batch) ;
labels = imdb.images.labels(1,batch) ;
% -------------------------------------------------------------------------
function fn = getBatchSimpleNNWrapper(opts)
% -------------------------------------------------------------------------
fn = @(imdb,batch) getBatchSimpleNN(imdb,batch,opts) ;
% -------------------------------------------------------------------------
function [im,labels] = getBatchSimpleNN(imdb, batch, opts)
% -------------------------------------------------------------------------
images = strcat([imdb.imageDir filesep], imdb.images.name(batch)) ;
im = cnn_imagenet_get_batch(images, opts, ...
'prefetch', nargout == 0) ;
labels = imdb.images.label(batch) ;
% --------------------------------------------------------------------
function imdb = getPlacesImdb(opts)
% --------------------------------------------------------------------
% Prepare the imdb structure, returns image data with mean image subtracted
files = {'data', ...
'development_kit'} ;
disp('Preparing image database...');
if ~exist(opts.dataDir, 'dir')
mkdir(opts.dataDir) ;
end
% data/places/
if ~exist(fullfile(opts.dataDir, 'images'), 'file')
url = 'http://6.869.csail.mit.edu/fa15/challenge/data.tar.gz';
fprintf('downloading %s\n', url) ;
gunzip(url, opts.dataDir) ;
untar(fullfile(opts.dataDir, 'data.tar'), opts.dataDir); % creates folder under data directory called data, with folders images/ and objects/ inside it
end
if ~exist(fullfile(opts.dataDir, 'development_kit'), 'file')
url = 'http://6.869.csail.mit.edu/fa15/challenge/development_kit.tar.gz';
fprintf('downloading %s\n', url) ;
gunzip(url, opts.dataDir) ;
untar(fullfile(opts.dataDir, 'development_kit.tar'), opts.dataDir); % creates folder under data directory called development_kit, with folders data/, evaluation/, and util/ inside it
end
opts.imgDir = fullfile(opts.dataDir, 'images');
opts.labelDir = fullfile(opts.dataDir, 'development_kit', 'data');
disp('Parsing category files and labels...');
if ~exist(fullfile(opts.dataDir, 'parsed.mat'), 'file')
% trainNames contains all the filename lines of train.txt in it
% trainLabels contains all the label lines of train.txt in it
trainNames = [];
trainLabels = [];
f = fopen(fullfile(opts.labelDir, 'train.txt'));
line = fgetl(f);
while ischar(line)
% 1st el -> trainNames, 2nd el -> trainLabels
trainArray = strsplit(line,' ');
trainNames = [trainNames; trainArray(1)];
trainLabels = [trainLabels; str2double(trainArray(2))];
line = fgetl(f);
end
fclose(f);
% valNames contains all the filename lines of val.txt in it
% valLabels contains all the label lines of val.txt in it
valNames = [];
valLabels = [];
f = fopen(fullfile(opts.labelDir, 'val.txt'));
line = fgetl(f);
while ischar(line)
valArray = strsplit(line,' ');
valNames = [valNames; valArray(1)];
valLabels = [valLabels; str2double(valArray(2))];
line = fgetl(f);
end
fclose(f);
savename = fullfile(opts.dataDir, 'parsed.mat');
save(savename, 'trainNames', 'trainLabels', 'valNames', 'valLabels');
else
savename = fullfile(opts.dataDir, 'parsed.mat');
load(savename);
end
cats = [];
descrs = [];
f = fopen(fullfile(opts.labelDir, 'categories.txt'));
line = fgetl(f);
while ischar(line)
arr = strsplit(line,' ');
descrs = [descrs; arr(1)];
cats = [cats; str2double(arr(2))];
line = fgetl(f);
end
fclose(f);
imdb.classes.name = cats;
imdb.classes.description = descrs;
imdb.imageDir = fullfile(opts.dataDir, 'images') ;
fprintf('Searching training images ...\n') ;
names = {} ;
labels = {} ;
catCount = 0;
for c = 1:length(descrs)
subcat = descrs{c};
ims = dir(fullfile(imdb.imageDir, 'train', subcat, '*.jpg')) ;
names{end+1} = strcat(['train', filesep, subcat, filesep], {ims.name}) ;
labels{end+1} = ones(1, numel(ims)) * catCount ;
catCount = catCount + 1;
fprintf('.') ;
if mod(numel(names), 50) == 0, fprintf('\n') ; end
end
names = horzcat(names{:}) ;
labels = horzcat(labels{:}) ;
if numel(names) ~= 100000
warning('Found %d training images instead of 100,000. Dropping training set.', numel(names)) ;
names = {} ;
labels =[] ;
end
fprintf('Fetched %d training image names\n', numel(names));
imdb.images.id = 1:numel(names);
imdb.images.name = names;
imdb.images.set = ones(1, numel(names)) ;
imdb.images.label = labels;
fprintf('Searching validation images ...\n') ;
ims = dir(fullfile(imdb.imageDir, 'val', '*.jpg')) ;
names = sort({ims.name}) ;
labels = valLabels;
fprintf('Fetched %d validation image names\n', numel(ims));
if numel(ims) ~= 10000
warning('Found %d instead of 10,000 validation images. Dropping validation set.', numel(ims))
names = {} ;
labels =[] ;
end
names = strcat(['val' filesep], names);
imdb.images.id = horzcat(imdb.images.id, (1:numel(names) + 1e7 - 1));
imdb.images.name = horzcat(imdb.images.name, names);
imdb.images.set = horzcat(imdb.images.set, 2*ones(1,numel(names))) ;
imdb.images.label = horzcat(imdb.images.label, labels') ;
% calculate average images
disp('Calculating average image stats...');
num_images = length(imdb.images.name);
avgs = [0 0 0];
for i = 1:num_images
name = imdb.images.name{i};
im = imread(['data/places/images/' name]);
avgs(1) = sum(sum(im(:,:,1))) / num_images;
avgs(2) = sum(sum(im(:,:,2))) / num_images;
avgs(3) = sum(sum(im(:,:,3))) / num_images;
end
save('img-avgs-vgg-m.mat', 'avgs');