Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- README.md +69 -14
- app.py +87 -0
- best_weights_balanced.h5 +3 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,14 +1,69 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
--
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Brain Tumor Classification with CNN and EfficientNetB3
|
| 2 |
+
|
| 3 |
+

|
| 4 |
+

|
| 5 |
+
|
| 6 |
+
## Overview
|
| 7 |
+
This project implements a deep learning pipeline for classifying brain tumor MRI images into four categories: **glioma**, **meningioma**, **notumor**, and **pituitary**. The model leverages transfer learning with EfficientNetB3 and includes interpretability with Grad-CAM visualizations.
|
| 8 |
+
|
| 9 |
+
## Motivation
|
| 10 |
+
Brain tumor classification from MRI images is a critical task for early diagnosis and treatment planning. Manual analysis is time-consuming and subjective. This project aims to:
|
| 11 |
+
- Automate tumor classification with high accuracy
|
| 12 |
+
- Provide interpretable results for clinicians using Grad-CAM
|
| 13 |
+
- Demonstrate a reproducible, professional deep learning workflow
|
| 14 |
+
|
| 15 |
+
## Dataset
|
| 16 |
+
- The dataset is not included due to size. Download it using the link in `dataset_link.txt`.
|
| 17 |
+
- Organize the data as:
|
| 18 |
+
- `Training/` (with subfolders for each class)
|
| 19 |
+
- `Testing/` (with subfolders for each class)
|
| 20 |
+
|
| 21 |
+
## Project Structure
|
| 22 |
+
- `model.ipynb`: Main notebook with all steps, explanations, and visualizations
|
| 23 |
+
- `best_weights_balanced.h5`: Best model weights (saved automatically)
|
| 24 |
+
- `dataset_link.txt`: Dataset download link
|
| 25 |
+
- `README.md`: This file
|
| 26 |
+
- Grad-CAM example images: `gradcam1.png`, `gradcam2.png`
|
| 27 |
+
|
| 28 |
+
## Approach & Steps
|
| 29 |
+
1. **Data Loading & Visualization**
|
| 30 |
+
- Loads images using TensorFlow's `image_dataset_from_directory`
|
| 31 |
+
- Visualizes sample images and class distribution
|
| 32 |
+
2. **Class Imbalance Analysis**
|
| 33 |
+
- Plots class counts to check for imbalance
|
| 34 |
+
- (If needed, you can add class weighting or augmentation)
|
| 35 |
+
3. **Model Architecture**
|
| 36 |
+
- Uses EfficientNetB3 (pretrained on ImageNet) as feature extractor
|
| 37 |
+
- Adds custom dense, batch normalization, and dropout layers
|
| 38 |
+
- Output layer: softmax for 4 classes
|
| 39 |
+
4. **Training**
|
| 40 |
+
- Early stopping, learning rate scheduling, and best weight saving
|
| 41 |
+
- Plots training/validation loss and accuracy
|
| 42 |
+
5. **Evaluation**
|
| 43 |
+
- Prints test loss, accuracy, precision, recall, F1-score
|
| 44 |
+
- Shows confusion matrix
|
| 45 |
+
6. **Interpretability: Grad-CAM**
|
| 46 |
+
- Generates Grad-CAM heatmaps for test images
|
| 47 |
+
- Helps understand which regions the model focuses on for its decision
|
| 48 |
+
|
| 49 |
+
## Example Results
|
| 50 |
+
Below are Grad-CAM visualizations showing the model's attention on MRI images:
|
| 51 |
+
|
| 52 |
+

|
| 53 |
+

|
| 54 |
+
|
| 55 |
+
## How to Run
|
| 56 |
+
1. Install requirements (see notebook for package list)
|
| 57 |
+
2. Download and extract the dataset as described above
|
| 58 |
+
3. Run `model.ipynb` step by step
|
| 59 |
+
4. Review the outputs, metrics, and Grad-CAM visualizations
|
| 60 |
+
|
| 61 |
+
## Key Points & Best Practices
|
| 62 |
+
- **Transfer Learning**: EfficientNetB3 enables strong feature extraction with limited data
|
| 63 |
+
- **Callbacks**: Early stopping and best weight saving prevent overfitting
|
| 64 |
+
- **Visualization**: Data and training curves are visualized for transparency
|
| 65 |
+
- **Interpretability**: Grad-CAM provides insight into model decisions
|
| 66 |
+
- **Reproducibility**: All steps are documented and reproducible
|
| 67 |
+
|
| 68 |
+
## License
|
| 69 |
+
This project is for educational and research purposes only.
|
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from tensorflow.keras.preprocessing import image
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
# Model yükle
|
| 9 |
+
model = load_model("best_weights_balanced.h5")
|
| 10 |
+
|
| 11 |
+
# Sınıf isimleri
|
| 12 |
+
class_names = ["Glioma", "Meningioma", "No Tumor", "Pituitary"]
|
| 13 |
+
|
| 14 |
+
# Grad-CAM fonksiyonu
|
| 15 |
+
def get_gradcam(img_array, model, last_conv_layer_name="top_conv"):
|
| 16 |
+
grad_model = tf.keras.models.Model(
|
| 17 |
+
[model.inputs],
|
| 18 |
+
[model.get_layer(last_conv_layer_name).output, model.output]
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
with tf.GradientTape() as tape:
|
| 22 |
+
conv_outputs, predictions = grad_model(img_array)
|
| 23 |
+
pred_index = tf.argmax(predictions[0])
|
| 24 |
+
class_channel = predictions[:, pred_index]
|
| 25 |
+
|
| 26 |
+
grads = tape.gradient(class_channel, conv_outputs)
|
| 27 |
+
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
|
| 28 |
+
|
| 29 |
+
conv_outputs = conv_outputs[0]
|
| 30 |
+
heatmap = conv_outputs @ pooled_grads[..., tf.newaxis]
|
| 31 |
+
heatmap = tf.squeeze(heatmap)
|
| 32 |
+
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
|
| 33 |
+
|
| 34 |
+
return heatmap.numpy(), pred_index.numpy()
|
| 35 |
+
|
| 36 |
+
def predict_and_explain(img):
|
| 37 |
+
# Görüntüyü hazırla
|
| 38 |
+
img_resized = cv2.resize(img, (300, 300))
|
| 39 |
+
img_array = np.expand_dims(img_resized / 255.0, axis=0)
|
| 40 |
+
|
| 41 |
+
# Tahmin
|
| 42 |
+
predictions = model.predict(img_array, verbose=0)
|
| 43 |
+
pred_class = np.argmax(predictions[0])
|
| 44 |
+
confidence = predictions[0][pred_class]
|
| 45 |
+
|
| 46 |
+
# Grad-CAM
|
| 47 |
+
heatmap, _ = get_gradcam(img_array, model)
|
| 48 |
+
heatmap = cv2.resize(heatmap, (img_resized.shape[1], img_resized.shape[0]))
|
| 49 |
+
heatmap = np.uint8(255 * heatmap)
|
| 50 |
+
heatmap_colored = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
| 51 |
+
|
| 52 |
+
# Overlay
|
| 53 |
+
superimposed = cv2.addWeighted(img_resized, 0.6, heatmap_colored, 0.4, 0)
|
| 54 |
+
|
| 55 |
+
# Sonuçlar
|
| 56 |
+
results = {class_names[i]: float(predictions[0][i]) for i in range(4)}
|
| 57 |
+
|
| 58 |
+
return results, superimposed
|
| 59 |
+
|
| 60 |
+
# Gradio arayüzü
|
| 61 |
+
demo = gr.Interface(
|
| 62 |
+
fn=predict_and_explain,
|
| 63 |
+
inputs=gr.Image(label="MRI Görüntüsü Yükle"),
|
| 64 |
+
outputs=[
|
| 65 |
+
gr.Label(num_top_classes=4, label="Tahmin"),
|
| 66 |
+
gr.Image(label="Grad-CAM Açıklaması")
|
| 67 |
+
],
|
| 68 |
+
title="🧠 Beyin Tümörü MRI Sınıflandırma",
|
| 69 |
+
description="""
|
| 70 |
+
**EfficientNetB3 + Grad-CAM ile Açıklanabilir AI**
|
| 71 |
+
|
| 72 |
+
Bu model beyin MRI görüntülerini 4 kategoride sınıflandırır:
|
| 73 |
+
- **Glioma** - Glial hücrelerden kaynaklanan tümör
|
| 74 |
+
- **Meningioma** - Meninks zarından kaynaklanan tümör
|
| 75 |
+
- **Pituitary** - Hipofiz bezi tümörü
|
| 76 |
+
- **No Tumor** - Tümör yok
|
| 77 |
+
|
| 78 |
+
Grad-CAM, modelin hangi bölgelere odaklandığını gösterir.
|
| 79 |
+
|
| 80 |
+
⚠️ *Bu araç sadece eğitim amaçlıdır, tıbbi teşhis için kullanılamaz.*
|
| 81 |
+
""",
|
| 82 |
+
examples=[], # Örnek görüntü ekleyebilirsin
|
| 83 |
+
theme=gr.themes.Soft()
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|
best_weights_balanced.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bc5f885e409ea0e1db1328870676557141420d4f4629f4f7b1a980177092507f
|
| 3 |
+
size 45567592
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
tensorflow==2.15.0
|
| 3 |
+
numpy
|
| 4 |
+
opencv-python-headless
|
| 5 |
+
Pillow
|