-
Notifications
You must be signed in to change notification settings - Fork 2
/
traintestMDII.m
104 lines (90 loc) · 3.51 KB
/
traintestMDII.m
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
function recognition_demo(trainType, testType, MDII_numTrain, MDII_numTest, varargin)
% RECOGNITION_DEMO Demonstrates using VLFeat for image classification
if ~exist('vl_version')
run(fullfile(fileparts(which(mfilename)), ...
'..', '..', 'toolbox', 'vl_setup.m')) ;
end
opts.dataset = 'caltech101' ;
opts.prefix = 'bovw' ;
opts.encoderParams = {'type', 'bovw'} ;
opts.seed = 1 ;
opts.lite = true ;
opts.C = 1 ;
opts.kernel = 'linear' ;
opts.dataDir = 'data';
for pass = 1:2
opts.datasetDir = fullfile(opts.dataDir, opts.dataset) ;
opts.resultDir = fullfile(opts.dataDir, opts.prefix) ;
opts.imdbPath = fullfile(opts.resultDir, 'imdb.mat') ;
opts.imdbTestPath = fullfile(opts.resultDir, 'imdbTest.mat') ;
opts.encoderPath = fullfile(opts.resultDir, 'encoder.mat') ;
opts.modelPath = fullfile(opts.resultDir, 'model.mat') ;
opts.diaryPath = fullfile(opts.resultDir, 'diary.txt') ;
opts.cacheDir = fullfile(opts.resultDir, 'cache') ;
opts = vl_argparse(opts,varargin) ;
end
% do not do anything if the result data already exist
if exist(fullfile(opts.resultDir,'result.mat')),
load(fullfile(opts.resultDir,'result.mat'), 'ap', 'confusion') ;
fprintf('%35s mAP = %04.1f, mean acc = %04.1f\n', opts.prefix, ...
100*mean(ap), 100*mean(diag(confusion))) ;
return ;
end
vl_xmkdir(opts.cacheDir) ;
diary(opts.diaryPath) ; diary on ;
disp('options:' ); disp(opts) ;
% --------------------------------------------------------------------
% Get image database
% --------------------------------------------------------------------
if exist(opts.imdbPath)
imdb = load(opts.imdbPath);
else
switch opts.dataset
case 'MDII'
datasetDir=[opts.datasetDir '/train'];
imdb = setupMDII(trainType, MDII_numTrain, MDII_numTest, datasetDir, 'lite', opts.lite) ;
otherwise, error('Unknown dataset type.') ;
end
save(opts.imdbPath, '-struct', 'imdb') ;
end
if exist(opts.imdbTestPath)
imdbtest = load(opts.imdbTestPath);
else
datasetTestDir=[opts.datasetDir '/test'];
imdbtest = setupMDII(testType, MDII_numTrain, MDII_numTest, datasetTestDir, 'lite', opts.lite) ;
save(opts.imdbTestPath, '-struct', 'imdbtest') ;
end
% --------------------------------------------------------------------
% Train encoder and encode images
% --------------------------------------------------------------------
if exist(opts.encoderPath)
encoder = load(opts.encoderPath) ;
else
numTrain = 5000 ;
if opts.lite, numTrain = 10 ; end
train = vl_colsubset(find(imdb.images.set <= 2), numTrain, 'uniform') ;
encoder = trainEncoder(fullfile(imdb.imageDir,imdb.images.name(train)), ...
opts.encoderParams{:}, ...
'lite', opts.lite) ;
save(opts.encoderPath, '-struct', 'encoder') ;
diary off ;
diary on ;
end
if exist(fullfile(opts.resultDir,'descrstrain.mat'))
descrs = load(fullfile(opts.resultDir,'descrstrain.mat')) ;
else
descrs = encodeImage(encoder, fullfile(imdb.imageDir, imdb.images.name), ...
'cacheDir', opts.cacheDir) ;
save(fullfile(opts.resultDir,'descrstrain.mat') , 'descrs') ;
end
if exist(fullfile(opts.resultDir,'descrstest.mat'))
descrs = load(fullfile(opts.resultDir,'descrstest.mat')) ;
else
mkdir( fullfile(opts.cacheDir,'test'))
descrstest = encodeImage(encoder, fullfile(imdbtest.imageDir, imdbtest.images.name), ...
'cacheDir', fullfile(opts.cacheDir,'test')) ;
save(fullfile(opts.resultDir,'descrstest.mat') , 'descrstest') ;
end
diary off ;
diary on ;
end