-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist_loader.cpp
More file actions
55 lines (38 loc) · 1.29 KB
/
mnist_loader.cpp
File metadata and controls
55 lines (38 loc) · 1.29 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
#include "mnist_loader.h"
#include <iostream>
#include <fstream>
using namespace std;
Image::Image(Matrix img, int label):img{img}, label{label} {}
int read(ifstream &file) {
unsigned char bytes[4];
file.read((char*)bytes, 4);
return int(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]);
}
vector<Image> load_images(const string &img_path, const string &label_path) {
ifstream img(img_path, ios::binary);
ifstream lbl(label_path, ios::binary);
if (!img.is_open() || !lbl.is_open()) {
cerr << "Error opening in file" << endl;
return {};
}
int magic_img = read(img);
int nos_img=read(img);
int rows=read(img);
int cols=read(img);
int magic_lbl = read(lbl);
int nos_lbl=read(lbl);
if (nos_lbl!=nos_img) {cerr<<"file read incorrectly" << endl; return {};}
vector<Image> ds(nos_img);
for (int i = 0; i < nos_img; ++i) {
vector<double> pxls(rows*cols);
for (int j = 0; j < rows*cols; j++) {
unsigned char byte;
img.read((char*)&byte, 1);
pxls[j]=byte/255.0; // Pixel value is > 0(Black) && < 255(White)
}
unsigned char lbl_byte;
lbl.read((char*) &lbl_byte, 1);
ds[i] = Image(Matrix(cols*rows, 1, pxls), lbl_byte);
}
return ds;
}