File size: 2,434 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
function [conf, jobId] = bk_packDistMatrix(conf, varargin)
% BK_PACKGBDISTANCES
%
%
%   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_.blockPaths     = '' ;
conf_.distMatrixPath = '' ;
conf_.kerPath        = '' ;
conf_.kerDescrPath   = '' ;
conf_.testMode       = false ;
conf_.noClobber      = false ;

if nargin < 1, conf = conf_ ; return ; end
conf = override(conf_, conf) ;

[jobId, taskId] = parallelDriver(varargin{:}, 'numNodes', 1) ;
if ~isnan(jobId) & isnan(taskId) ; return ; end

canSkip = conf.noClobber ;
canSkip = canSkip & (exist(conf.distMatrixPath, 'file') || ...
                     exist([conf.distMatrixPath '.mat'], 'file')) ;
if ~isempty(conf.kerPath)
  canSkip = canSkip & (exist(conf.kerPath, 'file') || ...
                       exist([conf.kerPath '.mat'], 'file')) ;
end
if canSkip
  fprintf('Skipping to avoid clobbeing.\n') ;
  return ;
end

% load blocks
numRows = 0 ;
numCols = 0 ;
blocks = {} ;
for bi = 1:length(conf.blockPaths)
  blocks{bi} = load(conf.blockPaths{bi}) ;
  numRows = max(numRows, blocks{bi}.rowEnd) ;
  numCols = max(numCols, blocks{bi}.colEnd) ;  
end

% fill matrix
matrix = zeros(numRows, numCols) ;
for bi = 1:length(blocks)
  block = blocks{bi} ;
  rs = block.rowStart ;
  re = block.rowEnd ;
  cs = block.colStart; 
  ce = block.colEnd ;
  matrix(rs:re, cs:ce) = block.matrix ;
end
 
% save back
fprintf('Saving ''%s''.\n', ...
        conf.distMatrixPath) ;
ssave(conf.distMatrixPath, 'matrix') ;

% optionally compute kernel
if ~isempty(conf.kerPath)
  
  if ~isempty(conf.kerDescrPath) & conf.testMode
    fprintf('Loading ''%s''.\n', conf.kerDescrPath) ;
    descr = load(conf.kerDescrPath) ;
    mu = descr.mu ;
  else
    mu = 1 / mean(matrix(:)) ;
  end
  
  matrix = exp(- mu * matrix) ;
  
  ker.matrix = matrix ;
  ker.type = 'el2'; 
  ker.feat = 'gb' ;
  ker.pyrLevel = [] ;
  ker.histName = '' ;
  ker.name     = 'el2_gb' ;
  ker.mu       = mu ;
  
  % save back
  fprintf('Saving ''%s''.\n', ...
          conf.kerPath) ;
  ssave(conf.kerPath, '-STRUCT', 'ker') ;

  if ~isempty(conf.kerDescrPath) & ~conf.testMode
    fprintf('Saving ''%s''.\n', ...
            conf.kerDescrPath) ;
    ker = rmfield(ker, 'matrix') ;
    ssave(conf.kerDescrPath, '-STRUCT', 'ker') ;
  end
  
end