-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_database.py
117 lines (71 loc) · 3.14 KB
/
mnist_database.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
import urllib
import struct
import os.path
import gzip
class mnist_database:
test_file_images = 't10k-images-idx3-ubyte'
test_file_labels = 't10k-labels-idx1-ubyte'
train_file_images = 'train-images-idx3-ubyte'
train_file_labels = 'train-labels-idx1-ubyte'
image_height = -1
image_width = -1
database_location = 'http://yann.lecun.com/exdb/mnist/'
def __init__(self):
self.test_file_images_data = None
self.test_file_labels_data = None
self.train_file_images_data = None
self.train_file_labels_data = None
def get_file(self, filename):
if not os.path.exists(filename + '.gz'):
url = self.database_location + filename + '.gz'
print('Getting {} ...'.format(url))
urllib.urlretrieve(self.database_location + filename + '.gz', filename + '.gz')
print('Done')
def read_images(self, filename):
with gzip.open(filename + '.gz', 'rb') as file:
magic, = struct.unpack('>i', file.read(4))
if magic != 2051:
raise ValueError("Magic number {} differs from {} in '{}'".format(magic, 2051, filename))
count, = struct.unpack('>i', file.read(4))
self.image_height, = rows, = struct.unpack('>i', file.read(4))
self.image_width, = columns, = struct.unpack('>i', file.read(4))
images = []
for i in range(count):
images.append(file.read(rows * columns))
return images
def read_labels(self, filename):
with gzip.open(filename + '.gz', 'rb') as file:
magic, = struct.unpack('>i', file.read(4))
if magic != 2049:
raise ValueError("Magic number {} differs from {} in '{}'".format(magic, 2049, filename))
count, = struct.unpack('>i', file.read(4))
return file.read(count)
def get_test_images(self):
if self.test_file_images_data:
return self.test_file_images_data
self.get_file(self.test_file_images)
self.test_file_images_data = self.read_images(self.test_file_images)
return self.test_file_images_data
def get_test_labels(self):
if self.test_file_labels_data:
return self.test_file_labels_data
self.get_file(self.test_file_labels)
self.test_file_labels_data = self.read_labels(self.test_file_labels)
return self.test_file_labels_data
def get_train_images(self):
if self.train_file_images_data:
return self.train_file_images_data
self.get_file(self.train_file_images)
self.train_file_images_data = self.read_images(self.train_file_images)
return self.train_file_images_data
def get_train_labels(self):
if self.train_file_labels_data:
return self.train_file_labels_data
self.get_file(self.train_file_labels)
self.train_file_labels_data = self.read_labels(self.train_file_labels)
return self.train_file_labels_data
def load_data(self):
self.get_test_images()
self.get_test_labels()
self.get_train_images()
self.get_train_labels()