Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🧠 Brain Tumor Detection — End to End

Python PyTorch Flask ResNet50 Accuracy License

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.


⚠️ Medical Disclaimer

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.


📌 Table of Contents


🔬 About the Project

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

⚙️ How It Works

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

📊 Dataset

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 Distribution

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

.mat File Structure

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

Data Augmentation

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

🏗️ Model Architecture

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%

📈 Model Performance

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.


📁 Project Structure

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

🚀 Getting Started

1. Clone the repository

git clone https://github.com/shsarv/Machine-Learning-Projects.git
cd "Machine-Learning-Projects/BRAIN TUMOR DETECTION [END 2 END]"

2. Set up environment

python -m venv venv
source venv/bin/activate        # Linux / macOS
venv\Scripts\activate           # Windows

pip install -r requirements.txt

3. Run the application

Option 1: Automatic Launcher (Recommended)

python launcher.py

This automatically detects and uses the correct Python environment.

Option 2: Virtual Environment

# Activate virtual environment
venv\Scripts\activate

# Run the app
python app.py

Option 3: Global Python

# Install dependencies globally
pip install -r requirements.txt

# Run the app
python app.py

Option 4: Batch File (Windows)

run.bat

The app will be available at: http://127.0.0.1:5000

4. Download & prepare the dataset

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_set4

Then run the dataset preparation notebook to convert .mat.jpg and generate labels:

jupyter notebook notebooks/brain_tumor_dataset_preparation.ipynb

4. Train the model (optional — pretrained weights included)

jupyter notebook notebooks/torch_brain_tumor_classifier.ipynb

5. Run the Flask app

python app.py

Navigate to → http://127.0.0.1:5000 and upload an MRI scan.

6. Quick CLI prediction

python test.py --image path/to/mri_scan.jpg

🖥️ App Preview

┌──────────────────────────────────────────────────┐
│           🧠 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.     │
└──────────────────────────────────────────────────┘

IMAGE OF THIS PROJECT -

image image image

🛠️ Tech Stack

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

📚 References & Citation

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:


⭐ Star the main repo if this helped you!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages