Rahul Samedavar commited on
Upload folder using huggingface_hub
Browse files- Brain-Tumor-Classifier.h5 +3 -0
- READEME.md +267 -0
Brain-Tumor-Classifier.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:30a0a7a0d3b20939b110e6138363cc25b60221d30795043f9b608d50a7eb074c
|
| 3 |
+
size 84955408
|
READEME.md
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# **Tumor Detection from MRI Scans**
|
| 2 |
+
|
| 3 |
+
This repository contains a deep learning model and a simple graphical user interface (GUI) for detecting tumors in MRI scans. The project is built using TensorFlow/Keras and leverages transfer learning with the Xception architecture for accurate classification. Grad-CAM and GRAD-CAM++ is supported for visualization.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## **Features**
|
| 8 |
+
|
| 9 |
+
- **Pre-trained Model**: The model is fine-tuned on MRI images for multi-class tumor classification.
|
| 10 |
+
- **Image Augmentation**: Improves model generalization through augmented data during training.
|
| 11 |
+
- **Training Notebook**: Includes a Jupyter notebook detailing the training process step-by-step.
|
| 12 |
+
- **Robust Image Handling**: MRI Scan Images of all Dimensions and Mode are accepted as they are handled Dynamically.
|
| 13 |
+
- **Visualization**: Grad-CAM and GRAD-CAM++ methods are used to visualize part of the image that model focused on.
|
| 14 |
+
- **Utilities**: functions required to predict, classify, and to get confidence are provided in util.py. Grad CAM and GRAD-CAM++ functions are available in GradCam.py
|
| 15 |
+
- **GUI Interface for Testing**: A Custom tkinter GUI is provided to test the model and visualization.
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## **Installation**
|
| 20 |
+
|
| 21 |
+
1. Clone the repository:
|
| 22 |
+
```bash
|
| 23 |
+
git clone https://github.com/aurogenic/Brain-Tumor-Detector-From-MRI-Scan
|
| 24 |
+
cd Brain-Tumor-Detector-From-MRI-Scan
|
| 25 |
+
```
|
| 26 |
+
2. Install dependencies:
|
| 27 |
+
```bash
|
| 28 |
+
pip install -r requirements.txt
|
| 29 |
+
```
|
| 30 |
+
3. Run the GUI:
|
| 31 |
+
```bash
|
| 32 |
+
python app.py
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
## **Repository Contents**
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
```
|
| 41 |
+
---
|
| 42 |
+
|
| 43 |
+
## **Usage**
|
| 44 |
+
|
| 45 |
+
- Open the GUI and upload an MRI image for classification.
|
| 46 |
+
- The application will display the predicted tumor type along with the confidence score.
|
| 47 |
+
|
| 48 |
+
---
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
## **Hugging Face Models**
|
| 52 |
+
The trained models are available on Hugging Face for easy access and inference:
|
| 53 |
+
|
| 54 |
+
- Model from epoch 11 (Best Loss): Hugging Face Link
|
| 55 |
+
|
| 56 |
+
- Model from epoch 13 (Best External Dataset Performance): Hugging Face Link
|
| 57 |
+
|
| 58 |
+
- Model from epoch 16 (Best Validation Performance): Hugging Face Link
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# **Using the Models via API**
|
| 62 |
+
|
| 63 |
+
Hugging Face provides an easy-to-use API to load the model directly into Python:
|
| 64 |
+
```
|
| 65 |
+
from transformers import AutoModel
|
| 66 |
+
import tensorflow as tf
|
| 67 |
+
from util import classify
|
| 68 |
+
|
| 69 |
+
# Load the model
|
| 70 |
+
model = tf.keras.models.load_model("aurogenic/Brain-Tumor-Detection")
|
| 71 |
+
|
| 72 |
+
img_path = "Path/to/image"
|
| 73 |
+
print(f"CLass: {classify(img_path, model)}")
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
## **Methodology**
|
| 77 |
+
|
| 78 |
+
1. **Dataset**:
|
| 79 |
+
|
| 80 |
+
- 7023 MRI Scans of Brain Tumor and No tumor Images are collected from [Kaggle](https://www.kaggle.com/datasets/masoudnickparvar/brain-tumor-mri-dataset)
|
| 81 |
+
- The Dataset is categoried into 4 classes
|
| 82 |
+
- No Tumor
|
| 83 |
+
- Glioma
|
| 84 |
+
- Meningioma
|
| 85 |
+
- Pituitary
|
| 86 |
+
|
| 87 |
+

|
| 88 |
+
- Disrtibution is as follows:
|
| 89 |
+

|
| 90 |
+

|
| 91 |
+
|
| 92 |
+
2. **PreProcessing**:
|
| 93 |
+
|
| 94 |
+
- Data Augumentation is necessary to avoid overfitting. So Image Data Generator from keras library is used to Augument the Images by randomly applying following transformations
|
| 95 |
+
|
| 96 |
+
- rescale
|
| 97 |
+
- rotation
|
| 98 |
+
- width shift
|
| 99 |
+
- height shift
|
| 100 |
+
- shear
|
| 101 |
+
- zoom
|
| 102 |
+
- horizontal flip
|
| 103 |
+
- fill nearest
|
| 104 |
+
- brightness
|
| 105 |
+
|
| 106 |
+
```
|
| 107 |
+
train_datagen = ImageDataGenerator(
|
| 108 |
+
rescale=1./255,
|
| 109 |
+
rotation_range=30,
|
| 110 |
+
width_shift_range=0.2,
|
| 111 |
+
height_shift_range=0.2,
|
| 112 |
+
shear_range=0.2,
|
| 113 |
+
zoom_range=0.2,
|
| 114 |
+
horizontal_flip=True,
|
| 115 |
+
fill_mode='nearest', brightness_range=(0.8, 1.2)
|
| 116 |
+
)
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
3. **Training**:
|
| 120 |
+
|
| 121 |
+
- Xception Architecture is used as base with imagenet weights.
|
| 122 |
+
- Further layers (Flatten, Dropout, Dense, Dropout and Dense) are added.
|
| 123 |
+
- This architecture is inspired from [Here](https://www.kaggle.com/code/yousefmohamed20/brain-tumor-mri-accuracy-99).
|
| 124 |
+
- But following modifications where carried out to minimize overfitting and support Grad Cam visualization.
|
| 125 |
+
- Instead of using Xception Architecture as a base Layer along with other layer, in this approach other layrers are added to inner layers of Xception architecture this allows to visualize inner layers of Xception with Grad CAM and also improves training.
|
| 126 |
+
- softmax activation in the Xceptions last layer is removed so that Grad Cam visualization can done in follwing layers.
|
| 127 |
+
- 0.0001 learning rate was choosen to reduce overfitting.
|
| 128 |
+
|
| 129 |
+
```
|
| 130 |
+
img_shape =(299, 299, 3)
|
| 131 |
+
|
| 132 |
+
base_model = Xception(
|
| 133 |
+
include_top = False,
|
| 134 |
+
weights = "imagenet",
|
| 135 |
+
input_shape = img_shape,
|
| 136 |
+
pooling='max'
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
base_model.layers[-1].activation = None
|
| 140 |
+
|
| 141 |
+
x = base_model.output
|
| 142 |
+
x = Flatten()(x)
|
| 143 |
+
x = Dropout(0.3)(x)
|
| 144 |
+
x = Dense(128, activation='relu')(x)
|
| 145 |
+
x = Dropout(0.25)(x)
|
| 146 |
+
output = Dense(4, activation='softmax')(x)
|
| 147 |
+
model = Model(inputs=base_model.input, outputs=output)
|
| 148 |
+
|
| 149 |
+
model.compile(
|
| 150 |
+
optimizer=Adam(learning_rate=0.0001),
|
| 151 |
+
loss='categorical_crossentropy',
|
| 152 |
+
metrics=['accuracy', Precision(), Recall()]
|
| 153 |
+
)
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
- The model was trained for 20 epochs with 50% of testing data for validation.
|
| 157 |
+
- Model was saved in each epoch using checkpoints.
|
| 158 |
+
|
| 159 |
+
```
|
| 160 |
+
checkpoint = ModelCheckpoint(
|
| 161 |
+
filepath = "Models/Xception/wave_2/model_epoch_{epoch:02}.keras",
|
| 162 |
+
save_weights_only = False,
|
| 163 |
+
save_best_only = False,
|
| 164 |
+
monitor = 'val_loss',
|
| 165 |
+
mode = 'min',
|
| 166 |
+
verbose = 0
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
history = model.fit(
|
| 170 |
+
train_generator,
|
| 171 |
+
steps_per_epoch=train_generator.samples // train_generator.batch_size,
|
| 172 |
+
validation_data=val_generator,
|
| 173 |
+
validation_steps=val_generator.samples // val_generator.batch_size,
|
| 174 |
+
epochs=20, batch_size=16,
|
| 175 |
+
callbacks = [checkpoint]
|
| 176 |
+
)
|
| 177 |
+
```
|
| 178 |
+
|
| 179 |
+
- The following validation results were obtained.
|
| 180 |
+
|
| 181 |
+

|
| 182 |
+
|
| 183 |
+
4. **Comparing models from each epoch**:
|
| 184 |
+
|
| 185 |
+
- Other 50% of the validation data with Augumentation was used to test models of each epoch for comparision.
|
| 186 |
+
- Results were outstanding!!.
|
| 187 |
+
- Epoch 11, 13 and 16 models got:
|
| 188 |
+
- Accuracy : 0.99847
|
| 189 |
+
- Precision : 0.99847
|
| 190 |
+
- Recall : 0.99847
|
| 191 |
+
- while epoch 11 model got the least loss.
|
| 192 |
+
- loss: 0.0076
|
| 193 |
+
|
| 194 |
+

|
| 195 |
+
|
| 196 |
+
5. **Evaluating the Model**:
|
| 197 |
+
|
| 198 |
+
- Model from epoch 16 was choosen for evaluation instead of epoch 11 by considering both testing and validation results.
|
| 199 |
+
- Following classification report was obtained.
|
| 200 |
+
|
| 201 |
+
```
|
| 202 |
+
precision recall f1-score support
|
| 203 |
+
|
| 204 |
+
glioma 0.993377 1.000000 0.996678 150
|
| 205 |
+
meningioma 1.000000 1.000000 1.000000 153
|
| 206 |
+
notumor 1.000000 1.000000 1.000000 202
|
| 207 |
+
pituitary 1.000000 0.993333 0.996656 150
|
| 208 |
+
|
| 209 |
+
accuracy 0.998473 655
|
| 210 |
+
macro avg 0.998344 0.998333 0.998333 655
|
| 211 |
+
weighted avg 0.998483 0.998473 0.998473 655
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
- Further Calculations yeilded
|
| 215 |
+
|
| 216 |
+
```
|
| 217 |
+
Matthews Correlation Coeficient: 0.9980
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
- Following ROC curve and AUC were obsered
|
| 221 |
+
|
| 222 |
+

|
| 223 |
+
|
| 224 |
+
- The Confusion Matrix shows that only one prediction was wrong.
|
| 225 |
+
|
| 226 |
+

|
| 227 |
+
|
| 228 |
+
5. **Testing with External Dataset**:
|
| 229 |
+
|
| 230 |
+
- Models from epoch 11, 13 and 16 were tested with larger External Dataset from [kaggle](https://www.kaggle.com/datasets/lakshancooray23/tumour-classification-images)
|
| 231 |
+
- This dataset contains 49704 Images sofficient enought for testing.
|
| 232 |
+
- Model from epoch 13 performed very well with this data and got 0.9950 accuracy while epoch 11 and 16 models got 0.9792 and 0.9872 respectively.
|
| 233 |
+
- Model 13s performance is as follows.
|
| 234 |
+
|
| 235 |
+
```
|
| 236 |
+
precision recall f1-score support
|
| 237 |
+
|
| 238 |
+
glioma 0.9963 0.9959 0.9961 13990
|
| 239 |
+
meningioma 0.9916 0.9956 0.9936 11870
|
| 240 |
+
notumor 0.9926 0.9983 0.9954 11786
|
| 241 |
+
pituitary 0.9992 0.9900 0.9946 12058
|
| 242 |
+
|
| 243 |
+
accuracy 0.9950 49704
|
| 244 |
+
macro avg 0.9949 0.9950 0.9949 49704
|
| 245 |
+
weighted avg 0.9950 0.9950 0.9950 49704
|
| 246 |
+
|
| 247 |
+
Matthews Correlation Coefficient: 0.9933
|
| 248 |
+
```
|
| 249 |
+
|
| 250 |
+

|
| 251 |
+

|
| 252 |
+
|
| 253 |
+
6. **Grad CAM and Grad CAM++**:
|
| 254 |
+
Gradient-Weighted Class Activation Mapping is a technique in computer vision and deep learning that helps users understand how convolutional neural networks (CNNs) make predictions.
|
| 255 |
+
It allows us to see what parts of the image a layer focuses on order to make predictions. Grad CAM++ improves this further by back propogation. This are visualized by masking the heatmap over the image.
|
| 256 |
+
|
| 257 |
+

|
| 258 |
+
|
| 259 |
+

|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
## **Contributing**
|
| 266 |
+
|
| 267 |
+
Feel free to contribute by submitting issues or pull requests. Let's improve this project together!
|