Skip to content

A modular Convolutional Neural Network (CNN) built with TensorFlow/Keras for binary image classification (cats vs. dogs). Includes a training notebook, sample dataset, performance plots, and a clean project structure for reproducibility and collaboration.

License

Notifications You must be signed in to change notification settings

ArianJr/cnn-image-classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

59 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CNN Image Classifier πŸ–ΌοΈ

PYTHON TENSORFLOW KERAS

A modular, end-to-end Convolutional Neural Network (CNN) for image classification built with TensorFlow/Keras.
Designed for researchers, students, and developers to train, evaluate, and deploy image classifiers efficiently.


πŸ“‘ Table of Contents


🎯 Project Overview

This repository provides a complete CNN workflow for image classification:

  • Load and preprocess images.
  • Build a customizable CNN architecture.
  • Train, validate, and evaluate the model.
  • Save and use the model for predictions on new images.
  • Visualize performance metrics and training curves.

It’s designed to be modular, readable, and easy to extend for custom datasets or advanced architectures.


πŸ“Έ Project Thumbnail

Project Thumbnail
Visual representation of the CNN Image Classifier workflow.


πŸ—οΈ Model Architecture

The CNN model is designed for binary image classification with a simple yet effective architecture. The model is implemented using TensorFlow/Keras.

Layer-by-Layer Description

  1. Input Layer

    • Accepts images with shape (height, width, channels) specified by input_shape.
  2. Convolutional Block 1

    • Conv2D with 32 filters, kernel size 3x3, activation ReLU.
    • MaxPooling2D with pool size 2x2 to reduce spatial dimensions.
  3. Convolutional Block 2

    • Conv2D with 32 filters, kernel size 3x3, activation ReLU.
    • MaxPooling2D with pool size 2x2.
  4. Flatten Layer

    • Converts the 2D feature maps into a 1D vector.
  5. Fully Connected (Dense) Layer

    • Dense layer with 128 neurons and ReLU activation.
  6. Output Layer

    • Dense layer with num_classes neurons and sigmoid activation (binary classification).

Model Compilation

  • Optimizer: Adam
  • Loss Function: Binary Crossentropy
  • Metrics: Accuracy

Summary Table

Layer Type Output Shape Parameters
Conv2D (32 filters) (62, 62, 32) 896
MaxPooling2D (2x2) (31, 31, 32) 0
Conv2D (32 filters) (29, 29,32) 9248
MaxPooling2D (2x2) (14, 14, 32) 0
Flatten 6,272 0
Dense (128 neurons) 128 802,944
Output Dense 1 129

πŸ“Š Accuracy & Loss Over Epochs

Visualizations generated during training provide insights into model performance:

Training Accuracy vs Epochs
Accuracy Plot

Training & Validation Loss vs Epochs
Loss Plot


⚑ Features

  • Modular CNN implementation (src/model.py)
  • Data preprocessing & augmentation utilities (src/data.py)
  • Notebook demonstrating full workflow (notebook/cnn_image_classifier_main.ipynb)
  • Model saving and loading for inference
  • Clear visualizations of metrics and predictions

πŸ“ Project Structure

Path Description
assets/ Thumbnail and performance plots
β”œβ”€β”€ cnn_image_classifier_thumbnail.png Project thumbnail image
β”œβ”€β”€ accuracy_over_epochs.png Accuracy plot over training
└── loss_over_epochs.png Loss plot over training
dataset/ Sample dataset (cats & dogs)
β”œβ”€β”€ training_set/ Training images
└── test_set/ Testing images
notebook/ Main Jupyter notebook
└── cnn_image_classifier_main.ipynb Contains training and evaluation
src/ Modular code components
└── __init__.py Marks src/ as a Python package
β”œβ”€β”€ model.py CNN architecture
└── data.py Data loading and preprocessing
saved_model/ (Optional) Saved trained model
└── cnn_model.h5 Saved model file
requirements.txt Python dependencies for reproducibility
.gitignore Git ignore rules
LICENSE Project license
README.md Project documentation

πŸ› οΈ Getting Started

Prerequisites

  • Python β‰₯ 3.7
  • TensorFlow 2.x
  • Numpy, Matplotlib, Pandas
  • (Optional) GPU for accelerated training

Installation

git clone https://github.com/ArianJr/cnn-image-classifier.git
cd cnn-image-classifier
python -m venv venv
source venv/bin/activate      # Windows: venv\Scripts\activate
pip install -r requirements.txt

πŸš€ Quick Start

Training the Model

# Example: run in the notebook or a script
from src.data import load_training_data, load_test_data
from src.model import build_cnn

training = load_training_data('dataset/training_set', (64, 64))
test = load_test_data('dataset/training_set', (64, 64))
model = build_cnn(input_shape=(64,64,3), num_classes=1)
history = model.fit(x=training, validation_data=test, epochs=25)

Using the Notebook

Launch the Jupyter notebook for an interactive demo:

jupyter notebook notebook/cnn_image_classifier_main.ipynb

πŸ“¦ Dataset Sample

This repository includes a sample subset of the Dogs vs. Cats dataset for demonstration purposes:

  • train/: 200 images (100 cats, 100 dogs)
  • test/: 50 images (25 cats, 25 dogs)

This subset is ideal for:

  • Testing the modular CNN pipeline
  • Validating config-driven training and evaluation
  • Keeping the repository lightweight

For full-scale training, download the complete dataset from Kaggle.


πŸ”§ Customization

  • Replace or augment dataset (dataset/ folder).
  • Modify CNN architecture (src/model.py).
  • Experiment with hyperparameters (learning rate, batch size, optimizer).
  • Implement transfer learning with pretrained networks.
  • Add evaluation metrics: confusion matrix, precision, recall, F1-score.

βš™οΈ Configuration

This project is designed to be easily customizable. You can modify key training parameters directly in the code files:

Parameter Location Default Value
input_shape model.py (64, 64, 3)
epochs cnn_image_classifier.ipynb 25
batch_size cnn_image_classifier.ipynb 32
train_dir data.py dataset/training_set
test_dir data.py dataset/test_set
model_path cnn_image_classifier.ipynb saved_model/cnn_model.h5

These values can be adjusted to suit your dataset size, image resolution, or training goals.


πŸ“ˆ Results & Performance

Metric Value
Training Accuracy 88.21%
Validation Accuracy 79.10%
Final Training Loss 0.2653
Final Validation Loss 0.5460

🀝 Contributing

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/YourFeature).
  3. Commit changes with clear messages.
  4. Open a pull request.

πŸ“œ License

MIT License – see the LICENSE file for details.


πŸ™ Acknowledgements

  • TensorFlow & Keras communities for excellent tools.
  • Open-source datasets used for training.
  • Tutorials and guides on CNNs that inspired this workflow.

πŸ‘€ Author

Arian Jr
πŸ“§ Contact Me β€’ 🌐 GitHub Profile


Made with ❀️ by ArianJr

⭐ If you found this project useful, please consider giving it a star! It helps others discover it and supports my work.


GitHub stars GitHub forks

About

A modular Convolutional Neural Network (CNN) built with TensorFlow/Keras for binary image classification (cats vs. dogs). Includes a training notebook, sample dataset, performance plots, and a clean project structure for reproducibility and collaboration.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published