Image Classification using ResNet
Last Updated :
13 Feb, 2025
This article will walk you through the steps to implement it for image classification using Python and TensorFlow/Keras.
Image classification classifies an image into one of several predefined categories. ResNet (Residual Networks), which introduced the concept of residual connections to address the vanishing gradient problem in very deep neural networks.
Here are the key reasons to use ResNet for image classification:
- Enables Deeper Networks: ResNet makes it possible to train networks with hundreds or even thousands of layers without performance degradation.
- Improved Performance: By using residual learning, ResNet achieves better accuracy in tasks like image classification.
- Better Generalization: The architecture helps avoid overfitting, improving model performance on unseen data.
Image Classification Using ResNet on CIFAR-10
Here’s a step-by-step guide to implement image classification using the CIFAR-10 dataset and ResNet50 in TensorFlow:
1. Import Libraries
We begin by importing the necessary libraries from TensorFlow and Keras:
Python
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
2. Load and Preprocess the CIFAR-10 Dataset
We load the CIFAR-10 dataset using tensorflow.keras.datasets.cifar10. Then, we normalize the pixel values of the images (by dividing by 255) to scale them to a range of 0 to 1. Lastly, we one-hot encode the labels to match the output format for categorical classification.
Python
# Load CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Preprocess the data
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# One-hot encode the labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
3. Load ResNet50 Pre-trained on ImageNet
We use ResNet50, pre-trained on the ImageNet dataset. The include_top=False parameter ensures that the fully connected layers (the classification head) are not included, so we can add our custom layers.
Python
base_model = ResNet50(weights='imagenet',
include_top=False,
input_shape=(32, 32, 3))
# Freeze the base model
base_model.trainable = False
4. Build the Classification Model
We now build the model using the pre-trained ResNet50 as a base. We add a GlobalAveragePooling2D layer to reduce the dimensions of the feature maps from the ResNet base model, followed by a Dense layer for classification.
The final layer has 10 neurons, one for each class in the CIFAR-10 dataset, with a softmax activation function.
Python
# Build the classification model
model = Sequential([
base_model,
GlobalAveragePooling2D(),
Dense(1024, activation='relu'),
Dense(10, activation='softmax')
])
5. Compile the Model
We use the Adam optimizer with a small learning rate to prevent overfitting and use categorical cross-entropy as the loss function for multi-class classification. We also track the accuracy metric during training.
Python
# Compile the model
model.compile(optimizer=Adam(learning_rate=0.0001),
loss='categorical_crossentropy',
metrics=['accuracy'])
6. Train the Model
We then train the model on the CIFAR-10 training data, using a batch size of 64 and 10 epochs. We also pass the test data for validation during training to monitor the model’s performance.
Python
# Train the model
model.fit(x_train, y_train,
batch_size=64,
epochs=10,
validation_data=(x_test, y_test))
7. Evaluate the Model
Once the model is trained, we evaluate it on the test data to check its accuracy.
Python
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc}")
Output:
Test accuracy: 0.8741999864578247
ResNet's residual connections enable us to train very deep models, and its pre-trained weights, when fine-tuned for specific tasks, can provide remarkable accuracy even with smaller datasets. By freezing the early layers of the model, we can focus on learning the final decision-making layers, which is ideal for many real-world applications in image classification.
Similar Reads
What is Image Classification?
In today's digital era, where visual data is abundantly generated and consumed, image classification emerges as a cornerstone of computer vision. It enables machines to interpret and categorize visual information, a task that is pivotal for numerous applications, from enhancing medical diagnostics t
10 min read
Top Pre-Trained Models for Image Classification
Pre-trained models are neural networks trained on large datasets before being fine-tuned for specific tasks. These models capture intricate patterns and features, making them highly effective for image classification. By leveraging pre-trained models, developers can save time and computational resou
5 min read
Multiclass image classification using Transfer learning
Image classification is one of the supervised machine learning problems which aims to categorize the images of a dataset into their respective categories or labels. Classification of images of various dog breeds is a classic image classification problem. So, we have to classify more than one class t
8 min read
Image Classification using Google's Teachable Machine
Machine learning is a scientific field that allows computers to learn without being programmed directly. When many learners, students, engineers, and data scientists use machine learning to create diverse projects and goods, the application of machine learning is trendy. However, the development of
2 min read
Dataset for Image Classification
The field of computer vision has witnessed remarkable progress in recent years, largely driven by the availability of large-scale datasets for image classification tasks. These datasets play a pivotal role in training and evaluating machine learning models, enabling them to recognize and categorize
12 min read
Basic Image Classification with keras in R
Image classification is a computer vision task where the goal is to assign a label to an image based on its content. This process involves categorizing an image into one of several predefined classes. For example, an image classification model might be used to identify whether a given image contains
10 min read
CIFAR-10 Image Classification in TensorFlow
Prerequisites:Image ClassificationConvolution Neural Networks including basic pooling, convolution layers with normalization in neural networks, and dropout.Data Augmentation.Neural Networks.Numpy arrays.In this article, we are going to discuss how to classify images using TensorFlow. Image Classifi
8 min read
Classify Images on Azure Custom Vision
Azure Custom Vision is a computer vision service offered by Microsoft Azure that allows users to create customized computer vision models for image classification and object detection. It is a no-code interface that simplifies the process of making computer vision models without code-based training.
3 min read
Image classification using CIFAR-10 and CIFAR-100 Dataset in TensorFlow
CIFAR10 and CIFAR100 are some of the famous benchmark datasets which are used to train CNN for the computer vision task. In this article we are supposed to perform image classification on both of these datasets CIFAR10 as well as CIFAR100 so, we will be using Transfer learning here. But how? First
4 min read
Displaying a Single Image in PyTorch
Displaying images is a fundamental task in data visualization, especially when working with machine learning frameworks like PyTorch. This article will guide you through the process of displaying a single image using PyTorch, covering various methods and best practices.Table of ContentUnderstanding
3 min read