-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
141 lines (108 loc) · 3.71 KB
/
dataset.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
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
# coding: utf-8
try:
import urllib.request
except ImportError:
raise ImportError("You should use Python 3.x")
import os.path
import gzip
import pickle
import os
import numpy as np
from PIL import Image
url_base = 'http://yann.lecun.com/exdb/mnist/'
ket_file = {
"train_image": "train-images-idx3-ubyte.gz",
"train_label": "train-labels-idx1-ubyte.gz",
"test_image": "t10k-images-idx3-ubyte.gz",
"test_label": "t10k-labels-idx1-ubyte.gz"
}
data_path = "./data/"
if not os.path.exists(data_path):
os.makedirs(data_path)
save_file = data_path + "mnist.pkl"
train_num = 60000
test_num = 10000
img_dim = (1, 28, 28)
img_size = 784
def _download(file_name):
file_path = os.path.join(data_path, file_name)
if os.path.exists(file_path):
return
print("Downing " + file_name + "...")
urllib.request.urlretrieve(url_base + file_name, file_path)
print("Done!")
def down_mnist():
for v in ket_file.values():
_download(v)
def _load_label(file_name):
file_path = os.path.join(data_path, file_name)
print("Coverting " + file_name + "to numpy arrry ...")
with gzip.open(file_path, "rb") as f:
labels = np.frombuffer(f.read(), np.uint8, offset=8)
print("Done !")
return labels
def _load_image(file_name):
file_path = os.path.join(data_path, file_name)
print("Converting " + file_name + "to numpy array ...")
with gzip.open(file_path, "rb") as f:
data = np.frombuffer(f.read(), np.uint8, offset=16)
data = data.reshape(-1, img_size)
print("Done !")
return data
def convert_numpy():
dataset = {}
dataset["train_image"] = _load_image(ket_file["train_image"])
dataset["test_image"] = _load_image(ket_file["test_image"])
dataset["train_label"] = _load_label(ket_file["train_label"])
dataset["test_label"] = _load_label(ket_file["test_label"])
return dataset
def init_mnist():
down_mnist()
dataset = convert_numpy()
print("Creating pickle file ....")
with open(save_file, "wb") as f:
pickle.dump(dataset, f, -1)
print("Done !")
def _change_one_hot_label(X, numclass=10):
return np.eye(numclass)[X]
def img_show(img):
plt_img = Image.fromarray(img)
plt_img.show()
def load_mnist(normolize=True, flatten=True, one_hot_lable=False):
"""load the dataset of mnist
Args:
normolize (bool, optional): Defaults to True.
flatten (bool, optional): Defaults to True.
one_hot_lable (bool, optional): Defaults to False.
Returns:
dict: the dataset
"""
init_mnist()
with open(save_file, "rb") as f:
dataset = pickle.load(f)
if normolize:
dataset["train_image"] = dataset["train_image"].astype(
np.float32) / 255.0
dataset["test_image"] = dataset["test_image"].astype(
np.float32) / 255.0
if one_hot_lable:
dataset["train_label"] = _change_one_hot_label(dataset["train_label"])
dataset["test_label"] = _change_one_hot_label(dataset["test_label"])
if not flatten:
for key in ["train_image", "test_image"]:
dataset[key] = dataset[key].reshape(-1, 1, 28, 28)
return (dataset["train_image"],
dataset["train_label"]), (dataset["test_image"],
dataset["test_label"])
if __name__ == "__main__":
(train_img, train_label), (test_img,
test_label) = load_mnist(normolize=False,
flatten=True,
one_hot_lable=False)
img = train_img[0]
label = train_label[0]
print(img.shape)
img = img.reshape(28, 28)
print(img.shape)
print(label)
img_show(img)