A production-ready deep learning system for classifying brain tumors from MRI scans.
EfficientNetB2 transfer learning Β· Grad-CAM explainability Β· FastAPI backend Β· Gradio frontend
| MRI Input | Grad-CAM | Prediction |
|---|---|---|
![]() |
![]() |
Glioma β 97.3% β‘ 18 ms |
π Live Demo: HuggingFace Spaces
ββββββββββββββββββββ 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 β
ββββββββββββββββββββββββββββββββ
- 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
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
git clone https://github.com/AImindcrafter/mri-brain-tumor-classifier.git
cd mri-brain-tumor-classifier
pip install -r requirements.txtmkdir -p models
# Copy your trained model file
cp /path/to/EfficientNetB2_brain_tumor_model.keras .β Already included β
EfficientNetB2_brain_tumor_model.kerasis pre-trained and ready to use.
uvicorn backend.main:app --host 0.0.0.0 --port 8000 --reloadβ API docs: http://localhost:8000/docs
python frontend/app.pyβ UI: http://localhost:7860
{ "status": "ok", "model_loaded": true }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"pytest tests/ -vSee notebooks/train.ipynb or run:
python scripts/train.pyDataset: Brain MRI Images β Kaggle
| Split | Images |
|---|---|
| Train | 5,712 |
| Test | 1,311 |
| Total | 7,023 |
| 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
| 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 |
Muhammad Zeeshan Malik β Generative AI Engineer
MIT License β see LICENSE for details.

