Fashion MNIST with Keras in 5 minutes

Łukasz Lipiński
3 min readAug 28, 2017

--

Source: https://github.com/zalandoresearch/fashion-mnist

The original MNIST database is a large set of handwritten digits that is used for training and benchmark in machine learning. This data contains 60,000 training images and 10,000 testing images. You can learn more on Wikipedia: https://en.wikipedia.org/wiki/MNIST_database

Now, there is a new proposal for replacement the MNIST database. People from Zalando Research Team created database of clothes images (https://github.com/zalandoresearch/fashion-mnist). This data also contains 60,000 training images and 10,000 testing images.

It is good way to practice deep learning. So, we can start now. I am going to use Keras for simplification. Maybe next time, I will use Tensorflow.

My short tutorial is mostly for people, who know Python or other programming language and have basic understanding of deep learning and convolutional neural network.

# Import librariesfrom mnist_reader import load_mnist
from keras.layers import Dense, MaxPool2D, Conv2D, Dropout
from keras.layers import Flatten, InputLayer
from keras.layers.normalization import BatchNormalization
from keras.models import Sequential
from keras.utils import np_utils
from keras.initializers import Constant
# Load data
# Function load_minst is available in git.
X_train, y_train = load_mnist('data', kind='train')
X_test, y_test = load_mnist('data', kind='t10k')
# Prepare datasets
# This step contains normalization and reshaping of input.
# For output, it is important to change number to one-hot vector.
X_train = X_train.astype('float32') / 255
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.astype('float32') / 255
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)
# Create model in Keras
# This model is linear stack of layers
clf = Sequential()
# This layer is used as an entry point into a graph.
# So, it is important to define input_shape.
clf.add(
InputLayer(input_shape=(1, 28, 28))
)
# Normalize the activations of the previous layer at each batch.
clf.add(
BatchNormalization()
)
# Next step is to add convolution layer to model.
clf.add(
Conv2D(
32, (2, 2),
padding='same',
bias_initializer=Constant(0.01),
kernel_initializer='random_uniform'
)
)
# Add max pooling layer for 2D data.
clf.add(MaxPool2D(padding='same'))
# Add this same two layers to model.
clf.add(
Conv2D(
32,
(2, 2),
padding='same',
bias_initializer=Constant(0.01),
kernel_initializer='random_uniform',
input_shape=(1, 28, 28)
)
)clf.add(MaxPool2D(padding='same'))
# It is necessary to flatten input data to a vector.
clf.add(Flatten())
# Last step is creation of fully-connected layers.
clf.add(
Dense(
128,
activation='relu',
bias_initializer=Constant(0.01),
kernel_initializer='random_uniform',
)
)
# Add output layer, which contains ten numbers.
# Each number represents cloth type.
clf.add(Dense(10, activation='softmax'))
# Last step in Keras is to compile model.
clf.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)

Function print_summary prints information about model.

print(clf.summary())
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) (None, 1, 28, 28) 0
_________________________________________________________________
batch_normalization_3 (Batch (None, 1, 28, 28) 112
_________________________________________________________________
conv2d_17 (Conv2D) (None, 1, 28, 32) 3616
_________________________________________________________________
max_pooling2d_17 (MaxPooling (None, 1, 14, 32) 0
_________________________________________________________________
conv2d_18 (Conv2D) (None, 1, 14, 32) 4128
_________________________________________________________________
max_pooling2d_18 (MaxPooling (None, 1, 7, 32) 0
_________________________________________________________________
flatten_9 (Flatten) (None, 224) 0
_________________________________________________________________
dense_13 (Dense) (None, 128) 28800
_________________________________________________________________
dense_14 (Dense) (None, 10) 1290
=================================================================
Total params: 37,946.0
Trainable params: 37,890.0
Non-trainable params: 56.0

This is very simply convolutional neural network. Model has only 37,946 params. Now is time to train this network for image classification.

clf.fit(
X_train,
y_train,
epochs=20,
batch_size=32,
validation_data=(X_test, y_test)
)

I run this model on CPU with 4 cores. It took about 15 minutes. I finished with a good model. I have accuracy 0.94 on training data and 0.90 on testing data.

clf.evaluate(X_test, y_test)9632/10000 [===========================>..] - ETA: 0s [0.33517245993614198, 0.89829999999999999]# You can check metrics name in a vector above.
clf.metrics_names
['loss', 'acc']
# Loss is a loss function. Acc is an accuracy.

Feel free to comment or give some feedback.

--

--