forked from PINTO0309/DeepLearningMugenKnock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_mnist.py
72 lines (51 loc) · 2.03 KB
/
load_mnist.py
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
import os
import gzip
import numpy as np
import matplotlib.pyplot as plt
def load_mnist():
dir_path = "mnist_datas"
files = ["train-images-idx3-ubyte.gz",
"train-labels-idx1-ubyte.gz",
"t10k-images-idx3-ubyte.gz",
"t10k-labels-idx1-ubyte.gz"]
# download mnist datas
if not os.path.exists(dir_path):
os.makedirs(dir_path)
data_url = "http://yann.lecun.com/exdb/mnist/"
for file_url in files:
after_file = file_url.split('.')[0]
if os.path.exists(dir_path + '/' + after_file):
continue
os.system("wget {}/{}".format(data_url, file_url))
os.system("mv {} {}".format(file_url, dir_path))
# load mnist data
# load train data
with gzip.open(dir_path + '/' + files[0], 'rb') as f:
train_x = np.frombuffer(f.read(), np.uint8, offset=16)
train_x = train_x.astype(np.float32)
train_x = train_x.reshape((-1, 28, 28))
print("train images >>", train_x.shape)
with gzip.open(dir_path + '/' + files[1], 'rb') as f:
train_y = np.frombuffer(f.read(), np.uint8, offset=8)
print("train labels >>", train_y.shape)
# load test data
with gzip.open(dir_path + '/' + files[2], 'rb') as f:
test_x = np.frombuffer(f.read(), np.uint8, offset=16)
test_x = test_x.astype(np.float32)
test_x = test_x.reshape((-1, 28, 28))
print("test images >>", test_x.shape)
with gzip.open(dir_path + '/' + files[3], 'rb') as f:
test_y = np.frombuffer(f.read(), np.uint8, offset=8)
print("test labels >>", test_y.shape)
"""
with open(dir_path + '/' + f_name, 'rb') as f:
#print(struct.unpack("b", f.read(1)))
a = f.readlines()
#print(struct.unpack("b", a[0]))
print(len(a))
for _a in a[:1]:
print(int.from_bytes(_a, 'little'))
print(_a)
"""
return train_x, train_y ,test_x, test_y
load_mnist()