-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_parser.cu
206 lines (169 loc) · 5 KB
/
mnist_parser.cu
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
#include <iostream>
#include <vector>
#include <assert.h>
#include <fstream>
using namespace std;
struct Image {
vector< vector<float_t> > img;// a image is represented by a 2-dimension vector
size_t size; // width or height
// construction
Image(size_t size_, vector< vector<float_t> > img_) :img(img_), size(size_){}
// display the image
void display(){
for (size_t i = 0; i < size; i++){
for (size_t j = 0; j < size; j++){
if (img[i][j] > 200)
cout << 1;
else
cout << 0;
}
cout << endl;
}
}
// up size to 32, make up with 0
void upto_32(){
assert(size < 32);
vector<float_t> row(32, 0);
for (size_t i = 0; i < size; i++){
img[i].insert(img[i].begin(), 0);
img[i].insert(img[i].begin(), 0);
img[i].push_back(0);
img[i].push_back(0);
}
img.insert(img.begin(), row);
img.insert(img.begin(), row);
img.push_back(row);
img.push_back(row);
size = 32;
}
vector<float_t> extend(){
vector<float_t> v;
for (size_t i = 0; i < size; i++){
for (size_t j = 0; j < size; j++){
v.push_back(img[i][j]);
}
}
return v;
}
};
typedef Image* Img;
struct Sample
{
uint8_t label; // label for a specific digit
vector<float_t> image;
Sample(float_t label_, vector<float_t> image_) :label(label_), image(image_){}
};
class Mnist_Parser
{
public:
Mnist_Parser(string data_path) :
test_img_fname(data_path + "/t10k-images-idx3-ubyte"),
test_lbl_fname(data_path + "/t10k-labels-idx1-ubyte"),
train_img_fname(data_path + "/train-images-idx3-ubyte"),
train_lbl_fname(data_path + "/train-labels-idx1-ubyte"){}
vector<Sample*> load_testing(){
test_sample = load(test_img_fname, test_lbl_fname);
return test_sample;
}
vector<Sample*> load_training(){
train_sample = load(train_img_fname, train_lbl_fname);
return train_sample;
}
void test(){
srand((int)time(0));
size_t i = (int)(rand());
cout << i << endl;
cout << (int)test_sample[i]->label << endl;
//test_sample[i]->image->display();
size_t j = (int)(rand() * 60000);
cout << (int)(train_sample[i]->label) << endl;
//train_sample[i]->image->display();
}
// vector for store test and train samples
vector<Sample*> test_sample;
vector<Sample*> train_sample;
private:
vector<Sample*> load(string fimage, string flabel){
ifstream in;
in.open(fimage, ios::binary | ios::in);
if (!in.is_open()){
cout << "file opened failed." << endl;
}
uint32_t magic = 0;
uint32_t number = 0;
uint32_t rows = 0;
uint32_t cols = 0;
in.read((char*)&magic, sizeof(uint32_t));
in.read((char*)&number, sizeof(uint32_t));
in.read((char*)&rows, sizeof(uint32_t));
in.read((char*)&cols, sizeof(uint32_t));
assert(swapEndien_32(magic) == 2051);
cout << "number:" << swapEndien_32(number) << endl;
assert(swapEndien_32(rows) == 28);
assert(swapEndien_32(cols) == 28);
vector< float_t> row;
vector< vector<float_t> > img;
vector<Img> images;
uint8_t pixel = 0;
size_t col_index = 0;
size_t row_index = 0;
while (!in.eof()){
in.read((char*)&pixel, sizeof(uint8_t));
col_index++;
row.push_back((float_t)pixel/256.0);
//row.push_back((float_t)pixel);
if (col_index == 28){
img.push_back(row);
row.clear();
col_index = 0;
row_index++;
if (row_index == 28){
Img i = new Image(28, img);
i->upto_32();
//i->display();
images.push_back(i);
img.clear();
row_index = 0;
}
}
}
in.close();
assert(images.size() == swapEndien_32(number));
//label
in.open(flabel, ios::binary | ios::in);
if (!in.is_open()){
cout << "failed opened label file";
}
in.read((char*)&magic, sizeof(uint32_t));
in.read((char*)&number, sizeof(uint32_t));
assert(2049 == swapEndien_32(magic));
assert(swapEndien_32(number) == images.size());
vector<uint8_t> labels;
uint8_t label;
while (!in.eof())
{
in.read((char*)&label, sizeof(uint8_t));
//cout << (int)label << endl;
labels.push_back(label);
}
vector<Sample*> samples;
for (int i = 0; i < swapEndien_32(number); i++){
samples.push_back(new Sample(labels[i], images[i]->extend()));
}
cout << "Loading complete" << endl;
in.close();
return samples;
}
// reverse endien for uint32_t
uint32_t swapEndien_32(uint32_t value){
return ((value & 0x000000FF) << 24) |
((value & 0x0000FF00) << 8) |
((value & 0x00FF0000) >> 8) |
((value & 0xFF000000) >> 24);
}
// filename for mnist data set
string test_img_fname;
string test_lbl_fname;
string train_img_fname;
string train_lbl_fname;
};