-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist.py
87 lines (75 loc) · 3.28 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Author: Ian Burke
# Module: Emerging Technologies
# Mnist Reader Problem sheet
#Problem Sheet: https://emerging-technologies.github.io/problems/digits.html
#import the following python libraries:
import gzip
# In shell, run pip install image..
from PIL import Image #image library
import numpy as np
# Question 1 - Read the data files
# Download the image and label files.
# Have Python decompress and read them byte by byte into appropriate data structures in memory.
#function to read label files
def read_labels_from_file(filename):
with gzip.open(filename,'rb') as f: #use gzip to open the file in read binary mode
magic = f.read(4) # magic number is the first 4 bytes
magic = int.from_bytes(magic,'big') # Convert bytes to integers.
print("Magic is:", magic) # print to console
# the same as above but with labels
nolab = f.read(4)
nolab = int.from_bytes(nolab,'big')
print("Num of labels is:", nolab)
# for looping through labels
labels = [f.read(1) for i in range(nolab)]
labels = [int.from_bytes(label, 'big') for label in labels]
return labels
print()
train_labels = read_labels_from_file("train-labels-idx1-ubyte.gz")
test_labels = read_labels_from_file("t10k-labels-idx1-ubyte.gz")
def read_images_from_file(filename):
with gzip.open(filename,'rb') as f:
magic = f.read(4)
magic = int.from_bytes(magic,'big')
print("Magic is:", magic)
# Number of images in next 4 bytes
noimg = f.read(4)
noimg = int.from_bytes(noimg,'big')
print("Number of images is:", noimg)
# Number of rows in next 4 bytes
norow = f.read(4)
norow = int.from_bytes(norow,'big')
print("Number of rows is:", norow)
# Number of columns in next 4 bytes
nocol = f.read(4)
nocol = int.from_bytes(nocol,'big')
print("Number of cols is:", nocol)
images = [] # create array
#for loop
for i in range(noimg):
rows = []
for r in range(norow):
cols = []
for c in range(nocol):
cols.append(int.from_bytes(f.read(1), 'big')) # append the current byte for every column
rows.append(cols) # append columns array for every row
images.append(rows) # append rows for every image
return images
print() #line break
# Call the functions and run them to read the files
train_images = read_images_from_file("train-images-idx3-ubyte.gz")
test_images = read_images_from_file("t10k-images-idx3-ubyte.gz")
# Question 2 - Output an image to the console
# Output the third image in the training set to the console.
# Do this by representing any pixel value less than 128 as a full stop and any other pixel value as a hash symbol.
for row in train_images[4999]:
for col in row:
print('.' if col <= 127 else '#', end='')
print()
# Question 3 - Output the image files as PNGs
# Download the image and label files.
# Have Python decompress and read them byte by byte into appropriate data structures in memory.
img = Image.fromarray(np.array(train_images[4999]).astype('uint8'))
img = img.convert('RGB') # convert into rgb format
img.show() # display image in window
img.save('train-4999-2.png') # save the image file as png