Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Classifying Clothing Images in Python
Image classification is a type of machine learning task that involves identifying objects or scenes in an image. 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
import numpy as np import matplotlib.pyplot as plt import tensorflow as tf
Loading and Pre-processing the Data
The Fashion-MNIST dataset is included in the TensorFlow library. We can load it and explore the data structure ?
# Load the Fashion-MNIST dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
# Check the shape of the data
print(f"Training data shape: {x_train.shape}")
print(f"Training labels shape: {y_train.shape}")
print(f"Test data shape: {x_test.shape}")
print(f"Test labels shape: {y_test.shape}")
Training data shape: (60000, 28, 28) Training labels shape: (60000,) Test data shape: (10000, 28, 28) Test labels shape: (10000,)
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.
The images are 28x28 pixels in size and grayscale. We need to pre-process the images by reshaping and normalizing them ?
# Reshape the images to add a channel dimension
x_train = x_train.reshape(60000, 28, 28, 1)
x_test = x_test.reshape(10000, 28, 28, 1)
# Normalize pixel values to range [0, 1]
x_train = x_train / 255.0
x_test = x_test / 255.0
# Convert labels to categorical one-hot encoding
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
print(f"Preprocessed training data shape: {x_train.shape}")
print(f"Preprocessed labels shape: {y_train.shape}")
Preprocessed training data shape: (60000, 28, 28, 1) Preprocessed labels shape: (60000, 10)
Building the Model
We will create a simple neural network with fully connected layers. The model consists of a flattening layer followed by dense layers ?
# Build the sequential 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')
])
# Display model summary
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
dense (Dense) (None, 128) 100480
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
The flattening layer converts 28x28 images into 784-dimensional vectors. The dense layer with 128 neurons uses ReLU activation, and the final layer outputs probabilities for 10 clothing classes using softmax activation.
Training the Model
We compile the model with Adam optimizer and categorical crossentropy loss, then train it for 10 epochs ?
# Compile the model
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Train the model
history = model.fit(
x_train, y_train,
epochs=10,
validation_split=0.2,
verbose=1
)
Epoch 1/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.5123 - accuracy: 0.8223 - val_loss: 0.4238 - val_accuracy: 0.8488 Epoch 2/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.3859 - accuracy: 0.8618 - val_loss: 0.3846 - val_accuracy: 0.8608 ... Epoch 10/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.2645 - accuracy: 0.9028 - val_loss: 0.3421 - val_accuracy: 0.8782
Evaluating the Model
After training, we evaluate the model's performance on the test dataset ?
# Evaluate on test data
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f'Test Loss: {test_loss:.4f}')
print(f'Test Accuracy: {test_accuracy:.4f}')
Test Loss: 0.3654 Test Accuracy: 0.8734
Making Predictions
Let's test our model by making predictions on some sample images ?
# Fashion-MNIST class names
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# Make predictions on test data
predictions = model.predict(x_test[:5])
# Display predictions for first 5 images
for i in range(5):
predicted_class = np.argmax(predictions[i])
actual_class = np.argmax(y_test[i])
print(f"Image {i+1}: Predicted - {class_names[predicted_class]}, "
f"Actual - {class_names[actual_class]}")
Image 1: Predicted - Ankle boot, Actual - Ankle boot Image 2: Predicted - Pullover, Actual - Pullover Image 3: Predicted - Trouser, Actual - Trouser Image 4: Predicted - Trouser, Actual - Trouser Image 5: Predicted - Shirt, Actual - Shirt
Conclusion
We successfully built a neural network model to classify Fashion-MNIST clothing images, achieving approximately 87% test accuracy. This demonstrates how Python and TensorFlow can be used for image classification tasks. The model can be further improved with more complex architectures like CNNs or by fine-tuning hyperparameters.
