#015 : Cat and Dog Images Classification

Key tools/skills:

  • Nural networks (Scikit learn, Tensorflow)
  • Tensorflow Serving (deploy the model)

Project : https://github.com/nairkivm/cat-dog-classifiers

In this project, I built a simple yet powerful image classification model that can do exactly that! I used TensorFlow and Keras with a convolutional neural network (CNN) and trained it using a dataset from Kaggle.

Dataset:

Dog and Cat Classification Dataset – Kaggle

This dataset contains 12,000+ images of cats and 12,000+ images of dogs.

Step-by-Step Workflow

1. Data Preparation

I started by downloading and extracting the dataset, which contained images of dogs and cats. I wrote a small script to:

  • Preview sample images
  • Organize them into folders (/Dog, /Cat)
  • Count them (around 12,000 images per category initially)
Preview of the datasets.
Preview of the datasets.

2. Data Augmentation

To improve generalization and prevent overfitting, I applied image augmentation using Keras’ ImageDataGenerator:

  • Rotation
  • Warp shifting
  • Zooming, etc.

This expanded the dataset to ~14,000 images per category, improving the diversity of training examples.

3. Data Splitting

I split the dataset using an 80:20 ratio:

  • 80% for training (with 20% of this used for validation)
  • 20% for testing

4. Model Building

I built the model using a Sequential CNN architecture. Here’s a brief overview of important layers:

  • Conv2D: Extracts spatial features from images.
  • MaxPooling2D: Reduces spatial dimensions, preserving key features.
  • BatchNormalization: Normalizes activations for faster, more stable training.
  • Dropout: Prevents overfitting by randomly dropping units during training.
  • Dense: Fully connected layer for final classification.

The model was compiled with an appropriate loss function (binary_crossentropy) and optimizer (Adam).

5. Training & Evaluation

I trained the model with callbacks like:

  • EarlyStopping – to halt training when no improvement
  • ReduceLROnPlateau – to lower learning rate when accuracy plateaus

Performance Results:

Training Accuracy: 95.55%

Testing Accuracy: 86.00%

Accuracy and lost evaluation over the training epoch.
Accuracy and lost evaluation over the training epoch.
Confusion matrix of the result.
Confusion matrix of the result.

Saving the Model

I saved the trained model in three formats for broader deployment possibilities:

  • .h5 (standard Keras format)
  • .tflite (TensorFlow Lite)
  • tfjs (TensorFlow.js)

Deployment with TensorFlow Serving

I tested the model using TensorFlow Serving in a Docker container. The deployment steps included:

  • Loading the model into a mounted container
  • Running the TensorFlow model server
  • Sending test images via POST request to the REST API

Test Results

I tried predicting two images:

  • A real photo of my cat 🐱
  • A meme image of Doge 🐶
The model is correctly predicting the two images.
The model is correctly predicting the two images.

The model successfully classified both images correctly, even though these were not part of training/testing data.

Summary

  • Feature Status
  • 14k+ images/class ✅
  • CNN with BatchNorm & Dropout ✅
  • Acc > 85% on test ✅
  • Saved in h5, tflite, tfjs ✅
  • Real-world inference ✅

Posted on 2025-04-16