A deep learning image classification project that uses a Convolutional Neural Network (CNN) to distinguish between cats and dogs. Built with TensorFlow and Keras, this model demonstrates fundamental computer vision techniques for binary image classification.
This project implements a CNN-based image classifier trained on a dataset of cat and dog images. The model achieves high accuracy by employing modern deep learning techniques including batch normalization, dropout regularization, and early stopping.
The CNN model consists of:
-
4 Convolutional Blocks, each containing:
- Conv2D layer (32 → 64 → 128 → 256 filters)
- Batch Normalization
- MaxPooling2D (2×2)
- Dropout (0.25)
-
Fully Connected Layers:
- Flatten layer
- Dense layer (512 neurons, ReLU activation)
- Dropout (0.5)
- Output layer (2 neurons, Softmax activation)
Total Parameters: Progressive feature extraction from 32 to 256 filters across layers
- Image Preprocessing: Grayscale conversion and resizing to 50×50 pixels
- Data Augmentation: Shuffled training data for better generalization
- Regularization Techniques:
- Dropout layers to prevent overfitting
- Batch normalization for stable training
- Early Stopping: Custom callback stops training at 95% validation accuracy
- Optimization: Adam optimizer with categorical cross-entropy loss
- Batch Processing: 32-image batches with TensorFlow Dataset API
- Image Size: 50×50 pixels (grayscale)
- Batch Size: 32
- Max Epochs: 50
- Early Stopping Threshold: 95% validation accuracy
- Training/Validation Split: 12,500 images each
pip install tensorflow opencv-python numpy pandas tqdmOrganize your dataset as follows:
train/
├── cat.0.jpg
├── cat.1.jpg
├── dog.0.jpg
├── dog.1.jpg
└── ...
test1/
├── 1.jpg
├── 2.jpg
└── ...
Update the directory paths in code.py:
TRAIN_DIR = 'path/to/your/train/folder'
TEST_DIR = 'path/to/your/test/folder'Run the training script:
python code.pyThe model will:
- Load and preprocess images
- Train on the dataset with progress bars (tqdm)
- Stop early if 95% validation accuracy is reached
- Save the trained model as
dog_cat_classifier.h5
code.py- Main training script with model architecture and data processingdog_cat_classifier.h5- Saved trained model (generated after training)
label_img(img)- Extracts labels from filenames using one-hot encodingcreate_train_data()- Loads, processes, and labels training imagesprocess_test_data()- Processes test images without labelsEarlyStoppingAtValAcc- Custom callback for early stopping at target accuracy
- Target validation accuracy: 95%
- Early stopping prevents overfitting
- Model summary available after training
- TensorFlow/Keras - Deep learning framework
- OpenCV (cv2) - Image processing
- NumPy - Numerical computations
- Pandas - Data manipulation
- tqdm - Progress visualization
- Images are converted to grayscale to reduce computational complexity
- The model uses categorical cross-entropy for binary classification
- Dataset paths are configured for local Windows environment (update as needed)
- Inspired by GeeksForGeeks image processing tutorials
- Add data augmentation (rotation, flipping, zoom)
- Experiment with larger image sizes
- Implement transfer learning with pre-trained models (VGG16, ResNet)
- Add prediction visualization
- Deploy model as a web application
This project is open source and available for educational purposes.
millingtonsully
Built as part of an Honors Programming project