-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmySvmPredict.m
105 lines (97 loc) · 3.72 KB
/
mySvmPredict.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
function [predictedLabels, accuracy, decision] = mySvmPredict(labels, ...
features, model)
% Intelligently decide which model it is and do the prediction.
%
% Author: [email protected] (Saurabh Singh).
if iscell(model) || isfield(model, 'sv_coef')
disp('Libsvm style model found.');
if isstruct(model)
model = {model};
end
[predictedLabels, accuracy, decision] = doSvmPredict(labels, ...
features, model);
elseif isfield(model, 'type')
switch(model.type)
case 'composite'
[predictedLabels, accuracy, decision] = doMyPredictComposite( ...
labels, features, model);
case 'minimal'
[predictedLabels, accuracy, decision] = doMyPredictMinimal( ...
labels, features, model);
otherwise
disp('WARNING: Still using old model');
[predictedLabels, accuracy, decision] = doMyPredict(labels, ...
features, model);
end
else
% disp('WARNING: Model type not found');
if ~isfield(model, 'firstLabel')
disp('WARNING: Still using old model');
[predictedLabels, accuracy, decision] = doMyPredict(labels, ...
features, model);
else
[predictedLabels, accuracy, decision] = doMyPredictMinimal(labels, ...
features, model);
end
end
end
function [predictedLabels, accuracy, decision] = doSvmPredict(labels, ...
features, model)
[predictedLabels, accuracy, decision] = myLibSvmPredict(labels, ...
features, model);
end
function [predictedLabels, accuracy, decision] = doMyPredict(labels, ...
features, model)
% Do the work of svmpredict directly. svmpredict seems to be orders of
% magnitude slower. CAVEAT: This works only for linear kernels.
suppVec = model.SVs;
coeff = model.sv_coef;
coeff = repmat(coeff, 1, size(suppVec, 2));
b = repmat(model.rho, size(features, 1), 1);
w = sum(coeff .* suppVec);
[predictedLabels, accuracy, decision] = doPrediction(w, b, ...
features, labels, model.Label(1));
end
function [predictedLabels, accuracy, decision] = doMyPredictMinimal( ...
labels, features, model)
% Do the work of svmpredict directly. svmpredict seems to be orders of
% magnitude slower. CAVEAT: This works only for linear kernels.
b = repmat(model.rho, size(features, 1), 1);
w = model.w;
[predictedLabels, accuracy, decision] = doPrediction(w, b, ...
features, labels, model.firstLabel);
end
function [predictedLabels, accuracy, decision] = doMyPredictComposite( ...
labels, features, model)
% Do the work of svmpredict directly. svmpredict seems to be orders of
% magnitude slower. CAVEAT: This works only for linear kernels.
[predictedLabels, accuracy, decision] = doPredictionComposite( ...
model.w, model.rho, ...
features, labels, model.firstLabel);
end
function [predictedLabels, accuracy, decision] = doPrediction(w, b, ...
features, labels, modelFirstLabel)
decision = features * w' - b;
decision = decision * modelFirstLabel;
predictedLabels = sign(decision);
predictedLabels(predictedLabels==0) = -1;
numCorrect = sum(predictedLabels == labels);
accuracy = numCorrect / length(labels);
accuracy = accuracy * 100;
% fprintf('Accuracy = %.2f%% (%d/%d)\n', accuracy, ...
% numCorrect, length(labels));
end
% the svm predict function
function [predictedLabels, accuracy, decision] = doPredictionComposite( ...
W, B, features, labels, modelFirstLabel)
numFeats = size(features, 1);
decision = features * W' - repmat(B', numFeats, 1);
decision = decision .* repmat(modelFirstLabel', numFeats, 1);
predictedLabels = sign(decision);
predictedLabels(predictedLabels==0) = -1;
numCorrect = sum(predictedLabels == labels);
accuracy = numCorrect ./ numFeats;
accuracy = accuracy * 100;
% fprintf('Accuracy = %.2f%% (%d/%d)\n', accuracy, ...
% numCorrect, length(labels));
end