In this project, we will explore how to perform image classification on the CIFAR-10 dataset using TensorFlow. We'll build a convolutional neural network (CNN) model to classify images into 10 different classes. We'll explain each step of the code and provide the final working code at the end.
Loading and Preparing the Data
To begin, we need to import the necessary libraries:
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow import keras
from tensorflow.keras import layers
We import tensorflow
and keras
for deep learning operations, and we specifically import cifar10
dataset from keras.datasets
for the CIFAR-10 dataset.
Next, we load and preprocess the CIFAR-10 dataset:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
y_train = keras.utils.to_categorical(y_train, num_classes=10)
y_test = keras.utils.to_categorical(y_test, num_classes=10)
We load the CIFAR-10 dataset into training and testing sets. We normalize the pixel values to the range [0, 1] by dividing by 255.0. We also convert the class labels to one-hot encoded vectors using to_categorical()
.
Building the CNN Model
We define our CNN model using the Sequential API:
model = keras.Sequential(
[
keras.Input(shape=(32, 32, 3)),
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dense(128, activation="relu"),
layers.Dense(10, activation="softmax"),
]
)
We create a sequential model with multiple layers. The model starts with an input layer of shape (32, 32, 3) for the image dimensions. We then add two sets of convolutional and max pooling layers, followed by a flattening layer to convert the 2D feature maps into a 1D vector. We add two dense layers, one with 128 units and ReLU activation, and another with 10 units and softmax activation for the 10 output classes.
Compiling and Training the Model
We compile the model by specifying the loss function, optimizer, and evaluation metrics:
model.compile(
loss="categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"],
)
We use categorical cross-entropy as the loss function since we have multi-class classification. We choose the Adam optimizer and specify accuracy as the evaluation metric.
Next, we train the model using the training data:
model.fit(x_train, y_train, batch_size=64, epochs=10, validation_split=0.1)
We use fit()
to train the model on the training data. We specify the batch size, number of epochs, and a validation split of 0.1 (10% of the training data) for validation during training.
Evaluating the Model
We evaluate the model on the testing data:
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
We use evaluate()
to calculate the loss and accuracy of the model on the testing data. The results are printed to the console.
Making Predictions
We make predictions on the testing data:
predictions = model.predict(x_test)
We use predict()
to obtain predictions from the model for the testing data.
Final Code
Here's the complete Python code for performing CIFAR-10 image classification using TensorFlow:
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow import keras
from tensorflow.keras import layers
# Load the CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Normalize pixel values to the range [0, 1]
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
# Convert class labels to one-hot encoded vectors
y_train = keras.utils.to_categorical(y_train, num_classes=10)
y_test = keras.utils.to_categorical(y_test, num_classes=10)
model = keras.Sequential(
[
keras.Input(shape=(32, 32, 3)),
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dense(128, activation="relu"),
layers.Dense(10, activation="softmax"),
]
)
model.compile(
loss="categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"],
)
model.fit(x_train, y_train, batch_size=64, epochs=10, validation_split=0.1)
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
predictions = model.predict(x_test)
That's it! You now have a Python code snippet that allows you to perform image classification on the CIFAR-10 dataset using TensorFlow. Feel free to modify the code or use it as a starting point for your own projects. Enjoy classifying the CIFAR-10 images!