-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatasetHelper.cpp
115 lines (104 loc) · 3.12 KB
/
DatasetHelper.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
#include "DatasetHelper.hpp"
unsigned int targetFromEnc(double* buffer){
unsigned int classEnc = 0;
if((int)buffer[0])
classEnc = BG_TARGET;
else if ((int)buffer[1])
classEnc = BOAT_TARGET;
else if ((int)buffer[2])
classEnc = SEA_TARGET;
else{
std::cout<<"Warning: unrecognized encoding "<<buffer[0]<<" "<<buffer[1]<<" "<<buffer[2]<<std::endl;
}
return classEnc;
}
void saveDataset(
const std::string& name,
std::vector<std::vector<double>>& descriptors
)
{
auto rng = std::default_random_engine(42);
std::shuffle(std::begin(descriptors), std::end(descriptors), rng);
std::ofstream output;
output.open(name, std::ofstream::binary);
std::ostream_iterator<char> outIt(output);
for(unsigned int i=0; i<descriptors.size(); ++i)
{
const char* byte_s = (char*)&descriptors[i][0];
const char* byte_e = (char*)&descriptors[i].back() + sizeof(double);
std::copy(byte_s, byte_e, outIt);
}
output.close();
output.close();
}
void appendDescriptors(std::vector<std::vector<double>>& vect, const cv::Mat& descriptors, char oneHotEnc, bool addEnc){
std::vector<double> line;
for(unsigned int r=0; r<descriptors.rows; ++r)
{
line.clear();
for(unsigned int c=0; c<descriptors.cols; ++c)
{
line.push_back(descriptors.at<float>(r,c));
}
if(addEnc)
{
int ch1 = ((oneHotEnc >> 2) & 0x01);
int ch2 = ((oneHotEnc >> 1) & 0x01);
int ch3 = ((oneHotEnc) & 0x01);
// the first three digits of the one hot encoding
line.push_back((double) ch1);
line.push_back((double) ch2);
line.push_back((double) ch3);
}
vect.push_back(line);
}
}
void loadDataset(
const std::string& name,
std::vector<std::vector<double>>& inputs,
std::vector<unsigned int>& outputs,
std::vector<std::vector<double>>& vInputs,
std::vector<unsigned int>& vOutputs,
std::vector<std::vector<double>>& tInputs,
std::vector<unsigned int>& tOutputs,
unsigned int inSize,
unsigned int vSize,
unsigned int tSize,
bool includeTargets
)
{
std::ifstream input;
input.open(name,std::ifstream::binary);
std::vector<double> in(128,0);
double buffer[3] = {0, 0, 0};
while(!input.eof() && inputs.size()<inSize)
{
input.read((char*)&in[0], 128*sizeof(double));
inputs.push_back(in);
if(includeTargets){
input.read((char*)&buffer[0], 3*sizeof(double));
outputs.push_back(targetFromEnc(buffer));
}
}
while(!input.eof() && vInputs.size()<vSize)
{
input.read((char*)&in[0], 128*sizeof(double));
vInputs.push_back(in);
if(includeTargets){
input.read((char*)&buffer[0], 3*sizeof(double));
vOutputs.push_back(targetFromEnc(buffer));
}
}
while(!input.eof() && tInputs.size()<tSize)
{
input.read((char*)&in[0], 128*sizeof(double));
tInputs.push_back(in);
if(includeTargets){
input.read((char*)&buffer[0], 3*sizeof(double));
tOutputs.push_back(targetFromEnc(buffer));
}
}
//std::cout<<"sz "<<inputs.size()<<std::endl;
if (vInputs.size() < vSize || tInputs.size() < tSize)
std::cout<<"Warning: not enough samples in the data file"<<std::endl;
}