-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis_mnist.py
64 lines (49 loc) · 1.51 KB
/
vis_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
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.examples.tutorials.mnist import mnist
import matplotlib.pyplot as plt
def pad(image, padding=1, color=0.5):
# Border of pixels.
v_border = np.ndarray((28, padding))
h_border = np.ndarray((padding, 28 + (padding * 2)))
v_border.fill(color)
h_border.fill(color)
new_img = image
new_img = np.concatenate((v_border, new_img), axis=1)
new_img = np.concatenate((new_img, v_border), axis=1)
new_img = np.concatenate((h_border, new_img))
new_img = np.concatenate((new_img, h_border))
return new_img
def show_images(indices):
input_data_dir= 'MNIST-data'
data_sets = input_data.read_data_sets(input_data_dir, fake_data=False)
images = data_sets.train.images
col = None
im = None
for i in range(len(indices)):
next_image = images[indices[i],:].reshape((28, 28))
next_image = pad(next_image)
if col is None:
col = next_image
else:
col = np.concatenate((col, next_image))
if i > 0 and i % 3 == 2:
# Column is full. Append to im.
if im is None:
im = col
else:
im = np.concatenate((im, col), axis=1)
col = None
if col is not None:
blank = pad(np.zeros((28, 28)))
while i % 3 != 2:
col = np.concatenate((col, blank))
i += 1
if im is None:
im = col
else:
im = np.concatenate((im, col), axis=1)
plt.imshow(im, cmap='gray')
plt.show()
if __name__=='__main__':
show_images([0, 1, 2, 3, 4, 5, 6, 7])