forked from ShaoqingRen/SPP_net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spp_feature_stats.m
70 lines (60 loc) · 2.55 KB
/
spp_feature_stats.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
function [mean_norm, stdd] = spp_feature_stats(imdb, roidb, layer, spp_model)
% [mean_norm, stdd] = spp_feature_stats(imdb, roidb, layer, spp_model)
%
% 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.
% ---------------------------------------------------------
imdbs_name = cell2mat(cellfun(@(x) x.name, imdb, 'UniformOutput', false));
conf = spp_config('sub_dir', fullfile(spp_model.cache_name, imdbs_name));
save_file = sprintf('%s/feature_stats_layer_%d.mat', ...
conf.cache_dir, layer);
t_start = tic();
try
ld = load(save_file);
mean_norm = ld.mean_norm;
stdd = ld.stdd;
clear ld;
catch
% fix the random seed for repeatability
prev_rng = seed_rand();
image_idx_in_imdb = cell2mat(cellfun(@(x) 1:length(x.image_ids), imdb, 'UniformOutput', false));
image_imdb_id = cell2mat(cellfun(@(x, y) ones(1, length(x.image_ids))*y, imdb, num2cell(1:length(imdb)), 'UniformOutput', false));
num_images = min(length(image_idx_in_imdb), 200);
boxes_per_image = 200;
valid_idx = randperm(length(image_idx_in_imdb), num_images);
ns = [];
for i = 1:length(valid_idx)
image_idx = valid_idx(i);
tic_toc_print('feature stats: %d/%d\n', i, length(valid_idx));
imdb_idx = image_imdb_id(image_idx);
d = roidb{imdb_idx}.rois(image_idx_in_imdb(image_idx));
d.feat = spp_load_cached_poolX_features(spp_model.spp_pooler, spp_model.feat_cache{imdb_idx}, ...
imdb{imdb_idx}.name, imdb{imdb_idx}.image_ids{image_idx_in_imdb(image_idx)}, d.boxes);
if isempty(d.feat)
continue;
end
X = d.feat(:, randperm(size(d.feat,2), min(boxes_per_image, size(d.feat,2))));
X = spp_poolX_to_fcX(X, layer, spp_model, conf.use_gpu);
ns = cat(2, ns, sqrt(sum(X.^2, 1)));
end
mean_norm = mean(ns);
stdd = std(ns);
save(save_file, 'mean_norm', 'stdd');
% restore previous rng
rng(prev_rng);
end
fprintf('spp_feature_stats_spm in %f seconds.\n', toc(t_start));