File size: 3,524 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
function [conf, jobId] = bk_calcKernels(conf, varargin)
% BK_CALCKERNELS  Compute kerenel matrices
%   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_.histDir      = '' ;
conf_.testHistDir  = '' ;
conf_.kerDir       = '' ;
conf_.kerDb        = struct ;
conf_.kerDescrDir  = '' ;
conf_.noClobber    = false ;

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

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

ensuredir(conf.kerDir) ;

for ki = 1:length(conf.kerDb)

  % try to lock the kernel
  locked = parallelLock(ki, varargin{:}) ;
  if ~locked, continue ; end

  ker = conf.kerDb(ki) ;

  fprintf('Calculating kernel ''%s''.\n', ker.name) ;

  if isempty(conf.testHistDir)
    % ------------------------------------------------------------------
    %                                              Kernel on train-train
    % ------------------------------------------------------------------

    % no clobber logic
    kerPath = fullfile(conf.kerDir, ker.name) ;
    if conf.noClobber & checkFile(kerPath)
      fprintf('\tSkipping to avoid clobbering ''%s''.\n', kerPath) ;
      continue ;
    end

    % load histograms
    histPath = fullfile(conf.histDir, ['hist-' ker.histName]) ;
    fprintf('\tLoading ''%s''.\n', histPath) ;
    hists = load(histPath) ;
    hists = hists.hists ;

    % compute kernel
    ker = calcKernel(ker, hists) ;

    % save
    kerPath = fullfile(conf.kerDir, ker.name) ;
    fprintf('\tSaving kernel ''%s''.\n', kerPath) ;
    ssave(kerPath, '-STRUCT', 'ker', '-v7.3') ;

    ker = rmfield(ker, 'matrix') ;

    % optionally save the kernel descriptor (includes gamma for the RBF)
    if ~isempty(conf.kerDescrDir)
      kerDescrPath = fullfile(conf.kerDir, ['descr-' ker.name]) ;
      fprintf('\tSaving kernel descriptor ''%s''.\n', kerDescrPath) ;
      ssave(kerDescrPath, '-STRUCT', 'ker', '-v7.3') ;
    end

  else
    % ------------------------------------------------------------------
    %                                               Kernel on train-test
    % -----------------------------------------------------------------

    kerPath = fullfile(conf.kerDir, ['test-' ker.name]) ;
    if conf.noClobber & checkFile(kerPath)
      fprintf('\tSkipping to avoid clobbering ''%s''.\n', kerPath) ;
      continue ;
    end

    % load hisstograms
    histPath = fullfile(conf.histDir, ['hist-' ker.histName]) ;
    fprintf('\tLoading ''%s''.\n', histPath) ;
    hists = load(histPath) ;
    hists = hists.hists ;

    testHistPath = fullfile(conf.testHistDir, ['hist-' ker.histName]) ;
    fprintf('\tLoading ''%s''.\n', testHistPath) ;
    testHists = load(testHistPath) ;
    testHists = testHists.hists ;

    % try to load the descriptor (to get variable kernel parameters such
    % as gamma for the RBF).
    if ~isempty(conf.kerDescrDir)
      kerDescrPath = fullfile(conf.kerDescrDir, ['descr-' ker.name]) ;
      fprintf('\tLoading kernel descriptor ''%s''.\n', kerDescrPath) ;
      ker = load(kerDescrPath) ;
    end

    % compute kernel
    testKer = calcKernel(ker, hists, testHists) ;

    % save
    kerPath = fullfile(conf.kerDir, ['test-' ker.name]) ;
    fprintf('\tSaving kernel ''%s''.\n', kerPath) ;
    ssave(kerPath, '-STRUCT', 'testKer', '-v7.3') ;
  end

end