File size: 10,945 Bytes
514826f 8a632cc 514826f 1a04788 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
---
license: mit
datasets:
- AIOmarRehan/AnimalsDataset
---
# Animal Image Classification (TensorFlow & CNN)
> "A complete endβtoβend pipeline for building, cleaning, preprocessing, training, evaluating, and deploying a deep CNN model for multiβclass animal image classification."
This project is designed to be **clean**, **organized**, and **human-friendly**, showing the full machineβlearning workflow β from **data validation** to **model evaluation & ROC curves**.
---
## Project Structure
| Component | Description |
|----------|-------------|
| **Data Loading** | Reads and extracts the ZIP dataset from Google Drive |
| **EDA** | Class distribution, file integrity, image sizes, brightness, contrast, samples display |
| **Preprocessing** | Resizing, normalization, augmentation, hashing, cleaning corrupted files |
| **Model** | Deep custom CNN with BatchNorm, Dropout & L2 Regularization |
| **Training** | Adam optimizer, LR scheduler, Early stopping |
| **Evaluation** | Confusion matrix, classification report, ROC curves |
| **Export** | Saves final `.h5` model |
---
## How to Run
### 1. Upload your dataset to Google Drive
Your dataset must be structured as:
```
Animals/
βββ Cats/
βββ Dogs/
βββ Snakes/
```
### 2. Update the ZIP path
```python
zip_path = '/content/drive/MyDrive/Animals.zip'
extract_to = '/content/my_data'
```
### 3. Run the Notebook
Once executed, the script will:
- Mount Google Drive
- Extract images
- Build a DataFrame of paths
- Run EDA checks
- Clean and prepare images
- Train the CNN model
- Export results
---
## Data Preparation & EDA
This project performs **deep dataset validation** including:
### Class Distribution
```python
class_count = df['class'].value_counts()
class_count.plot(kind='bar')
```
### Image Size Properties
```python
image_df['Channels'].value_counts().plot(kind='bar')
```
### Duplicate Image Detection
Using MD5 hashing:
```python
def get_hash(file_path):
with open(file_path, 'rb') as f:
return hashlib.md5(f.read()).hexdigest()
```
### Brightness & Contrast Issues
```python
stat = ImageStat.Stat(img.convert("L"))
brightness = stat.mean[0]
contrast = stat.stddev[0]
```
### Autoβfixing poor images
Brightness/contrast enhanced using:
```python
img = ImageEnhance.Brightness(img).enhance(1.5)
img = ImageEnhance.Contrast(img).enhance(1.5)
```
---
## Image Preprocessing
All images are resized to **256Γ256** and normalized to **[0,1]**.
```python
def preprocess_image(path, target_size=(256, 256)):
img = tf.io.read_file(path)
img = tf.image.decode_image(img, channels=3)
img = tf.image.resize(img, target_size)
return tf.cast(img, tf.float32) / 255.0
```
### Data Augmentation
```python
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip("horizontal"),
tf.keras.layers.RandomRotation(0.1),
tf.keras.layers.RandomZoom(0.1),
])
```
---
## CNN Model Architecture
Below is a simplified view of the model:
```
Conv2D (32) β BatchNorm β Conv2D (32) β BatchNorm β MaxPool β Dropout
Conv2D (64) β BatchNorm β Conv2D (64) β BatchNorm β MaxPool β Dropout
Conv2D (128) β BatchNorm β Conv2D (128) β BatchNorm β MaxPool β Dropout
Conv2D (256) β BatchNorm β Conv2D (256) β BatchNorm β MaxPool β Dropout
Flatten β Dense (softmax)
```
Example code:
```python
model.add(Conv2D(32, (3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2)))
```
---
## Training
```python
epochs = 50
optimizer = Adam(learning_rate=0.0005)
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
```
### Callbacks
| Callback | Purpose |
|----------|---------|
| **ReduceLROnPlateau** | Autoβreduce LR when val_loss stagnates |
| **EarlyStopping** | Stop training when no improvement |
---
## Model Evaluation
### Accuracy
```python
test_loss, test_accuracy = model.evaluate(test_ds)
```
### Classification Report
```python
print(classification_report(y_true, y_pred, target_names=le.classes_))
```
### Confusion Matrix
```python
sns.heatmap(cm, annot=True, cmap='Blues')
```
### ROC Curve (One-vs-Rest)
```python
fpr, tpr, _ = roc_curve(y_true_bin[:, i], y_probs[:, i])
```
---
## Saving the Model
```python
model.save("Animal_Classification.h5")
```
---
## Full Code Organization (High-Level)
| Step | Description |
|------|-------------|
| 1 | Import libraries, mount drive |
| 2 | Extract ZIP |
| 3 | Build DataFrame |
| 4 | EDA & cleaning |
| 5 | Preprocessing & augmentation |
| 6 | Dataset pipeline (train/val/test) |
| 7 | CNN architecture |
| 8 | Training |
| 9 | Evaluation |
|10 | Save model |
---
## Final Notes
This README is crafted to feel **human**, clean, and attractive β not autogenerated. It can be directly used in any GitHub repository.
If you want, I can also:
- Generate a **short version**
- Add **badges** (TensorFlow, Python, etc.)
- Write an **installation section**
- Turn it into a **Hugging Face Space README**
# Animal Image Classification β Complete Pipeline (README)
> "A clean dataset is half the modelβs accuracy. The rest is just engineering."
This project presents a **complete end-to-end deep learning pipeline** for **multi-class animal image classification** using TensorFlow/Keras. It includes everything from data extraction, cleaning, and analysis, to model training, evaluation, and exporting.
---
## Table of Contents
| Section | Description |
|--------|-------------|
| **1. Project Overview** | What this project does & architecture overview |
| **2. Features** | Key capabilities of this pipeline |
| **3. Directory Structure** | Recommended project layout |
| **4. Installation** | How to install and run this project |
| **5. Dataset Processing** | Extraction, cleaning, inspections |
| **6. Exploratory Data Analysis** | Visualizations & summary statistics |
| **7. Preprocessing & Augmentation** | Data preparation logic |
| **8. CNN Model Architecture** | Layers, blocks, hyperparameters |
| **9. Training & Callbacks** | How the model is trained |
| **10. Evaluation Metrics** | Reports, ROC curve, confusion matrix |
| **11. Model Export** | Saving and downloading the model |
| **12. Code Examples** | Important snippets explained |
---
## 1. Project Overview
This project builds a **Convolutional Neural Network (CNN)** to classify images of animals into multiple categories. The process includes:
- Dataset extraction from Google Drive
- Data validation (duplicates, corrupt files, mislabeled images)
- Image enhancement & cleaning
- Class distribution analysis
- Image size analysis and outlier detection
- Data augmentation
- CNN model training with regularization
- Performance evaluation using multiple metrics
- Model export to `.h5`
The pipeline is designed to be **robust, explainable, and production-friendly**.
---
## 2. Features
| Feature | Description |
|---------|-------------|
| **Automatic Dataset Extraction** | Unzips and loads images from Google Drive |
| **Image Validation** | Detects duplicates, corrupted images, and mislabeled files |
| **Data Cleaning** | Brightness/contrast enhancements for dark or overexposed samples |
| **EDA Visualizations** | Class distribution, size, color modes, outliers |
| **TensorFlow Dataset Pipeline** | Efficient TFRecords-like batching & prefetching |
| **Deep CNN Model** | 32 β 64 β 128 β 256 filters with batch norm and dropout |
| **Model Evaluation Dashboard** | Confusion matrix, ROC curves, precision/recall/F1 |
| **Model Export** | Saves final model as `Animal_Classification.h5` |
---
## 3. Recommended Directory Structure
```text
Animal-Classification
β£ data
β β Animals (extracted folders)
β£ notebooks
β£ src
β β£ preprocessing.py
β β£ model.py
β β utils.py
β£ README.md
β requirements.txt
```
---
## 4. Installation
```bash
pip install tensorflow pandas matplotlib seaborn scikit-learn pillow tqdm
```
If using **Google Colab**, the project already supports:
- `google.colab.drive`
- `google.colab.files`
---
## 5. Dataset Extraction & Loading
Example snippet:
```python
zip_path = '/content/drive/MyDrive/Animals.zip'
extract_to = '/content/my_data'
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_to)
```
Images are collected into a DataFrame:
```python
paths = [(path.parts[-2], path.name, str(path)) for path in Path(extract_to).rglob('*.*')]
df = pd.DataFrame(paths, columns=['class','image','full_path'])
```
---
## 6. Exploratory Data Analysis
Examples of generated visualizations:
- Barplot of class distribution
- Pie chart of percentage per class
- Scatter plots of image width and height
- Image mode (RGB/Gray) distribution
Example:
```python
plt.figure(figsize=(32,16))
class_count.plot(kind='bar')
```
---
## 7. Preprocessing & Augmentation
### Preprocessing function
```python
def preprocess_image(path, target_size=(256,256)):
img = tf.io.read_file(path)
img = tf.image.decode_image(img, channels=3)
img = tf.image.resize(img, target_size)
return tf.cast(img, tf.float32)/255.0
```
### Augmentation
```python
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip("horizontal"),
tf.keras.layers.RandomRotation(0.1),
tf.keras.layers.RandomZoom(0.1),
])
```
---
## 8. CNN Model Architecture
| Block | Layers |
|------|---------|
| **Block 1** | Conv2D(32) β BN β Conv2D(32) β BN β MaxPool β Dropout(0.2) |
| **Block 2** | Conv2D(64) β BN β Conv2D(64) β BN β MaxPool β Dropout(0.3) |
| **Block 3** | Conv2D(128) β BN β Conv2D(128) β BN β MaxPool β Dropout(0.4) |
| **Block 4** | Conv2D(256) β BN β Conv2D(256) β BN β MaxPool β Dropout(0.5) |
| **Output** | Flatten β Dense(num_classes, softmax) |
Example snippet:
```python
model.add(Conv2D(64,(3,3),activation='relu',padding='same'))
model.add(BatchNormalization())
```
---
## 9. Training
```python
optimizer = Adam(learning_rate=0.0005)
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy', metrics=['accuracy'])
```
Using callbacks:
```python
ReduceLROnPlateau(...)
EarlyStopping(...)
```
---
## 10. Evaluation Metrics
This project computes:
- Precision, Recall, F1 (macro & per class)
- Confusion matrix (heatmap)
- ROC curves (one-vs-rest)
- Macro-average ROC
Example:
```python
cm = confusion_matrix(y_true, y_pred)
sns.heatmap(cm, annot=True)
```
---
## 11. Model Export
```python
model.save("Animal_Classification.h5")
files.download("Animal_Classification.h5")
```
---
## 12. Example Snippets
### Checking corrupted files
```python
try:
with Image.open(path) as img:
img.verify()
except:
corrupted.append(path)
```
### Filtering duplicate images
```python
df['file_hash'] = df['full_path'].apply(get_hash)
df_unique = df.drop_duplicates(subset='file_hash')
``` |