A full end-to-end deep learning web application that classifies brain tumors in MRI scans into three tumor types using a fine-tuned ResNet50 via Transfer Learning — achieving 99.3% accuracy — deployed as a live Flask web app.
This tool is for educational and research purposes only. It is not a substitute for professional medical diagnosis. Always consult a qualified radiologist or medical professional for clinical decisions.
- About the Project
- How It Works
- Dataset
- Model Architecture
- Model Performance
- Project Structure
- Getting Started
- App Preview
- Tech Stack
- References & Citation
Brain tumors are among the most critical conditions in medicine — accurate classification directly guides treatment decisions (surgery, radiation, chemotherapy). This project demonstrates how Transfer Learning with a pre-trained ResNet50 can achieve near-perfect classification accuracy on MRI scans, far outperforming a CNN trained from scratch.
The model is trained on the Jun Cheng Figshare brain tumor dataset (3,064 T1-weighted CE-MRI images from 233 patients) and deployed as a Flask web application where users can upload an MRI image and receive a real-time classification with confidence score.
What this project covers:
- Converting
.mat(MATLAB) MRI files to images and extracting tumor masks/borders - Data augmentation with custom real-time transformations
- Fine-tuning ResNet50 with Transfer Learning using PyTorch
- Model evaluation with accuracy, loss curves, and per-class metrics
- Serving predictions via a Flask web app
User Uploads MRI Scan (.jpg / .png)
│
▼
Image Preprocessing
(Resize 224×224 → Normalize → Tensor)
│
▼
Fine-tuned ResNet50 Forward Pass
(ImageNet weights → custom classifier head)
│
▼
3-Class Softmax Output
┌───────────┬─────────────┬────────────┐
│ Glioma │ Meningioma │ Pituitary │
└───────────┴─────────────┴────────────┘
│
▼
Predicted Class + Confidence Score
Displayed in Browser
| Property | Details |
|---|---|
| Name | Brain Tumor Dataset |
| Author | Jun Cheng |
| Source | Figshare — DOI: 10.6084/m9.figshare.1512427 |
| Total Images | 3,064 T1-weighted CE-MRI scans |
| Patients | 233 |
| Format | .mat (MATLAB) → converted to .jpg |
| Task | Multi-class classification (3 tumor types) |
| Class | Description | Slices |
|---|---|---|
| 🔴 Glioma | Arises from glial cells; most common & aggressive brain tumor | 1,426 |
| 🟡 Meningioma | Grows on membranes surrounding the brain; often benign | 708 |
| 🟢 Pituitary | Forms on the pituitary gland at the brain's base; usually slow-growing | 930 |
| Total | 3,064 |
Each .mat file contains the following fields:
| Field | Description |
|---|---|
cjdata.image |
The MRI scan image matrix |
cjdata.label |
Tumor type label (1=Meningioma, 2=Glioma, 3=Pituitary) |
cjdata.tumorBorder |
Coordinates of discrete points on the tumor border [x1,y1,x2,y2,...] |
cjdata.tumorMask |
Binary image with 1s marking the tumor region |
Custom real-time augmentations applied during training:
| Technique | Purpose |
|---|---|
| Horizontal & Vertical Flip | Positional variance |
| Random Rotation (±15°) | Scan orientation variance |
| Brightness / Contrast Jitter | Scanner setting variance |
| Random Crop / Zoom | Variable tumor scale |
| Normalization (ImageNet μ/σ) | Stable gradient flow |
Rather than training a CNN from scratch, this project applies Transfer Learning by fine-tuning a ResNet50 pretrained on ImageNet.
Input MRI Image (224 × 224 × 3)
│
▼
┌──────────────────────────────────────┐
│ ResNet50 Backbone │
│ (Pretrained on ImageNet) │
│ │
│ Conv1 → BN → ReLU → MaxPool │
│ Layer1: 3× Bottleneck blocks │
│ Layer2: 4× Bottleneck blocks │
│ Layer3: 6× Bottleneck blocks │
│ Layer4: 3× Bottleneck blocks │
│ Adaptive AvgPool → 2048-dim vector │
└──────────────────────────────────────┘
│
▼
┌──────────────────────────────────────┐
│ Custom Classifier Head │
│ FC (2048 → 512) + ReLU │
│ Dropout (0.5) │
│ FC (512 → 3) │
│ Softmax │
└──────────────────────────────────────┘
│
▼
Glioma / Meningioma / Pituitary
Training configuration:
| Parameter | Value |
|---|---|
| Base Model | ResNet50 (ImageNet pretrained) |
| Strategy | Fine-tune full network after warm-up |
| Optimizer | Adam |
| Learning Rate | 0.001 (backbone), 0.01 (head) |
| LR Scheduler | StepLR (decay every 7 epochs) |
| Loss Function | Cross-Entropy Loss |
| Epochs | 25 |
| Batch Size | 32 |
| Train / Val / Test Split | 70% / 15% / 15% |
| Metric | Score |
|---|---|
| Overall Accuracy | ~99.3% |
| Glioma F1 | ~99% |
| Meningioma F1 | ~98% |
| Pituitary F1 | ~99% |
Why Transfer Learning? ResNet50 pre-trained on ImageNet already understands low-level features (edges, textures, shapes) that transfer well to MRI images. Fine-tuning requires far less data and training time while achieving significantly higher accuracy than training from scratch.
Meningioma shows slightly lower scores due to class imbalance (708 vs 1,426 glioma images) and its high visual similarity to surrounding tissue.
BRAIN TUMOR DETECTION [END 2 END]/
│
├── 📂 Dataset/
│ ├── 📂 bt_images/ # Converted MRI images (.jpg)
│ ├── 📂 bt_mask/ # Tumor mask images
│ └── 📂 new_dataset/ # Processed dataset with labels
│
├── 📂 Model/
│ └── brain_tumor_model.pt # Saved ResNet50 fine-tuned weights
│
├── 📂 notebooks/
│ ├── brain_tumor_dataset_preparation.ipynb # .mat → .jpg conversion & preprocessing
│ └── torch_brain_tumor_classifier.ipynb # Training, evaluation & results
│
├── 📂 static/
│ ├── 📂 css/style.css # App styling
│ └── 📂 uploads/ # Temporarily stores uploaded MRI scans
│
├── 📂 templates/
│ ├── index.html # Upload page
│ └── result.html # Prediction result page
│
├── app.py # Flask application entry point
├── test.py # CLI script: classify a single image by path
├── requirements.txt # Python dependencies
└── README.md # You are here
git clone https://github.com/shsarv/Machine-Learning-Projects.git
cd "Machine-Learning-Projects/BRAIN TUMOR DETECTION [END 2 END]"python -m venv venv
source venv/bin/activate # Linux / macOS
venv\Scripts\activate # Windows
pip install -r requirements.txtpython launcher.pyThis automatically detects and uses the correct Python environment.
# Activate virtual environment
venv\Scripts\activate
# Run the app
python app.py# Install dependencies globally
pip install -r requirements.txt
# Run the app
python app.pyrun.batThe app will be available at: http://127.0.0.1:5000
The raw dataset is in .mat (MATLAB) format, split across 4 zip files on Figshare:
# Download all 4 parts from Figshare
# https://figshare.com/articles/dataset/brain_tumor_dataset/1512427
# After downloading, extract each zip:
unzip brainTumorDataPublic_1-766.zip -d dataset/bt_set1
unzip brainTumorDataPublic_767-1532.zip -d dataset/bt_set2
unzip brainTumorDataPublic_1533-2298.zip -d dataset/bt_set3
unzip brainTumorDataPublic_2299-3064.zip -d dataset/bt_set4Then run the dataset preparation notebook to convert .mat → .jpg and generate labels:
jupyter notebook notebooks/brain_tumor_dataset_preparation.ipynbjupyter notebook notebooks/torch_brain_tumor_classifier.ipynbpython app.pyNavigate to → http://127.0.0.1:5000 and upload an MRI scan.
python test.py --image path/to/mri_scan.jpg┌──────────────────────────────────────────────────┐
│ 🧠 Brain Tumor Classifier │
│ │
│ Upload a T1-weighted CE-MRI scan: │
│ ┌────────────────────────────────────┐ │
│ │ [ Choose File ] mri_scan.jpg │ │
│ └────────────────────────────────────┘ │
│ │
│ [ Analyze Scan ] │
│ │
│ ────────────────────────────────────────────── │
│ │
│ Result: 🔴 GLIOMA │
│ Confidence: 98.7% │
│ │
│ ⚠️ Please consult a medical professional. │
└──────────────────────────────────────────────────┘
| Layer | Technology |
|---|---|
| Language | Python 3.7+ |
| Deep Learning | PyTorch, Torchvision |
| Model | ResNet50 (Transfer Learning) |
| Image Processing | OpenCV, PIL (Pillow) |
| Data / EDA | Pandas, NumPy, Matplotlib, Seaborn, h5py |
| Web Framework | Flask |
| Frontend | HTML5, CSS3, Bootstrap |
| Model Serialization | torch.save / .pt |
| Notebook | Jupyter / Google Colab |
Dataset — please cite if you use this work:
@article{Cheng2015,
author = {Cheng, Jun and others},
title = {Enhanced Performance of Brain Tumor Classification via Tumor Region Augmentation and Partition},
journal = {PLoS ONE},
volume = {10},
number = {10},
year = {2015}
}
@article{Cheng2016,
author = {Cheng, Jun and others},
title = {Retrieval of Brain Tumors by Adaptive Spatial Pooling and Fisher Vector Representation},
journal = {PLoS ONE},
volume = {11},
number = {6},
year = {2016}
}Further reading:
- Jun Cheng Brain Tumor Dataset — Figshare
- Deep Residual Learning for Image Recognition — He et al. (2015)
- A survey on deep learning in medical image analysis — Litjens et al. (2017)
- PyTorch Transfer Learning Tutorial
⭐ Star the main repo if this helped you!