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.
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 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.
We use VGG16, a pre-trained CNN on ImageNet, as a feature extractor and fine-tune its top layers.
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:

where:
- W^{(k)}: convolutional kernel for feature map k
- x: input patch
- ฯ: activation (ReLU in VGG16)
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.
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.
- Dropout(0.3, 0.2): Randomly zeroes units during training, reducing overfitting.
- Output Layer: Softmax classifier for 4 classes:
Sections:
- Dataset loading with KaggleHub
- EDA: Class distributions, sample MRI visualization

- Preprocessing: Resize, normalize, split
- Model: VGG16 base + custom classifier
- Training: Adam optimizer, categorical crossentropy loss
- 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
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# 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- Local: http://localhost:8501
- LAN: http://:8501
- Global: deploy on Streamlit Cloud

- Dataset: Brain Tumor MRI Dataset
- Pre-trained Model: MRI Brain Tumor Model
- Base Architecture: VGG16

