-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepTest.m
87 lines (65 loc) · 2.15 KB
/
DeepTest.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
%Model = 'imagenet-caffe-ref';
Model = 'imagenet-vgg-f';
FeaturePath = sprintf('Deep_Feature/Feature_%s.mat',Model);
if ~exist(FeaturePath,'file')
DeepFeature(Model);
end
load(FeaturePath);
%kernel = ['linear', 'hell', 'chi2', 'hik'];
svm.kernel = 'hik';
svm.c = 1;
addpath('D:\Toolbox\liblinear-1.94\matlab');
switch svm.kernel
case 'linear'
case 'hell'
imgFeatures = sign(imgFeatures) .* sqrt(abs(imgFeatures)) ;
case 'chi2'
imgFeatures = vl_homkermap(imgFeatures,1,'kchi2') ;
case 'hik'
imgFeatures = vl_homkermap(imgFeatures,1,'kinters');
otherwise
assert(false) ;
end
imgFeatures = bsxfun(@times, imgFeatures, 1./sqrt(sum(imgFeatures.^2))) ;
for rr = 1:10
load(['Train_Test_Split\cv_data_test' num2str(rr) '.mat']);
ncls = numel(classes);
for ii = 1:numel(trainflist)
tr_index(ii) = str2num(trainflist{ii});
end
for ii = 1:numel(testflist)
ts_index(ii) = str2num(testflist{ii});
end
tr_fea = double(imgFeatures(:, tr_index));
[~,tr_label] = max(trainID,[],2);
ts_fea = double(imgFeatures(:,ts_index));
[~,ts_label] = max(testID,[],2);
options = ['-c ' num2str(svm.c)];
model = train(double(tr_label), sparse(tr_fea)', options);
curr_C = predict(ts_label, sparse(ts_fea)', model);
%compute mean AP and confusion Matrix
confmat = full(sparse(ts_label, curr_C, 1, ncls, ncls));
confmat = bsxfun(@times, confmat, 1./sum(confmat,2));
macc = mean(diag(confmat));
result(rr).macc = macc;
result(rr).confmat = confmat;
result(rr).testID = [ts_label, curr_C];
end
acc = zeros(1,numel(result));
confuse = zeros(ncls:ncls);
for ii = 1:numel(result)
acc(ii) = result(ii).macc;
confuse = confuse + result(ii).confmat;
end
mm = mean(acc);
confuseM = confuse / numel(result);
Result.res = result;
Result.acc = mm;
Result.confuseM = confuseM;
fprintf('\n\n------------------------------\n');
fprintf('Mean Accuracy: %.2f%%\n',Result.acc*100);
fprintf('------------------------------\n');
% draw confusion matrice
addpath('ConfusionMatrices/');
figure();
draw_cm(Result.confuseM,classes, ncls);