top of page

Learn through our Blogs, Get Expert Help & Innovate with Colabcodes

Welcome to Colabcodes, where technology meets innovation. Our articles are designed to provide you with the latest news and information about the world of tech. From software development to artificial intelligence, we cover it all. Stay up-to-date with the latest trends and technological advancements. If you need help with any of the mentioned technologies or any of its variants, feel free to contact us and connect with our freelancers and mentors for any assistance and guidance. 

blog cover_edited.jpg

ColabCodes

Writer's picturesamuel black

Classifying Fashion MNIST with Neural Networks Using TensorFlow in Python

Fashion MNIST is a more challenging and realistic dataset compared to the original MNIST, featuring images of various clothing items. This tutorial will guide you through implementing a neural network to classify these fashion items using TensorFlow and Python. We’ll cover everything from loading and preparing the dataset to building, training, and evaluating a neural network model.

Classifying Fashion MNIST with Neural Networks Using TensorFlow in Python - colabcodes

What is the Fashion MNIST Dataset?

Fashion MNIST is a dataset of Zalando's article images, consisting of 70,000 grayscale images in 10 categories. Each image is 28x28 pixels, similar to the original MNIST dataset, but instead of digits, the images represent items of clothing such as T-shirts, trousers, and shoes. The dataset is divided into 60,000 training images and 10,000 test images. Each image is labeled with one of the 10 categories, making it a multiclass classification problem. Here’s a breakdown of the classes:


  1. T-shirt/top

  2. Trouser

  3. Pullover

  4. Dress

  5. Coat

  6. Sandal

  7. Shirt

  8. Sneaker

  9. Bag

  10. Ankle boot


Why Use Fashion MNIST?

Fashion MNIST is used to challenge the limitations of traditional MNIST while remaining a straightforward dataset for those new to machine learning. Unlike the digit images in MNIST, fashion images are more complex and have less obvious differences between classes, making this dataset a bit more challenging and a better test of a model's capability.


  1. Increased Complexity: Compared to MNIST digits, the Fashion MNIST images are more challenging due to subtle differences between categories, providing a better test for modern machine learning models.

  2. Real-World Relevance: Fashion MNIST represents a more realistic task, as recognizing and classifying clothing items is directly applicable in fields like e-commerce and retail.

  3. Compatibility with MNIST: Fashion MNIST has the same format as MNIST, making it easy to switch datasets without changing much of your code.


Implementing Neural Networks on the Fashion MNIST Dataset with TensorFlow in Python

Implementing neural networks on the Fashion MNIST dataset with TensorFlow in Python involves several key steps to build an effective image classification model. First, you load and preprocess the Fashion MNIST dataset, which includes normalizing the pixel values of 28x28 grayscale images to a range of 0 to 1. Next, you design and construct a neural network model using TensorFlow's Keras API, typically comprising an input layer to flatten the image data, multiple dense hidden layers with activation functions to learn complex patterns, and an output layer with softmax activation to classify the images into one of ten categories. The model is then compiled with an optimizer and loss function, trained on the training set, and evaluated on the test set to gauge its performance. This approach demonstrates how deep learning models can be effectively applied to image classification tasks, providing a practical introduction to neural network implementation in TensorFlow.


Loading and Preparing Fashion MNIST Dataset

TensorFlow provides a convenient way to load the Fashion MNIST dataset through its tf.keras.datasets module. The dataset is already split into training and testing sets, and we need to normalize the pixel values to improve model performance.


import tensorflow as tf


# Load the Fashion MNIST dataset

fashion_mnist = tf.keras.datasets.fashion_mnist


# Split into training and testing data

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()


# Normalize the pixel values to be between 0 and 1

x_train, x_test = x_train / 255.0, x_test / 255.0


Visualizing Fashion MNIST Dataset

To understand what our dataset looks like, it’s helpful to visualize some images along with their labels.


import matplotlib.pyplot as plt


# Define class names for the labels

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',

               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']


# Display the first 10 images and their labels

plt.figure(figsize=(10,10))

for i in range(10):

    plt.subplot(2, 5, i+1)

    plt.xticks([])

    plt.yticks([])

    plt.grid(False)

    plt.imshow(x_train[i], cmap='gray')

    plt.title(class_names[y_train[i]])

plt.show()


Output for the above code:

Visualizing Fashion MNIST Dataset - colabcodes

Building the Neural Network in Tensorflow - Keras

Now, we’ll create a simple neural network model using TensorFlow’s Keras API. Our model will include an input layer, two hidden layers, and an output layer.


# Build the model

model = tf.keras.models.Sequential([

    tf.keras.layers.Flatten(input_shape=(28, 28)), # Flatten the input image

    tf.keras.layers.Dense(128, activation='relu'), # First hidden layer

    tf.keras.layers.Dense(64, activation='relu'), # Second hidden layer

    tf.keras.layers.Dense(10, activation='softmax') # Output layer with 10 neurons (one for each class)

])


# Compile the model

model.compile(optimizer='adam',

              loss='sparse_categorical_crossentropy',

              metrics=['accuracy'])


Training the Neural Networks

We’ll train the model on the training data. During training, the model will learn to classify the images based on the features extracted by the network.


# Train the model

model.fit(x_train, y_train, epochs=10)


Output for the above code:

Epoch 8/10
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 7s 4ms/step - accuracy: 0.9042 - loss: 0.2521
Epoch 9/10
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 9s 3ms/step - accuracy: 0.9093 - loss: 0.2381
Epoch 10/10
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 11s 3ms/step - accuracy: 0.9105 - loss: 0.2347

Evaluating the Neural Model

After training, it’s crucial to evaluate the model on the test data to see how well it performs on unseen images.


# Evaluate the model

test_loss, test_acc = model.evaluate(x_test, y_test)


print('\nTest accuracy:', test_acc)


Output for the above code:

313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8845 - loss: 0.3408

Test accuracy: 0.883400022983551

Making Predictions

With the trained model, you can now make predictions on new data and see how well it classifies images.


# Make predictions

predictions = model.predict(x_test)


# Display the prediction for the first test image

print("Predicted label:", class_names[predictions[0].argmax()])

print("True label:", class_names[y_test[0]])


Output for the above code:

313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step
Predicted label: Ankle boot
True label: Ankle boot

Full code for Classifying Fashion MNIST with Neural Networks Using TensorFlow in Python

The full code for classifying Fashion MNIST with neural networks using TensorFlow in Python involves loading and normalizing the dataset, building a neural network model with TensorFlow's Keras API, and training the model on the training data. The model is then evaluated on the test data to measure accuracy and make predictions on new images. This code provides a comprehensive framework for tackling image classification tasks with deep learning.


import tensorflow as tf

import matplotlib.pyplot as plt


# Load the Fashion MNIST dataset

fashion_mnist = tf.keras.datasets.fashion_mnist


# Split into training and testing data

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()


# Normalize the pixel values to be between 0 and 1

x_train, x_test = x_train / 255.0, x_test / 255.0



# Define class names for the labels

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',

               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']


# Display the first 10 images and their labels

plt.figure(figsize=(10,10))

for i in range(10):

    plt.subplot(2, 5, i+1)

    plt.xticks([])

    plt.yticks([])

    plt.grid(False)

    plt.imshow(x_train[i], cmap='gray')

    plt.title(class_names[y_train[i]])

plt.show()


# Build the model

model = tf.keras.models.Sequential([

    tf.keras.layers.Flatten(input_shape=(28, 28)), # Flatten the input image

    tf.keras.layers.Dense(128, activation='relu'), # First hidden layer

    tf.keras.layers.Dense(64, activation='relu'), # Second hidden layer

    tf.keras.layers.Dense(10, activation='softmax') # Output layer with 10 neurons (one for each class)

])


# Compile the model

model.compile(optimizer='adam',

              loss='sparse_categorical_crossentropy',

              metrics=['accuracy'])


# Train the model

model.fit(x_train, y_train, epochs=10)


# Evaluate the model

test_loss, test_acc = model.evaluate(x_test, y_test)


print('\nTest accuracy:', test_acc)


# Make predictions

predictions = model.predict(x_test)


# Display the prediction for the first test image

print("Predicted label:", class_names[predictions[0].argmax()])

print("True label:", class_names[y_test[0]])


Conclusion

Implementing neural networks on the Fashion MNIST dataset with TensorFlow in Python is a powerful way to explore and understand deep learning. By following this guide, you’ve learned how to load and preprocess the data, build and train a neural network, and evaluate its performance. This practical experience is crucial for building more complex models and tackling real-world machine learning challenges. Keep experimenting with different architectures and hyperparameters to further enhance your understanding and skills in deep learning.

Related Posts

See All

Comments


Get in touch for customized mentorship and freelance solutions tailored to your needs.

bottom of page