Artificial Intelligence 16 min read

Fashion MNIST Image Classification Using TensorFlow 2.x in Python

This tutorial demonstrates how to load the Fashion MNIST dataset, explore and preprocess the images, build and compile a neural network with TensorFlow 2.x, train the model, evaluate its accuracy, and use the trained model to make predictions on clothing images, providing complete Python code examples throughout.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Fashion MNIST Image Classification Using TensorFlow 2.x in Python

Overview

This guide uses TensorFlow 2.x and Python to classify images of clothing items from the Fashion MNIST dataset.

Dataset

The Fashion MNIST dataset contains 60,000 training images and 10,000 test images, each 28×28 pixels, representing ten clothing categories such as T‑shirt/top, trouser, dress, and ankle boot.

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

Data Exploration

Each image is a 28×28 grayscale array with pixel values ranging from 0 to 255. The training set has 60,000 images and labels; the test set has 10,000.

<code>plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()</code>

Preprocessing

Pixel values are scaled to the 0‑1 range before feeding them to the network.

<code>train_images = train_images / 255.0
test_images = test_images / 255.0</code>

To verify preprocessing, the first 25 training images are displayed with their class names.

<code>plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()</code>

Model Construction

A simple sequential model is built with a flatten layer, a dense hidden layer of 128 ReLU units, and an output dense layer of 10 logits.

<code>model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])</code>

Compilation

The model is compiled with the Adam optimizer, sparse categorical cross‑entropy loss (from logits), and accuracy as the metric.

<code>model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])</code>

Training

The model is trained for 10 epochs on the training data.

<code>model.fit(train_images, train_labels, epochs=10)</code>

Training logs show loss and accuracy, reaching about 91% training accuracy.

Evaluation

The trained model is evaluated on the test set, yielding a slightly lower accuracy that indicates mild over‑fitting.

<code>test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nTest accuracy:', test_acc)</code>

Prediction

A softmax layer is added to convert logits to probabilities, and predictions are made on the test images.

<code>probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
print(predictions[0])
print(np.argmax(predictions[0]))</code>

Visualization functions display each image with the predicted label (blue for correct, red for incorrect) and a bar chart of class probabilities.

<code>def plot_image(i, predictions_array, true_label, img):
    ...

def plot_value_array(i, predictions_array, true_label):
    ...</code>

Single‑Image Inference

A single test image is expanded to a batch dimension and passed through the probability model to obtain confidence scores.

<code>img = test_images[0]
img = np.expand_dims(img, 0)
predictions_single = probability_model.predict(img)
print('Confidence per class:', predictions_single)
print('Predicted class:', np.argmax(predictions_single[0]))</code>

Full Source Code

The complete Python script, including imports, dataset loading, preprocessing, model definition, training, evaluation, and visualization, is provided below.

<code># TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

# Load Fashion MNIST
fashion_mnist = keras.datasets.fashion_mnist
(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']

# Preprocess
train_images = train_images / 255.0
test_images = test_images / 255.0

# Build model
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])

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

# Train
model.fit(train_images, train_labels, epochs=10)

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

# Predict
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
print(predictions[0])
print(np.argmax(predictions[0]))

# Visualization functions (plot_image, plot_value_array) omitted for brevity
</code>
image classificationPythondeep learningNeural NetworksTensorFlowFashion MNIST
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.