-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRegressor.cpp
211 lines (183 loc) · 6.03 KB
/
Regressor.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
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <iostream>
#include "H5Cpp.h"
using namespace H5;
using namespace std;
#define NUM_OF_TREES 204//238
#define RADIUS 5//10//30
#define HEIGHT 9
#define WIDTH 15
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "Regressor.h"
#include <unistd.h>
void Regressor::load_model(void) {
ifstream myfile;
myfile.open("example.txt");
importForestFromTxt(myfile);
importNearestTrees();
myfile.close();
}
int Regressor::find_nearest_tree(float *headpose) {
float minsum = 100;
int minTree = -1;
float tmpsum=-1;
static float last_headpose[2] = {-5,-5};
static int last_tree=-14;
if ( abs(headpose[0]-last_headpose[0])+abs(headpose[1]-last_headpose[1]) > 0.3) {
last_headpose[0] = headpose[0];
last_headpose[1] = headpose[1];
for (int i=0; i < NUM_OF_TREES; i++) {
tmpsum= abs(headpose[0]-this->centers[2*i])+abs(this->centers[2*i+1]-headpose[1]);
if (tmpsum< minsum) {
minsum = tmpsum;
minTree = i;
last_tree=i;
//cout << "new tree found" << endl;
}
//cout <<i<<": center:("<< this->centers[2*i]<<","<<this->centers[2*i+1] <<").Mine("<<headpose[0]<<","<<headpose[1]<<").MeanError:"<<minsum<<endl;
//sleep(1);
}
}
else
return last_tree;
return minTree;
}
void Regressor::checkForGazes(treeT *tree) {
if (!tree) {
//cout << "predict:(" <<tree->mean[0]<<","<<tree->mean[1]<<")" << endl;
}
else {
checkForGazes(tree->right);
checkForGazes(tree->left);
}
}
void Regressor::predict(float *headpose, unsigned char *eyeImg, float *predict) {
predict[0] = 0;
predict[1] = 0;
treeT *temp_predict;
//for (int l=0;l<NUM_OF_TREES;l++)
//checkForGazes(this->trees[l]);
//calculate center(=c) or N closest centers
int c=find_nearest_tree(headpose);
//cout << "root tree:" << c << endl;
//cout << "pose:(" << headpose[0]<<","<<headpose[1]<<") is near tree:" << c << endl;
for (int k = 0; k < RADIUS+1; k++) {
//cout << "nearest:" << this->nearests[c*60+k] << endl;
temp_predict = testSampleInTree(this->trees[this->nearests[c*60+k]-1], eyeImg);
predict[0] = predict[0] + temp_predict->mean[0];
predict[1] = predict[1] + temp_predict->mean[1];
//cout << "headpose:(" << headpose[0]<<","<<headpose[1]<<")" << endl;
//if (temp_predict->mean[0] >0)
// cout << "temp_predict:(" << temp_predict->mean[0]* 180.0/M_PI<<","<<temp_predict->mean[1]* 180.0/M_PI<<")" << endl;
}
predict[0] = predict[0]/(RADIUS+1);
predict[1] = predict[1]/(RADIUS+1);
//cout << "predict:(" << predict[0]* 180.0/M_PI<<"," << predict[1]* 180.0/M_PI << ")" << endl;
}
void Regressor::close() {
for (int i = 0; i < NUM_OF_TREES; i++) {
free(this->trees[i]);
}
free(this->trees);
free(this->nearests);
free(this->centers);
}
//functions:
treeT *Regressor::loadTree(treeT *t,std::stringstream& lineStream) {
std::string temp;
if (lineStream >> temp) {
if (std::isdigit(temp[0]) || temp[0] == '-' ) {
t = (treeT *)malloc( sizeof(treeT) );
t->right = NULL;
t->left = NULL;
t->mean[0] = atof(temp.c_str());
lineStream >> t->mean[1];
lineStream >> t->stdev[0];
lineStream >> t->stdev[1];
lineStream >> t->mse;
lineStream >> t->thres;
lineStream >> t->minPx1_hor;
lineStream >> t->minPx2_hor;
lineStream >> t->minPx1_vert;
lineStream >> t->minPx2_vert;
t->left = loadTree(t->left, lineStream);
t->right = loadTree(t->right, lineStream);
//cout << t->mean[0] << " " << t->mean[1] << endl;
}
else {
return NULL;//leaf reached
}
}
else {
return NULL;//new line detected
}
return t;
}
void Regressor::importForestFromTxt(ifstream& infile) {
this->trees = (treeT **)malloc( NUM_OF_TREES * sizeof(treeT *) );
for (int i = 0; i < NUM_OF_TREES; i++) {
std::string line;
std::getline(infile, line);
std::stringstream lineStream(line);
std::string temp;
this->trees[i] = loadTree(this->trees[i],lineStream);
}
}
void Regressor::importNearestTrees(void) {
H5File *file = new H5File(/*"TRAIN_samples_10000_dist0.05.h5"*/"../train_dataset.h5", H5F_ACC_RDWR);
int rank;
char grpName[10];
Group *group = NULL;
hsize_t dims[4]; /* memory space dimensions */
this->nearests = (int *)malloc(NUM_OF_TREES*60* sizeof(int));
this->centers = (double *)malloc(NUM_OF_TREES*2*sizeof(double));
for (int i = 0; i < NUM_OF_TREES; i++ ) {
//this->nearests[i] = (int *)malloc(60*sizeof(int));
//this->centers[i] = (double *)malloc(2*sizeof(double));
sprintf(grpName, "g%d", i+1);
group = new Group(file->openGroup( grpName ) );
DataSet dataset = group->openDataSet("nearestIDs");
DataSpace dataspace = dataset.getSpace();//dataspace???
rank = dataspace.getSimpleExtentDims( dims );// get rank = numOfDims
DataSpace memspace( rank, dims );
dataset.read(this->nearests+i*60, PredType::NATIVE_INT, memspace, dataspace );
dataset = group->openDataSet("center");
dataspace = dataset.getSpace();//dataspace???
rank = dataspace.getSimpleExtentDims( dims );// get rank = numOfDims
memspace.setExtentSimple( rank, dims );
dataset.read(this->centers+i*2, PredType::NATIVE_DOUBLE, memspace, dataspace );
}
}
treeT *Regressor::testSampleInTree(treeT *curr,unsigned char *eyeImg) {
if (curr->right == NULL) {//leaf reached
return curr;
}
else { // right or left?
if ( abs(eyeImg[curr->minPx1_vert*WIDTH+curr->minPx1_hor]-eyeImg[curr->minPx2_vert*WIDTH+curr->minPx2_hor ]) >= curr->thres ) {
curr = testSampleInTree(curr->right, eyeImg);
}
else {
curr = testSampleInTree(curr->left, eyeImg);
}
}
return curr;
}
/*
int main() {
Regressor forest;
double prediction[2];
double headpose[2];
headpose[0]=0;headpose[1]=0;
//unsigned char *eyeImg;
//eyeImg=(unsigned char *)malloc(WIDTH*HEIGHT);
cv::Mat eyeImg = cv::Mat::zeros(36,60,cv::DataType<unsigned char>::type);
forest.load_model();
forest.predict(headpose,eyeImg.data,prediction);
cout << "predicted:(" << prediction[0]<<"," << prediction[1]<<")" << endl;
forest.close();
return 0;
}
*/