Skip to content

An AI-powered deep learning system using VGG16 transfer learning to classify brain tumors (glioma, meningioma, pituitary, no tumor) from MRI scans. Built with TensorFlow, deployed on Render with Flask.

Notifications You must be signed in to change notification settings

PasinduSuraweera/MRI-Brain-Tumor-Diagnosis-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

18 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  MRI Brain Tumor Diagnosis System

Brain Tumor Detection Banner Python TensorFlow Jupyter License

An AI-powered deep learning system for automated brain tumor detection and classification from MRI scans

Live Demo

Features โ€ข Tech Stack โ€ข Installation โ€ข Usage โ€ข Deployment


๐Ÿ“‹ Overview

This project harnesses the power of deep learning to tackle a critical medical challenge: accurately classifying brain tumors from MRI scans. Built using transfer learning with the renowned VGG16 architecture, it classifies tumors into four key categories: glioma, meningioma, pituitary, or no tumor.

Leveraging Python and TensorFlow, this system aims to deliver reliable, interpretable AI diagnostics that can support medical imaging workflows.

๐Ÿš€ Key Features

๐Ÿงฌ Transfer Learning with VGG16 Leveraging the pre-trained VGG16 convolutional base, freezing most layers to retain powerful learned features, while fine-tuning the top layers to specialize the model for brain tumor MRI classification.

๐Ÿ–ผ๏ธ Custom Image Augmentation To maximize model robustness despite a limited dataset, brightness and contrast adjustments are applied on-the-fly during training, improving the model's ability to generalize.

๐Ÿ”ข Dynamic Data Loading & Label Encoding Images are efficiently loaded from organized folder structures, shuffled, and labels encoded dynamically for seamless batch training.

โš™๏ธ Tailored Model Architecture The core model stacks the VGG16 base (excluding its classification head) with flattening, dropout layers, and fully connected dense layers topped with softmax for multi-class tumor prediction.

๐Ÿ“ˆ Thorough Training and Evaluation Training uses the Adam optimizer and sparse categorical cross-entropy loss. The system monitors accuracy and loss trends and validates model performance via detailed classification reports, confusion matrices, and ROC/AUC analyses.

๐Ÿ’พ Model Persistence & Practical Inference After training, the model is saved (model.h5) and can predict tumor type and confidence on new MRI images through an easy-to-use inference function.

๐Ÿงฐ Tech Stack & Libraries

Technology Purpose
TensorFlow Deep learning model construction, transfer learning, and training
Keras High-level neural networks API
Pillow Image preprocessing and augmentation
NumPy Numerical data manipulation
Matplotlib Visualization of training metrics
Seaborn Statistical data visualization
Scikit-learn Performance metrics and analytical tools
Flask Web application backend
Python Scripting and automation backbone

๐Ÿ—๏ธ Model Architecture

Input MRI Image (224x224x3)
         โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   VGG16 Base Model     โ”‚
โ”‚   (Pre-trained on      โ”‚
โ”‚    ImageNet)           โ”‚
โ”‚   - Conv Layers        โ”‚
โ”‚   - MaxPooling         โ”‚
โ”‚   - (Most layers frozen)โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Flatten Layer        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Dropout (0.5)        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Dense (256 units)    โ”‚
โ”‚   ReLU Activation      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Dropout (0.5)        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Dense (4 units)      โ”‚
โ”‚   Softmax Activation   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ†“
    Output Classes:
    - Glioma
    - Meningioma
    - Pituitary
    - No Tumor

๐Ÿ› ๏ธ Development Workflow

1๏ธโƒฃ Data Preparation

  • Loaded MRI images from well-structured training and testing folders
  • Shuffled datasets and encoded labels dynamically
  • Applied brightness and contrast augmentations in real-time during training to boost robustness

2๏ธโƒฃ Model Setup

  • Initialized VGG16 pretrained on ImageNet without its top layers
  • Froze most layers to preserve pretrained features; unfroze several top layers for targeted fine-tuning
  • Added custom classification layers with dropout and dense connections

3๏ธโƒฃ Training

  • Used a custom data generator for efficient batch processing
  • Trained the model over multiple epochs with the Adam optimizer
  • Tracked and visualized accuracy and loss metrics throughout the training cycle

4๏ธโƒฃ Evaluation

  • Assessed model performance with classification reports and confusion matrices on test data
  • Visualized results through heatmaps and ROC/AUC curves for detailed understanding

5๏ธโƒฃ Inference

  • Preprocessed new MRI images and ran predictions with confidence scoring
  • Presented results alongside input images for interpretability

๐Ÿ–ฅ๏ธ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager
  • Jupyter Notebook
  • (Optional) Virtual environment

Setup

  1. Clone the repository
git clone https://github.com/PasinduSuraweera/MRI-Brain-Tumor-Diagnosis-System.git
cd MRI-Brain-Tumor-Diagnosis-System
  1. Create a virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Launch Jupyter Notebook
jupyter notebook

๐Ÿ“ฆ Required Dependencies

Create a requirements.txt file with the following packages:

tensorflow>=2.10.0
keras>=2.10.0
numpy>=1.21.0
pandas>=1.3.0
matplotlib>=3.4.0
seaborn>=0.11.0
pillow>=8.3.0
scikit-learn>=0.24.0
opencv-python>=4.5.0
flask>=2.0.0
gunicorn>=20.1.0

๐Ÿš€ Usage

Training the Model

  1. Open the training notebook in Jupyter
  2. Organize your dataset in the following structure:
data/
โ”œโ”€โ”€ Training/
โ”‚   โ”œโ”€โ”€ glioma/
โ”‚   โ”œโ”€โ”€ meningioma/
โ”‚   โ”œโ”€โ”€ pituitary/
โ”‚   โ””โ”€โ”€ notumor/
โ””โ”€โ”€ Testing/
    โ”œโ”€โ”€ glioma/
    โ”œโ”€โ”€ meningioma/
    โ”œโ”€โ”€ pituitary/
    โ””โ”€โ”€ notumor/
  1. Run the cells sequentially to train the model
  2. Model checkpoints will be saved as model.h5

Making Predictions

In Jupyter Notebook:

# Load the trained model
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np

model = load_model('model.h5')

# Preprocess and predict
def predict_tumor(image_path):
    img = Image.open(image_path).resize((224, 224))
    img_array = np.array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    
    prediction = model.predict(img_array)
    classes = ['Glioma', 'Meningioma', 'No Tumor', 'Pituitary']
    
    predicted_class = classes[np.argmax(prediction)]
    confidence = np.max(prediction) * 100
    
    print(f"Prediction: {predicted_class}")
    print(f"Confidence: {confidence:.2f}%")
    
    return predicted_class, confidence

# Use the function
predict_tumor('path/to/mri_scan.jpg')

โ˜๏ธ Deployment

The system is deployed on Render to provide seamless web access. The backend is built using Flask, serving the model and handling image uploads smoothly.

๐ŸŒ Live Demo Explore the live application here:

Deploying Your Own Instance

  1. Create a Flask Application (app.py)
  2. Configure requirements.txt with all dependencies
  3. Create Procfile for Render:
web: gunicorn app:app
  1. Deploy to Render:
    • Connect your GitHub repository
    • Configure build settings
    • Deploy automatically on push

๐Ÿ“ Project Structure

MRI-Brain-Tumor-Diagnosis-System/
โ”‚
โ”œโ”€โ”€ notebooks/                   # Jupyter notebooks
โ”‚   โ”œโ”€โ”€ training.ipynb          # Model training pipeline
โ”‚   โ”œโ”€โ”€ evaluation.ipynb        # Model evaluation & metrics
โ”‚   โ””โ”€โ”€ inference.ipynb         # Prediction demonstrations
โ”‚
โ”œโ”€โ”€ data/                       # Dataset directory
โ”‚   โ”œโ”€โ”€ Training/               # Training images
โ”‚   โ”‚   โ”œโ”€โ”€ glioma/
โ”‚   โ”‚   โ”œโ”€โ”€ meningioma/
โ”‚   โ”‚   โ”œโ”€โ”€ pituitary/
โ”‚   โ”‚   โ””โ”€โ”€ notumor/
โ”‚   โ””โ”€โ”€ Testing/                # Testing images
โ”‚       โ”œโ”€โ”€ glioma/
โ”‚       โ”œโ”€โ”€ meningioma/
โ”‚       โ”œโ”€โ”€ pituitary/
โ”‚       โ””โ”€โ”€ notumor/
โ”‚
โ”œโ”€โ”€ models/                     # Saved models
โ”‚   โ””โ”€โ”€ model.h5               # Trained VGG16 model
โ”‚
โ”œโ”€โ”€ app.py                      # Flask web application
โ”œโ”€โ”€ requirements.txt            # Project dependencies
โ”œโ”€โ”€ Procfile                    # Render deployment config
โ””โ”€โ”€ README.md                   # Project documentation

๐Ÿ”ฌ Dataset

This project works with MRI brain scan datasets organized into four categories:

  • Glioma - A type of tumor that occurs in the brain and spinal cord
  • Meningioma - A tumor that arises from the meninges
  • Pituitary - A tumor in the pituitary gland
  • No Tumor - Healthy brain scans

Recommended Datasets:

Important: Ensure you have proper authorization and follow ethical guidelines when using medical data.

โš ๏ธ Important Disclaimer

This model is not 100% accurate and is trained on a relatively small dataset, which may impact its performance on new, unseen cases.

This system should be viewed as a supportive diagnostic aid rather than a definitive medical tool. It is designed for research and educational purposes and should NOT be used as a substitute for professional medical diagnosis.

Always consult qualified healthcare professionals for medical decisions.

๐ŸŽฏ Conclusion

This MRI Brain Tumor Diagnosis System showcases how transfer learning with VGG16 can be effectively applied to challenging medical imaging tasks, even when working with limited data. By combining strategic data augmentation, fine-tuning, and comprehensive evaluation, the system achieves promising tumor classification accuracy.

This project lays a solid foundation for future AI-driven diagnostic applications and underscores deep learning's potential to empower healthcare professionals with faster, more informed decision-making.

๐Ÿ”ฎ Future Directions

  • ๐Ÿ“ˆ Expanding the dataset with more diverse MRI scans
  • ๐Ÿฅ Including additional tumor classes and subtypes
  • ๐Ÿ”ฌ Implementing explainable AI techniques (Grad-CAM, attention maps)
  • ๐Ÿ—๏ธ Progressing toward clinical deployment and validation
  • โšก Exploring more advanced architectures (ResNet, EfficientNet, Vision Transformers)
  • ๐ŸŒ Multi-center validation studies

๐Ÿค Contributing

Contributions are welcome! Whether it's bug fixes, feature additions, or documentation improvements, your help is appreciated.

How to Contribute:

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ‘ค Author

Pasindu Suraweera

๐Ÿ™ Acknowledgments

  • ๐Ÿฅ Medical imaging research community
  • ๐Ÿค– TensorFlow and Keras development teams
  • ๐ŸŽ“ ImageNet and VGG16 researchers
  • ๐Ÿ’ก Open-source contributors
  • ๐Ÿ‘จโ€โš•๏ธ Healthcare professionals providing guidance and feedback

๐Ÿ“ง Contact

For questions, feedback, or collaboration opportunities, please open an issue in the repository or reach out through GitHub.


โญ If you find this project helpful, please consider giving it a star! โญ

Made for advancing healthcare AI

Visitors

Building the future of medical diagnostics, one model at a time ๐Ÿš€

About

An AI-powered deep learning system using VGG16 transfer learning to classify brain tumors (glioma, meningioma, pituitary, no tumor) from MRI scans. Built with TensorFlow, deployed on Render with Flask.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages