AIOmarRehan commited on
Commit
0bdf4e4
·
verified ·
1 Parent(s): f46707a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +363 -3
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Brain Tumor Classification With InceptionV3-Grad-CAM
3
- emoji: 🏢
4
  colorFrom: pink
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 6.0.0
8
  app_file: app.py
@@ -10,5 +10,365 @@ pinned: false
10
  license: mit
11
  short_description: InceptionV3-based brain tumor detection with Grad-CAM.
12
  ---
 
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Brain Tumor Classification With InceptionV3-Grad-CAM
3
+ emoji: 🧠
4
  colorFrom: pink
5
+ colorTo: red
6
  sdk: gradio
7
  sdk_version: 6.0.0
8
  app_file: app.py
 
10
  license: mit
11
  short_description: InceptionV3-based brain tumor detection with Grad-CAM.
12
  ---
13
+ # **Brain Tumor Classification Using InceptionV3 and Grad-CAM**
14
 
15
+ A complete deep learning pipeline for **brain tumor classification** using MRI scans.
16
+ This project demonstrates:
17
+
18
+ * **End-to-end data preprocessing**
19
+ * **Augmentation & dataset balancing**
20
+ * **Efficient tf.data pipelines**
21
+ * **Transfer learning with InceptionV3**
22
+ * **Deep model evaluation**
23
+ * **Grad-CAM interpretability**
24
+ * **LaTeX mathematical explanations**
25
+
26
+ ---
27
+
28
+ ## **1. Dataset Exploration & Inspection**
29
+
30
+ We begin by recursively scanning all MRI images and creating a structured DataFrame:
31
+
32
+ ```python
33
+ from pathlib import Path
34
+ import pandas as pd
35
+
36
+ image_extensions = {'.jpg', '.jpeg', '.png'}
37
+ paths = [
38
+ (path.parts[-2], path.name, str(path))
39
+ for path in Path("/content/my_data").rglob('*.*')
40
+ if path.suffix.lower() in image_extensions
41
+ ]
42
+
43
+ df = pd.DataFrame(paths, columns=['class', 'image', 'full_path'])
44
+ df = df.sort_values('class').reset_index(drop=True)
45
+ df.head()
46
+ ```
47
+
48
+ Count images per class:
49
+
50
+ ```python
51
+ class_count = df['class'].value_counts()
52
+ print(class_count)
53
+ ```
54
+
55
+ ### **Visualizations**
56
+
57
+ ```python
58
+ import matplotlib.pyplot as plt
59
+
60
+ plt.figure(figsize=(32,16))
61
+ class_count.plot(kind='bar', edgecolor='black')
62
+ plt.title('Number of Images per Class')
63
+ plt.show()
64
+ ```
65
+
66
+ ### **Insights**
67
+
68
+ * Classes are **imbalanced**
69
+ * Images have **variable resolution**
70
+ * Some outliers require **cleaning**
71
+
72
+ ---
73
+
74
+ ## **2. Data Cleaning & Quality Checks**
75
+
76
+ ### **Duplicate removal using MD5 hashes**
77
+
78
+ ```python
79
+ import hashlib
80
+
81
+ def get_hash(file_path):
82
+ with open(file_path, 'rb') as f:
83
+ return hashlib.md5(f.read()).hexdigest()
84
+
85
+ df['file_hash'] = df['full_path'].apply(get_hash)
86
+ df_unique = df.drop_duplicates(subset='file_hash', keep='first')
87
+ ```
88
+
89
+ ### **Additional checks**
90
+
91
+ * Corrupted image detection
92
+ * Resolution anomalies
93
+ * Brightness/contrast outliers
94
+
95
+ Cleaning ensures a **robust dataset** with minimal noise.
96
+
97
+ ---
98
+
99
+ ## **3. Data Augmentation & Class Balancing**
100
+
101
+ Target ~200 images per class using heavy augmentation:
102
+
103
+ ```python
104
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
105
+
106
+ datagen = ImageDataGenerator(
107
+ rotation_range=20,
108
+ width_shift_range=0.1,
109
+ height_shift_range=0.1,
110
+ shear_range=0.1,
111
+ zoom_range=0.1,
112
+ horizontal_flip=True,
113
+ fill_mode='nearest'
114
+ )
115
+ ```
116
+
117
+ Used for minority class upsampling and preventing overfitting.
118
+
119
+ ---
120
+
121
+ ## **4. Image Preprocessing Pipeline**
122
+
123
+ ```python
124
+ import tensorflow as tf
125
+
126
+ def preprocess_image(path, target_size=(512, 512), augment=True):
127
+ img = tf.io.read_file(path)
128
+ img = tf.image.decode_image(img, channels=3)
129
+ img = tf.image.resize(img, target_size)
130
+ img = tf.cast(img, tf.float32) / 255.0
131
+
132
+ if augment:
133
+ img = tf.image.random_flip_left_right(img)
134
+ img = tf.image.random_flip_up_down(img)
135
+ img = tf.image.random_brightness(img, max_delta=0.1)
136
+ img = tf.image.random_contrast(img, 0.9, 1.1)
137
+
138
+ return tf.clip_by_value(img, 0.0, 1.0)
139
+ ```
140
+
141
+ * **Train set:** augmentation enabled
142
+ * **Validation/Test sets:** kept clean
143
+
144
+ ---
145
+
146
+ ## **5. Dataset Preparation with `tf.data`**
147
+
148
+ ```python
149
+ AUTOTUNE = tf.data.AUTOTUNE
150
+ batch_size = 32
151
+
152
+ train_ds = tf.data.Dataset.from_tensor_slices((train_paths, train_labels))
153
+ train_ds = train_ds.shuffle(len(train_paths))
154
+ train_ds = train_ds.map(
155
+ lambda x, y: (preprocess_image(x, augment=True), y),
156
+ num_parallel_calls=AUTOTUNE
157
+ )
158
+ train_ds = train_ds.batch(batch_size).prefetch(AUTOTUNE)
159
+ ```
160
+
161
+ Benefits:
162
+
163
+ * Parallel loading
164
+ * Smart prefetching
165
+ * GPU utilization maximized
166
+
167
+ ---
168
+
169
+ ## **6. Model Architecture: InceptionV3**
170
+
171
+ Transfer learning from ImageNet:
172
+
173
+ ```python
174
+ from tensorflow.keras.applications.inception_v3 import InceptionV3
175
+ from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout
176
+ from tensorflow.keras.models import Model
177
+
178
+ inception = InceptionV3(input_shape=input_shape, weights='imagenet', include_top=False)
179
+
180
+ for layer in inception.layers:
181
+ layer.trainable = False
182
+
183
+ x = GlobalAveragePooling2D()(inception.output)
184
+ x = Dense(512, activation='relu')(x)
185
+ x = Dropout(0.5)(x)
186
+ prediction = Dense(len(le.classes_), activation='softmax')(x)
187
+
188
+ model = Model(inputs=inception.input, outputs=prediction)
189
+ ```
190
+
191
+ ### Why InceptionV3?
192
+
193
+ * Factorized convolutions
194
+ * Multi-scale feature extraction
195
+ * Lightweight and fast
196
+ * Strong performance in medical imaging
197
+
198
+ ---
199
+
200
+ ## **7. Training & Callbacks**
201
+
202
+ ```python
203
+ from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
204
+
205
+ model.compile(
206
+ loss='sparse_categorical_crossentropy',
207
+ optimizer='adam',
208
+ metrics=['accuracy']
209
+ )
210
+
211
+ callbacks = [
212
+ EarlyStopping(monitor='val_loss', patience=40, restore_best_weights=True),
213
+ ModelCheckpoint("best_model.h5", save_best_only=True, monitor='val_loss'),
214
+ ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, min_lr=1e-5)
215
+ ]
216
+ ```
217
+
218
+ Training:
219
+
220
+ ```python
221
+ history = model.fit(train_ds, validation_data=val_ds, epochs=50, callbacks=callbacks)
222
+ ```
223
+
224
+ ---
225
+
226
+ ## **8. Training Curves**
227
+
228
+ ```python
229
+ import matplotlib.pyplot as plt
230
+
231
+ plt.plot(history.history['accuracy'], label='Train Accuracy')
232
+ plt.plot(history.history['val_accuracy'], label='Val Accuracy')
233
+ plt.title('Training vs Validation Accuracy')
234
+ plt.legend()
235
+ plt.show()
236
+ ```
237
+
238
+ * Curves indicate **smooth convergence**
239
+ * Small train/val gap → **limited overfitting**
240
+
241
+ <p align="center">
242
+ <img src="https://files.catbox.moe/le1mbk.png" width="100%">
243
+ </p>
244
+
245
+ ---
246
+
247
+ ## **9. Performance Metrics**
248
+
249
+ ### Confusion Matrix
250
+
251
+ ```python
252
+ from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
253
+
254
+ cm = confusion_matrix(y_true, y_pred)
255
+ ConfusionMatrixDisplay(cm, display_labels=le.classes_).plot(cmap='Blues')
256
+ ```
257
+ <p align="center">
258
+ <img src="https://files.catbox.moe/wuynop.png" width="100%">
259
+ </p>
260
+
261
+ ### Multi-class AUC (One-vs-Rest)
262
+
263
+ **Macro AUC formula:**
264
+
265
+ <img src="https://latex.codecogs.com/svg.image?\color{white}\text{AUC}_{macro}=\frac{1}{K}\sum_{i=1}^{K}\text{AUC}_i"/>
266
+
267
+ ```python
268
+ from sklearn.preprocessing import label_binarize
269
+ from sklearn.metrics import roc_curve, auc
270
+
271
+ y_true_bin = label_binarize(y_true, classes=np.arange(len(le.classes_)))
272
+ ```
273
+ <p align="center">
274
+ <img src="https://files.catbox.moe/w3fazk.png" width="100%">
275
+ </p>
276
+
277
+ ---
278
+
279
+ ## **10. Grad-CAM: Interpretability**
280
+
281
+ Grad-CAM highlights regions the model uses for classification.
282
+
283
+ ### Grad-CAM heatmap:
284
+
285
+ <img src="https://latex.codecogs.com/svg.image?\color{white}L^c_{\text{Grad-CAM}}=\text{ReLU}\left(\sum_k\alpha_k^cA^k\right)" />
286
+
287
+ Where:
288
+
289
+ <img src="https://latex.codecogs.com/svg.image?\color{white}%5Calpha_k%5Ec%3D%5Cfrac%7B1%7D%7BZ%7D%5Csum_%7Bi%7D%5Csum_%7Bj%7D%5Cfrac%7B%5Cpartial%20y%5Ec%7D%7B%5Cpartial%20A_%7Bij%7D%5Ek%7D" />
290
+
291
+ Python implementation:
292
+
293
+ ```python
294
+ def gradcam(model, img, cls=None):
295
+ # last conv
296
+ lc = next(l for l in reversed(model.layers) if "conv" in l.name.lower())
297
+ gm = tf.keras.Model(model.input, [lc.output, model.output])
298
+
299
+ with tf.GradientTape() as t:
300
+ conv, pred = gm(img[None])
301
+ cls = tf.argmax(pred[0]) if cls is None else cls
302
+ loss = pred[:, cls]
303
+
304
+ g = t.gradient(loss, conv)
305
+ w = tf.reduce_mean(g, axis=(0,1,2))
306
+ cam = tf.reduce_sum(w * conv[0], -1)
307
+
308
+ cam = tf.nn.relu(cam)
309
+ cam /= tf.reduce_max(cam) + 1e-8
310
+ return cam.numpy()
311
+ ```
312
+
313
+ Visualization example:
314
+
315
+ ```python
316
+ plt.figure(figsize=(20,10))
317
+ for i, img in enumerate(sample_images):
318
+ overlay, info = VizGradCAM(model, img)
319
+ plt.subplot(2, 5, i+1)
320
+ plt.imshow(overlay)
321
+ plt.axis("off")
322
+ plt.title(f"True Label: {le.classes_[sample_labels[i]]}")
323
+ plt.show()
324
+ ```
325
+
326
+ <p align="center">
327
+ <img src="https://files.catbox.moe/ysg2yc.png" width="100%">
328
+ </p>
329
+
330
+ > **Note:** When the model is highly confident in a prediction, the Grad-CAM gradients become near-zero, producing little to no heatmap activation.
331
+
332
+ ---
333
+
334
+ ## **11. Technical LaTeX Notes**
335
+
336
+ ### Sparse Categorical Crossentropy
337
+
338
+ <img src="https://latex.codecogs.com/svg.image?\color{white}L=-\frac{1}{N}\sum_{i=1}^{N}\log(p_{i,y_i})" />
339
+
340
+ ### Global Average Pooling
341
+
342
+ <img src="https://latex.codecogs.com/svg.image?\color{white}f_c%3D%5Cfrac%7B1%7D%7Bh%20%5Ccdot%20%5Comega%7D%20%5Csum_%7Bi%3D1%7D%5E%7Bh%7D%20%5Csum_%7Bj%3D1%7D%5E%7B%5Comega%7D%20F_%7Bi%2Cj%2Cc%7D" />
343
+
344
+ ---
345
+
346
+ ## **12. Model Saving**
347
+
348
+ ```python
349
+ model.save("InceptionV3_Brain_Tumor_MRI.h5")
350
+ ```
351
+
352
+ ---
353
+
354
+ ## **13. Results**
355
+ > **Note:** Click the image below to view the video showcasing the project’s results.
356
+ <a href="https://files.catbox.moe/27ct3j.mp4">
357
+ <img src="https://images.unsplash.com/photo-1611162616475-46b635cb6868?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" width="400">
358
+ </a>
359
+
360
+ <hr style="border-bottom: 5px solid gray; margin-top: 10px;">
361
+
362
+ > **Note:** If the video above is not working, you can access it directly via the link below.
363
+
364
+ [Watch Demo Video](Results/InceptionV3_Brain_Tumor_MRI.mp4)
365
+
366
+ ---
367
+
368
+ ## **Key Takeaways**
369
+
370
+ * Strong data cleaning = reliable model
371
+ * Heavy augmentation reduces bias
372
+ * InceptionV3 provides excellent feature extraction
373
+ * Evaluation metrics reveal clinical reliability
374
+ * Grad-CAM adds essential interpretability