-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.cpp
176 lines (162 loc) · 6.02 KB
/
dataset.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
#include "dataset.h"
dataset::dataset(){
train_val_features = new std::vector<RowVectorXd>;
train_features = new std::vector<RowVectorXd>;
valid_features = new std::vector<RowVectorXd>;
test_features = new std::vector<RowVectorXd>;
train_val_labels = new std::vector<uint8_t>;
train_labels = new std::vector<uint8_t>;
valid_labels = new std::vector<uint8_t>;
test_labels = new std::vector<uint8_t>;
}
dataset::~dataset(){
}
void dataset::read_train_val_features(std::string path){
uint32_t header[4]; // |MAGIC|NUM IMAGES| ROWSIZE | COLSIZE
unsigned char bytes[4];
FILE *f = fopen(path.c_str(), "r");
if(f){
for(int i = 0; i < 4; i++){
if(fread(bytes, sizeof(bytes), 1, f)){
header[i] = convert_to_little_endian(bytes);
}
}
printf("Done getting Input file header.\n");
int image_size = header[2]*header[3];
for(int i = 0; i < header[1]; i++){
RowVectorXd tmp(784);
uint8_t element[1];
double ok;
for(int j=0; j < image_size; j++){
if(fread(element, sizeof(element[0]), 1, f)){
tmp(0,j)= element[0]/255.0;
}else{
printf("Error Reading from File.\n");
exit(1);
}
}
train_val_features->push_back(tmp);
}
printf("Successfully read and stored %lu train_val feature vectors.\n", train_val_features->size());
}else{
printf("Could not open train_val file");
exit(1);
}
}
void dataset::read_test_features(std::string path){
uint32_t header[4]; // |MAGIC|NUM IMAGES| ROWSIZE | COLSIZE
unsigned char bytes[4];
FILE *f = fopen(path.c_str(), "r");
if(f){
for(int i = 0; i < 4; i++){
if(fread(bytes, sizeof(bytes), 1, f)){
header[i] = convert_to_little_endian(bytes);
}
}
printf("Done getting Input file header.\n");
int image_size = header[2]*header[3];
for(int i = 0; i < header[1]; i++){
RowVectorXd tmp(784);
uint8_t element[1];
for(int j=0; j < image_size; j++){
if(fread(element, sizeof(element[0]), 1, f)){
tmp(0,j) = element[0]/255.0;
}else{
printf("Error Reading from File.\n");
exit(1);
}
}
test_features->push_back(tmp);
}
printf("Successfully read and stored %lu test feature vectors.\n", test_features->size());
}else{
printf("Could not open file");
exit(1);
}
}
void dataset::read_train_val_labels(std::string path){
uint32_t header[2]; // |MAGIC|NUM LABELS
unsigned char bytes[4];
FILE *f = fopen(path.c_str(), "rb");
if(f){
for(int i = 0; i < 2; i++){
if(fread(bytes, sizeof(bytes), 1, f)){
header[i] = convert_to_little_endian(bytes);
}
}
printf("Done getting label file header.\n");
for(int i = 0; i < header[1]; i++){
uint8_t element[1];
if(fread(element, sizeof(element[0]), 1, f)){
train_val_labels->push_back(element[0]);
}else{
printf("Erro Reading from File.\n");
exit(1);
}
}
printf("Successfully read and stored %lu train_val labels.\n", train_val_labels->size());
}else{
printf("Could not open file");
exit(1);
}
}
void dataset::read_test_labels(std::string path) {
uint32_t header[2]; // |MAGIC|NUM LABELS
unsigned char bytes[4];
FILE *f = fopen(path.c_str(), "rb");
if(f){
for(int i = 0; i < 2; i++){
if(fread(bytes, sizeof(bytes), 1, f)){
header[i] = convert_to_little_endian(bytes);
}
}
printf("Done getting label file header.\n");
for(int i = 0; i < header[1]; i++){
uint8_t element[1];
if(fread(element, sizeof(element[0]), 1, f)){
test_labels->push_back(element[0]);
}else{
printf("Erro Reading from File.\n");
exit(1);
}
}
printf("Successfully read and stored %lu test labels.\n", train_val_labels->size());
}else{
printf("Could not open file");
exit(1);
}
}
void dataset::split_data(){
std::unordered_set<int> used_indexes;
std::unordered_set<int> used_labels;
int train_size = train_val_features->size() * TRAIN_SET_PERCENT;
int validation_size = train_val_features->size() * VALIDATION_PERCENT;
// training data
int count = 0;
while(count < train_size){
int rand_index = rand() % (train_val_features->size());
if(used_indexes.find(rand_index) == used_indexes.end() && used_labels.find(train_val_labels->at(rand_index)) == used_labels.end()){
train_features->push_back(train_val_features->at(rand_index));
train_labels->push_back(train_val_labels->at(rand_index));
used_indexes.insert(rand_index);
used_labels.insert(train_val_labels->at(rand_index));
if(used_labels.size()>=10)
used_labels.erase(used_labels.begin(), used_labels.end());
count++;
}
}
// validation data
for(int rand_index=0; rand_index < train_val_features->size(); rand_index++)
if(used_indexes.find(rand_index) == used_indexes.end()){
valid_features->push_back(train_val_features->at(rand_index));
valid_labels->push_back(train_val_labels->at(rand_index));
used_indexes.insert(rand_index);
count++;
}
printf("Training Data Size: %lu.\n", train_features->size());
printf("Validation Data Size: %lu.\n", valid_features->size());
}
uint32_t dataset::convert_to_little_endian(const unsigned char* bytes){
uint32_t ret = (uint32_t) ((bytes[0]<<24) | (bytes[1]<<16) | (bytes[2]<<8) | bytes[3]);
return ret;
}