CIFAR-10 Image Classification in TensorFlow

Image classification is an essential task in computer vision that involves recognizing and categorizing images based on their content. CIFAR-10 is a well-known dataset that contains 60,000 32×32 color images in 10 classes, with 6,000 images per class.

TensorFlow is a powerful framework that provides a variety of tools and APIs for building and training machine learning models. It provides a high-level API called Keras, which makes it easy to build and train deep neural networks for image classification tasks.

In this tutorial, we will explore how to perform image classification on CIFAR-10 using TensorFlow's Keras API ?

Loading the Data

The first step in any machine learning project is to prepare the data. In this case, we will use the CIFAR-10 dataset, which can be easily downloaded using TensorFlow's built-in datasets module.

Let's start by importing the necessary modules ?

import tensorflow as tf
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
import numpy as np

Next, we can load the CIFAR-10 dataset using the load_data() function from the cifar10 module ?

# Load the CIFAR-10 dataset
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()

# Define the class names for visualization purposes
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']

print(f"Training images shape: {train_images.shape}")
print(f"Training labels shape: {train_labels.shape}")
print(f"Test images shape: {test_images.shape}")
print(f"Test labels shape: {test_labels.shape}")

This code loads the training and test images and their respective labels into four NumPy arrays. The train_images and test_images arrays contain the images themselves, while the train_labels and test_labels arrays contain the corresponding labels (integers from 0 to 9 representing the 10 classes).

Preprocessing the Data

Before we can train a model on the CIFAR-10 dataset, we need to preprocess the data. There are two main preprocessing steps we need to take ?

Normalize the Pixel Values

The pixel values in the images range from 0 to 255. We can improve the training performance of our model by scaling these values down to the range 0 to 1 ?

# Normalize the pixel values to be between 0 and 1
train_images = train_images.astype('float32') / 255.0
test_images = test_images.astype('float32') / 255.0

One-hot Encode the Labels

The labels in the CIFAR-10 dataset are integers from 0 to 9. However, in order to train a model to classify the images, we need to convert these integers into one-hot encoded vectors. TensorFlow provides a convenient function for doing this ?

# Convert labels to one-hot encoded format
train_labels = tf.keras.utils.to_categorical(train_labels, 10)
test_labels = tf.keras.utils.to_categorical(test_labels, 10)

Building the Model

Now that we have preprocessed the data, we can start building our model. We will use a convolutional neural network (CNN), which is a type of neural network that is particularly well-suited for image classification tasks.

Here's the architecture we will use for our CIFAR-10 model ?

  • Convolutional Layers We will start with three convolutional layers with increasing filter sizes, each followed by max pooling layers. These layers learn features from the input images.

  • Flatten Layer We will then flatten the output of the convolutional layers into a 1D vector for the fully connected layers.

  • Dense Layers We will use two dense layers with ReLU activation to learn complex patterns from the extracted features.

  • Output Layer Finally, we will add an output layer with 10 neurons (one for each class) and a softmax activation function.

Here's the code to build this model ?

# Define the CNN model
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# Display the model architecture
model.summary()

Compiling and Training the Model

Now that we have defined our model, we need to compile it and train it on the CIFAR-10 dataset. We will use the compile() method to specify the loss function, optimizer, and metrics to use during training ?

# Compile the model
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

We use the adam optimizer, which is a popular stochastic gradient descent variant that adapts the learning rate during training. We also use the categorical_crossentropy loss function for multi-class classification problems.

To train the model, we call the fit method and pass in the training data and labels ?

# Train the model
history = model.fit(train_images, train_labels, 
                   epochs=10, 
                   batch_size=32,
                   validation_data=(test_images, test_labels),
                   verbose=1)

In the above code, we train our model for 10 epochs using the training data and validate it on the test data. The fit() method returns a History object that contains information about the training process.

Evaluating the Model

After training the model, we can evaluate its performance on the test set using the evaluate method ?

# Evaluate the model on the test set
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=0)
print(f'Test accuracy: {test_acc:.4f}')
print(f'Test loss: {test_loss:.4f}')

We can also visualize the training and validation accuracy over time using Matplotlib ?

# Plot the training and validation accuracy over time
plt.figure(figsize=(12, 4))

plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title('Model Accuracy')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Model Loss')
plt.legend()

plt.tight_layout()
plt.show()

Making Predictions

After training and evaluating the model, we can use it to make predictions on new images. Let's create a function to predict and visualize results from the test set ?

def predict_image(model, image, true_label, class_names):
    """Make a prediction on a single image and display results"""
    # Expand dimensions to create a batch of size 1
    image_batch = np.expand_dims(image, axis=0)
    
    # Make prediction
    predictions = model.predict(image_batch, verbose=0)
    predicted_class_index = np.argmax(predictions[0])
    predicted_class_name = class_names[predicted_class_index]
    confidence = predictions[0][predicted_class_index]
    
    # Get true class name
    true_class_name = class_names[true_label]
    
    # Display image with prediction
    plt.figure(figsize=(6, 4))
    plt.imshow(image)
    plt.axis('off')
    plt.title(f'True: {true_class_name}\nPredicted: {predicted_class_name}\nConfidence: {confidence:.2f}')
    plt.show()
    
    return predicted_class_name, confidence

# Test on a few images from the test set
for i in range(3):
    # Convert one-hot label back to integer
    true_label = np.argmax(test_labels[i])
    predict_image(model, test_images[i], true_label, class_names)

Model Performance Summary

Metric Training Validation
Accuracy ~75% ~67%
Loss ~0.7 ~1.0

Key Points

  • Data Preprocessing Normalizing pixel values and one-hot encoding labels are crucial preprocessing steps.

  • CNN Architecture Convolutional layers extract spatial features while dense layers learn complex patterns for classification.

  • Model Performance The model achieves reasonable accuracy on CIFAR-10, though there's room for improvement with techniques like data augmentation and deeper networks.

Conclusion

In this tutorial, we successfully implemented CIFAR-10 image classification using TensorFlow and Keras. We built a CNN that achieved around 67% test accuracy, demonstrating the effectiveness of convolutional neural networks for image classification tasks. The model can be further improved with data augmentation, regularization techniques, and more sophisticated architectures.

Updated on: 2026-03-27T16:38:18+05:30

843 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements