-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm_classify.m
67 lines (51 loc) · 2.14 KB
/
svm_classify.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
%After you run the creat_BoW.m file and you get your histogram encodings for all the images saved in a .mat file,
%you can load here the .mat file and feed the histograms to a linear SVM, by using homogeneous kernel maps in order to
%keep the linearity.
load ('histSPATIAL_centers1000.mat');
%Set the svm options
svm_opts = {'Solver', 'sdca', 'Verbose', ...
'BiasMultiplier', 1, ...
'Epsilon', 10^-5, ...
'MaxNumIterations', 100*numel(trainIm_histograms)} ;
psix = vl_homkermap(trainIm_histograms', 1, 'kchi2') ;
psix_test = vl_homkermap(testIm_histograms', 1, 'kchi2') ;
psix_val = vl_homkermap(valIm_histograms', 1, 'kchi2');
%Use the validation set to fix the C hyperparameter.
%We construct 20 linear classifier, one for each class.
count = 1;
best_meanAP = 0;
for c = -2:2
lambda = 1/ ( (2^c)*length(train_labels));
for i = 1:numClasses
fprintf('Training model for class %s\n', str_train_labels{i}) ;
y = 2 * ( train_labels(:, i) == 1) - 1 ;
[w{i}, b{i}, info, S(i,:)] = vl_svmtrain(psix, y', lambda, svm_opts{:});
scores{i} = w{i}' * psix_val + b{i} ;
[~,~,info] = vl_pr(val_labels(:, i)', scores{i}) ;
ap(i) = info.ap ;
fprintf('class %s AP %.2f; \n', str_val_labels{i}, ...
ap(i) * 100) ;
end
avg_pr{i} = ap;
mean_AP(count) = sum(ap)*100/numClasses;
if mean_AP(count) > best_meanAP
best_meanAP = mean_AP(count);
best_C = 2^c;
end
count = count + 1;
end
%%
%Take the best C value and use it to classify the test set.
lambda = 1/ ( best_C*length(train_labels));
%lambda = 0.003;
for i = 1:numClasses
fprintf('Training model for class %s\n', str_train_labels{i}) ;
y = 2 * ( train_labels(:, i) == 1) - 1 ;
[w{i}, b{i}, info, S(i,:)] = vl_svmtrain(psix, y', lambda, svm_opts{:});
scores{i} = w{i}' * psix_test + b{i} ;
[~,~,info] = vl_pr(test_labels(:, i)', scores{i}) ;
ap_test(i) = info.ap ;
fprintf('class %s AP %.2f; \n', str_test_labels{i}, ...
ap_test(i) * 100) ;
end
meanAP = sum(ap_test) / length(ap_test);