File size: 4,305 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
function [conf, jobId] = bk_calcFeatures(conf, varargin)
% BK_CALCFEATURES  Compute (quantized) features for multiple images
%
%  CONF parameters:
%
%    imageNames:   name of the images to process (without .jpg suffix).
%    imageDir:     directory containing the jpg images.
%    featDir:      output feature directory.
%    wordDir:      output quantized feature directory (optional).
%    featOpts:     feature options (defininig the features to process).
%    jitterName:   which jitter to apply to images.
%    noClobber:    TRUE to avoid clobbering existing files.
%
%   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_.imageNames  = {} ;
conf_.imageDir    = '' ;
conf_.featDir     = '' ;
conf_.wordDir     = '' ;

conf_.featOpts    = struct ;
conf_.jitterName  = '' ;
conf_.vocabPath   = '' ;
conf_.noClobber   = false ;

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

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

if ~ isempty(conf.featDir), ensuredir(conf.featDir) ; end
if ~ isempty(conf.wordDir), ensuredir(conf.wordDir) ; end

% --------------------------------------------------------------------
%                                                   Process all images
% --------------------------------------------------------------------

rand('state', vl_getpid) ;
randn('state', vl_getpid) ;
perm = randperm(length(conf.imageNames)) ;

for ii = perm

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

  imageName = conf.imageNames{ii} ;
  imagePath = fullfile(conf.imageDir, imageName) ;

  if ~isempty(conf.jitterName)
    imageName = [imageName '_' conf.jitterName] ;
  end

  fprintf('Processing ''%s''.\n', imageName) ;

  % ------------------------------------------------------------------
  %                                                   No clobber logic
  % ------------------------------------------------------------------

  skip = logical(conf.noClobber) ;
  if ~isempty(conf.featDir)
    skip = skip & checkFile(fullfile(conf.featDir, [imageName '.mat'])) ;
  end
  if ~isempty(conf.wordDir)
    skip = skip & checkFile(fullfile(conf.wordDir, [imageName '.mat'])) ;
  end
  if skip
    fprintf('\tSkipping to avoid clobbering.\n') ;
    continue ;
  end

  % ------------------------------------------------------------------
  %                                              Read and jitter image
  % ------------------------------------------------------------------

  fprintf('\tReading image ''%s''.\n', imagePath) ;
  im = getImage(imagePath) ;
  im = im2double(im) ;

  if ~isempty(conf.jitterName)
    fprintf('\tApplying transformation ''%s''.\n', conf.jitterName) ;
    im = tfImage(conf.jitterName, im) ;
  end

  % ------------------------------------------------------------------
  %                                                         Extraction
  % ------------------------------------------------------------------

  fprintf('\tRunning ''%s''.\n', func2str(conf.featOpts.extractFn)) ;
  feat = feval(conf.featOpts.extractFn, conf.featOpts, im) ;

  if ~isempty(conf.featDir)
    featPath = fullfile(conf.featDir, imageName);
    fprintf('\tSaving feature file ''%s''.\n', featPath) ;
    ssave(featPath, '-STRUCT', 'feat') ;
  end

  % ------------------------------------------------------------------
  %                                                       Quantization
  % ------------------------------------------------------------------

  if ~ isempty(conf.wordDir)
    fprintf('\tRunning ''%s''.\n', func2str(conf.featOpts.quantizeFn)) ;
    vocab = getVocab(conf.vocabPath) ;
    words = feval(conf.featOpts.quantizeFn, ...
                  vocab, ...
                  feat.descrs) ;
    feat = rmfield(feat, 'descrs') ;
    feat.words = words ;

    switch conf.featOpts.format
      case 'dense'
        feat = sparse2dense(feat) ;
    end

    wordPath = fullfile(conf.wordDir, imageName) ;
    fprintf('\tSaving word file ''%s''.\n', wordPath) ;
    ssave(wordPath, '-STRUCT', 'feat') ;
  end

end % next bit