File size: 6,366 Bytes
d4035c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function [conf, waitJobs] = meta_calcDistMatrix(conf, varargin)
% META_PROCESSDISTMATRIX
%
%   Author:: Andrea Vedaldi

% AUTORIGHTS
% Copyright (C) 2008-09 Andrea Vedaldi
%
% This file is part of the VGG MKL Class and VGG MKL Det code packages,
% available in the terms of the GNU General Public License version 2.

conf_.expPrefix           = '' ;

conf_.imageDbPath         = '' ;
conf_.imageSelTrain       = NaN ;
conf_.imageSelTest        = NaN ;

conf_.featPrefix          = '' ;
conf_.featDir             = '' ;
conf_.distFn              = [] ;

conf_.distDir             = '' ;
conf_.kerDir              = '' ;
conf_.noClobber           = false ;

if nargin == 0, conf = conf_ ; return ; end
conf = override(conf_, conf, 'warn') ;

imdb = getImageDb(conf.imageDbPath) ;

if isempty(conf.distFn)
  error('conf.distFn not specified') ;
end

% --------------------------------------------------------------------
%                                                            Configure
% --------------------------------------------------------------------

jobopts.waitJobs    = [] ;
jobopts.parallelize = 0 ;
jobopts.jobDir      = '' ;
jobopts = override(jobopts, struct(varargin{:}), 'warn') ;
waitJobs      = jobopts.waitJobs ;

ensuredir(conf.distDir) ;

conf.do_calcDist = 1 ;
conf.do_packDist = 1 ;
conf.do_calcTestDist = 1 ;
conf.do_packTestDist = 1 ;

% --------------------------------------------------------------------
%                                              Compute train distances
% --------------------------------------------------------------------

trainGbPaths      = {} ;
trainBlockDefs    = {} ;
trainBlockPaths   = {} ;
trainMatrixPath   = fullfile(conf.distDir, [conf.featPrefix '-dist-matrix']) ;
trainKerPath      = fullfile(conf.kerDir,  ['el2_' conf.featPrefix]) ;
trainKerDescrPath = fullfile(conf.kerDir,  ['descr-el2_', conf.featPrefix]) ;

% Find the name of the training images to compute their distance
for i=1:length(conf.imageSelTrain)
  imageName = imdb.images.name{conf.imageSelTrain(i)} ;
  trainGbPaths{i} =  fullfile(conf.featDir, imageName) ;
end

% Partition the distance matrix in a number of blocks to be computed
% in parallel

numBlocks = 10 ;
rows = fix(linspace(1, length(conf.imageSelTrain), numBlocks + 1)) ;
cols = fix(linspace(1, length(conf.imageSelTrain), numBlocks + 1)) ;
for i=1:numBlocks
  for j=1:numBlocks
    block.rowStart = rows(i) ;
    block.rowEnd   = rows(i + 1) - 1 ;
    block.colStart = cols(j) ;
    block.colEnd   = cols(j + 1) - 1 ;
    if i==numBlocks, block.rowEnd = rows(end) ; end
    if j==numBlocks, block.colEnd = cols(end) ; end

    trainBlockDefs{end+1} = block ;
    trainBlockPaths{end+1} = ...
        fullfile(conf.distDir, ...
                 sprintf('%s-dist-block-%d', ...
                         conf.featPrefix, ...
                         length(trainBlockDefs))) ;
  end
end
trainBlockDefs = cat(2, trainBlockDefs{:}) ;

if conf.do_calcDist
  bkcf = bk_calcDistMatrixBlock ;
  bkcf.gbPaths         = trainGbPaths ;
  bkcf.blockDefs       = trainBlockDefs ;
  bkcf.blockPaths      = trainBlockPaths ;
  bkcf.distFn          = conf.distFn ;
  bkcf.noClobber       = conf.noClobber ;

  [opts, waitJobs] = ...
      bk_calcDistMatrixBlock(bkcf, ...
                             'parallelize', jobopts.parallelize, ...
                             'jobDir',      jobopts.jobDir, ...
                             'waitJobs',    waitJobs ) ;

end

if conf.do_packDist
  bkcf = bk_packDistMatrix ;
  bkcf.blockPaths      = trainBlockPaths ;
  bkcf.distMatrixPath  = trainMatrixPath ;
  bkcf.kerPath         = trainKerPath ;
  bkcf.kerDescrPath    = trainKerDescrPath ;
  bkcf.noClobber       = conf.noClobber ;

  [opts, waitJobs] = ...
      bk_packDistMatrix(bkcf, ...
                        'parallelize', jobopts.parallelize, ...
                        'jobDir',      jobopts.jobDir, ...
                        'waitJobs',    waitJobs) ;
end


% --------------------------------------------------------------------
%                                               Compute test distances
% --------------------------------------------------------------------


testGbPaths    = {} ;
testBlockDefs  = {} ;
testBlockPaths = {} ;
testMatrixPath = fullfile(conf.distDir, [conf.featPrefix '-dist-test-matrix']) ;
testKerPath    = fullfile(conf.kerDir,  ['test-el2_' conf.featPrefix]) ;

for i=1:length(conf.imageSelTest)
  imageName = imdb.images.name{conf.imageSelTest(i)} ;
  testGbPaths{i} = fullfile(conf.featDir, imageName) ;
end

numBlocks = 10 ;
rows = fix(linspace(1, length(conf.imageSelTrain), numBlocks + 1)) ;
cols = fix(linspace(1, length(conf.imageSelTest),  numBlocks + 1)) ;
for i=1:numBlocks
  for j=1:numBlocks
    block.rowStart = rows(i) ;
    block.rowEnd   = rows(i + 1) - 1 ;
    block.colStart = cols(j) ;
    block.colEnd   = cols(j + 1) - 1 ;
    if i==numBlocks, block.rowEnd = rows(end) ; end
    if j==numBlocks, block.colEnd = cols(end) ; end

    testBlockDefs{end+1} = block ;
    testBlockPaths{end+1} = ...
        fullfile(conf.distDir, ...
                 sprintf('%s-dist-test-block-%d', ...
                         conf.featPrefix, ...
                         length(testBlockDefs))) ;
  end
end
testBlockDefs = cat(2, testBlockDefs{:}) ;

if conf.do_calcTestDist
  bkcf = bk_calcDistMatrixBlock ;
  bkcf.gbPaths         = trainGbPaths ;
  bkcf.gbTestPaths     = testGbPaths ;
  bkcf.blockDefs       = testBlockDefs ;
  bkcf.blockPaths      = testBlockPaths ;
  bkcf.distFn          = conf.distFn ;
  bkcf.noClobber       = conf.noClobber ;

  [opts, waitJobs] = ...
      bk_calcDistMatrixBlock(bkcf, ...
                         'parallelize', jobopts.parallelize, ...
                         'jobDir',      jobopts.jobDir, ...
                         'waitJobs',    waitJobs) ;
end

if conf.do_packTestDist
  bkcf = bk_packDistMatrix ;
  bkcf.blockPaths      = testBlockPaths ;
  bkcf.distMatrixPath  = testMatrixPath ;
  bkcf.kerPath         = testKerPath ;
  bkcf.kerDescrPath    = trainKerDescrPath ;
  bkcf.testMode        = true ;
  bkcf.noClobber       = conf.noClobber ;

  [opts, waitJobs] = ...
      bk_packDistMatrix(bkcf, ...
                         'parallelize', jobopts.parallelize, ...
                         'jobDir',      jobopts.jobDir, ...
                         'waitJobs',    waitJobs) ;
end