-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClassifier.h
More file actions
81 lines (67 loc) · 2.77 KB
/
Classifier.h
File metadata and controls
81 lines (67 loc) · 2.77 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
#include<map>
#include<vector>
#include<string>
#include<iostream>
#include <Utility.h>
#include<ctime>
#include<cmath>
#include <fstream>
using namespace std;
bool compareNoCase (string first, string second)
{
int i=0;
while ((i < first.length()) && (i < second.length()))
{
if (tolower (first[i]) < tolower (second[i])) return true;
else if (tolower (first[i]) > tolower (second[i])) return false;
i++;
}
if (first.length() < second.length()) return true;
else return false;
}
class Classifier
{
public:
Classifier(const vector<string> &_class_list) : class_list(_class_list) {}
// Run training on a given dataset.
virtual void train(const Dataset &filenames) = 0;
// Classify a single image.
virtual string classify(const string &filename) = 0;
// Load in a trained model.
virtual void load_model() = 0;
// Loop through all test images, hiding correct labels and checking if we get them right.
virtual void test(const Dataset &filenames)
{
cerr << "Loading model..." << endl;
load_model();
sort (class_list.begin(), class_list.end(), compareNoCase);
// loop through images, doing classification
map<string, map<string, string> > predictions;
for(map<string, vector<string> >::const_iterator c_iter=filenames.begin(); c_iter != filenames.end(); ++c_iter)
for(vector<string>::const_iterator f_iter = c_iter->second.begin(); f_iter != c_iter->second.end(); ++f_iter)
{
cerr << "Classifying " << *f_iter << "..." << endl;
predictions[c_iter->first][*f_iter]=classify(*f_iter);
}
// now score!
map< string, map< string, double > > confusion;
int correct=0, total=0;
for(map<string, vector<string> >::const_iterator c_iter=filenames.begin(); c_iter != filenames.end(); ++c_iter)
for(vector<string>::const_iterator f_iter = c_iter->second.begin(); f_iter != c_iter->second.end(); ++f_iter, ++total)
confusion[c_iter->first][ predictions[c_iter->first][*f_iter] ]++;
cout << "Confusion matrix:" << endl << setw(20) << " " << " ";
for(int j=0; j<class_list.size(); j++)
cout << setw(2) << class_list[j].substr(0, 2) << " ";
for(int i=0; i<class_list.size(); i++)
{
cout << endl << setw(20) << class_list[i] << " ";
for(int j=0; j<class_list.size(); j++)
cout << setw(2) << confusion[ class_list[i] ][ class_list[j] ] << (j==i?".":" ");
correct += confusion[ class_list[i] ][ class_list[i] ];
}
cout << endl << "Classifier accuracy: " << correct << " of " << total << " = " << setw(5) << setprecision(2) << correct/double(total)*100 << "%";
cout << " (versus random guessing accuracy of " << setw(5) << setprecision(2) << 1.0/class_list.size()*100 << "%)" << endl;
}
protected:
vector<string> class_list;
};