Classifying Clothing Images in Python


Image classification is a type of machine learning task that involves identifying the objects or scenes in an image. It is a challenging task, but it has many applications in real−world problems such as facial recognition, object detection, and medical image analysis.

In this article, we will discuss how to classify clothing images using Python. We will use the Fashion−MNIST dataset, which is a collection of 60,000 grayscale images of 10 different clothing items. We will build a simple neural network model to classify these images.

Import the Modules

The first step is to import the necessary modules. We will need the following modules:

  • numpy: for working with arrays

  • matplotlib.pyplot: for plotting images

  • tensorflow: for building and training neural networks.

Consider the commands shown below to import the modules.

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

Loading and Pre−processing the Data

The next step is to load the Fashion−MNIST dataset. This dataset is included in the TensorFlow library. We can load it using the following code:

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

The x_train and x_test variables contain the training and testing images, respectively. The y_train and y_test variables contain the labels for the training and testing images, respectively.

The images in the Fashion−MNIST dataset are 28x28 pixels in size. They are also grayscale, which means that they have only one channel. We need to pre−process the images before we can train our model. The following code shows how to do this:

x_train = x_train.reshape(60000, 28, 28, 1) x_test = x_test.reshape(10000, 28, 28, 1) x_train = x_train / 255.0 x_test = x_test / 255.0

This code reshapes the images to have a fourth dimension, which represents the number of channels. It also normalises the images to have values between 0 and 1.

Building the Model

Now that the data is pre−processed, we can build the model. We will use a simple neural network with two hidden layers. The following code shows how to build the model:

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

The first layer in the model is a flattening layer. This layer flattens the 28x28 images into a 784−dimensional vector. The next two layers are dense layers. These layers are fully connected layers, which means that every neuron in one layer is connected to every neuron in the next layer. The final layer is a softmax layer. This layer outputs a probability distribution over the 10 possible classes.

Training the Model

Now that the model is built, we can train it. We will use the Adam optimizer and the categorical cross−entropy loss function. The following code shows how to train the model:

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=10)

The model is trained for 10 epochs. An epoch is a complete pass through the training data. After 10 epochs, the model has learned to classify clothing images with an accuracy of about 92%.

Evaluating the Model

Now that the model is trained, we can evaluate it on the testing data. The following code shows how to do this:

loss, accuracy = model.evaluate(x_test, y_test) print('Test loss:', loss) print('Test accuracy:', accuracy)

The model achieves a test loss of 0.27 and a test accuracy of 91.4%

Conclusion

In conclusion, we have discussed how to classify clothing images using Python. We have used the Fashion−MNIST dataset, which is a collection of 60,000 grayscale images of 10 different clothing items. We have built a simple neural network model to classify these images. The model has achieved a test accuracy of 91.4%. This is a promising result, and it shows that machine learning can be used to solve real−world problems.

In the future, we can improve the accuracy of the model by using a larger dataset, using a more complex model, and using a better optimization algorithm. We can also use the model to classify clothing images in real−time. This would be useful for applications such as online shopping and self−checkout machines.

Updated on: 04-Aug-2023

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements