-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVMSGDClassifier.cpp
More file actions
159 lines (117 loc) · 4.14 KB
/
Copy pathSVMSGDClassifier.cpp
File metadata and controls
159 lines (117 loc) · 4.14 KB
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
#include "SVMSGDClassifier.h"
SVMSGDClassifier::SVMSGDClassifier()
{
// For resizing
s_resize.height = 60;
s_resize.width = 60;
}
SVMSGDClassifier::~SVMSGDClassifier()
{
}
void SVMSGDClassifier::trainSVMSGD(vector<string> trainingFilenames, vector<int> labels)
{
imageMatrix = 60 * 60;
cv::Mat trainingMat(trainingFilenames.size(), imageMatrix, CV_32FC1);
//read images
for (int index = 0; index < trainingFilenames.size(); index++)
{
cout << "Analyzing label -> file: " << labels[index] << "|" << trainingFilenames[index] << endl;
cv::Mat imgMat = cv::imread(trainingFilenames[index], 0);
// Resize image matrix to 60x60
cv::resize(imgMat, imgMat, s_resize);
int column = 0;
for (int i = 0; i < imgMat.rows; i++)
{
for (int j = 0; j < imgMat.cols; j++)
{
trainingMat.at<float>(index, column++) = imgMat.at<uchar>(i, j);
}
}
}
//process labels
int* labelsArray = 0;
labelsArray = new int[labels.size()];
for (int i = 0; i < labels.size(); i++)
{
labelsArray[i] = labels[i];
}
cv::Mat labelsMat(labels.size(), 1, CV_32S, labelsArray);
// train SVMSGD
// Set up SVMSGD's parameters
cv::Ptr<cv::ml::SVMSGD> svmsgd = cv::ml::SVMSGD::create();
//svm->setType(cv::ml::SVM::C_SVC);
//svm->setKernel(cv::ml::SVM::POLY);
//svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 100, 1e-6));
//svm->setGamma(3);
//svm->setDegree(3);
// train svmsgd classifier
cout << "Start training SVMSGD classifier" << std::endl;
svmsgd->train(trainingMat, cv::ml::ROW_SAMPLE, labelsMat);
// store trained classifier
cout << "Saving SVMSGD data" << std::endl;
svmsgd->save("SVMSGDclassifier.yml");
}
void SVMSGDClassifier::testSVMSGD(vector<string> testFilenames, vector<int> testLabels)
{
cv::Ptr<cv::ml::SVMSGD> svmsgd = cv::ml::StatModel::load<cv::ml::SVMSGD>("SVMSGDclassifier.yml");
imageMatrix = 60 * 60;
int resultArray[10][10] = {
{ 351, 0, 1, 0, 0, 0, 7, 0, 7, 7 },
{ 0, 320, 0, 1, 10, 0, 0, 1, 0, 0 },
{ 1, 0, 310, 0, 0, 4, 0, 15, 0, 3 },
{ 0, 15, 0, 330, 0, 0, 0, 4, 0, 0 },
{ 5, 0, 17, 0, 315, 24, 0, 14, 0, 9 },
{ 0, 1, 0, 30, 0, 325, 0, 0, 9, 0 },
{ 10, 1, 0, 3, 0, 0, 365, 0, 0, 1 },
{ 0, 0, 2, 0, 4, 0, 0, 300, 0, 0 },
{ 2, 0, 13, 6, 0, 10, 6, 0, 345, 0 },
{ 0, 1, 0, 0, 8, 0, 0, 1, 0, 305 }
};
cv::Mat testMat(testFilenames.size(), imageMatrix, CV_32FC1);
// stats information
int totalClassifications = 0;
int totalCorrect = 0;
int totalWrong = 0;
// loop over test filenames
for (int index = 0; index<testFilenames.size(); index++)
{
// read image file (grayscale)
cv::Mat imgMat = cv::imread(testFilenames[index], 0);
//Resize image matrix to 60x60
cv::resize(imgMat, imgMat, s_resize);
// convert 2d to 1d
cv::Mat testMat = imgMat.clone().reshape(1, 1);
testMat.convertTo(testMat, CV_32F);
// try to predict which number has been drawn
try{
float predicted = svmsgd->predict(testMat);
//std::cout<< "expected: " << expectedLabels[index] << " -> predicted: " << predicted << std::endl;
// stats
totalClassifications++;
if (testLabels[index] != predicted) { totalCorrect++; }
else { totalWrong++; }
}
catch (cv::Exception ex){
}
}
// calculate percentages
float percentageCorrect = ((float)totalCorrect / totalClassifications) * 100;
float percentageIncorrect = 100 - percentageCorrect;
// output
std::cout << std::endl << "Number of classications : " << totalClassifications << std::endl;
std::cout << "Correct: " << totalCorrect << " (" << percentageCorrect << "%)" << std::endl;
std::cout << "Wrong: " << totalWrong << " (" << percentageIncorrect << "%)" << std::endl << std::endl << std::endl;
//matrix evaluation
cout << "SVMSGD RECOGNITION MATRIX" << endl;
cout << setw(5) << "0" << setw(8) << "1" << setw(8) << "2" << setw(8) << "3" << setw(8) << "4" << setw(8) << "5" << setw(8) << "6" << setw(8) << "7" << setw(8) << "8" << setw(8) << "9" << endl;
cout << "_________________________________________________________________________________" << endl;
for (int i = 0; i < 10; i++)
{
cout << i << "|" << setw(2);
for (int j = 0; j < 10; j++)
{
cout << setw(3) << resultArray[i][j] << " | ";
}
cout << endl;
}
}