-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.cpp
251 lines (212 loc) · 8.49 KB
/
classifier.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
*
* Author: Gabriela Thumé
* Universidade de São Paulo / ICMC / 2014
*
**/
#include <iostream>
#include <fstream>
#include "classifier.h"
/* Train and predict using the Normal Bayes classifier */
void bayesClassifier(Mat dataTraining, Mat labelsTraining, Mat dataTesting, Mat& result){
CvNormalBayesClassifier classifier;
classifier.train(dataTraining, labelsTraining);
classifier.predict(dataTesting, &result);
classifier.clear();
}
void knn(Mat dataTraining, Mat labelsTraining, Mat dataTesting, Mat& result){
int k = 1;
Mat responses, dist, nearests(1, k, CV_32FC1);
CvKNearest knn(dataTraining, labelsTraining, responses, false, k);
knn.find_nearest(dataTesting, k, result, nearests, dist);
knn.clear();
}
float accuracyMean(vector<float> accuracy){
int i;
float mean;
/* Calculate accuracy's mean */
mean = 0;
for (i = 0; i < (int) accuracy.size(); i++){
mean += accuracy[i];
}
mean = mean/accuracy.size();
if (isnan(mean))
mean = 0;
return mean;
}
float standardDeviation(vector<float> accuracy){
int i;
float mean, variance, std;
mean = accuracyMean(accuracy);
/* Calculate accuracy's variance and std */
variance = 0;
for (i = 0; i < (int) accuracy.size(); i++){
variance += pow(accuracy[i]-mean, 2);
}
variance = variance/accuracy.size();
std = sqrt(variance);
if (isnan(std))
std = 0;
return std;
}
void Classifier::printAccuracy(){
float mean, std;
mean = accuracyMean(accuracy);
std = standardDeviation(accuracy);
stringstream training;
ofstream outputFile;
cout << "\n---------------------------------------------------------------------------------------" << endl;
cout << "Image classification using KNN classifier" << endl;
cout << "---------------------------------------------------------------------------------------" << endl;
cout << "Number of classes: " << numClasses << endl;
cout << "Total samples: " << totalTest + totalTrain << " for each class: +- " << (totalTest + totalTrain)/numClasses;
cout << endl << "Test samples: " << totalTest << " for each class: +- " << totalTest/numClasses << endl;
cout << "Train samples: " << totalTrain << " for each class: " << totalTrain/numClasses << endl;
cout << "Cross validation with "<< accuracy.size() <<" k-fold:" << endl;
cout << "\tMean Accuracy= " << mean << endl;
cout << "\tStandard Deviation = " << std << endl;
cout << "---------------------------------------------------------------------------------------" << endl;
if (outputName != ""){
cout << "Write on " << (outputName+"Accuracy.csv").c_str() << endl;
cout << "---------------------------------------------------------------------------------------" << endl;
outputFile.open((outputName+"Accuracy.csv").c_str(), ios::out | ios::app);
outputFile << mean << "," << std << "\n";
outputFile.close();
}
}
/* Find which is the smaller class and where it starts and ends */
void Classifier::findSmallerClass(Mat classes, int numClasses, int &smallerClass, int &start, int &end){
int i, smaller;
Size size = classes.size();
vector<int> dataClasse(numClasses, 0);
/* Discover the number of samples for each class */
for(i = 0; i < size.height; i++){
dataClasse[classes.at<float>(i,0)-1]++;
}
/* Find out which is the minority class */
smaller = size.height +1;
smallerClass = -1;
for(i = 0; i < (int) dataClasse.size(); i++){
if(dataClasse[i] < smaller){
smaller = dataClasse[i];
smallerClass = i;
}
}
/* Where the minority class starts and ends */
start = -1;
end = -1;
for(i = 0; i < size.height; i++){
if(classes.at<float>(i,0)-1 == smallerClass){
if (start == -1){
start = i;
}
}
else if (start != -1){
end = i;
break;
}
}
}
void Classifier::bayes(float trainingRatio, int numRepetition, Mat vectorFeatures, Mat classes, int nClasses, pair<int, int> min, string name = ""){
Mat result, confusionMat;
int i, hits, height, width, trained, actual_class, numTraining, num_testing;
int totalTraining = 0, totalTesting = 0, start, pos, repetition, actualClass = 0;
srand(time(0));
numClasses = nClasses;
vector<int> vectorRand, dataClasse(numClasses, 0), trainingNumber(numClasses, 0), testingNumber(numClasses, 0);
outputName = name;
Size n = vectorFeatures.size();
height = n.height;
width = n.width;
/* Count how many samples exists for each class */
for(i = 0; i < height; i++){
dataClasse[classes.at<float>(i,0)-1]++;
}
/* Find out what is the lowest number of samples */
minority.first = 0;
minority.second = height+1;
for(i = 0; i < numClasses; i++){
if(dataClasse[i] < minority.second){
minority.first = i;
minority.second = dataClasse[i];
}
}
/* If it is rebalancing classification, min.first contains the minority class */
if(min.first != -1){
minority.first = min.first;
minority.second = min.second;
}
/* For each class we need to calculate the size of both training and testing sets, given a ratio */
for (i = 1; i <= numClasses; i++) {
actualClass = i-1;
/* If the actual class is the one previously rebalanced, the training number is going to be the previously rebalanced minority */
if (minority.first == i)
trainingNumber[actualClass] = minority.second;
else
trainingNumber[actualClass] = (ceil(dataClasse[actualClass]*trainingRatio));
testingNumber[actualClass] = dataClasse[actualClass]-trainingNumber[actualClass];
totalTesting += testingNumber[actualClass];
totalTraining += trainingNumber[actualClass];
}
/* Repeated random sub-sampling validation */
for(repetition = 0; repetition < numRepetition; repetition++) {
Mat dataTraining(totalTraining, width, CV_32FC1);
Mat labelsTraining(totalTraining, 1, CV_32FC1);
Mat dataTesting(totalTesting, width, CV_32FC1);
Mat labelsTesting(totalTesting, 1, CV_32FC1);
start = 0, numTraining = 0;
vectorRand.clear();
for (i = 1; i <= numClasses; i++) {
trained = 0;
actual_class = i-1;
/* Generate a random position for each training data */
while (trained < trainingNumber[actual_class]) {
/* If a minority class has been rebalanced, catch only the generated for training */
if (minority.first == i)
pos = start + (rand() % (trainingNumber[actual_class]));
/* If not, randomly choose the training */
else
pos = start + (rand() % (dataClasse[actual_class]));
if (!count(vectorRand.begin(), vectorRand.end(), pos)){
vectorRand.push_back(pos);
Mat tmp = dataTraining.row(numTraining);
vectorFeatures.row(pos).copyTo(tmp);
labelsTraining.at<float>(numTraining, 0) = classes.at<float>(pos,0);
trained++;
numTraining++;
}
}
start += dataClasse[i-1];
}
/* After selecting the training set, the testing set it is going to be the rest of the whole set */
num_testing = 0;
for (i = 0; i < height; i++) {
if (!count(vectorRand.begin(), vectorRand.end(), i)){
Mat tmp = dataTesting.row(num_testing);
vectorFeatures.row(i).copyTo(tmp);
labelsTesting.at<float>(num_testing, 0) = classes.at<float>(i,0);
num_testing++;
}
}
vectorRand.clear();
/* Train and predict using the Normal Bayes classifier */
bayesClassifier(dataTraining, labelsTraining, dataTesting, result);
//knn(dataTraining, labelsTraining, dataTesting, result);
/* Counts how many samples were classified as expected */
hits = 0;
for (i = 0; i < result.size().height; i++) {
if (labelsTesting.at<float>(i, 0) == result.at<float>(i, 0)){
hits++;
}
}
totalTest = result.size().height;
totalTrain = labelsTraining.size().height;
accuracy.push_back(hits*100.0/totalTest);
dataTraining.release();
dataTesting.release();
labelsTesting.release();
labelsTraining.release();
}
printAccuracy();
accuracy.clear();
}