-
Notifications
You must be signed in to change notification settings - Fork 0
/
inriaTrainedWivMIT.cpp
183 lines (163 loc) · 6.96 KB
/
inriaTrainedWivMIT.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
#include <iostream>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
using namespace cv;
const string DATAPATH = "/home/tau/INRIAPerson/";
int main()
{
cv::Ptr<cv::ml::SVM> svm_ = cv::ml::SVM::create();
svm_->setType(cv::ml::SVM::C_SVC);
svm_->setKernel(cv::ml::SVM::LINEAR);
svm_->setC(0.1); //one-class svm 不需要
svm_->setNu(0.5); //C_SVC 不需要
svm_->setTermCriteria(TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, FLT_EPSILON ));
svm_->setCoef0(0.0); //实际上对于one_class和c_SVC, 这个和下面两个参数都用不上
svm_->setDegree(3);
svm_->setGamma(0);
HOGDescriptor hog(Size(64,128),Size(16,16),Size(8,8),Size(8,8),9,1,
-1,HOGDescriptor::L2Hys,0.2,true,HOGDescriptor::DEFAULT_NLEVELS);
cv::Mat sample;
vector<int> labels;
ifstream rd(string(DATAPATH + "train_64x128_H96/pos.lst"), ifstream::in);
if(!rd.is_open()){
cout << "please check the path of pos.lst" << endl;
return -1;
}
string line;
while(getline(rd,line)){
string posPATH = "/home/tau/INRIAPerson/96X160H96/" + line;
//cout << posPATH << endl; //for debugging
Mat pos = imread(posPATH);
//imshow("pos",pos);
if(!pos.empty()){
Mat posCLIP(pos,cv::Rect(16,16,64,128));
//cv::imshow("64x128",posCLIP);
//std::cout << posCLIP.size() <<std::endl;
//cv::waitKey(0);
vector<float> descriptor;
hog.compute(posCLIP,descriptor,Size(8,8),Size(0,0));
Mat rowSample(1,(int)descriptor.size(), CV_32FC1,&descriptor[0]);
sample.push_back(rowSample);
labels.push_back(1);
}else
cout << "please check the pos files" << endl;
}
rd.close();
rd.open(string(DATAPATH + "MITpedestrians128x64/pos.lst"), ifstream::in);
if(!rd.is_open()){
cout << "please check the path of mit pos.lst" << endl;
return -1;
}
while(getline(rd,line)){
string posPATH = "/home/tau/INRIAPerson/MITpedestrians128x64/pos/" + line;
//cout << posPATH << endl; //for debugging
Mat pos = imread(posPATH);
//imshow("pos",pos);
if(!pos.empty()){
vector<float> descriptor;
hog.compute(pos,descriptor,Size(8,8),Size(0,0));
Mat rowSample(1,(int)descriptor.size(), CV_32FC1,&descriptor[0]);
sample.push_back(rowSample);
labels.push_back(1);
}else
cout << "please check the pos files" << endl;
}
rd.close();
rd.open(string(DATAPATH + "neg.lst"), ifstream::in);
if(!rd.is_open()){
cout << "please check the path of mit pos.lst" << endl;
return -1;
}
int count = 0;
while(getline(rd,line)){
string negPATH = DATAPATH+line;
//cout << negPATH << endl; //for debugging
Mat neg = imread(negPATH);
//imshow("neg",neg);
if(!neg.empty()){
vector<float> descriptor;
hog.compute(neg,descriptor,Size(8,8),Size(0,0));
Mat rowSample(1,(int)descriptor.size(), CV_32FC1,&descriptor[0]);
sample.push_back(rowSample);
labels.push_back(-1);
++count;
}else
cout << "please check the neg files" << endl;
}
rd.close();
if(!sample.empty()){
cv::Ptr<cv::ml::TrainData> trainData =
cv::ml::TrainData::create(sample, cv::ml::SampleTypes::ROW_SAMPLE,
labels);
cv::ml::ParamGrid c_grid(0.0001,1000,5);
svm_->trainAuto(trainData,10,c_grid);
std::cout << "training process finished " << std::endl;
svm_->save(DATAPATH + "hog_svm.xml");
std::cout << "SVM data saved under the directory of" << DATAPATH << std::endl;
//////// opencv3.1.0 sample code , construct custom SVMDetector /////////
Mat sv = svm_->getSupportVectors();
//cout << sv << endl;
const int sv_total = sv.rows;
Mat alpha,svidx;
double rho = svm_->getDecisionFunction(0,alpha,svidx); //0 for one-class classification(regression, one/two class classification)
alpha.convertTo(alpha, CV_32FC1);
CV_Assert( alpha.total() == 1 && svidx.total() == 1 && sv_total == 1 );
CV_Assert( (alpha.type() == CV_64F && alpha.at<double>(0) == 1.) ||
(alpha.type() == CV_32F && alpha.at<float>(0) == 1.f) );
CV_Assert( sv.type() == CV_32F );
std::vector<float> hog_detector;
hog_detector.clear();
hog_detector.resize(sv.cols + 1);
memcpy(&hog_detector[0], sv.ptr(), sv.cols*sizeof(hog_detector[0]));
hog_detector[sv.cols] = (float)-rho;
////////////////////////////////////////////////
hog.setSVMDetector(hog_detector);
///////////////////////////////////////////////
ifstream negList(DATAPATH+"Train/neg.lst");
if(!negList.is_open()){
cout << "please check the path of neg.lst" << endl;
return -1;
}
ofstream outList(string(DATAPATH + "neg.lst"),ofstream::app);
if(!outList.is_open()){
cout << "error writing img path to the neg.lst" << endl;
return -1;
}
cout << "finding hard example" << endl;
string negLine;
while(getline(negList,negLine)){
string negPATH = DATAPATH + negLine;
Mat neg = imread(negPATH);
//cv::imshow("neg",neg);
//cv::waitKey(0);
if(!neg.empty()){
std::vector< Rect > foundLocations;
hog.detectMultiScale(neg,foundLocations,0.07,Size(2,2),Size(0,0),1.05,2,false);
for(auto rect : foundLocations){
string filename = "b"+to_string(count);
string negImgPath =DATAPATH +"neg/" + filename +".png";
Mat foundNEG(neg,rect);
resize(foundNEG,foundNEG,Size(64,128));
// negImgPath路径一定要正确, 不然不会有写操作, 比如缺少neg文件夹
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(0);
imwrite(negImgPath,foundNEG,compression_params);
outList << string("neg/" + filename +".png")<<'\n' ;
++count;
}
}
}
cout << "all the neg img have been saved to the " << DATAPATH << "neg/ " << endl
<< "you can refer to neg.lst in the " <<DATAPATH << endl;
printf( "train error: %f\n", svm_->calcError(trainData, false, noArray()) );
printf( "test error: %f\n\n", svm_->calcError(trainData, true, noArray()) );
negList.close();
outList.close();
}else
std::cout <<" training failed, please check the sample file" << std::endl;
}