Cat vs Dog Image Classification Using MobileNetV2 and Transfer Learning.

Introduction

Image classification is a common application of deep learning in computer vision. In this project, we build a model that can identify whether an image contains a cat or a dog.

The project uses TensorFlow and Keras along with MobileNetV2, a pretrained deep learning model. The dataset is cleaned and divided into training, validation, and test sets, followed by image augmentation and model training. The model is then fine-tuned to improve its performance on the Cat vs Dog classification task.

The final model achieves 97.74% accuracy on the test dataset and can also be used to classify new images uploaded by the user.

In this article, we will understand the complete workflow of the project, from dataset preparation to training, evaluation, and prediction.

What is Cat vs Dog Image Classification?

Cat vs Dog Image Classification is a binary image classification problem where a deep learning model learns to distinguish between two classes: Cat and Dog.

The model is trained using labeled images and learns visual patterns from them. Once trained, it can take a new image as input and predict which class it belongs to.

In this project, the model uses MobileNetV2 with transfer learning to perform the classification. The final output is a probability between 0 and 1, which is used to decide whether the image is a Cat or Dog.

Why CNNs / Transfer Learning for Image Classification? 

Images contain important visual patterns such as edges, shapes, textures, and colors. CNN-based models are designed to learn these patterns and use them to identify objects in images.

Training a deep CNN from scratch can require a large amount of data and computational resources. Transfer learning provides a more efficient approach by starting with a model that has already learned useful visual features from a large dataset.

In this project, MobileNetV2 is used as the pretrained feature extractor. Its learned features are reused for the Cat vs Dog classification task, and the higher-level layers are later fine-tuned to adapt the model to the new dataset.

This combination allows us to build an accurate image classifier without training the entire deep network from the beginning.

Dataset Used

For this project, we use the Microsoft Kaggle Cats and Dogs dataset, which contains images belonging to two classes: Cat and Dog. The dataset is downloaded and organized into separate Cat and Dog folders.

Initially, the dataset contains 12,501 files for each class. Before training, the images are checked for validity, and 1,590 corrupted or invalid images are removed. This leaves 23,410 valid images for the project.

Dataset Summary

PropertyDetails
DatasetMicrosoft Kaggle Cats and Dogs
ClassesCat, Dog
Original files per class12,501
Invalid files removed1,590
Valid images used23,410
Visualization: A dataset sample grid showing a few Cat and Dog images would work well here. The notebook already contains a 3×3 visualization of training images, so we can use that section as the basis for the article

Dataset Cleaning

Before training the model, the dataset needs to be checked for corrupted or invalid image files. Such files can cause errors during data loading or negatively affect the training process.

In this project, each image in the Cat and Dog folders is checked to verify that it is a valid JPEG file. Invalid files are removed before the dataset is used for training. A total of 1,590 corrupted or invalid images are deleted.

After cleaning, 23,410 valid images remain for the classification task.