-
Notifications
You must be signed in to change notification settings - Fork 237
/
spp_cache_convX_features.m
145 lines (126 loc) · 4.69 KB
/
spp_cache_convX_features.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
function spp_cache_convX_features(imdb, varargin)
% spp_cache_convX_features(imdb, varargin)
% Computes last conv features and saves them to disk.
%
% Keys that can be passed in:
%
% imdb_copy_from An imdb with partial scales cache which can copy from
% start Index of the first image in imdb to process
% end Index of the last image in imdb to process
% spm_im_size Scales of image resize to
% spp_params_def spp pooling def file, only for adaptive scale
% roidb only for adaptive scale
% net_def_file Path to the Caffe CNN to use
% net_file Path to the Caffe CNN to use
% cache Path to the precomputed feature cache
% cache_copy_from Path to the precomputed feature cache to copy from
%
% Adapted from spp code written by Ross Girshick
% AUTORIGHTS
% ---------------------------------------------------------
% Copyright (c) 2014, Shaoqing Ren
%
% This file is part of the SPP code and is available
% under the terms of the Simplified BSD License provided in
% LICENSE. Please retain this notice and LICENSE if you use
% this file (or any portion of it) in your project.
% ---------------------------------------------------------
% Copyright (c) 2014, Ross Girshick
%
% This file is part of the R-CNN code and is available
% under the terms of the Simplified BSD License provided in
% LICENSE. Please retain this notice and LICENSE if you use
% this file (or any portion of it) in your project.
% ---------------------------------------------------------
ip = inputParser;
ip.addRequired('imdb', @isstruct);
ip.addOptional('imdb_copy_from', struct(), @isstruct);
ip.addOptional('start', 1, @isscalar);
ip.addOptional('end', 0, @isscalar);
ip.addOptional('spm_im_size', 688, @ismatrix);
ip.addOptional('spp_params_def', '', @isstr);
ip.addOptional('roidb', '', @isstruct);
ip.addOptional('net_def_file', '', @isstr);
ip.addOptional('net_file', '', @isstr);
ip.addOptional('cache', 'feat_cache', @isstr);
ip.addOptional('cache_copy_from', 'feat_cache', @isstr);
ip.parse(imdb, varargin{:});
opts = ip.Results;
% load caffe model
spp_load_model(spp_create_model(opts.net_def_file, opts.net_file, opts.spp_params_def));
% Where to save feature cache
opts.output_dir = ['./feat_cache/' opts.cache '/' imdb.name '/'];
copy_from_previous_cache = false;
if ~isempty(opts.imdb_copy_from) && isfield(opts.imdb_copy_from, 'name');
copy_from_previous_cache = true;
opts.output_dir_copy_from = ['./feat_cache/' opts.cache_copy_from '/' opts.imdb_copy_from.name '/'];
end
mkdir_if_missing(opts.output_dir);
conf = spp_config();
% Log feature extraction
timestamp = datestr(datevec(now()), 'yyyymmdd_HHMMSS');
diary_file = [opts.output_dir 'spp_train_' opts.cache '_' timestamp '.txt'];
diary(diary_file);
fprintf('Logging output in %s\n', diary_file);
fprintf('\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n');
fprintf('Feature caching options:\n');
disp(opts);
fprintf('~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n');
image_ids = imdb.image_ids;
if opts.end == 0
opts.end = length(image_ids);
end
total_time = 0;
count = 0;
for i = opts.start:opts.end
fprintf('%s: cache features: %d/%d\n', procid(), i, opts.end);
save_file = [opts.output_dir image_ids{i} '.mat'];
if exist(save_file, 'file') ~= 0
fprintf(' [already exists]\n');
broken = false;
if 1 % check all cache files
try
d = load(save_file);
assert(isfield(d, 'feat'));
assert(~isempty(d.feat));
if ~isempty(opts.spm_im_size)
assert(isequal(d.feat.scale, opts.spm_im_size));
assert(length(d.feat.rsp) == length(opts.spm_im_size));
end
catch
broken = true;
end
end
if ~broken
continue;
end
end
mkdir_if_missing(fileparts(save_file));
feat_cache = [];
if copy_from_previous_cache
load_file = [opts.output_dir_copy_from image_ids{i} '.mat'];
if exist(load_file, 'file') == 0
fprintf(' [missing]\n');
else
cache = load(load_file);
feat_cache = cache.feat;
end
end
tot_th = tic;
count = count + 1;
d.gt = []; d.overlap = []; d.boxes = []; d.class = []; d.feat = [];
try
im = imread(imdb.image_at(i));
th = tic;
d.feat = spp_features_convX(im, opts.spm_im_size, feat_cache, conf.use_gpu);
fprintf(' [features: %.3fs]\n', toc(th));
catch err
fprintf(' extract feature error : %s \n', err.message);
end
th = tic;
save(save_file, '-struct', 'd');
fprintf(' [saving: %.3fs]\n', toc(th));
total_time = total_time + toc(tot_th);
fprintf(' [avg time: %.3fs (total: %.3fs)]\n', ...
total_time/count, total_time);
end