-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist.py
88 lines (73 loc) · 2.78 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
88
from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
from keras.models import Sequential
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow import keras
import random
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print("x_train shape", x_train.shape)
print("y_train shape", y_train.shape)
print("x_test shape", x_test.shape)
print("y_test shape", y_test.shape)
# 咱给变成4维的
x_train = x_train.reshape(*x_train.shape, 1).astype('float32')
x_test = x_test.reshape(*x_test.shape, 1).astype('float32')
input_shape = (28, 28, 1)
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print('Number of images in x_train', x_train.shape[0])
print('Number of images in x_test', x_test.shape[0])
# Creating a Sequential Model and adding the layers
model = Sequential()
model.add(Conv2D(30, kernel_size=(3, 3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # Flattening the 2D arrays for fully connected layers
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10, activation=tf.nn.softmax))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Visualize all the layers
print("--" * 60, model.summary())
for layer in model.layers:
print(layer.name)
print(layer)
if layer.name == 'conv2d_1':
filters, biases = layer.get_weights()
fmin, fmax = filters.min(), filters.max()
filters = (filters - fmin) / (fmax - fmin)
print(filters.round(1))
print('filter shape', filters.shape)
ix = 1
for i in range(6):
plt.imshow(filters[:, :, 0, 0], cmap='gray')
print(filters[:, :, 0, 0])
plt.show()
# plot first few filters
n_filters, ix = 6, 1
for i in range(n_filters):
# get the filter
f = filters[:, :, :, i]
print(f'Filter # {i}', f)
# plot each channel separately
for j in range(0):
# specify subplot and turn of axis
ax = plt.subplot(n_filters, 3, ix)
ax.set_xticks([])
ax.set_yticks([])
# plot filter channel in grayscale
print('j', j, f.shape)
plt.imshow(f[:, :, j], cmap='gray')
ix += 1
# show the figure
plt.show()
model.fit(x=x_train, y=y_train, epochs=10)
result = model.evaluate(x_test, y_test)
print(result)
image_index = random.choice(range(0, 10000))
plt.imshow(x_test[image_index].reshape(28, 28), cmap='Greys')
pred = model.predict(x_test[image_index].reshape(1, 28, 28, 1))
print('predict', pred.argmax(), 'fact', y_test[image_index])