AIOmarRehan commited on
Commit
514826f
Β·
verified Β·
1 Parent(s): 24e4a53

Update README.md

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