Skip to content

๐Ÿง  Deep learning project for brain tumor classification using MRI images. Built with transfer learning (VGG16 + fine-tuning), TensorFlow/Keras, and deployed via Streamlit. Dataset & model loaded dynamically from KaggleHub. Includes training notebook, evaluation, and interactive web app.

Notifications You must be signed in to change notification settings

Imswappy/Brain-Tumor-Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  NeuroVision: Brain Tumor MRI Classification with Deep Learning

Streamlit Keras TensorFlow Python


๐Ÿ“Œ Project Overview

This project focuses on automated brain tumor detection using MRI images.
We apply transfer learning with VGG16, a convolutional neural network (CNN) pre-trained on the ImageNet dataset, and fine-tune it to classify brain MRIs into:

  • Meningioma
  • Glioma
  • Pituitary Tumor
  • No Tumor

The trained model is deployed via a Streamlit app, where users can upload MRI images and get predictions with confidence scores.


๐Ÿ“‚ Project Structure

brain-tumor-mri/
โ”‚
โ”œโ”€โ”€ brain_tumour_detection.ipynb   # Training & evaluation (KaggleHub dataset)
โ”œโ”€โ”€ streamlit_app.py                       # Deployment UI with Streamlit
โ”œโ”€โ”€ requirements.txt                       # Dependencies
โ””โ”€โ”€ README.md                              # Documentation

๐Ÿ“Š Dataset

Dataset is fetched from KaggleHub:

import kagglehub
path = kagglehub.dataset_download("masoudnickparvar/brain-tumor-mri-dataset")

Structure:

Training/
   โ”œโ”€โ”€ glioma/
   โ”œโ”€โ”€ meningioma/
   โ”œโ”€โ”€ pituitary/
   โ””โ”€โ”€ notumor/
Testing/
   โ”œโ”€โ”€ glioma/
   โ”œโ”€โ”€ meningioma/
   โ”œโ”€โ”€ pituitary/
   โ””โ”€โ”€ notumor/
  • Training samples: ~2870
  • Testing samples: ~394
  • Each subdirectory corresponds to a tumor class.

๐Ÿ—๏ธ Model Architecture (Transfer Learning with VGG16)

We use VGG16, a pre-trained CNN on ImageNet, as a feature extractor and fine-tune its top layers.

๐Ÿ”น Step 1: Base Model

from tensorflow.keras.applications import VGG16

base_model = VGG16(
    input_shape=(128,128,3),
    include_top=False,
    weights='imagenet'
)
  • Input size: 128 ร— 128 ร— 3 (resized MRI images).
  • include_top=False: removes VGG16โ€™s fully connected (FC) head.
  • weights="imagenet": initializes weights from ImageNet (~1.4M images, 1000 classes).

Mathematically, each convolutional layer applies: image

where:

  • W^{(k)}: convolutional kernel for feature map k
  • x: input patch
  • ฯƒ: activation (ReLU in VGG16)

๐Ÿ”น Step 2: Freezing and Fine-Tuning

for layer in base_model.layers:
    layer.trainable = False

# Unfreeze top 3 layers
base_model.layers[-2].trainable = True
base_model.layers[-3].trainable = True
base_model.layers[-4].trainable = True
  • Lower layers retain general features (edges, textures).
  • Top 3 layers fine-tuned to capture domain-specific features of MRI tumors.

๐Ÿ”น Step 3: Custom Classification Head

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dropout, Dense

model = Sequential([
    base_model,
    Flatten(),
    Dropout(0.3),
    Dense(128, activation='relu'),
    Dropout(0.2),
    Dense(len(unique_labels), activation='softmax')
])
  • Flatten: Reshapes VGG16 feature maps (4 ร— 4 ร— 512) โ†’ (8192,).
  • Dense(128, relu): Fully connected layer learns non-linear combinations of features.
image
  • Dropout(0.3, 0.2): Randomly zeroes units during training, reducing overfitting.
  • Output Layer: Softmax classifier for 4 classes:
image

๐Ÿ“’ Notebook (brain_tumour_detection_vs_code.ipynb)

Sections:

  1. Dataset loading with KaggleHub
  2. EDA: Class distributions, sample MRI visualization image
  3. Preprocessing: Resize, normalize, split
  4. Model: VGG16 base + custom classifier
  5. Training: Adam optimizer, categorical crossentropy loss
image image
  1. Evaluation: Accuracy, confusion matrix, classification report image image

  2. Saving model in .h5 format


๐Ÿงฎ Training Statistics

  • Optimizer: Adam (ฮฒโ‚=0.9, ฮฒโ‚‚=0.999)
  • Learning Rate: 1e-4
  • Batch Size: 32
  • Loss: Categorical Crossentropy
  • Regularization: Dropout (0.3 & 0.2)

Performance:

  • Training Accuracy: ~95%
  • Test Accuracy: ~92%
  • Balanced F1 scores across all classes

๐ŸŽจ Streamlit App (streamlit_app.py)

Interactive deployment UI:

  • Upload MRI image (png/jpg/jpeg)
  • Preprocessing: resize 128 ร— 128, normalize [0,1]
  • Prediction from VGG16-based model
  • Displays:
    • Label
    • Confidence
    • Uploaded image
    • Raw probabilities

Run:

streamlit run streamlit_app.py

โš™๏ธ Installation (VS Code)

# 1. Create environment
python -m venv .venv
.venv\Scripts\activate   # Windows
source .venv/bin/activate  # Mac/Linux

# 2. Install deps
pip install -r requirements.txt

# 3. Run app
streamlit run streamlit_app.py

๐Ÿš€ Deployment


๐Ÿ™Œ Acknowledgements


About

๐Ÿง  Deep learning project for brain tumor classification using MRI images. Built with transfer learning (VGG16 + fine-tuning), TensorFlow/Keras, and deployed via Streamlit. Dataset & model loaded dynamically from KaggleHub. Includes training notebook, evaluation, and interactive web app.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published