-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
48 lines (33 loc) · 1.23 KB
/
utility.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
""" Utility functions for interacting with the dataset """
def get_training_image_vector(image_number):
file = open('Dataset/train-images-idx3-ubyte', 'rb')
file.seek(16 + (image_number - 1) * 784, 0)
image_vector = []
for i in range(0, 784):
byte = int.from_bytes(file.read(1), byteorder='big')
image_vector.append(byte)
file.close()
return image_vector
def get_training_image_label(image_number):
file = open('Dataset/train-labels-idx1-ubyte', 'rb')
file.seek(8 + (image_number - 1), 0)
byte = int.from_bytes(file.read(1), byteorder='big')
image_label = byte
file.close()
return image_label
def get_test_image_vector(image_number):
file = open('Dataset/t10k-images-idx3-ubyte', 'rb')
file.seek(16 + (image_number - 1) * 784, 0)
image_vector = []
for i in range(0, 784):
byte = int.from_bytes(file.read(1), byteorder='big')
image_vector.append(byte)
file.close()
return image_vector
def get_test_image_label(image_number):
file = open('Dataset/t10k-labels-idx1-ubyte', 'rb')
file.seek(8 + (image_number - 1), 0)
byte = int.from_bytes(file.read(1), byteorder='big')
image_label = byte
file.close()
return image_label