Classify Images of Clothing using Tensorflow

Original article was published on Deep Learning on Medium

TensorFlow works based on data flow graphs that have nodes and edges. It accepts data in the form Tensors, which are nothing but multi-dimensional arrays of higher dimensions and can handle large amounts of data.

Project — Classify images of clothing

1. Import important Libraries

We will be using tf.keras, which is TensorFlow’s high-level API for building and training deep learning models. It’s used for fast prototyping and create new layers, metrics, loss functions, and develop state-of-the-art models.

Numpy is a core library for scientific computing in Python and Matplotlib is a library for creating static, animated, and interactive visualizations.

# import TensorFlow, tf.keras, numpy and matplotlib
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

2. Import the dataset

We will be using Fashion MNIST dataset in this project. The image contains 70,000 grayscale images in 10 categories, out of which 60,000 images are used to train the network and 10,000 images to evaluate how accurately the network learned to classify images.

Individual articles of clothing at low resolution (28 by 28 pixels)
fashion_mnist = keras.datasets.fashion_mnist
#returns four numpy 28x28 arrays with pixel values ranging from 0 to #255
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

Each image is mapped to a single label. Labels are nothing but the array of integers, ranging from 0 to 9 and correspond to the class of the clothing the image represents.

Clothing Labels and Classes

Training sets represents the data that the model uses to learn — train_images and train_labels arrays.

Test sets represent the data that the model uses to test — test_images, and test_labels arrays.

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

3. Data Exploration


(60000, 28, 28)




(10000, 28, 28)



4. Data Preprocessing

For both training and test sets, you can see that the pixel values of the images fall in the range of 0 to 255, which needs to be scaled in the range of 0 and 1 before feeding it in the neural network. To do so, divide the values by 255.

train_images = train_images / 255.0test_images = test_images / 255.0

To verify that the data is in the correct format, display the first 25 images from the training set, and display the class name below each image.

for i in range(25):

5. Build the model

To build the model, first chain the different layers, configure them and then compile the model.

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

The layers —

tf.keras.layers.Flatten— transforms the format of the images from a two-dimensional array (28 x 28 pixels) to a one-dimensional array (of 28 * 28 = 784 pixels). Once the pixels are flattened, the network consists of a sequence of two dense layers.

tf.keras.layers.Dense — These 2 layers are dense/fully-connected, neural layers with 128 nodes in the first dense layer, and the second layer returns a logits array with a length of 10.

After this compile the model, where —

  • Optimizer — This is how the model is updated based on the data it sees and its loss function. Adam is an optimization algorithm that can be used instead of the classical stochastic gradient descent procedure to update network weights iterative based on training data. It’s simple to use.
  • Loss function — Loss Function being one of the important components of Neural Networks is nothing but a prediction error of Neural Network. This measures how accurate the model is during training.
  • Metrics — To monitor the training and testing steps, we use metrics. In this example, we will be using accuracy metrics, which indicates nothing but the fraction of the images that are correctly classified.

6. Model Training

This happens in 4 steps — Feed the training data, Learn, Predict, Verify the Prediction

#fit the model to the training data, train_labels, epochs=10)Epoch 1/10
1875/1875 [==============================] - 4s 2ms/step - loss: 1.0486 - accuracy: 0.6656
Epoch 2/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.6211 - accuracy: 0.7744
Epoch 3/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.5498 - accuracy: 0.8026
Epoch 4/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.5096 - accuracy: 0.8185
Epoch 5/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4832 - accuracy: 0.8284
Epoch 6/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4636 - accuracy: 0.8363
Epoch 7/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4485 - accuracy: 0.8418
Epoch 8/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4365 - accuracy: 0.8456
Epoch 9/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.4265 - accuracy: 0.8496
Epoch 10/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4175 - accuracy: 0.8539<tensorflow.python.keras.callbacks.History at 0x7faeadc6d048>

You can see that loss and accuracy are calculated in each epoch. The model achieved an accuracy of around 85% on the training data.

7. Evaluate accuracy on test data

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)print('\nTest accuracy:', test_acc)

Test accuracy: 0.8379999995231628

This is a case of overfitting where the accuracy of the test dataset is less than the accuracy of the training dataset. Overfitting happens when your model “memorizes” the noise and details in the training dataset to a point where it negatively impacts the performance of the model on the new data.

8. Prediction time

probability_model = tf.keras.Sequential([model, 
predictions = probability_model.predict(test_images)
#Output is an array of 10 numbers which represent model's confidencearray
([2.0062183e-07, 3.7709762e-11, 7.3624884e-10, 2.9959158e-12, 1.1696175e-09, 3.5686058e-04, 2.6559412e-08, 1.0531254e-03, 7.2562241e-11, 9.9858981e-01], dtype=float32)

You can see which label has the highest confidence value.

Output : 0

So, the model is most confident that this image is a T-shirt/Top, or class_names[0].

Let’s plot several images 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)
Cloth images with their predictions

Note: This is a very basic project using TensorFlow (Code Reference: I created this post to give an introduction to the neural network, Tensor Flow, and demonstrate it’s inner working through a project. Many Thanks to Google for providing the TensorFlow library.

Recommended Articles

Resources and References