Training Code CNN + MNIST
..
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Input
from keras.layers import Conv2D, MaxPooling2D
"""Build CNN Model"""
num_classes = 10
input_shape = (28, 28, 1) #mnist channels first format
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.summary()
"""Download MNIST Data"""
from keras.datasets import mnist
import numpy as np
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#(60000, 28, 28) -> (60000, 28, 28, 1)
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
"""Show some images"""
import matplotlib.pyplot as plt
row = 10
col = 10
n = row * col
plt.figure(figsize=(4, 4))
for i in range(n):
# display original
#https://jakevdp.github.io/PythonDataScienceHandbook/04.08-multiple-subplots.html
ax = plt.subplot(row, col, i+1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
"""set up tensorboard"""
from datetime import datetime
import os
logdir="logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S")
os.makedirs(logdir, exist_ok=True)
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)
"""Train model"""
from keras.callbacks import TensorBoard
batch_size = 128
epochs = 1
model.fit(x_train, y_train,
epochs=epochs,
batch_size=batch_size,
shuffle=True,
validation_data=(x_test, y_test),
callbacks=[TensorBoard(log_dir=logdir)])
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
"""test one image data """
x_test[0].shape
one_image = x_test[0].reshape(1,28,28,1)
y_pred_all = model.predict(one_image)
y_pred_it = model.predict_classes(one_image)
print(y_pred_all, y_pred_it)
plt.imshow(x_test[0].reshape(28, 28))
plt.show()
"""save model to drive"""
model.save('my_cnn_mnist_model.h5')
..
CNN network Layout
Dataset
Run Tensorboard
>cd ./logs/scalars/20190730-105257
>tensorboard --logdir=./
almost 99% accuracy
Load Model and test one mnist image
...
"""load model from drive"""
from keras.models import load_model
new_model = load_model('my_cnn_mnist_model.h5')
"""load 1 image from drive"""
from PIL import Image
import numpy as np
"""test prediction"""
img_path = './mnist_7_450.jpg'
img = Image.open(img_path) #.convert("L")
img = np.resize(img, (28,28,1))
im2arr = np.array(img)
im2arr = im2arr.reshape(1,28,28,1)
y_pred = new_model.predict_classes(im2arr)
print(y_pred)
...
Test image
output
[7]
download minist jpeg file on here:
http://study.marearts.com/2015/09/mnist-image-data-jpg-files.html