-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_test_combine3.cpp
415 lines (338 loc) · 12.9 KB
/
svm_test_combine3.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "svm_test.h"
#include "read_reduce.h"
void combine_3()
{
combine3_motionOrBaseline();
}
void combine_3_leopard()
{
// late fusion
// multiple kernel learning !
// need to have the 3 classifier at hand.. 3 independently good classifier ? or what else is possible ?
//its late fusion ! so have to imagine independence at the earlier level
// read in the 3 test codewords !
int num_test_pos = NUM_POS_TEST_LEOPARD;
int num_test_neg = NUM_NEG_TEST_LEOPARD;
int total_test = num_test_pos + num_test_neg;
vector<float> margin_hog;
vector<float> margin_hof;
vector<float> margin_mbh;
readCode_DofusionPrediction_leopard("hog",num_test_pos,num_test_neg, margin_hog);
readCode_DofusionPrediction_leopard("hof",num_test_pos,num_test_neg, margin_hof);
readCode_DofusionPrediction_leopard("mbh",num_test_pos,num_test_neg, margin_mbh);
cout<<"size of margins is "<<margin_mbh.size();
cout<<"Total test codes is "<<total_test<<endl;
// the first num_test_pos are positive !
// the next num_test_neg are negative
vector<float> labels(num_test_neg+num_test_pos);
cout<<"size of labels is "<<labels.size()<<endl;
for(int i=0; i<num_test_pos; i++)
{
labels[i] = 1;
}
for(int i=num_test_pos; i<num_test_pos+num_test_neg; i++)
{
labels[i] = -1;
}
int right_pos = 0;
int right_neg = 0;
int num_correct_test = 0;
for(int i=0; i<labels.size(); i++)
{
cout<<"Test shot # "<<i<<endl;
float avg_margin = (margin_hog[i] + margin_hof[i] + margin_mbh[i] ) / 3.0f;
cout<<margin_hog[i]<<"\t"<<margin_hof[i]<<"\t"<<margin_mbh[i]<<"\t"<<avg_margin<<"\t"<<labels[i]<<endl;
// cout<<"Average margin is "<<avg_margin<<". Label is "<<labels[i]<<endl;
float pred;
if(avg_margin > 0)
{
pred = 1;
}
else
{
pred = -1;
}
if(pred == labels[i])
{
if(labels[i] == 1)
right_pos++;
else
right_neg++;
num_correct_test ++ ;
}
}
cout<<"The total number of correct prediction is "<<num_correct_test<<" (out of "<<total_test<<"). Accuracy: "<<num_correct_test*100.0f/total_test<<endl;
cout<<"The total positive correct prediction is = "<<right_pos<<"(out of "<<num_test_pos<<"). Accuracy: "<<right_pos*100.0f/num_test_pos<<endl;
cout<<"The total negative correct prediction is = "<<right_neg<<"(out of "<<num_test_neg<<"). Accuracy: "<<right_neg*100.0f/num_test_neg<<endl;
}
void readCode_DofusionPrediction_leopard(string featurename, int num_test_pos, int num_test_neg, vector<float>& margins)
{
cout<<"*************** FEATURE "<<featurename<<" ************************ "<<endl;
cv::Mat testcodewords = cvCreateMat(0,dictionarySize,CV_32FC1);
cv::Mat testlabels = cvCreateMat(0,1,CV_32FC1);
stringstream testposs ;
testposs<<"leopard_code_"<<featurename<<"_testpos";
stringstream testnegs ;
testnegs<<"leopard_code_"<<featurename<<"_testneg";
readTestCodeWords(testcodewords,testlabels,testposs.str(),testnegs.str(),num_test_pos,num_test_neg);
string svmfilename = "leopard_" + featurename + "_svm" ;
fusionPrediction(testcodewords, testlabels, margins, svmfilename);
}
void combine_3_tiger()
{
int num_test_pos = NUM_POS_TEST;
int num_test_neg = NUM_NEG_TEST;
int total_test = num_test_pos + num_test_neg;
vector<float> margin_hog;
vector<float> margin_hof;
vector<float> margin_mbh;
readCode_DofusionPrediction("hog",num_test_pos,num_test_neg, margin_hog);
readCode_DofusionPrediction("hof",num_test_pos,num_test_neg, margin_hof);
readCode_DofusionPrediction("baseline",num_test_pos,num_test_neg, margin_mbh);
cout<<"size of margins is "<<margin_mbh.size();
cout<<"Total test codes is "<<total_test<<endl;
// the first num_test_pos are positive !
// the next num_test_neg are negative
vector<float> labels(num_test_neg+num_test_pos);
cout<<"size of labels is "<<labels.size()<<endl;
for(int i=0; i<num_test_pos; i++)
{
labels[i] = 1;
}
for(int i=num_test_pos; i<num_test_pos+num_test_neg; i++)
{
labels[i] = -1;
}
int right_pos = 0;
int right_neg = 0;
int num_correct_test = 0;
for(int i=0; i<labels.size(); i++)
{
cout<<"Test shot # "<<i<<endl;
float avg_margin = (margin_hog[i] + margin_hof[i] + margin_mbh[i] ) / 3.0f;
cout<<margin_hog[i]<<"\t"<<margin_hof[i]<<"\t"<<margin_mbh[i]<<"\t"<<avg_margin<<"\t"<<labels[i]<<endl;
// cout<<"Average margin is "<<avg_margin<<". Label is "<<labels[i]<<endl;
float pred;
if(avg_margin > 0)
{
pred = 1;
}
else
{
pred = -1;
}
if(pred == labels[i])
{
if(labels[i] == 1)
right_pos++;
else
right_neg++;
num_correct_test ++ ;
}
}
cout<<"The total number of correct prediction is "<<num_correct_test<<" (out of "<<total_test<<"). Accuracy: "<<num_correct_test*100.0f/total_test<<endl;
cout<<"The total positive correct prediction is = "<<right_pos<<"(out of "<<num_test_pos<<"). Accuracy: "<<right_pos*100.0f/num_test_pos<<endl;
cout<<"The total negative correct prediction is = "<<right_neg<<"(out of "<<num_test_neg<<"). Accuracy: "<<right_neg*100.0f/num_test_neg<<endl;
cout<<"Precision is "<<right_pos*100.0f / (right_pos + (num_test_neg - right_neg))<<endl;
cout<<"Recall is "<<right_pos *100.0f/ (right_pos + (num_test_pos - right_pos))<<endl;
}
void readCode_DofusionPrediction(string featurename, int num_test_pos, int num_test_neg, vector<float>& margins)
{
cout<<"*************** FEATURE "<<featurename<<" ************************ "<<endl;
cv::Mat testcodewords = cvCreateMat(0,dictionarySize,CV_32FC1);
cv::Mat testlabels = cvCreateMat(0,1,CV_32FC1);
stringstream testposs ;
stringstream testnegs ;
string svmfilename;
if(featurename == "baseline")
{
testposs<<"tiger_";
testnegs<<"tiger_";
svmfilename = "tiger_" + featurename + "_svm" ;
}
else
{
svmfilename= featurename + "_svm";
}
testposs<<"code_"<<featurename<<"_testpos";
testnegs<<"code_"<<featurename<<"_testneg";
readTestCodeWords(testcodewords,testlabels,testposs.str(),testnegs.str(),num_test_pos,num_test_neg);
fusionPrediction(testcodewords, testlabels, margins, svmfilename);
}
void fusionPrediction(cv::Mat& testingcodes, cv::Mat& testingLabels,vector<float>& margins, string svmfilename)
{
CvSVM svm ;
cout<<"using svm file name "<<svmfilename<<endl;
svm.load(svmfilename.c_str());
int total_test = testingcodes.rows;
cout<<"Total test codewords here is "<<total_test<<endl;
for(int i=0; i<total_test; i++)
{
float pred_margin = svm.predict(testingcodes.row(i),true);
// cout<<"Pred_margin is "<<pred_margin;
float pred = svm.predict(testingcodes.row(i));
// cout<<"\t Prediction is "<<pred<<endl;
margins.push_back(pred*abs(pred_margin));
}
}
void combine3_tiger_choose()
{
int num_test_pos = NUM_POS_TEST;
int num_test_neg = NUM_NEG_TEST;
int total_test = num_test_pos + num_test_neg;
vector<string> features;
features.push_back("hog");
features.push_back("hof");
features.push_back("mbh");
features.push_back("baseline");
int num_features = features.size();
vector< vector<float> > margin(num_features);
for(int i=0; i<num_features; i++)
{
cout<<"Using feature "<<features[i]<<endl;
readCode_DofusionPrediction(features[i],num_test_pos,num_test_neg, margin[i]);
}
// the first num_test_pos are positive !
// the next num_test_neg are negative
vector<float> labels(num_test_neg+num_test_pos);
cout<<"size of labels is "<<labels.size()<<endl;
for(int i=0; i<num_test_pos; i++)
{
labels[i] = 1;
}
for(int i=num_test_pos; i<num_test_pos+num_test_neg; i++)
{
labels[i] = -1;
}
int right_pos = 0;
int right_neg = 0;
int num_correct_test = 0;
for(int i=0; i<labels.size(); i++)
{
cout<<"Test shot # "<<i<<endl;
float avg_margin =0;
for(int k=0; k<num_features; k++)
{
avg_margin += margin[k][i];
cout<<margin[k][i]<<"\t";
}
cout<<labels[i]<<endl;
avg_margin = avg_margin / num_features;
float pred;
if(avg_margin > 0)
{
pred = 1;
}
else
{
pred = -1;
}
if(pred == labels[i])
{
if(labels[i] == 1)
right_pos++;
else
right_neg++;
num_correct_test ++ ;
}
}
cout<<"The total number of correct prediction is "<<num_correct_test<<" (out of "<<total_test<<"). Accuracy: "<<num_correct_test*100.0f/total_test<<endl;
cout<<"The total positive correct prediction is = "<<right_pos<<"(out of "<<num_test_pos<<"). Accuracy: "<<right_pos*100.0f/num_test_pos<<endl;
cout<<"The total negative correct prediction is = "<<right_neg<<"(out of "<<num_test_neg<<"). Accuracy: "<<right_neg*100.0f/num_test_neg<<endl;
cout<<"Precision is "<<right_pos*100.0f / (right_pos + (num_test_neg - right_neg))<<endl;
cout<<"Recall is "<<right_pos *100.0f/ (right_pos + (num_test_pos - right_pos))<<endl;
}
void combine3_motionOrBaseline()
{
int num_test_pos = NUM_POS_TEST;
int num_test_neg = NUM_NEG_TEST;
int total_test = num_test_pos + num_test_neg;
vector<string> features;
features.push_back("hog");
// features.push_back("hof");
features.push_back("mbh");
int num_features = features.size();
vector< vector<float> > margin(num_features);
for(int i=0; i<num_features; i++)
{
cout<<"Using feature "<<features[i]<<endl;
readCode_DofusionPrediction(features[i],num_test_pos,num_test_neg, margin[i]);
}
// the first num_test_pos are positive !
// the next num_test_neg are negative
vector<float> labels(num_test_neg+num_test_pos);
vector<float> avg_margins;
cout<<"size of labels is "<<labels.size()<<endl;
for(int i=0; i<num_test_pos; i++)
{
labels[i] = 1;
}
for(int i=num_test_pos; i<num_test_pos+num_test_neg; i++)
{
labels[i] = -1;
}
int right_pos = 0;
int right_neg = 0;
int num_correct_test = 0;
for(int i=0; i<labels.size(); i++)
{
float avg_margin =0;
for(int k=0; k<num_features; k++)
{
avg_margin += margin[k][i];
// cout<<margin[k][i]<<"\t";
}
// cout<<labels[i]<<endl;
avg_margin = avg_margin / num_features;
avg_margins.push_back(avg_margin);
}
//have average margin for motion
// get baseline margin
vector<float> baselinemargin;
readCode_DofusionPrediction("baseline",num_test_pos,num_test_neg,baselinemargin );
cout<<"Test shot # "<<"\t"<<"BaselineMargin "<<"MotionMargin"<<" Prediction"<<" Actual"<<endl;
// cout<<"Test shot # "<<"\t"<<"Avg MotionMargin "<<"BaselineMargin"<<"Average "<<"Prediction"<<" Actual"<<endl;
for(int i=0; i<labels.size(); i++)
{
// take a decision based on the value of max seperation ??
cout<<"Test shot # "<<i<<"\t";
float basemargin = baselinemargin[i];
float motionmargin = avg_margins[i];
// float bestmargin = (baselinemargin[i] + avg_margins[i]) / 2.0f;
// cout<<motionmargin<<"\t"<<basemargin<<"\t"<<bestmargin<<"\t";
cout<<basemargin<<"\t"<<motionmargin<<"\t";
float bestmargin = max(abs(basemargin),abs(motionmargin));
if(bestmargin == abs(motionmargin))
{
bestmargin = motionmargin;
}
else
{
bestmargin = basemargin;
}
float pred;
if(bestmargin > 0)
{
pred = 1;
}
else
{
pred = -1;
}
cout<<bestmargin<<"\t";
cout<<pred<<"\t"<<labels[i]<<endl;
if(pred == labels[i])
{
if(labels[i] == 1)
right_pos++;
else
right_neg++;
num_correct_test ++ ;
}
}
cout<<"The total number of correct prediction is "<<num_correct_test<<" (out of "<<total_test<<"). Accuracy: "<<num_correct_test*100.0f/total_test<<endl;
cout<<"The total positive correct prediction is = "<<right_pos<<"(out of "<<num_test_pos<<"). Accuracy: "<<right_pos*100.0f/num_test_pos<<endl;
cout<<"The total negative correct prediction is = "<<right_neg<<"(out of "<<num_test_neg<<"). Accuracy: "<<right_neg*100.0f/num_test_neg<<endl;
cout<<"Precision is "<<right_pos*100.0f / (right_pos + (num_test_neg - right_neg))<<endl;
cout<<"Recall is "<<right_pos *100.0f/ (right_pos + (num_test_pos - right_pos))<<endl;
}