YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

Dog๐Ÿถ and Cat๐Ÿฑ Classification

Kaggle

Github Link

๐Ÿง  Project Overview

This project performs binary image classification to distinguish between cats and dogs using a Convolutional Neural Network (CNN) built with TensorFlow and Keras. The model was trained on the Kaggle Cats and Dogs dataset in a Google Colab environment.

๐Ÿ“‘ Table of Contents

-๐Ÿ““ Model Development (Notebook) - Step 1: Dataset Download - Step 2: Data Preprocessing & Augmentation - ๐Ÿ“Š Class Distribution - Augmentation - Step 3: ๐Ÿ”น CNN Model Architecture - Step 4: Model Training - Step 5: Model Evaluation & Results - Results - Evaluation - Confusion Matrix and Classification Report


๐Ÿ““ Model Development (Notebook)

The initial development, including data exploration, preprocessing, and model training, was performed in CNN_Dogs_and_Cats.ipynb.

Step 1: Dataset Download

The dataset for cats and dogs was obtained from Kaggle. First, the Kaggle API key was uploaded to Colab (kaggle.json) to authenticate the account. Then, the dataset was downloaded using the Kaggle CLI:

!kaggle datasets download -d tongpython/cat-and-dog

Samples from the dataset:

Image

Step 2: Data Preprocessing & Augmentation

๐Ÿ“Š Class Distribution

Below are the class distributions for the training and test datasets used in this project:

Test Distribution Training Distribution

The visualizations show that both datasets are balanced between the two classes (cats ๐Ÿฑ and dogs ๐Ÿถ), ensuring fair model training and evaluation.

Augmentation

The dataset was loaded using ImageDataGenerator with the flow_from_directory() method.

  • Training images were augmented with rotation, horizontal and vertical flips, shear, zoom, and width/height shifts to generate diverse samples, while pixel values were normalized to the [0,1] range.
  • A 20% subset of the training data was reserved for validation using the validation_split parameter, ensuring that the training and validation sets do not share any images.
  • The training generator (train_generator) takes 80% of the data, while the validation generator (validation_generator) uses the remaining 20%. Test data was loaded separately from a different directory without augmentation to evaluate the model's performance.
  • This approach not only enhances model generalization by providing diverse training data but also ensures efficient memory usage in Google Colab.
Image

After preprocessing, the data distribution was as follows:

  • Training images: 6404
  • Validation images: 1601
  • Test images: 2023

The ImageDataGenerator was used to create diverse and augmented datasets for model training and validation, ensuring efficient memory usage in Google Colab.


Step 3: ๐Ÿ”น CNN Model Architecture

View model
Image

The CNN model consists of:

  1. Convolutional layers with ReLU activations to extract features from input images
  2. MaxPooling layers to reduce spatial dimensions and retain important features
  3. Batch normalization to improve training stability and convergence
  4. Flatten layer to convert feature maps into a one-dimensional vector
  5. Dense layers for classification, including a dropout layer (35%) to prevent overfitting
  6. Sigmoid output layer for binary classification (Cat vs Dog)

Step 4: Model Training

The CNN model was trained using the Keras Sequential API for binary classification of cats and dogs.

Training Configuration :

  • Loss Function: Binary Crossentropy
  • Optimizer: Adam
  • Metrics: Accuracy, Precision, and Recall
  • Epochs: 100
  • Validation Split: 20%
  • Callbacks:
    • ReduceLROnPlateau: Reduces learning rate when validation loss plateaus
    • EarlyStopping: Stops training early if validation loss does not improve, preventing overfitting.
    • ModelCheckpoint: Saved the best-performing model based on validation loss as cat-and-dog.keras.

Training was performed on Google Colabโ€™s GPU runtime, which significantly accelerated the process. The model achieved stable convergence with gradually decreasing validation loss, indicating effective learning and minimal overfitting.


Step 5: Model Evaluation & Results

Results

The key metrics monitored during training were Accuracy, Loss, Precision and Recall.

Training & Validation Metrics

Observations :

Accuracy: The model achieved approximately 88% validation accuracy, showing strong performance on unseen data.

Loss: Validation loss decreased steadily, indicating effective learning and minimal overfitting.

Precision & Recall: Both metrics gradually improved, reaching ~0.85โ€“0.90, which demonstrates a balanced ability to correctly identify both cats and dogs.

Evaluation

Model results for test data :

  • Total test image = 2023
  • batch_size = 64 => 31(step)*64 + 39 = 2023
  • steps = 32

evaluate => 1-31.(step) : 1984 => 32.(step) : 39

  • Test Accuracy: 0.87
  • Test Loss: 0.37
  • Test Precision: 0.85
  • Test Recall: 0.90
Confusion Matrix and Classification Report

Below is the confusion matrix and classification report for the test data, which shows the model's performance in predicting two classes.

Confusion Matrix Classification Report

๐Ÿ“Œ Not: The test set generator was configured with shuffle=False to ensure that predictions align correctly with the true labels for accurate evaluation.

=> After loading the saved (.keras) model, the prediction results for new images were examined using the predict_image function.

Image image image


Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support