-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.cpp
More file actions
112 lines (105 loc) · 2.28 KB
/
Copy pathdataset.cpp
File metadata and controls
112 lines (105 loc) · 2.28 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
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
#ifndef DATASET_CPP
#define DATASET_CPP
#include "dataset.h"
#include <fstream>
char Dataset::read_char(std::ifstream &fin)
{
char c;
fin.read(&c, 1);
return c;
}
unsigned char Dataset::read_u_char(std::ifstream &fin)
{
unsigned char c;
fin.read((char *)&c, 1);
return c;
}
int Dataset::read_int(std::ifstream &fin)
{
char c[4];
for (int i = 3; i >= 0; i--)
fin.read(c + i, 1);
return *((int *)c);
}
double Dataset::read_double(std::ifstream &fin)
{
char c[4];
for (int i = 3; i >= 0; i--)
fin.read(c + i, 1);
return *((double *)&c);
}
int Dataset::readin_images(const std::string &file_name)
{
std::ifstream fin(file_name, std::ios::in | std::ios::binary);
int magic_number;
magic_number = read_int(fin);
size = read_int(fin);
row = read_int(fin);
col = read_int(fin);
std::cout << size << " " << row << " " << col << std::endl;
int u;
datas.resize(size);
for (int i = 0; i < size; i++)
{
Matrix &data = datas[i];
data.set_shape(row, col);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
u = read_u_char(fin);
data.data[i][j] = u / 255.0;
}
}
}
return 0;
}
int Dataset::readin_labels(const std::string &file_name)
{
std::ifstream fin(file_name, std::ios::in | std::ios::binary);
int magic_number;
magic_number = read_int(fin);
size = read_int(fin);
row = 10;
col = 1;
std::cout << size << " " << row << " " << col << std::endl;
int u;
datas.resize(size);
for (int i = 0; i < size; i++)
{
Matrix &data = datas[i];
data.set_shape(row, col);
u = read_u_char(fin);
data.zero();
data.data[u][0] = 1.0;
}
return 0;
}
int Dataset::reshape(int n, int m)
{
if (!((n == -1 && row * col % m == 0) ||
(m == -1 && row * col % n == 0) ||
n * m == row * col))
{
return -1;
}
if (n == -1)
{
n = row * col / m;
}
if (m == -1)
{
m = row * col / n;
}
row = n;
col = m;
for (int i = 0; i < datas.size(); i++)
{
if (datas[i].reshape(row, col))
{
return -1;
}
}
return 0;
}
#endif