How can TensorFlow be used to download and explore the Fashion MNIST dataset using Python?

TensorFlow is a machine learning framework provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.

The Fashion MNIST dataset contains grayscale images of clothing items from 10 different categories. It consists of 70,000 low-resolution images (28 x 28 pixels) making it perfect for machine learning experiments and benchmarking.

Installing TensorFlow

The 'tensorflow' package can be installed using the following command ?

pip install tensorflow

Loading and Exploring the Dataset

Let's download and explore the Fashion MNIST dataset using TensorFlow's built-in dataset loader ?

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

print("The tensorflow version used is")
print(tf.__version__)

print("The dataset is being loaded")
fashion_mnist = tf.keras.datasets.fashion_mnist

print("The dataset is being classified into training and testing data")
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

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

print("The dimensions of training data")
print(train_images.shape)

print("The number of rows in the training data")
print(len(train_labels))

print("The labels in training data")
print(train_labels)

print("The dimensions of test data")
print(test_images.shape)

print("The number of rows in the test data")
print(len(test_labels))

The output of the above code is ?

The tensorflow version used is
2.4.0
The dataset is being loaded
The dataset is being classified into training and testing data
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
The dimensions of training data
(60000, 28, 28)
The number of rows in the training data
60000
The labels in training data
[9 0 0 ... 3 0 5]
The dimensions of test data
(10000, 28, 28)
The number of rows in the test data
10000

Dataset Structure

The Fashion MNIST dataset is organized as follows ?

  • Training set: 60,000 images with their corresponding labels

  • Test set: 10,000 images with their corresponding labels

  • Image dimensions: 28 x 28 pixels (grayscale)

  • Classes: 10 different clothing categories (0-9)

Understanding the Data

Each image is a 28x28 NumPy array with pixel values ranging from 0 to 255. The labels are integers from 0 to 9, where each number corresponds to a class of clothing ?

  • 0: T-shirt/top

  • 1: Trouser

  • 2: Pullover

  • 3: Dress

  • 4: Coat

  • 5: Sandal

  • 6: Shirt

  • 7: Sneaker

  • 8: Bag

  • 9: Ankle boot

Conclusion

TensorFlow provides easy access to the Fashion MNIST dataset through tf.keras.datasets.fashion_mnist. This dataset is ideal for practicing image classification tasks with its 70,000 labeled images across 10 clothing categories.

Updated on: 2026-03-25T15:33:49+05:30

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements