Instructions to use havvanur92/dog-cat-cnn with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use havvanur92/dog-cat-cnn with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://havvanur92/dog-cat-cnn") - Notebooks
- Google Colab
- Kaggle
YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
Dog๐ถ and Cat๐ฑ Classification
๐ง 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:
Step 2: Data Preprocessing & Augmentation
๐ Class Distribution
Below are the class distributions for the training and test datasets used in this project:
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_splitparameter, 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.
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
The CNN model consists of:
- Convolutional layers with ReLU activations to extract features from input images
- MaxPooling layers to reduce spatial dimensions and retain important features
- Batch normalization to improve training stability and convergence
- Flatten layer to convert feature maps into a one-dimensional vector
- Dense layers for classification, including a dropout layer (35%) to prevent overfitting
- 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.
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.
๐ 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.
- Downloads last month
- -