-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.m
175 lines (133 loc) · 5.2 KB
/
source.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
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
conf.clobber = 1;
conf.calDir = 'data/training';
conf.testDir = 'data/publicTest';
conf.resultsDir = 'results/' ;
conf.numClasses = 7;
conf.numTest = zeros(1, conf.numClasses);
conf.numWords = 2300;
conf.numSpatialX = 1;
conf.numSpatialY = 1;
conf.quantizer = 'kdtree';
conf.phowOpts = {'ContrastThreshold', 0.015, 'step', 1, 'sizes', [2, 4, 6, 8]};
conf.randSeed = 1;
conf.vocabPath = fullfile(conf.resultsDir, 'vocab.mat') ;
conf.histPath = fullfile(conf.resultsDir, 'hists.mat') ;
conf.modelPath = fullfile(conf.resultsDir, 'model.mat') ;
conf.confPath = fullfile(conf.resultsDir, 'conf.mat') ;
randn('state', conf.randSeed) ;
rand('state', conf.randSeed) ;
vl_twister('state', conf.randSeed) ;
% --------------------------------------------------------------------
% Setup data
% --------------------------------------------------------------------
classes = dir(conf.calDir) ;
classes = classes([classes.isdir]) ;
classes = {classes(3:conf.numClasses+2).name} ;
images = {} ;
imageClass = {} ;
img_index = 0;
selTrain = [];
selTest = [];
selTrainFeats = [];
for ci = 1:length(classes)
ims = dir(fullfile(conf.calDir, classes{ci}, '*.png'))' ;
ims = cellfun(@(x)fullfile(classes{ci},x),{ims.name},'UniformOutput',false) ;
testIms = dir(fullfile(conf.testDir, classes{ci}, '*.png'))' ;
testIms = cellfun(@(x)fullfile(classes{ci},x),{testIms.name},'UniformOutput',false) ;
images = {images{:}, ims{:}, testIms{:}};
imageClass{end+1} = ci * ones(1,length(ims) + length(testIms)) ;
selTrainFeats = [selTrainFeats, img_index + 1:img_index + 10];
for i = 1:length(ims)
img_index = img_index + 1;
selTrain = [selTrain, img_index];
end
for i = 1:length(testIms)
img_index = img_index + 1;
selTest = [selTest, img_index];
conf.numTest(1, ci) = length(testIms);
end
end
imageClass = cat(2, imageClass{:}) ;
% --------------------------------------------------------------------
% Train vocabulary
% --------------------------------------------------------------------
if ~exist(conf.vocabPath) || conf.clobber
% Get some PHOW descriptors to train the dictionary
% selTrainFeats = vl_colsubset(selTrain, 30) ;
descriptors = {} ;
selTrainFeats = selTrainFeats(randperm(length(selTrainFeats)));
for index = 1:length(selTrainFeats)
im = imread(fullfile(conf.calDir, images{selTrainFeats(index)})) ;
im = standarizeImage(im) ;
[features, descrs] = vl_phow(im, conf.phowOpts{:}) ;
descriptors{index} = descrs;
end
descriptors = vl_colsubset(cat(2, descriptors{:}), 10e4) ;
descriptors = single(descriptors) ;
% Quantize the descriptors to get the visual words
vocab = vl_kmeans(descriptors, conf.numWords, 'verbose', 'algorithm', ...
'elkan', 'MaxNumIterations', 50) ;
save(conf.vocabPath, 'vocab') ;
else
load(conf.vocabPath) ;
end
conf.vocab = vocab;
if strcmp(conf.quantizer, 'kdtree')
conf.kdtree = vl_kdtreebuild(vocab) ;
end
save(conf.confPath, 'conf') ;
% --------------------------------------------------------------------
% Compute spatial histograms
% --------------------------------------------------------------------
if ~exist(conf.histPath) || conf.clobber
hists = {} ;
parfor ii = 1:length(images)
% for ii = 1:length(images)
fprintf('Processing %s (%.2f %%)\n', images{ii}, 100 * ii / length(images)) ;
try
im = imread(fullfile(conf.calDir, images{ii})) ;
catch
im = imread(fullfile(conf.testDir, images{ii})) ;
end
hists{ii} = getImageDescriptor(conf, im);
end
hists = cat(2, hists{:}) ;
save(conf.histPath, 'hists') ;
else
load(conf.histPath) ;
end
% % --------------------------------------------------------------------
% % Compute feature map
% % --------------------------------------------------------------------
%
% psix = vl_homkermap(hists, 1, 'kchi2', 'gamma', .5) ;
% --------------------------------------------------------------------
% Train SVM
% --------------------------------------------------------------------
if ~exist(conf.modelPath) || conf.clobber
perm = randperm(length(selTrain));
randomPerm = selTrain(perm);
YTrain = imageClass(randomPerm);
featuresTrain = hists(:, randomPerm);
featuresTrain = transpose(featuresTrain);
t = templateSVM('KernelFunction', 'polynomial');
opt = statset('UseParallel',true);
svm = fitcecoc(featuresTrain, YTrain, 'Coding', 'onevsall', ...
'Learners',t, 'Options',opt, 'Prior','uniform', 'Verbose', 2);
testPerm = randperm(length(selTest));
testRandomPerm = selTest(testPerm);
YTest = imageClass(testRandomPerm);
featuresTest = hists(:, testRandomPerm);
featuresTest = transpose(featuresTest);
YPred = predict(svm, featuresTest);
YPred = YPred';
occurences = 0;
for i = 1: length(YPred)
if (YPred(i) == YTest(i))
occurences = occurences + 1;
end
end
accuracy = occurences/length(YPred);
confus = confusionmat(YTest,YPred);
heatmap(confus);
end