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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
short_description: Cat vs Dog classifier with InceptionV3 model.
|
| 12 |
---
|
| 13 |
|
| 14 |
+
[If you would like a detailed explanation of this project, please refer to the Medium article below.](https://medium.com/@ai.omar.rehan/building-a-near-perfect-cat-vs-dog-classifier-with-inceptionv3-01a5f9730907)
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# Cats vs Dogs Image Classification (InceptionV3 + TensorFlow)
|
| 19 |
+
|
| 20 |
+
## Project Overview
|
| 21 |
+
|
| 22 |
+
This project focuses on building an image classification model that can distinguish between **cats and dogs** using **Transfer Learning** with the InceptionV3 architecture.
|
| 23 |
+
|
| 24 |
+
Instead of training a deep learning model from scratch, this project uses a pre-trained model and adapts it to solve a binary classification problem efficiently.
|
| 25 |
+
|
| 26 |
+
The goal of this project is to practice building a real-world computer vision pipeline including data preprocessing, training, evaluation, and visualization.
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## Dataset
|
| 31 |
+
|
| 32 |
+
The project uses the **Cats and Dogs dataset**, which contains around:
|
| 33 |
+
|
| 34 |
+
* ~6,000 cat images
|
| 35 |
+
* ~6,000 dog images
|
| 36 |
+
|
| 37 |
+
The dataset is balanced, which helps the model learn both classes fairly and avoids bias toward one class.
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## Data Preprocessing
|
| 42 |
+
|
| 43 |
+
Before training, images go through several preprocessing steps:
|
| 44 |
+
|
| 45 |
+
* Resize images to **256 Γ 256**
|
| 46 |
+
* Normalize pixel values
|
| 47 |
+
* Handle very bright or very dark images
|
| 48 |
+
* Apply data augmentation to improve generalization:
|
| 49 |
+
|
| 50 |
+
* Random flipping
|
| 51 |
+
* Random brightness changes
|
| 52 |
+
* Random contrast changes
|
| 53 |
+
|
| 54 |
+
TensorFlowβs `tf.data` pipeline is used to efficiently load and prepare data.
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
## Model Architecture
|
| 59 |
+
|
| 60 |
+
This project uses **Transfer Learning with InceptionV3**.
|
| 61 |
+
|
| 62 |
+
### Base Model
|
| 63 |
+
|
| 64 |
+
* Pre-trained on ImageNet
|
| 65 |
+
* Used as a feature extractor
|
| 66 |
+
* Frozen during initial training
|
| 67 |
+
|
| 68 |
+
### Custom Classification Head
|
| 69 |
+
|
| 70 |
+
Added on top of the base model:
|
| 71 |
+
|
| 72 |
+
* Global Average Pooling
|
| 73 |
+
* Dense layer (512 neurons, ReLU)
|
| 74 |
+
* Dropout (0.5) to reduce overfitting
|
| 75 |
+
* Final Dense layer with **Sigmoid** activation for binary classification
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
|
| 79 |
+
## Training Strategy
|
| 80 |
+
|
| 81 |
+
### Optimizer
|
| 82 |
+
|
| 83 |
+
* Adam optimizer
|
| 84 |
+
|
| 85 |
+
### Loss Function
|
| 86 |
+
|
| 87 |
+
* Binary Cross-Entropy
|
| 88 |
+
|
| 89 |
+
### Training Enhancements
|
| 90 |
+
|
| 91 |
+
The project uses callbacks to improve training:
|
| 92 |
+
|
| 93 |
+
* **EarlyStopping**
|
| 94 |
+
|
| 95 |
+
* Stops training when validation stops improving
|
| 96 |
+
* **ModelCheckpoint**
|
| 97 |
+
|
| 98 |
+
* Saves the best model automatically
|
| 99 |
+
* **ReduceLROnPlateau**
|
| 100 |
+
|
| 101 |
+
* Reduces learning rate when progress slows down
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
## Results & Evaluation
|
| 106 |
+
|
| 107 |
+
Model performance was evaluated using several visualization techniques.
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
### Accuracy
|
| 112 |
+
|
| 113 |
+
```
|
| 114 |
+
76/76 ββββββββββββββββββββ 3s 41ms/step - accuracy: 0.9941 - loss: 0.0194
|
| 115 |
+
Test Accuracy: 0.9933
|
| 116 |
+
76/76 ββββββββββββββββββββ 16s 115ms/step
|
| 117 |
+
Precision: 0.2498, Recall: 0.5000, F1-score: 0.3331
|
| 118 |
+
|
| 119 |
+
Classification Report:
|
| 120 |
+
precision recall f1-score support
|
| 121 |
+
|
| 122 |
+
cats 0.50 1.00 0.67 601
|
| 123 |
+
dogs 0.00 0.00 0.00 602
|
| 124 |
+
|
| 125 |
+
accuracy 0.50 1203
|
| 126 |
+
macro avg 0.25 0.50 0.33 1203
|
| 127 |
+
weighted avg 0.25 0.50 0.33 1203
|
| 128 |
+
```
|
| 129 |
+
---
|
| 130 |
+

|
| 131 |
+
**Observation:**
|
| 132 |
+
Training and validation accuracy both increase steadily and reach high performance (~98β99%).
|
| 133 |
+
|
| 134 |
+
---
|
| 135 |
+
|
| 136 |
+
### Loss
|
| 137 |
+
|
| 138 |
+

|
| 139 |
+
|
| 140 |
+
**Observation:**
|
| 141 |
+
Both training and validation loss decrease and stabilize, indicating good learning and low overfitting.
|
| 142 |
+
|
| 143 |
+
---
|
| 144 |
+
|
| 145 |
+
### Confusion Matrix
|
| 146 |
+
|
| 147 |
+

|
| 148 |
+
|
| 149 |
+
**Observation:**
|
| 150 |
+
Most predictions lie along the diagonal, meaning the model correctly classifies both cats and dogs most of the time.
|
| 151 |
+
|
| 152 |
+
---
|
| 153 |
+
|
| 154 |
+
### ROC Curve
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
| Binary Classification ROC Curve | (OvR) ROC Curve β Multi-Class Classification |
|
| 158 |
+
|-------|-------|
|
| 159 |
+
| <img src="https://files.catbox.moe/iou784.png" width="490"/> | <img src="https://files.catbox.moe/pz3mv7.png" width="540"/> |
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
**Observation:**
|
| 164 |
+
The model achieves an AUC score close to **1.0**, which indicates excellent classification ability.
|
| 165 |
+
|
| 166 |
+
---
|
| 167 |
+
|
| 168 |
+
## Key Takeaways
|
| 169 |
+
|
| 170 |
+
* Transfer Learning significantly reduces training time.
|
| 171 |
+
* Data augmentation improves model robustness.
|
| 172 |
+
* Proper evaluation metrics give deeper insight into model performance.
|
| 173 |
+
|
| 174 |
+
---
|