Exploring Fashion-MNIST with TensorFlow and Keras

Discover The Neural Network Power For Classifying Images

Abdelhakim Ouafi
Sep 30 · 6 min read

One of the most excellent things concerning deep learning is that it assists you to match the x and y relationship and that’s what enables you to do magnificent things like have computers examine an image and do recognition or even examine an image and tells you that’s a dress or a combination of pants.

In this blog post, we tend to build a neural network (NN) and train it with the “Fashion MNIST Dataset”. Fashion MNIST Dataset is a data with 70.000 images and contains 10 classes of clothing with a dimension of 28 by 28 grayscale image color.

Fashion MNIST Dataset

You can explore the code from Google Colab.

Import the Fashion MNIST dataset

Let’s jump to the code and start by just importing the relevant libraries :

import keras
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout from keras.optimizers import Adam
import numpy as np
import matplotlib.pyplot as plt

We declare a variable called fashion and set it to an object on the MNIST dataset from the Keras module:

fashion = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion.load_data()

When creating a neural network, it’s a good idea to use some of the data for training, and a part of them that the model didn’t see before to test the performance of your neural network.

Every picture is mapped to one label and because the class names are not added with the MNIST dataset, we are going to store them here to use them later on when plotting the images with matplotlib:

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress','Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

Explore the Fashion MNIST dataset

Now, we gonna examine the format of the images before start training the neural network.

print(train_images.shape)
print(test_images.shape)

The following print statement shows there are 60,000 pictures in the training set and 10,000 pictures in the testing set and every image have 28 x 28 pixels dimensions.

Preprocess the data

The MNIST dataset or any kind of data needs to be preprocessed before you feed it to the neural network for training, and if you plot the first image in the training set by using matplotlib, you will see all the pixel values fall between 0 to 255:

plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
Image From MNIST Dataset

Now, we need to normalize the data before feeding it to the model so that the neural network will require less computational power for training. To do that we divide all the training image and the test images by 255 so it will be scaled to a range of 0 to 1:

train_images = train_images / 255.0
test_images = test_images / 255.0

You must confirm that the training dataset is in the right format, and do that, we are going to display the first 25 images from the training set in the matplotlib subplot and display the name of each class below every picture.

plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
First 25 Images From MNIST Dataset

Build the model

The most important theory of creating block neural networks is the layers. Layers will extract a pattern from the data that have fed it into them.

Most of the neural network models contain multiple layers that are connected to each other and most of them such as tf.keras.layers.Dense has parameters that are learned during training.

model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])

The first layer in this model: tf.keras.layers.Flatten transforms the format of each picture to a one-dimensional array.

Upon the pixels of pictures that have been flattened by the Flatten layer, the rest of the neural network contains a sequence of two tf.keras.layers.Dense layers and that is what we call the Fully connected layers.

The first layer which is the Dense has 128 neurons and the second one has 10 neurons.

You can use this line of code to get a great understanding of the architecture of your own neural network:

model.summary()
The Architecture of The Neural Network

Compile the model

Before we start running our neural networks, we need to make sure that we have set up a few more things and the first thing is:

The Loss function that Will detect how precise the neural network is during the compile process or in other words how the model is doing well.

The second one is Metrics which will observe the testing and training during the running of the model.

The third one is the Optimizer that will make the neural network update the parameters for our neural network during the training process.

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

Train the model

Train the artificial neural network demands the next steps:

  1. Feed the training data to the model.
  2. The artificial neural network learns to link images and labels.
  3. You ask the neural network to make predictions about a test set

To begin the training process, call the following method model.fit :

model.fit(train_images, train_labels, epochs=10)
The Training Process

Evaluate accuracy

Now, let’s test how the neural network works on the test dataset that it has never seen before:

test_loss, test_acc = model.evaluate(test_images, test_labels)
Test Accuracy

Make predictions

You can now use the model to make a prediction on any pictures that is related to what we trained on.

predictions = model.predict(test_images)

Plot this to see all set of 10 class predictions.

def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array, true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])

Our last step in this mini project is to plot different pictures with their predictions.

# Plot the first X test images, their predicted labels, and the true labels.
# Color correct predictions in blue and incorrect predictions in red.
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()
The Final Result

I hope you enjoy reading this brief introduction on how to classify the Fashion-Mnist Dataset with TensorFlow and Keras, you can do a lot more complex programs with Artificial Intelligence and Deep Neural Networks as we are going to discover more examples in the next set of articles.

The Startup

Medium's largest active publication, followed by +525K people. Follow to join our community.

Abdelhakim Ouafi

Written by

Experience in python, Deep learning, machine learning and data science.

The Startup

Medium's largest active publication, followed by +525K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade