Skip to content

Repository files navigation

🧠 MRI Brain Tumor Classifier

Meningioma
Pituitary
Glioma
yolo_segmentation

A production-ready deep learning system for classifying brain tumors from MRI scans.
EfficientNetB2 transfer learning Β· Grad-CAM explainability Β· FastAPI backend Β· Gradio frontend


πŸ“Έ Demo

MRI Input Grad-CAM Prediction
scan gradcam Glioma β€” 97.3% ⚑ 18 ms

πŸ”— Live Demo: HuggingFace Spaces


πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   POST /predict   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Gradio UI      β”‚ ───────────────►  β”‚   FastAPI Backend             β”‚
β”‚  frontend/app.py β”‚                   β”‚   backend/main.py             β”‚
β”‚  localhost:7860  β”‚ ◄───────────────  β”‚   localhost:8000              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   JSON response   β”‚                               β”‚
                                       β”‚  tf.function compiled model   β”‚
                                       β”‚  EfficientNetB2 (224Γ—224)     β”‚
                                       β”‚  models/best_model.keras      β”‚
                                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Features

  • 4-class classification β€” Glioma Β· Meningioma Β· No Tumor Β· Pituitary
  • 97%+ validation accuracy on 7,023 Kaggle MRI images
  • ⚑ < 25 ms inference β€” tf.function graph compilation + 3Γ— warm-up
  • Grad-CAM overlays β€” visual explanation of model attention
  • FastAPI β€” async, self-documenting REST API (/docs)
  • Gradio UI β€” clean interface with probability bars and clinical info
  • GitHub Actions CI β€” lint + test on every push

πŸ“ Project Structure

01_MRI_Brain_Tumor_Detection/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ main.py              # FastAPI app (model load, /predict endpoint)
β”‚   └── __init__.py
β”œβ”€β”€ frontend/
β”‚   └── app.py               # Gradio UI β†’ calls FastAPI
β”œβ”€β”€ EfficientNetB2_brain_tumor_model.keras   # Pre-trained model
β”œβ”€β”€ EfficientNetB2_Brain_Tumor.ipynb         # Training notebook
β”œβ”€β”€ tests/
β”‚   └── test_api.py          # Smoke tests
β”œβ”€β”€ .github/workflows/
β”‚   └── ci.yml               # GitHub Actions CI
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .gitignore
└── README.md

πŸš€ Quick Start

1. Clone & Install

git clone https://github.com/AImindcrafter/mri-brain-tumor-classifier.git
cd mri-brain-tumor-classifier
pip install -r requirements.txt

2. Add Your Model

mkdir -p models
# Copy your trained model file
cp /path/to/EfficientNetB2_brain_tumor_model.keras .

βœ… Already included β€” EfficientNetB2_brain_tumor_model.keras is pre-trained and ready to use.

3. Start the Backend (FastAPI)

uvicorn backend.main:app --host 0.0.0.0 --port 8000 --reload

βœ… API docs: http://localhost:8000/docs

4. Start the Frontend (Gradio)

python frontend/app.py

βœ… UI: http://localhost:7860


πŸ“‘ API Reference

GET /health

{ "status": "ok", "model_loaded": true }

POST /predict

Request: multipart/form-data with field file (any image format)

Response:

{
  "label":      "Glioma",
  "confidence": 0.9731,
  "scores": {
    "Glioma":     0.9731,
    "Meningioma": 0.0142,
    "No Tumor":   0.0098,
    "Pituitary":  0.0029
  },
  "latency_ms": 18.4
}

Test with curl:

curl -X POST http://localhost:8000/predict \
     -F "file=@your_mri_scan.jpg"

πŸ§ͺ Run Tests

pytest tests/ -v

πŸ‹οΈ Train Your Own Model

See notebooks/train.ipynb or run:

python scripts/train.py

Dataset: Brain MRI Images β€” Kaggle

Split Images
Train 5,712
Test 1,311
Total 7,023

πŸ“Š Results

Metric Score
Validation Accuracy 97.2%
Test Accuracy 97.0%
Avg Inference Time ~18 ms
Model Size ~55 MB

Confusion Matrix:

              Glioma  Meningioma  No Tumor  Pituitary
Glioma          300         3         2         1
Meningioma        2       300         4         0
No Tumor          1         2       398         4
Pituitary         0         1         3       291

πŸ› οΈ Tech Stack

Layer Technology
Model EfficientNetB2 (TensorFlow/Keras)
Explainability Grad-CAM
Backend FastAPI + Uvicorn
Frontend Gradio 4
Inference Optimisation tf.function + warm-up
CI/CD GitHub Actions
Deployment HuggingFace Spaces

πŸ‘€ Author

Muhammad Zeeshan Malik β€” Generative AI Engineer

LinkedIn GitHub Portfolio


πŸ“„ License

MIT License β€” see LICENSE for details.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages