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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
short_description: InceptionV3-based animal image classifier with data cleaning
|
| 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-clean-reliable-and-accurate-animal-classifier-using-inceptionv3-175f30fbe6f3)
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
# Animal Image Classification Using InceptionV3
|
| 18 |
+
|
| 19 |
+
*A complete end-to-end pipeline for building a clean, reliable deep-learning classifier.*
|
| 20 |
+
|
| 21 |
+
This project implements a **full deep-learning workflow** for classifying animal images using **TensorFlow + InceptionV3**, with a major focus on **dataset validation and cleaning**. Before training the model, I built a comprehensive system to detect corrupted images, duplicates, brightness/contrast issues, mislabeled samples, and resolution outliers.
|
| 22 |
+
|
| 23 |
+
This repository contains the full pipelineβfrom dataset extraction to evaluation and model saving.
|
| 24 |
+
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
+
## Features
|
| 28 |
+
|
| 29 |
+
### Full Dataset Validation
|
| 30 |
+
|
| 31 |
+
The project includes automated checks for:
|
| 32 |
+
|
| 33 |
+
* Corrupted or unreadable images
|
| 34 |
+
* Hash-based duplicate detection
|
| 35 |
+
* Duplicate filenames
|
| 36 |
+
* Misplaced or incorrectly labeled images
|
| 37 |
+
* File naming inconsistencies
|
| 38 |
+
* Extremely dark/bright images
|
| 39 |
+
* Very low-contrast (blank) images
|
| 40 |
+
* Outlier resolutions
|
| 41 |
+
|
| 42 |
+
### Preprocessing & Augmentation
|
| 43 |
+
|
| 44 |
+
* Resize to 256Γ256
|
| 45 |
+
* Normalization
|
| 46 |
+
* Light augmentation (probabilistic)
|
| 47 |
+
* Efficient `tf.data` pipeline with caching, shuffling, prefetching
|
| 48 |
+
|
| 49 |
+
### Transfer Learning with InceptionV3
|
| 50 |
+
|
| 51 |
+
* Pretrained ImageNet weights
|
| 52 |
+
* Frozen base model
|
| 53 |
+
* Custom classification head (GAP β Dense β Dropout β Softmax)
|
| 54 |
+
* EarlyStopping + ModelCheckpoint + ReduceLROnPlateau callbacks
|
| 55 |
+
|
| 56 |
+
### Clean & Reproducible Training
|
| 57 |
+
|
| 58 |
+
* 80% training
|
| 59 |
+
* 10% validation
|
| 60 |
+
* 10% test
|
| 61 |
+
* High stability due to dataset cleaning
|
| 62 |
+
|
| 63 |
+
---
|
| 64 |
+
|
| 65 |
+
## 1. Dataset Extraction
|
| 66 |
+
|
| 67 |
+
The dataset is stored as a ZIP file (Google Drive). After mounting the drive, it is extracted and indexed into a Pandas DataFrame:
|
| 68 |
+
|
| 69 |
+
```python
|
| 70 |
+
drive.mount('/content/drive')
|
| 71 |
+
|
| 72 |
+
zip_path = '/content/drive/MyDrive/Animals.zip'
|
| 73 |
+
extract_to = '/content/my_data'
|
| 74 |
+
|
| 75 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 76 |
+
zip_ref.extractall(extract_to)
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
Each image entry records:
|
| 80 |
+
|
| 81 |
+
* Class
|
| 82 |
+
* Filename
|
| 83 |
+
* Full path
|
| 84 |
+
|
| 85 |
+
---
|
| 86 |
+
|
| 87 |
+
## 2. Dataset Exploration
|
| 88 |
+
|
| 89 |
+
Before any training, I analyzed:
|
| 90 |
+
|
| 91 |
+
* Class distribution
|
| 92 |
+
* Image dimensions
|
| 93 |
+
* Grayscale vs RGB
|
| 94 |
+
* Unique sizes
|
| 95 |
+
* Folder structures
|
| 96 |
+
|
| 97 |
+
Example class-count visualization:
|
| 98 |
+
|
| 99 |
+
```python
|
| 100 |
+
plt.figure(figsize=(32, 16))
|
| 101 |
+
class_count.plot(kind='bar')
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
This revealed imbalance and inconsistent image sizes early.
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
## 3. Visual Sanity Checks
|
| 109 |
+
|
| 110 |
+
Random images were displayed with their brightness, contrast, and shape to manually confirm dataset quality.
|
| 111 |
+
|
| 112 |
+
This step prevents hidden issuesβespecially in community-created or scraped datasets.
|
| 113 |
+
|
| 114 |
+
---
|
| 115 |
+
|
| 116 |
+
## 4. Data Quality Detection
|
| 117 |
+
|
| 118 |
+
The system checks for:
|
| 119 |
+
|
| 120 |
+
### Duplicate Images (Using MD5 Hashing)
|
| 121 |
+
|
| 122 |
+
```python
|
| 123 |
+
def get_hash(path):
|
| 124 |
+
with open(path, 'rb') as f:
|
| 125 |
+
return hashlib.md5(f.read()).hexdigest()
|
| 126 |
+
|
| 127 |
+
df['file_hash'] = df['full_path'].apply(get_hash)
|
| 128 |
+
duplicate_hashes = df[df.duplicated('file_hash', keep=False)]
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
### Corrupted Files
|
| 132 |
+
|
| 133 |
+
```python
|
| 134 |
+
try:
|
| 135 |
+
with Image.open(file_path) as img:
|
| 136 |
+
img.verify()
|
| 137 |
+
except:
|
| 138 |
+
corrupted_files.append(file_path)
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
### Brightness/Contrast Outliers
|
| 142 |
+
|
| 143 |
+
Using PILβs `ImageStat` to detect very dark/bright samples.
|
| 144 |
+
|
| 145 |
+
### Label Consistency Check
|
| 146 |
+
|
| 147 |
+
```python
|
| 148 |
+
folder = os.path.basename(os.path.dirname(row["full_path"]))
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
This catches mislabeled entries where folder name β actual class.
|
| 152 |
+
|
| 153 |
+
---
|
| 154 |
+
|
| 155 |
+
## 5. Preprocessing Pipeline
|
| 156 |
+
|
| 157 |
+
Custom preprocessing:
|
| 158 |
+
|
| 159 |
+
* Resize β Normalize
|
| 160 |
+
* Optional augmentation
|
| 161 |
+
* Efficient `tf.data` batching
|
| 162 |
+
|
| 163 |
+
```python
|
| 164 |
+
def preprocess_image(path, target_size=(256, 256), augment=True):
|
| 165 |
+
img = tf.image.decode_image(...)
|
| 166 |
+
img = tf.image.resize(img, target_size)
|
| 167 |
+
img = img / 255.0
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
Split structure:
|
| 171 |
+
|
| 172 |
+
| Split | Percent |
|
| 173 |
+
| ---------- | ------- |
|
| 174 |
+
| Train | 80% |
|
| 175 |
+
| Validation | 10% |
|
| 176 |
+
| Test | 10% |
|
| 177 |
+
|
| 178 |
+
---
|
| 179 |
+
|
| 180 |
+
## 6. Model β Transfer Learning with InceptionV3
|
| 181 |
+
|
| 182 |
+
The model is built using **InceptionV3 pretrained on ImageNet** as a feature extractor.
|
| 183 |
+
|
| 184 |
+
```python
|
| 185 |
+
inception = InceptionV3(
|
| 186 |
+
input_shape=input_shape,
|
| 187 |
+
weights="imagenet",
|
| 188 |
+
include_top=False
|
| 189 |
+
)
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
At first, **all backbone layers are frozen** to preserve pretrained representations:
|
| 193 |
+
|
| 194 |
+
```python
|
| 195 |
+
for layer in inception.layers:
|
| 196 |
+
layer.trainable = False
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
A custom classification head is added:
|
| 200 |
+
|
| 201 |
+
* GlobalAveragePooling2D
|
| 202 |
+
* Dense(512, ReLU)
|
| 203 |
+
* Dropout(0.5)
|
| 204 |
+
* Dense(N, Softmax) β where *N = number of classes*
|
| 205 |
+
|
| 206 |
+
This setup allows the model to learn dataset-specific patterns while avoiding overfitting during early training.
|
| 207 |
+
|
| 208 |
+
---
|
| 209 |
+
|
| 210 |
+
## 7. Initial Training (Frozen Backbone "Feature Extraction")
|
| 211 |
+
|
| 212 |
+
The model is compiled using:
|
| 213 |
+
|
| 214 |
+
* **Loss**: `sparse_categorical_crossentropy`
|
| 215 |
+
* **Optimizer**: Adam
|
| 216 |
+
* **Metric**: Accuracy
|
| 217 |
+
|
| 218 |
+
Training is performed with callbacks to improve stability:
|
| 219 |
+
|
| 220 |
+
* **EarlyStopping** (restore best weights)
|
| 221 |
+
* **ModelCheckpoint** (save best model)
|
| 222 |
+
* **ReduceLROnPlateau**
|
| 223 |
+
|
| 224 |
+
```python
|
| 225 |
+
history = model.fit(
|
| 226 |
+
train_ds,
|
| 227 |
+
validation_data=val_ds,
|
| 228 |
+
epochs=5,
|
| 229 |
+
callbacks=callbacks
|
| 230 |
+
)
|
| 231 |
+
```
|
| 232 |
+
|
| 233 |
+
This stage allows the new classification head to converge while keeping the pretrained backbone intact.
|
| 234 |
+
|
| 235 |
+
---
|
| 236 |
+
|
| 237 |
+
## 8. Fine-Tuning the InceptionV3 Backbone
|
| 238 |
+
|
| 239 |
+
After the initial convergence, **fine-tuning is applied** to improve performance.
|
| 240 |
+
|
| 241 |
+
The **last 30 layers** of InceptionV3 are unfrozen:
|
| 242 |
+
|
| 243 |
+
The model is then recompiled and trained again:
|
| 244 |
+
|
| 245 |
+
```python
|
| 246 |
+
history = model.fit(
|
| 247 |
+
train_ds,
|
| 248 |
+
validation_data=val_ds,
|
| 249 |
+
epochs=10,
|
| 250 |
+
callbacks=callbacks
|
| 251 |
+
)
|
| 252 |
+
```
|
| 253 |
+
|
| 254 |
+
Fine-tuning allows higher-level convolutional filters to adapt to the animal dataset, resulting in better class separation and generalization.
|
| 255 |
+
|
| 256 |
+
---
|
| 257 |
+
|
| 258 |
+
## 9. Model Evaluation
|
| 259 |
+
|
| 260 |
+
The final model is evaluated on a **held-out test set**.
|
| 261 |
+
|
| 262 |
+
### Accuracy & Loss Curves
|
| 263 |
+
|
| 264 |
+
Training and validation curves are plotted to monitor:
|
| 265 |
+
|
| 266 |
+
* Convergence behavior
|
| 267 |
+
* Overfitting
|
| 268 |
+
* Generalization stability
|
| 269 |
+
|
| 270 |
+
These plots confirm that fine-tuning improves validation performance without introducing instability.
|
| 271 |
+
|
| 272 |
+

|
| 273 |
+
|
| 274 |
+
---
|
| 275 |
+
|
| 276 |
+
### Confusion Matrix
|
| 277 |
+
|
| 278 |
+
A confusion matrix is computed to visualize class-level performance:
|
| 279 |
+
|
| 280 |
+
* Highlights misclassification patterns
|
| 281 |
+
* Reveals class confusion (e.g., visually similar animals)
|
| 282 |
+
|
| 283 |
+
Both annotated and heatmap-style confusion matrices are generated.
|
| 284 |
+
|
| 285 |
+

|
| 286 |
+
|
| 287 |
+
---
|
| 288 |
+
|
| 289 |
+
### Classification Metrics
|
| 290 |
+
|
| 291 |
+
The following metrics are computed on the test set:
|
| 292 |
+
|
| 293 |
+
* **Accuracy**
|
| 294 |
+
* **Precision (macro)**
|
| 295 |
+
* **Recall (macro)**
|
| 296 |
+
* **F1-score (macro)**
|
| 297 |
+
|
| 298 |
+
A detailed **per-class classification report** is also produced:
|
| 299 |
+
|
| 300 |
+
* Precision
|
| 301 |
+
* Recall
|
| 302 |
+
* F1-score
|
| 303 |
+
* Support
|
| 304 |
+
|
| 305 |
+
This provides a deeper understanding beyond accuracy alone.
|
| 306 |
+
|
| 307 |
+
```
|
| 308 |
+
10/10 ββββββββββββββββββββ 1s 106ms/step - accuracy: 0.9826 - loss: 0.3082
|
| 309 |
+
Test Accuracy: 0.9900
|
| 310 |
+
10/10 ββββββββββββββββββββ 1s 93ms/step
|
| 311 |
+
|
| 312 |
+
Classification Report:
|
| 313 |
+
precision recall f1-score support
|
| 314 |
+
|
| 315 |
+
cats 0.99 0.97 0.98 100
|
| 316 |
+
dogs 0.97 0.99 0.98 100
|
| 317 |
+
snakes 1.00 1.00 1.00 100
|
| 318 |
+
|
| 319 |
+
accuracy 0.99 300
|
| 320 |
+
macro avg 0.99 0.99 0.99 300
|
| 321 |
+
weighted avg 0.99 0.99 0.99 300
|
| 322 |
+
```
|
| 323 |
+
|
| 324 |
+
---
|
| 325 |
+
|
| 326 |
+
### ROC Curves (Multi-Class)
|
| 327 |
+
|
| 328 |
+
To further evaluate model discrimination:
|
| 329 |
+
|
| 330 |
+
* One-vs-Rest ROC curves are generated per class
|
| 331 |
+
* A **macro-average ROC curve** is computed
|
| 332 |
+
* AUC is reported for overall performance
|
| 333 |
+
|
| 334 |
+
These curves demonstrate strong separability across all classes.
|
| 335 |
+
|
| 336 |
+

|
| 337 |
+
|
| 338 |
+
---
|
| 339 |
+
|
| 340 |
+
## 10. Final Model
|
| 341 |
+
|
| 342 |
+
The best-performing model (after fine-tuning) is saved and later used for deployment:
|
| 343 |
+
|
| 344 |
+
```python
|
| 345 |
+
model.save("Inception_V3_Animals_Classification.h5")
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
This trained model is deployed using **FastAPI + Docker** for inference and **Gradio on Hugging Face Spaces** for public interaction.
|
| 349 |
+
|
| 350 |
+
---
|
| 351 |
+
|
| 352 |
+
## Updated Key Takeaways
|
| 353 |
+
|
| 354 |
+
> **Clean data enables strong fine-tuning.**
|
| 355 |
+
|
| 356 |
+
By combining:
|
| 357 |
+
|
| 358 |
+
* Rigorous dataset validation
|
| 359 |
+
* Transfer learning
|
| 360 |
+
* Selective fine-tuning
|
| 361 |
+
* Comprehensive evaluation
|
| 362 |
+
|
| 363 |
+
the model achieves **high accuracy, stable convergence, and reliable real-world performance**.
|
| 364 |
+
|
| 365 |
+
Fine-tuning only a subset of pretrained layers strikes the optimal balance between **generalization and specialization**.
|
| 366 |
+
|
| 367 |
+
---
|
| 368 |
+
|