-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraining.cpp
More file actions
83 lines (67 loc) · 2.28 KB
/
Training.cpp
File metadata and controls
83 lines (67 loc) · 2.28 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
82
83
#include "Training.h"
#include "Utility.h"
Training::Training(String positiveImgs, String negativeImgs, int boxWidth, int boxHeight){
this->positiveImgs = positiveImgs;
this->negativeImgs = negativeImgs;
this->boxHeight = boxHeight;
this->boxWidth = boxWidth;
}
void Training::setPadding(int x, int y){
paddingX = x;
paddingY = y;
}
void Training::setStride(int x, int y){
strideX = x;
strideY = y;
}
void Training::calcHOG(Size boxSize, vector<Mat> imgs, vector<Mat> &gradients){
HOGDescriptor hogDescriptor;
hogDescriptor.winSize = boxSize;
Rect rect = Rect(0, 0, boxSize.width, boxSize.height);
Mat gray;
vector<float> descVecs;
for(int i=0 ; i < imgs.size(); i++){
hogDescriptor.compute(imgs[i](rect), descVecs, Size(strideX, strideX), Size(paddingX, paddingX));
gradients.push_back(Mat(descVecs));
}
}
vector<float> Training::calcDetector(Ptr<SVM> svm){
Mat sv = svm->getSupportVectors();
vector<float> detector(sv.cols + 1);
Mat alpha, i;
double rho = svm->getDecisionFunction(0, alpha, i);
memcpy(&detector[0], sv.ptr(), sv.cols*sizeof(float));
detector[sv.cols] = (float)-rho;
return detector;
}
void Training::saveDescriptor(String f){
tHOG.save(f);
}
void Training::train(){
vector<Mat> posSet, negSet, negSetTrain, gradients;
Mat trainData;
vector<float> hogDetector;
vector<int> numSamples;
Utility::loadImgs(positiveImgs, posSet);
Utility::loadImgs(negativeImgs, negSet);
Size boxSize = Size(boxWidth, boxHeight);
tHOG.winSize = boxSize;
negSetTrain = Utility::cropImgRandom(negSet, boxSize);
for(int i = 0; i < posSet.size();i++)
numSamples.push_back(1);
for(int i = 0;i < negSetTrain.size();i++)
numSamples.push_back(-1);
calcHOG(boxSize, posSet, gradients);
calcHOG(boxSize, negSetTrain, gradients);
Utility::convertData(gradients, trainData);
Ptr<SVM> svm = SVM::create();
svm->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, 1e-3));
svm->setKernel(SVM::LINEAR);
svm->setNu(0.5);
svm->setP(0.1);
svm->setC(0.01);
svm->setType(SVM::EPS_SVR);
svm->train(trainData, ROW_SAMPLE, Mat(numSamples));
hogDetector = calcDetector(svm);
tHOG.setSVMDetector(hogDetector);
}