This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-mnist-data.cpp
125 lines (97 loc) · 2.55 KB
/
create-mnist-data.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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
const int imgSize = 28*28;
const int trainDataNum = 60000;
const int trainEpoch = 16;
const int trainDataTotal = trainEpoch * trainDataNum;
unsigned char ibuf_train[trainDataNum][imgSize];
unsigned char lbuf_train[trainDataNum];
unsigned char idat_train[trainDataTotal][imgSize];
unsigned char ldat_train[trainDataTotal];
char ibuf[16];
char lbuf[8];
void input();
void shuffle();
void output();
int main(int argc,char **argv)
{
input();
int sample=24321;
int tample=800000;
for(int i=0;i<28;++i){
for(int j=0;j<28;++j){
printf("%u ",ibuf_train[sample][i*28+j]);
}
printf("\n");
}
printf("%u\n",lbuf_train[sample]);
shuffle();
for(int i=0;i<28;++i){
for(int j=0;j<28;++j){
printf("%u ",idat_train[tample][i*28+j]);
}
printf("\n");
}
printf("%u\n",ldat_train[tample]);
output();
return 0;
}
/******************************************************************
input and output
******************************************************************/
void input()
{
char train_img[] = "/vagrant/train-images.idx3-ubyte";
char train_lbl[] = "/vagrant/train-labels.idx1-ubyte";
FILE* fp;
// training data : image
if((fp=fopen(train_img,"rb"))==NULL){
printf("Error opening %s.\n",train_img);
exit(1);
}
fread(ibuf, sizeof(ibuf),1,fp);
fread(ibuf_train, sizeof(unsigned char),trainDataNum*imgSize,fp);
fclose(fp);
// training data : label
if((fp=fopen(train_lbl,"rb"))==NULL){
printf("Error opening %s.\n",train_lbl);
exit(1);
}
fread(lbuf, sizeof(lbuf),1,fp);
fread(lbuf_train, sizeof(unsigned char),trainDataNum,fp);
fclose(fp);
}
void shuffle()
{
srand(392);
int idx=0;
for(int i=0;i<trainDataTotal;++i){
idx = rand() % trainDataNum;
memcpy(&idat_train[i][0], &ibuf_train[idx][0], imgSize);
ldat_train[i] = lbuf_train[idx];
}
}
void output()
{
char train_img[] = "/vagrant/train-images.idx3-ubyte-shuffle";
char train_lbl[] = "/vagrant/train-labels.idx1-ubyte-shuffle";
FILE* fp;
// training data : image
if((fp=fopen(train_img,"wb"))==NULL){
printf("Error opening %s.\n",train_img);
exit(1);
}
fwrite(ibuf, sizeof(ibuf),1,fp);
fwrite(idat_train, sizeof(unsigned char),trainDataTotal*imgSize,fp);
fclose(fp);
// training data : label
if((fp=fopen(train_lbl,"wb"))==NULL){
printf("Error opening %s.\n",train_lbl);
exit(1);
}
fwrite(lbuf, sizeof(lbuf),1,fp);
fwrite(ldat_train, sizeof(unsigned char),trainDataTotal,fp);
fclose(fp);
}