-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_allclasses.cpp.save
306 lines (276 loc) · 10.1 KB
/
test_allclasses.cpp.save
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "test_allclasses.h"
const string test_path_file = "test.txt";
const float TOTAL_TEST_IMG = 431;
int test_allclasses_main(){
cout << "Testing Start ....." << endl;
fstream fs ;
map<string,map<string,int> > confusion_matrix; // confusionMatrix[classA][classB] = number_of_times_A_voted_for_B;
map<string,CvSVM*> classes_classifiers1;
//map<string,CvSVM*> classes_classifiers2;
//vector <string> lines;
//vector<string> files; //load up with images
//vector<string> classes; //load up with the respective classes
//map <string,int> classes_count;
//fs.open(test_path_file.c_str() );
//if ( !fs.is_open() ){
// cerr << "cannot open file" << test_path_file << endl ;
// return -1;
//}
//string line ;
//while ( getline (fs,line) ){
//
// lines.push_back(line);
//}
//fs.close() ;
////extract the filepath and class label from test.txt
//string filepath;
//string tmp;
//string class_;
//for ( size_t i = 0 ; i < lines.size() ; i++ ){
//
// stringstream ss(lines[i] );
// ss >> tmp;
// filepath = tmp ;
// filepath += " ";
// ss >>tmp ;
// filepath += tmp ;
// ss >> class_ ;
// class_ = "class_" + class_ ;
// classes_count[class_]++;
// files.push_back(filepath) ;
// classes.push_back(class_);
//
//}
//for (map<string,int>::iterator it = classes_count.begin(); it != classes_count.end(); ++it) {
// cout << it->first <<" : " << it->second << endl ;
// }
//declare the decscriptor & load the vocabulary into bowdescriptor
// Mat vocabulary;
// FileStorage fs1("dictionary/dictionary-800.yaml", FileStorage::READ);
// if (!fs1.isOpened() ){
//
// cout << " cannot open" << "dicitnary file" << endl ;
// return -1 ;
// }
// fs1["vocabulary"] >> vocabulary;
// fs1.release();
//
// Ptr<FeatureDetector> detector = FeatureDetector::create("SIFT");
// Ptr<DescriptorExtractor> extractor = DescriptorExtractor::create("SIFT");
// Ptr<DescriptorMatcher > matcher(new BruteForceMatcher<L2<float> >());
// BOWImgDescriptorExtractor bowide(extractor,matcher);
// bowide.setVocabulary(vocabulary);
//
// cout << "BOW has Vocabulary Size = " << vocabulary.rows << endl ;
{
//load the classifier parameters
const string params_prefix = "SVM_parameter_files/svm_1000_c130_g4(best)/SVM_classifier_class_";
const string class_ = "class_";
for ( size_t i = 1 ; i <=24; i++ ){
stringstream ss1 , ss2 ;
ss1 << class_ << i ;
ss2 << params_prefix <<i << ".yaml";
string label = ss1.str() ;
string params_path = ss2.str() ;
classes_classifiers1[label] = new CvSVM ();
classes_classifiers1[label]->load(params_path.c_str() ) ;
}
}
map <string , Mat> test_examples;
FileStorage fs2 ("test_samples/test_examples_1000.yaml" , FileStorage::READ) ;
if (!fs2.isOpened() ){
cout << " cannot open" << "test examples file " << endl ;
return -1 ;
}
//read the test examples from disk
{
string class_ = "class_" ;
for ( size_t i = 1 ; i <= 24 ; i++ ){
stringstream ss;
ss <<class_ << i ;
fs2[ss.str().c_str()] >> test_examples[ss.str()] ;
// cout <<test_examples[ss.str()].rows;
}
}
fs2.release();
// extract/compute the responses of test files .
//{
//for(size_t i = 0 ; i < files.size() ; i++ ) {
// Mat img = imread(files[i],0),resposne_hist;
// vector<KeyPoint> keypoints;
// detector->detect(img,keypoints);
// bowide.compute(img, keypoints, resposne_hist);
// //cout << "row x cols =" << resposne_hist.rows <<"x" << resposne_hist.cols << endl ; //1k
//
//
// response_examples[ classes[i] ].push_back(resposne_hist ) ;
//}
//inilize the confusion matrix
{
for (map<string,CvSVM*>::iterator it = classes_classifiers1.begin(); it != classes_classifiers1.end(); ++it) {
string class1 = it->first ;
for (map<string,CvSVM*>::iterator it1 = classes_classifiers1.begin(); it1 != classes_classifiers1.end(); ++it1) {
string class2 = it1->first ;
confusion_matrix[class1][class2] = 0;
}
}
}
// 1vs rest classification
for (map<string , Mat>::iterator it1 = test_examples.begin(); it1 != test_examples.end(); ++it1) {
Mat class_examples = it1->second;
for (int i = 0; i < class_examples.rows; i++ ){
string current_class = it1->first ;
float minf = FLT_MAX;
string minclass;
for (map<string,CvSVM*>::iterator it = classes_classifiers1.begin(); it != classes_classifiers1.end(); ++it) {
if (it->first.compare("class_22") == 0 ){
float res = (*it).second->predict(class_examples.row(i),true);
float reslabel = (*it).second->predict(class_examples.row(i),false);
cout << reslabel << endl;
return 1;
}
if (res < minf) {
minf = res;
minclass = (*it).first;
}
}
confusion_matrix[current_class][minclass]++;
}
}
Mat confus (24,24,CV_8U,Scalar (0));
//inilize confus matrix class1 ...class24
// class2 ... class24
{
for ( int i = 1 ; i <= 24; i++ ){
string class1;
stringstream ss1 ;
ss1 << "class_" << i ;
class1 = ss1.str() ;
for (int j = 1; j <= 24; j++ ){
string class2;
stringstream ss2 ;
ss2 << "class_" << j ;
class2 = ss2.str() ;
confus.at<uchar>(i-1,j-1) = confusion_matrix[class1][class2];
}
}
}
//cout << confus ;
cout << format(confus,"python");
//compute the whole accuracy
{
float true_pos =0 ;
float num_of_samples = 431 ;
int i =0;
int j = 0 ;
while ( i < 24 && j < 24 ){
true_pos += confus.at<uchar> (i,j) ;
i++;
j++;
}
cout << endl
<< "Accuracy = " << ( (true_pos/num_of_samples)*100 ) << endl ;
}
//compute the precion ,and recall for each classifier
{
map<string ,float> precisions;//precision of class(i)
map<string,float> recall;//recall of class(i)
map <string , float> wights ;//wights of class(i)
init(wights);
vector<size_t> true_pos;//the diagnoal entry in the confusion matrix for a class (i) = mat(i,i)
vector<size_t> false_pos;//the sum column of confusion matrix for a class(i) without the entry (i,i)
vector<size_t> false_neg;//the sum of row (i) without row(i)cols(j) (i.e without the true pos)
for ( size_t col = 0 ; col < 24 ; col++ ){
size_t tp = confus.at<uchar>(col,col) ;
size_t false_positive =0.0;
size_t false_negative =0.0;
true_pos.push_back(tp) ;
//sumation of all column for classifier i (false positive) with out the true_pos
for ( size_t row = 0 ; row < 24 ; row++ ){
if ( col == row )
continue ;
false_positive += confus.at<uchar>(row,col) ;
}
false_pos.push_back(false_positive) ;
float precision = ( (float)tp/(tp + false_positive ) ) * 100;
stringstream ss ;
ss <<"class_";
ss << col+1 ;
precisions[ss.str() ] = precision ;
//compute the false negative for each class
//now col index is represnt row i ,and k represent col k
for ( size_t k = 0 ; k < 24; k++ ){
//this entry is t.p
if (col == k ){
continue ;
}
false_negative += confus.at<uchar>(col , k);
}
float recall_i = ( (float)tp / (tp + false_negative ) )*100 ;
recall[ss.str()] = recall_i ;
}
//display the precions
float avg_precion = 0.0;
for (map<string,float>::iterator it = precisions.begin(); it != precisions.end(); ++it) {
cout << "Precion of " << it->first << " = " <<it->second << "%" << endl ;
avg_precion += it->second * wights[it->first];
}
cout << endl << endl ;
//display the recall
float avg_recall = 0.0;
for (map<string,float>::iterator it = recall.begin(); it != recall.end(); ++it) {
cout << "Recall of " << it->first << " = " <<it->second << "%" << endl ;
avg_recall += it->second * wights[it->first] ;
}
cout << endl << endl ;
cout << "Average Precsion = " << (avg_precion) <<"%" << endl ;
cout << "Average Recall = " << (avg_recall) << "%" << endl ;
}
for (map<string,CvSVM*>::iterator it = classes_classifiers1.begin(); it != classes_classifiers1.end(); ++it) {
delete it->second ;
}
// //store the response examples on disk
// string filename ;
// stringstream ss;
// ss << "test_samples/test_examples_" <<response_examples["class_1"].cols <<".yaml" ;
// filename = ss.str() ;
// FileStorage response_file(filename.c_str() , FileStorage::WRITE) ;
// if (!response_file.isOpened() ){
//
// cout << " cannot open " << filename <<"for writing " << endl ;
// return -1 ;
// }
// for (map<string,Mat >::iterator it = response_examples.begin(); it != response_examples.end(); ++it){
// response_file << it->first << it->second ;
// }
// cout <<"Write the test response examples to vocabulary of size " << response_examples["class_1"].cols
// << "is done succfully " << endl ;
// response_file.release() ;
return 0;
}
void init(map<string,float>& w){
w["class_1"] = 48/TOTAL_TEST_IMG;
w["class_2"] = 12/TOTAL_TEST_IMG;
w["class_3"] = 14/TOTAL_TEST_IMG;
w["class_4"] = 27/TOTAL_TEST_IMG;
w["class_5"] = 10/TOTAL_TEST_IMG;
w["class_6"] = 14/TOTAL_TEST_IMG;
w["class_7"] = 9/TOTAL_TEST_IMG;
w["class_8"] = 6/TOTAL_TEST_IMG;
w["class_9"] = 27/TOTAL_TEST_IMG;
w["class_10"] = 10/TOTAL_TEST_IMG;
w["class_11"] = 4/TOTAL_TEST_IMG;
w["class_12"] = 28/TOTAL_TEST_IMG;
w["class_13"] = 5/TOTAL_TEST_IMG;
w["class_14"] = 11/TOTAL_TEST_IMG;
w["class_15"] = 23/TOTAL_TEST_IMG;
w["class_16"] = 27/TOTAL_TEST_IMG;
w["class_17"] = 12/TOTAL_TEST_IMG;
w["class_18"] = 28/TOTAL_TEST_IMG;
w["class_19"] = 14/TOTAL_TEST_IMG;
w["class_20"] = 8/TOTAL_TEST_IMG;
w["class_21"] = 6/TOTAL_TEST_IMG;
w["class_22"] = 20/TOTAL_TEST_IMG;
w["class_23"] = 24/TOTAL_TEST_IMG;
w["class_24"] = 44/TOTAL_TEST_IMG;
}