Creating a Neural Network for Classifying Clothing Images into Different Categories using AI with TensorFlow

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

In this tutorial, I will show you how to create a sample neural network using TensorFlow and a public dataset. For this example, we will create a neural network that can classify images of clothing into different categories.

Before we get started, you’ll need to have TensorFlow installed on your machine. You can install it using the following command:

pip install tensorflow

Once you have TensorFlow installed, we can start working on our neural network example.

Step 1: Load the Dataset

For our example, we will be using the Fashion-MNIST dataset, which contains 70,000 grayscale images of clothing in 10 different categories. You can download the dataset from this link: https://github.com/zalandoresearch/fashion-mnist.

Once you have downloaded the .gz file, you’ll need to extract the .idx files using a decompression program like 7-Zip.

Now, we need to load the data into our Python program. To do that, we’ll use the Numpy library to read the .idx files and convert them into arrays that we can use to train our neural network.

import numpy as np

# Load the training data
with open('train-images-idx3-ubyte', 'rb') as f:
    train_images = np.frombuffer(f.read(), np.uint8, offset=16).reshape((-1, 28, 28, 1))

with open('train-labels-idx1-ubyte', 'rb') as f:
    train_labels = np.frombuffer(f.read(), np.uint8, offset=8)

# Load the test data
with open('t10k-images-idx3-ubyte', 'rb') as f:
    test_images = np.frombuffer(f.read(), np.uint8, offset=16).reshape((-1, 28, 28, 1))

with open('t10k-labels-idx1-ubyte', 'rb') as f:
    test_labels = np.frombuffer(f.read(), np.uint8, offset=8)

In this code, we are opening the .idx files using the Python open function and reading the data using the np.frombuffer function from Numpy. We are using the offset=16 option to skip the first 16 bytes of each file, which contain header information. We are also using the reshape function to reshape the image arrays into a 28×28 pixel shape with a single channel (grayscale). Finally, we are storing the data in the variables train_images, train_labels, test_images, and test_labels.

Step 2: Data Preprocessing

Before training our neural network, we need to perform some preprocessing steps on the data. In particular, we need to normalize the images so that all pixels are in the range of 0 to 1.

# Normalize the pixel values of the images
train_images = train_images / 255.0
test_images = test_images / 255.0

In this code, we are dividing each pixel value by 255 to normalize them into the range of 0 to 1.

After preprocessing the data, we can create our neural network model. For our example, we will use a simple Convolutional Neural Network (CNN) architecture with two convolutional layers and two dense layers.

import tensorflow as tf

# Create the neural network model
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
    tf.keras.layers.MaxPooling2D((2,2)),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2,2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])

In this code, we are using the TensorFlow Sequential class to create our neural network model. We are adding two convolutional layers with 3×3 filters and ReLU activation functions. After each convolutional layer, we are using a max pooling layer with a window size of 2×2. After the convolutional layers, we are flattening the feature maps using the Fltten layer and adding two dense layers with 128 and 10 units respectively. The final dense layer doesn’t have an activation function since we want the outputs to be the scores for each class.

Step 4: Compile and Train the Model

Once we have created our neural network model, we need to compile and train it using the training data.

# Compile the model
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

# Train the model
model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))

In this code, we are using the TensorFlow compile function to compile our neural network model. We are using the adam optimizer, sparse categorical cross-entropy loss, and accuracy metric.

After compiling the model, we are using the fit function to train our model with the training data. We are using 10 epochs of training and specifying the test data as the validation data.

Step 5: Model Evaluation

After training our model, we can evaluate its performance using the test data.

# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('Test accuracy:', test_acc)

In this code, we are using the evaluate function of TensorFlow to evaluate our model with the test data. We are printing the test accuracy to the console.

Step 6: Predicting New Images

Finally, we can use our trained model to make predictions on new clothing images.

# Make predictions on new images
predictions = model.predict(test_images)

# Print predictions for the first 5 images
for i in range(5):
    print('Prediction:', np.argmax(predictions[i]))
    print('True label:', test_labels[i])

In this code, we are using the predict function of TensorFlow to make predictions on the test images. We are then iterating over the first 5 images and printing the model’s prediction and the true label of the image to the console.

Leave a Comment