Fashion MNIST - MLP

Image Classification - 60,000 Training, 10,000 Test, 10 Class

Load Libraries

In [1]:
# For data processing
import numpy as np
import pandas as pd

# For Deep Learning
import keras

# Visualisation
import matplotlib.pyplot as plt
%matplotlib inline
import vis
Using TensorFlow backend.

Step 0: Load the Data

In [2]:
from keras.datasets import fashion_mnist
In [3]:
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
Downloading data from http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 3s 0us/step
Downloading data from http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 2s 0us/step
In [4]:
x_train.shape, y_train.shape, x_test.shape, y_test.shape
Out[4]:
((60000, 28, 28), (60000,), (10000, 28, 28), (10000,))
In [6]:
labels = vis.fashion_mnist_label()
labels
Out[6]:
{0: 'T-shirt/top',
 1: 'Trouser',
 2: 'Pullover',
 3: 'Dress',
 4: 'Coat',
 5: 'Sandal',
 6: 'Shirt',
 7: 'Sneaker',
 8: 'Bag',
 9: 'Ankle boot'}

See an image

In [8]:
x_train[0].shape
Out[8]:
(28, 28)
In [9]:
vis.imshow(x_train[0])
Out[9]:

Let us see the first 10 unique image

In [25]:
import importlib
importlib.reload(vis)
Out[25]:
<module 'vis' from '/volumes/notebooks/vis.py'>
In [26]:
vis.imshow_unique(x_train, y_train, labels)
In [27]:
vis.imshow_sprite(x_train[:500])

Step 1: Prepare our input(images) and output(labels)

In [28]:
# Normalize the data 
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") /255
In [33]:
# Flatten the input
x_train_flatten = x_train.reshape(60000, 28*28)
x_test_flatten = x_test.reshape(10000, 28*28)
x_train_flatten.shape, x_test_flatten.shape
Out[33]:
((60000, 784), (10000, 784))
In [34]:
from keras.utils import to_categorical
In [38]:
# Output data to Categorical Encoding
y_train_class = to_categorical(y_train, 10)
y_test_class = to_categorical(y_test, 10)
In [37]:
y_train.shape, y_train_class.shape
Out[37]:
((60000,), (60000, 10))
In [39]:
y_train[0], y_train_class[0]
Out[39]:
(9, array([0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], dtype=float32))

Step 2: Create the feature transformer and Classifier

In [41]:
from keras.models import Sequential
from keras.layers import Dense, Activation
In [42]:
mlp = Sequential()
mlp.add(Dense(100, activation="relu", input_dim=28*28))
mlp.add(Dense(50, activation="relu"))
mlp.add(Dense(10, activation="softmax"))
In [43]:
mlp.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 100)               78500     
_________________________________________________________________
dense_2 (Dense)              (None, 50)                5050      
_________________________________________________________________
dense_3 (Dense)              (None, 10)                510       
=================================================================
Total params: 84,060
Trainable params: 84,060
Non-trainable params: 0
_________________________________________________________________

Step 3: Compile the Model - loss, optimizer and Fit to data

In [44]:
mlp.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=['accuracy'])
In [49]:
%%time
output_mlp = mlp.fit(x_train_flatten, y_train_class, epochs=10, verbose=2,
                     validation_data=(x_test_flatten, y_test_class))
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
 - 4s - loss: 0.3880 - acc: 0.8635 - val_loss: 0.4317 - val_acc: 0.8488
Epoch 2/10
 - 4s - loss: 0.3757 - acc: 0.8668 - val_loss: 0.4075 - val_acc: 0.8563
Epoch 3/10
 - 5s - loss: 0.3644 - acc: 0.8709 - val_loss: 0.4272 - val_acc: 0.8495
Epoch 4/10
 - 5s - loss: 0.3548 - acc: 0.8735 - val_loss: 0.3913 - val_acc: 0.8597
Epoch 5/10
 - 5s - loss: 0.3449 - acc: 0.8771 - val_loss: 0.3930 - val_acc: 0.8596
Epoch 6/10
 - 5s - loss: 0.3373 - acc: 0.8788 - val_loss: 0.3968 - val_acc: 0.8555
Epoch 7/10
 - 4s - loss: 0.3298 - acc: 0.8822 - val_loss: 0.3745 - val_acc: 0.8645
Epoch 8/10
 - 3s - loss: 0.3234 - acc: 0.8838 - val_loss: 0.3724 - val_acc: 0.8653
Epoch 9/10
 - 3s - loss: 0.3152 - acc: 0.8862 - val_loss: 0.3743 - val_acc: 0.8655
Epoch 10/10
 - 3s - loss: 0.3095 - acc: 0.8885 - val_loss: 0.3814 - val_acc: 0.8631
CPU times: user 1min 7s, sys: 7.76 s, total: 1min 14s
Wall time: 41.2 s

Step 4: Check the performance

In [48]:
vis.metrics(output_mlp.history)
Out[48]:
In [50]:
score = mlp.evaluate(x_test_flatten, y_test_class)
10000/10000 [==============================] - 0s 19us/step
In [51]:
print('Test loss', score[0])
print('Test accuracy', score[1])
Test loss 0.3814000262141228
Test accuracy 0.8631

Step 5: Make the Prediction & Visualise

In [59]:
predict_classes = mlp.predict_classes(x_test_flatten)
In [61]:
pd.crosstab(y_test, predict_classes)
Out[61]:
col_0 0 1 2 3 4 5 6 7 8 9
row_0
0 829 0 30 46 2 1 80 0 12 0
1 2 969 2 21 2 0 4 0 0 0
2 14 1 865 13 48 0 54 0 4 1
3 16 12 25 901 23 1 16 0 6 0
4 0 1 201 44 688 0 62 0 4 0
5 1 0 0 1 0 939 0 31 2 26
6 152 2 132 44 51 1 607 0 11 0
7 0 0 0 0 0 28 0 905 0 67
8 2 1 15 6 6 1 10 4 954 1
9 0 0 0 1 0 3 1 21 0 974
In [62]:
proba = mlp.predict_proba(x_test_flatten)
In [63]:
i = 4
In [64]:
vis.imshow(x_test[i], labels[y_test[i]]) | vis.predict(proba[i], y_test[i], labels)
Out[64]: