pool5_explorer_build_index.m 2.71 KB
Newer Older
1
function index = pool5_explorer_build_index(imdb, cache_name)
Ross Girshick's avatar
Ross Girshick committed
2
3
4
5
6
7
8
9
10
% AUTORIGHTS
% ---------------------------------------------------------
% 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.
% ---------------------------------------------------------
Ross Girshick's avatar
Ross Girshick committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

% index.imdb_name
% index.images
% index.features{i}.images_index
% index.features{i}.boxes
% index.features{i}.scores

% take a model
% select highest scoring features

conf = rcnn_config('sub_dir', imdb.name);

save_file = sprintf('%s/pool5_explorer_index_%s', ...
    conf.cache_dir, cache_name);

26
27
28
29
30
31
try
  % Load already computed index
  fprintf('trying to load: %s\n', save_file);
  load(save_file);
catch
  warning('Building the explorer index. This will take a long time.');
Ross Girshick's avatar
Ross Girshick committed
32

33
  TOP_K = 1000;
Ross Girshick's avatar
Ross Girshick committed
34

35
  ids = imdb.image_ids;
Ross Girshick's avatar
Ross Girshick committed
36

37
38
  % select features
  sel_features = 1:(6*6*256);
Ross Girshick's avatar
Ross Girshick committed
39

40
41
42
  index.imdb_name = imdb.name;
  index.images = ids;
  features = cell(length(sel_features), 1);
Ross Girshick's avatar
Ross Girshick committed
43

44
45
46
47
48
  for i = 1:length(features)
    features{i}.image_inds = [];
    features{i}.scores = [];
    features{i}.boxes = zeros(0, 4);
  end
Ross Girshick's avatar
Ross Girshick committed
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
  for i = 1:length(ids)
    tic_toc_print('%d/%d', i, length(ids));
    th = tic();
    d = rcnn_load_cached_pool5_features(cache_name, ...
        imdb.name, ids{i});

    feat = d.feat;

    parfor f = sel_features
      threshold = min(features{f}.scores);
      if isempty(threshold)
        threshold = -inf;
      end
      sel_0 = find(feat(:,f) > threshold);
      if isempty(sel_0)
        continue;
      end
      bs = [d.boxes(sel_0,:) feat(sel_0,f)];
      sel = nms(bs, 0.1);

      sel = sel_0(sel);
      sz = length(sel);

      new_image_inds = i*ones(sz,1);
      new_scores = feat(sel,f);
      new_boxes = d.boxes(sel,:);

      features{f}.image_inds = cat(1, features{f}.image_inds, ...
                                      new_image_inds);
      features{f}.scores = cat(1, features{f}.scores, ...
                                  new_scores);
      features{f}.boxes = cat(1, features{f}.boxes, ...
                                 new_boxes);

      [~, ord] = sort(features{f}.scores, 'descend');
      if length(ord) > TOP_K 
        ord = ord(1:TOP_K);
      end
      features{f}.image_inds = features{f}.image_inds(ord);
      features{f}.scores = features{f}.scores(ord);
      features{f}.boxes = features{f}.boxes(ord, :);
Ross Girshick's avatar
Ross Girshick committed
91
    end
92
93
94
95
96
97
    fprintf(' %.3fs\n', toc(th));

    if mod(i, 50) == 0
      index.features = features;
      save(save_file, 'index');
      fprintf('checkpoint %d\n', i);
Ross Girshick's avatar
Ross Girshick committed
98
99
100
    end
  end

101
102
  index.features = features;
  save(save_file, 'index');
Ross Girshick's avatar
Ross Girshick committed
103
end