This tutorial shows how to perform image classification using Python and TensorFlow. In this project, we build a classifier to distinguish between different types of clothing items. We are using the fashion_mnist pre-trained model. It is a good example of how to use pre-trained models in TensorFlow.

What is Image Classification?

Image classification is a class of machine learning algorithms that use computers to look at images and classify them. Classifying images is a way for machines to learn about the world around us. Image classification is well suited for visual recognition, wherein the images have a natural one-to-one correspondence with the data for the classes. This is different from text categorization, where the classes have little or no natural one-to-one correspondence with the words in a textual description of the categorization. Image classification has been widely used in the field of astronomy to identify objects like galaxies, planets,

Prerequisites

Anaconda:

The Anaconda distribution is a collection of packages that consists of Python, R, and over 120 of the most popular open-source packages for science and data processing. This means that this distribution can be installed on a single machine which makes it immensely convenient to do data analytics on a large scale! You can download and install the conda by going to the following link.

https://www.anaconda.com/products/individual

After that, you’ll get access to the terminal which you’ll use to install all of your libraries.

Jupyter notebook:

Jupyter notebook is a free and open-source web application. Its commonly used by data scientists and other technical users who are looking to share their work, but anyone can use this platform for knowledge sharing. You can install the Jupyter notebook using the following command in your conda terminal.

pip install jupyter notebook

For accessing the notebook you can use this command.

jupyter notebook

Tensorflow:

TensorFlow is an open-source software library for numerical computation using data flow graphs. The graph nodes represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) that flow between them. The flexible architecture allows you to deploy computation to one or more CPUs or GPUs in a desktop, server, or mobile device with a single API.

You can install the Jupyter notebook using the following command in your conda terminal.

pip install --upgrade tensorflow

Numpy:

NumPy, short for Numerical Python, is a Python library used for scientific computing and data processing. This library makes it easier to run Python code on arrays and matrices instead of lists. It has many functions to make your mathematics faster.

You can install the Jupyter notebook using the following command in your conda terminal.

pip install numpy

Code

Import the dependency

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

load the data 

we are using the keras.datasets.fashion_mnist because it’s already there in Keras

fashion_mnist = keras.datasets.fashion_mnist

loading the data return four numpy arrays and these are the data that the model uses to learn 

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

Loading the labels

each image is mapped to a single label since the class is not included with the data set we have to store them here

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

exploring the data: (format the dataset)

Train_images.shape

Output:

(60000, 28, 28)

As you can see from the output we have 60000 images in the training set

len(train_labels)

Output:

60000

We have also 60000 labels in the datasets

For image classification with TensorFlow, data must go through a process called pre-processing to condition the images so that they are in line with the ImageNet 1k categories. If you inspect the first frame of your dataset, you will see that it has pixel values ​​ranging from 0 – 255:

plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

Output:

we divided the train images and test images by 255 because the training set and the testing set must be preprocessed in the same way 

train_images = train_images / 255.0

test_images = test_images / 255.0

now we are going to is to display the 1st 25 images from the training set and display the class’s name below in each image.

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()

Output:

Now we are ready to build and train the network

Building the network:

Building the neural network requires configuring the layers of the model and then compiling the model

# setup the layer
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])
#Fitting the Model
model.fit(train_images, train_labels, epochs=10)
#Evaluating Accuracy
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

Training Model

#Fitting the Model
model.fit(train_images, train_labels, epochs=10)
#Evaluating Accuracy
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

Output:

So here you can see that the accuracy is 0.8862000107765198

Make prediction:

probability_model = tf.keras.Sequential([model,
                                        tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
predictions[0]

Output:

Creating a graph to look at the prediction

# defined the plot image
def plot_image(i, predictions_array, true_label, img):
  true_label, img = true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  # defining the plot value array
  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
  true_label = true_label[i]
  plt.grid(False)
  plt.xticks(range(10))
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

So now let’s look at the 1st number of images to verify the prediction

i = 1
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
plt.show()

Output:

Predict all the images at the same time 

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()

Output

Here the prediction of the image with blue is right and the prediction with red is wrong.

Final Words

This blog post provides an example of how to use TensorFlow, the Python machine learning library that Google open-sourced, to train a model that can distinguish between different types of images. We used an existing dataset called fashion_mnist , and specifically the one trained on it by Google, whose goal is to identify which item of clothing is shown in a given image. So hope you liked the tutorial and if you have any questions, please feel free to leave them down below and I’ll do my best to answer them!

Here are some useful tutorials that you can read: