A machine learning project that uses Convolutional Neural Networks (CNN) to classify images as either cats or dogs. This project implements a binary image classifier using TensorFlow and Keras.
This project builds and trains a CNN model to automatically detect and classify images of cats and dogs. The model uses deep learning techniques including convolutional layers, pooling layers, and data augmentation to achieve accurate classification results.
- CNN Architecture: Custom convolutional neural network with multiple Conv2D and MaxPooling2D layers
- Data Augmentation: Implements image preprocessing techniques like rescaling, shearing, zooming, and horizontal flipping
- Binary Classification: Distinguishes between cat and dog images with high accuracy
- Real-time Prediction: Load and classify new images using the trained model
- Python: Core programming language
- TensorFlow: Machine learning framework
- Keras: High-level neural networks API
- NumPy: Numerical computing
- Jupyter Notebook: Development environment
tensorflow
keras
numpy
matplotlib (optional, for visualization)The CNN model consists of:
- Convolutional Layers:
- Conv2D(32, (3,3)) with ReLU activation
- Conv2D(16, (3,3)) with ReLU activation
- Pooling Layers: MaxPooling2D with (2,2) pool size
- Dense Layers:
- Multiple fully connected layers (64, 32, 16, 8, 4 neurons)
- Output layer with sigmoid activation for binary classification
- Compilation: Uses binary crossentropy loss and Adam optimizer
The model expects:
- Training Data: Images organized in folders by class (cats/dogs)
- Testing Data: Separate validation dataset
- Image Size: 64x64 pixels
- Color Channels: RGB (3 channels)
- Data Augmentation: Applied to training data to improve model generalization
- Open
catsdogs.ipynbin Jupyter Notebook - Ensure your dataset is organized in the following structure:
training data/ ├── cats/ └── dogs/ testing data/ ├── cats/ └── dogs/ - Update the file paths in the notebook to match your dataset location
- Run all cells to train the model
import numpy as np
from keras.preprocessing import image
# Load and preprocess image
img = image.load_img('path_to_your_image.jpg', target_size=(64,64))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
# Make prediction
prediction = cnn.predict(img)
# Interpret result
if prediction[0][0] > 0.5:
print("DOG")
else:
print("CAT")- Epochs: 50
- Batch Size: 32
- Steps per Epoch: 2000
- Validation Steps: 100
- Input Shape: (64, 64, 3)
catsdogs.ipynb: Main notebook containing the complete model implementationcatsdogs-checkpoint.ipynb: Checkpoint file for the notebookREADME.md: Project documentation
The model uses binary crossentropy loss function and is optimized with the Adam optimizer. Performance can be evaluated using:
- Training accuracy
- Validation accuracy
- Loss curves
- Confusion matrix
- Add model evaluation metrics and visualization
- Implement transfer learning with pre-trained models
- Add support for multi-class classification (more animal types)
- Create a web interface for easy image upload and prediction
- Optimize model architecture for better performance
- Add data visualization and exploratory data analysis
Feel free to fork this project and submit pull requests for any improvements!
This project is open source and available under the MIT License.
Note: Make sure to update the file paths in the notebook to match your local dataset location before training the model.