A deep learning project to classify images of dogs and cats using a Convolutional Neural Network (CNN) built with the TensorFlow library. This project was a first-time exploration into Python and deep learning, following tutorials and resources to understand the fundamentals of model training and evaluation.\
-
Custom CNN Model: A custom-built neural network architecture for image classification.
-
TensorFlow Implementation: The model is built and trained using the powerful TensorFlow library.
-
Visualized Metrics: The training process is visualized with charts showing the model's accuracy and loss progression using Matplotlib.
-
Pre-trained Model: The final trained model is saved for easy re-use, allowing for quick testing without needing to retrain.
- Libraries Before you begin, ensure you have the following installed:
- Python 3.x: The project is developed in Python.
- TensorFlow: The core deep learning library.
- Matplotlib: Used for visualizing the training metrics.
- Numpy: A fundamental package for scientific computing with Python.
You can install the required packages using pip:
pip install tensorflow matplotlib numpy- Dataset The model was trained on the Dogs and Cats Classification Dataset from Kaggle, which contains 24,998 images (12,499 cat images and 12,499 dog images).
-
Download: You can download the dataset from https://www.kaggle.com/datasets/bhavikjikadara/dog-and-cat-classification-dataset.
-
Structure: Place the unzipped dataset in the root of the project directory.
- Training the model The main script (main.py) trains the CNN
-
Execution: Go to model_training folder and run the following command
python main.py
-
Output: After training, the model will be saved in the trained_model/ directory.
-
Charts: After the model is trained and saved the following charts are visualized showing the accuracy and loss progression
- Testing the model To test the trained model on a new image, use the model_test.py script.
-
Prepare your image: Place the image you want to test in a designated folder, or specify the image path in the script.
-
Execution: Run the test script:
python model_test.py
The script will output the classification result (either "Dog" or "Cat").
During training, the model's accuracy and loss were tracked and saved. The final metrics are stored in a JSON file (training_history.json) located in trained_model folder.
- Accuracy: The graph below shows the model's accuracy on both the training and validation sets over each epoch.
- Loss: This graph illustrates how the model's loss decreased over the training period.
- Model Architecture: The model uses a standard CNN architecture with convolutional layers, max-pooling layers, and a final dense layer for classification.
- Optimizer: The RMSprop optimizer was chosen for its effectiveness in training neural networks. While other optimizers exist, RMSprop is a solid choice for this type of problem, dynamically adjusting the learning rate for each parameter.
- Tutorials: This project was developed by learning from these valuable resources:
By reading the Tensorflow documentation I had found that the TensorFlow provides a powerful visualization tool called TensorBoard that's essential for the machine learning workflow. It helps you track experiment metrics like loss and accuracy, visualize your model graph, project embeddings into a lower-dimensional space, and much more. To use TensorBoard, you need to save log files during model training. Typically, these logs are stored in a designated directory, like logs/fit.
tensorboard --logdir logs/fitUnfortunately this command didn't work for me. After some research I have found another command which is this:
python -m tensorboard.main --logdir logs/fitBy running it I was able to access the TensorBoard on http://localhost:6006/



