In [12]:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow
import keras
from keras import backend as K
K.clear_session()
%matplotlib inline
In [13]:
# Import the MNIST dataset
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
In [14]:
# Check the amount of data we have
print x_train.shape, y_train.shape
print x_test.shape, y_test.shape
In [15]:
# Normalize the MNIST data
x_train = x_train/255.
x_test = x_test/255.
# Reshape the data
x_train = x_train.reshape( x_train.shape+(1,) )
x_test = x_test.reshape( x_test.shape+(1,) )
# Change the one-hot-encoding
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
In [16]:
# Check the amount of data we have (again)
print x_train.shape, y_train.shape
print x_test.shape, y_test.shape
In [17]:
# Check the data we have
fig,axs = plt.subplots(3,8)
axs = [a for b in axs for a in b]
for idx in range(len(axs)):
axs[idx].imshow(x_train[idx,:,:,0])
axs[idx].set_title(y_train[idx].argmax())
axs[idx].axis('off')
plt.show()
In [18]:
# Training Parameters
batch_size = 128
num_classes = 10
epochs = 5
In [19]:
# Define the model
img_input = keras.layers.Input(shape=(28, 28, 1))
x = keras.layers.Convolution2D(32, (3, 3), padding='same', activation='relu')(img_input)
x = keras.layers.MaxPooling2D(pool_size=[2,2])(x)
x = keras.layers.Convolution2D(32, (3, 3), padding='same', activation='relu')(x)
x = keras.layers.MaxPooling2D(pool_size=[2,2])(x)
x = keras.layers.Convolution2D(32, (3, 3), padding='same', activation='relu')(x)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(num_classes, activation='softmax')(x)
model = keras.models.Model(img_input, x, name='model_perso')
In [20]:
# Compile the model
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Nadam(),
metrics=['accuracy'])
In [21]:
# Train the model
tensorboard = keras.callbacks.TensorBoard(log_dir='./', histogram_freq=1)
training = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[tensorboard,],
)
In [22]:
# Show some results
n_test_imgs = 3
fig,axs = plt.subplots(n_test_imgs,2)
preds = model.predict(x_test[:n_test_imgs])
for idx in range(n_test_imgs):
axs[idx][0].imshow(x_test[idx,:,:,0])
axs[idx][0].set_title(y_test[idx].argmax())
axs[idx][0].axis('off')
axs[idx][1].bar(range(10), preds[idx])
plt.show()
print "predicted", preds.argmax(axis=1)
print "with proba", preds.max(axis=1)
In [ ]: