Dog Breed Classification using Transfer Learning
Last Updated :
23 Jul, 2025
In this tutorial, we will demonstrate how to build a dog breed classifier using transfer learning. This method allows us to use a pre-trained deep learning model and fine-tune it to classify images of different dog breeds.
Why to use Transfer Learning for Dog Breed Classification
Transfer learning is a machine learning technique where a pre-trained model, which has been trained on a large dataset, is adapted to a new task.
For example, models trained on ImageNet can be reused for other tasks like dog breed classification. The key advantage of transfer learning is that it allows us to utilize the feature-extraction capabilities of a model that has already learned useful representations from millions of images. By reusing the convolutional layers of these models, we can achieve high accuracy with less training data and reduced computational effort.
Benefits of Transfer Learning in Dog Breed Classification
- Faster Training: Pre-trained models have already learned to identify key image features, saving time on training.
- Improved Accuracy: Using a pre-trained model on a similar task helps in achieving better performance than training from scratch.
- Less Data Requirement: Transfer learning requires fewer data for fine-tuning, making it ideal for tasks like dog breed classification where labeled data might be limited.
Implementing Dog Breed Classification using Transfer Learning
Step 1: Import Necessary Libraries
To implement this project, we will use the following Python libraries, each suited for specific tasks such as data handling, model development, and image processing:
- Pandas: For data manipulation and preprocessing.
- Numpy: For numerical computations and array manipulations.
- Matplotlib and Seaborn: For visualizing the dataset and model performance.
- Scikit-learn (Sklearn): For data preprocessing, splitting datasets, and model evaluation.
- OpenCV: For image processing tasks such as resizing, cropping, and converting images.
- TensorFlow and Keras: For building and training the deep learning model using transfer learning.
Python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
import cv2
import tensorflow as tf
from tensorflow import keras
from keras import layers
from functools import partial
import warnings
warnings.filterwarnings('ignore')
AUTO = tf.data.experimental.AUTOTUNE
Step 2: Loading Dataset for Dog Breed Classification
The dataset contains 10,000 images of 120 different dog breeds. The dataset includes:
- Training images: Contains labeled images of dog breeds.
- Test images: Unlabeled images used for testing the model.
- CSV file: Contains metadata about the images and their corresponding dog breed labels.
You can access the dataset here: Dog Breed Identification Dataset
To start using the dataset, we will unzip the file to extract the contents.
Python
from zipfile import ZipFile
data_path = 'dog-breed-identification.zip'
with ZipFile(data_path, 'r') as zip:
zip.extractall()
print('The data set has been extracted.')
Output:
The data set has been extracted.
Step 3: Exploratory Data Analysis
Now that we have the dataset, let's perform some basic Exploratory Data Analysis (EDA).
Python
df = pd.read_csv('labels.csv')
df.head()
Output:
First Five rows of the dataset
Python
Output:
(10222, 2)
Let's check the number of unique breeds of dog images we have in the training data.
Python
Output:
120
So, here we can see that there are 120 unique breed data which has been provided to us.
Python
plt.figure(figsize=(10, 5))
df['breed'].value_counts().plot.bar()
plt.axis('off')
plt.show()
Output:
The number of images present in each classHere we can observe that there is a data imbalance between the classes of different breeds of dogs.
Python
df['filepath'] = 'train/' + df['id'] + '.jpg'
df.head()
Output:
First Five rows of the datasetAlthough visualizing one image from each class is not feasible but let's view some of them.
Python
plt.subplots(figsize=(10, 10))
for i in range(12):
plt.subplot(4, 3, i+1)
# Selecting a random image
# index from the dataframe.
k = np.random.randint(0, len(df))
img = cv2.imread(df.loc[k, 'filepath'])
plt.imshow(img)
plt.title(df.loc[k, 'breed'])
plt.axis('off')
plt.show()
Output:
Sample images from the training dataThe images are not of the same size which is natural as real-world images tend to be of different sizes and shapes. We will take care of this while loading and processing the images.
Python
le = LabelEncoder()
df['breed'] = le.fit_transform(df['breed'])
df.head()
Output:
First Five rows of the datasetStep 4: Data Preparation
When working with large datasets in deep learning, memory limitations often prevent loading the entire dataset at once. To efficiently handle data loading and augmentation, tools like TensorFlow’s tf.data.Dataset and Albumentations are used to create optimized input pipelines and apply real-time image augmentations.
First, the dataset is split into training and validation sets, enabling model training on one subset and evaluation on another.
Python
features = df['filepath']
target = df['breed']
X_train, X_val, Y_train, Y_val = train_test_split(features, target,
test_size=0.15,
random_state=10)
X_train.shape, X_val.shape
Output:
((8688,), (1534,))
Step 5: Applying Image Augmentation
Below are some of the augmentations which we would like to have in our training data.
Python
import albumentations as A
transforms_train = A.Compose([
A.VerticalFlip(p=0.2),
A.HorizontalFlip(p=0.7),
A.CoarseDropout(p=0.5),
A.RandomGamma(p=0.5),
A.RandomBrightnessContrast(p=1)
])
Let's view an example of albumentation by applying it to some sample images.
Python
img = cv2.imread('train/00792e341f3c6eb33663e415d0715370.jpg')
plt.imshow(img)
plt.show()
Output:
Sample image of a dogNext, we apply several augmentations, such as VerticalFlip, HorizontalFlip, CoarseDropout, and CLAHE, and visualize the results:
Python
augments = [A.VerticalFlip(p=1), A.HorizontalFlip(p=1),
A.CoarseDropout(p=1), A.CLAHE(p=1)]
plt.subplots(figsize=(10, 10))
for i, aug in enumerate(augments):
plt.subplot(2, 2, i+1)
aug_img = aug(image=img)['image']
plt.imshow(aug_img)
plt.show()
Output:
Different data augmentations applied to themDifferent augmentations applied to the sample image, showing how the data transformation looks visually.
Step 6: Building the Input Pipeline
Now, let's define utility functions to handle image loading, augmentation, and normalization. We will create functions to read images from disk, resize them, normalize the pixel values, and apply augmentations.
Below we have implemented some utility functions which will be used while building the input pipeline.
- decode_image: This function will read the image from the path and resize them to be of the same size along with it will normalize as well. Finally, we will convert the labels into one_hot vectors as well.
- process_data: This is the function that will be used to introduce image augmentation to the image.
Python
def aug_fn(img):
aug_data = transforms_train(image=img)
aug_img = aug_data['image']
return aug_img
@tf.function
def process_data(img, label):
aug_img = tf.numpy_function(aug_fn,
[img],
Tout=tf.float32)
return img, label
def decode_image(filepath, label=None):
img = tf.io.read_file(filepath)
img = tf.image.decode_jpeg(img)
img = tf.image.resize(img, [128, 128])
img = tf.cast(img, tf.float32) / 255.0
if label == None:
return img
return img, tf.one_hot(indices=label,
depth=120,
dtype=tf.float32)
Now by using the above function we will be implementing our training data input pipeline and the validation data pipeline.
Python
train_ds = (
tf.data.Dataset
.from_tensor_slices((X_train, Y_train))
.map(decode_image, num_parallel_calls=AUTO)
.map(partial(process_data), num_parallel_calls=AUTO)
.batch(32)
.prefetch(AUTO)
)
val_ds = (
tf.data.Dataset
.from_tensor_slices((X_val, Y_val))
.map(decode_image, num_parallel_calls=AUTO)
.batch(32)
.prefetch(AUTO)
)
We must observe here that we do not apply image data augmentation on validation or testing data.
Python
for img, label in train_ds.take(1):
print(img.shape, label.shape)
Output:
(32, 128, 128, 3) (32, 120)
From here we can confirm that the images have been converted into (128, 128) shapes and batches of 64 images have been formed.
Step 7: Model Building Using Transfer Learning
1. Load Pre-trained InceptionV3 Model
We first load the InceptionV3 model from TensorFlow's Keras API with the weights pre-trained on ImageNet. The include_top=False argument excludes the fully connected layers at the top of the network, allowing us to customize the final layers for our task.
Python
from tensorflow.keras.applications.inception_v3 import InceptionV3
pre_trained_model = InceptionV3(
input_shape=(128, 128, 3),
weights='imagenet',
include_top=False
)
Output:
87916544/87910968 [==============================] - 1s 0us/step
87924736/87910968 [==============================] - 1s 0us/step
The model is successfully loaded with the weights from ImageNet, and we can now access the feature extraction layers.
2. Inspect the Model's Depth
InceptionV3 is a deep network with many layers, which makes it effective in learning complex features from images. Let's check the number of layers in this pre-trained model.
Python
len(pre_trained_model.layers)
Output:
311
This deep architecture, consisting of 311 layers, makes it highly efficient at extracting detailed features from images.
3. Freeze Pre-Trained Layers
Since the convolutional layers of the InceptionV3 model have already been trained on millions of images, we freeze these layers so that their weights are not updated during our fine-tuning process.
Python
for layer in pre_trained_model.layers:
layer.trainable = False
last_layer = pre_trained_model.get_layer('mixed7')
# Access the output shape from the output tensor of the layer
print('last layer output shape: ', last_layer.output.shape)
last_output = last_layer.output
Output:
last layer output shape: (None, 6, 6, 768)
This tells us that the last convolutional layer outputs a 6x6 grid of feature maps with 768 channels.
5. Define the Custom Model Architecture
Using the Keras Functional API, we can build a custom classification head on top of the pre-trained model. This includes flattening the output, adding fully connected layers, BatchNormalization for stable training, Dropout for regularization, and finally, an output layer with softmax activation for multi-class classification.
Python
# Model Architecture
x = layers.Flatten()(last_output)
x = layers.Dense(256, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Dense(256, activation='relu')(x)
x = layers.Dropout(0.3)(x)
x = layers.BatchNormalization()(x)
output = layers.Dense(120, activation='softmax')(x)
model = keras.Model(pre_trained_model.input, output)
# Model Compilation
model.compile(
optimizer='adam',
loss=keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.AUC()]
)
6. Implement Callbacks
Callbacks are used to monitor the model's performance during training. We use the following callbacks:
- EarlyStopping: Stops training if validation AUC doesn't improve for 3 consecutive epochs, preventing overfitting.
- ReduceLROnPlateau: Reduces the learning rate when the validation loss plateaus, helping the model converge better.
- Custom Callback: Stops training if the validation AUC exceeds 0.99.
Python
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if logs.get('val_auc') is not None and logs.get('val_auc') > 0.99:
print('\n Validation accuracy has reached upto 90% so, stopping further training.')
self.model.stop_training = True
es = EarlyStopping(patience=3,
monitor='val_auc',
restore_best_weights=True,
mode='max')
lr = ReduceLROnPlateau(monitor='val_loss',
patience=2,
factor=0.5,
verbose=1)
Step 8: Train the Model
We train the model using the fit() method with training and validation datasets, a maximum of 50 epochs, and the callbacks defined above.
Python
history = model.fit(train_ds,
validation_data=val_ds,
epochs=50,
verbose=1,
callbacks=[es, lr, myCallback()])
Output:
Training and validation loss and AUC scoreThe output will display the training and validation loss, as well as the AUC score after each epoch. If the validation AUC exceeds 0.99, the training will stop early.
Step 9: Evaluate the Model
Once the model is trained, we evaluate its performance on the test dataset. We visualize the training history to observe the model's learning curve and make sure it has converged effectively.
Python
history_df = pd.DataFrame(history.history)
history_df.loc[:, ['loss', 'val_loss']].plot()
history_df.loc[:, ['auc', 'val_auc']].plot()
plt.show()
Output:
Graph of loss and accuracy epoch by epoch for training and validation data lossThe training and validation AUC curves are plotted, showing how the model's performance evolved over time. The test loss and test AUC are displayed, providing insight into how well the model generalizes to unseen data.
Source Code: Dog Breed Classification
Similar Reads
Deep Learning Tutorial Deep Learning is a subset of Artificial Intelligence (AI) that helps machines to learn from large datasets using multi-layered neural networks. It automatically finds patterns and makes predictions and eliminates the need for manual feature extraction. Deep Learning tutorial covers the basics to adv
5 min read
Deep Learning Basics
Introduction to Deep LearningDeep Learning is transforming the way machines understand, learn and interact with complex data. Deep learning mimics neural networks of the human brain, it enables computers to autonomously uncover patterns and make informed decisions from vast amounts of unstructured data. How Deep Learning Works?
7 min read
Artificial intelligence vs Machine Learning vs Deep LearningNowadays many misconceptions are there related to the words machine learning, deep learning, and artificial intelligence (AI), most people think all these things are the same whenever they hear the word AI, they directly relate that word to machine learning or vice versa, well yes, these things are
4 min read
Deep Learning Examples: Practical Applications in Real LifeDeep learning is a branch of artificial intelligence (AI) that uses algorithms inspired by how the human brain works. It helps computers learn from large amounts of data and make smart decisions. Deep learning is behind many technologies we use every day like voice assistants and medical tools.This
3 min read
Challenges in Deep LearningDeep learning, a branch of artificial intelligence, uses neural networks to analyze and learn from large datasets. It powers advancements in image recognition, natural language processing, and autonomous systems. Despite its impressive capabilities, deep learning is not without its challenges. It in
7 min read
Why Deep Learning is ImportantDeep learning has emerged as one of the most transformative technologies of our time, revolutionizing numerous fields from computer vision to natural language processing. Its significance extends far beyond just improving predictive accuracy; it has reshaped entire industries and opened up new possi
5 min read
Neural Networks Basics
What is a Neural Network?Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental
12 min read
Types of Neural NetworksNeural networks are computational models that mimic the way biological neural networks in the human brain process information. They consist of layers of neurons that transform the input data into meaningful outputs through a series of mathematical operations. In this article, we are going to explore
7 min read
Layers in Artificial Neural Networks (ANN)In Artificial Neural Networks (ANNs), data flows from the input layer to the output layer through one or more hidden layers. Each layer consists of neurons that receive input, process it, and pass the output to the next layer. The layers work together to extract features, transform data, and make pr
4 min read
Activation functions in Neural NetworksWhile building a neural network, one key decision is selecting the Activation Function for both the hidden layer and the output layer. It is a mathematical function applied to the output of a neuron. It introduces non-linearity into the model, allowing the network to learn and represent complex patt
8 min read
Feedforward Neural NetworkFeedforward Neural Network (FNN) is a type of artificial neural network in which information flows in a single direction i.e from the input layer through hidden layers to the output layer without loops or feedback. It is mainly used for pattern recognition tasks like image and speech classification.
6 min read
Backpropagation in Neural NetworkBack Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Deep Learning Models
Deep Learning Frameworks
TensorFlow TutorialTensorFlow is an open-source machine-learning framework developed by Google. It is written in Python, making it accessible and easy to understand. It is designed to build and train machine learning (ML) and deep learning models. It is highly scalable for both research and production.It supports CPUs
2 min read
Keras TutorialKeras high-level neural networks APIs that provide easy and efficient design and training of deep learning models. It is built on top of powerful frameworks like TensorFlow, making it both highly flexible and accessible. Keras has a simple and user-friendly interface, making it ideal for both beginn
3 min read
PyTorch TutorialPyTorch is an open-source deep learning framework designed to simplify the process of building neural networks and machine learning models. With its dynamic computation graph, PyTorch allows developers to modify the networkâs behavior in real-time, making it an excellent choice for both beginners an
7 min read
Caffe : Deep Learning FrameworkCaffe (Convolutional Architecture for Fast Feature Embedding) is an open-source deep learning framework developed by the Berkeley Vision and Learning Center (BVLC) to assist developers in creating, training, testing, and deploying deep neural networks. It provides a valuable medium for enhancing com
8 min read
Apache MXNet: The Scalable and Flexible Deep Learning FrameworkIn the ever-evolving landscape of artificial intelligence and deep learning, selecting the right framework for building and deploying models is crucial for performance, scalability, and ease of development. Apache MXNet, an open-source deep learning framework, stands out by offering flexibility, sca
6 min read
Theano in PythonTheano is a Python library that allows us to evaluate mathematical operations including multi-dimensional arrays efficiently. It is mostly used in building Deep Learning Projects. Theano works way faster on the Graphics Processing Unit (GPU) rather than on the CPU. This article will help you to unde
4 min read
Model Evaluation
Deep Learning Projects