Skip to content

purushottambende/Brain_Tumor_Classification_and_Segmentation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  Brain Tumor Classification & Segmentation Web Application

A comprehensive deep learning-based web application for automated brain tumor detection, classification, and segmentation from MRI scans. This project leverages convolutional neural networks (CNN) and U-Net architecture to assist in medical diagnosis.

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

This web application provides an end-to-end solution for brain tumor analysis using MRI images. It combines two powerful deep learning models:

  1. Classification Model: Identifies the type of brain tumor (Glioma, Meningioma, Pituitary, or No Tumor)
  2. Segmentation Model: Precisely delineates tumor boundaries within the MRI scan

The system is designed to assist medical professionals in making faster and more accurate diagnoses by providing both tumor classification and visual segmentation results.

โœจ Features

  • ๐Ÿ” Multi-class Tumor Classification: Classifies brain MRI scans into four categories:

    • Glioma
    • Meningioma
    • Pituitary Tumor
    • No Tumor
  • ๐ŸŽจ Tumor Segmentation: Provides pixel-level segmentation of tumor regions using U-Net architecture

  • ๐Ÿ‘ค Patient Information Management: Captures and displays patient demographics (name, age, gender)

  • ๐Ÿ“Š Visual Results: Side-by-side display of original MRI and segmented output

  • ๐ŸŒ Web-Based Interface: User-friendly Flask web application accessible via browser

  • โšก Real-time Prediction: Fast inference on uploaded MRI images

๐Ÿ“ Project Structure

brain-tumor-webapp/
โ”‚
โ”œโ”€โ”€ app.py                          # Main Flask application
โ”œโ”€โ”€ readme.md                       # Project documentation
โ”‚
โ”œโ”€โ”€ models/                         # Trained model files and prediction scripts
โ”‚   โ”œโ”€โ”€ brain_tumor_segmentation.h5 # U-Net segmentation model
โ”‚   โ”œโ”€โ”€ Model m2.h5                 # CNN classification model
โ”‚   โ””โ”€โ”€ predict.py                  # Prediction functions (inference logic)
โ”‚
โ”œโ”€โ”€ static/                         # Static files
โ”‚   โ”œโ”€โ”€ uploads/                    # Uploaded MRI images
โ”‚   โ””โ”€โ”€ results/                    # Segmentation results
โ”‚
โ”œโ”€โ”€ templates/                      # HTML templates
โ”‚   โ””โ”€โ”€ index.html                  # Main web interface
โ”‚
โ””โ”€โ”€ training_scripts/               # Model training notebooks
    โ”œโ”€โ”€ Brain_Tumor_Classification.ipynb  # Classification model training
    โ””โ”€โ”€ Tumor_Segmentation.ipynb         # Segmentation model training

๐Ÿ› ๏ธ Technologies Used

Backend

  • Flask: Web framework for Python
  • TensorFlow/Keras: Deep learning framework for model training and inference
  • NumPy: Numerical computing
  • OpenCV: Image processing
  • Pillow: Image manipulation

Frontend

  • HTML5: Structure
  • Bootstrap 5.3.2: Responsive UI design
  • CSS: Styling

Machine Learning

  • Convolutional Neural Networks (CNN): For tumor classification
  • U-Net Architecture: For tumor segmentation
  • Image Data Generators: For data augmentation during training

๐Ÿงฌ Models

1. Classification Model (Model m2.h5)

Architecture:

  • Custom CNN with multiple convolutional blocks
  • Architecture layers:
    • Conv2D (64 filters, 3x3 kernel) โ†’ ReLU
    • Conv2D (64 filters, 3x3 kernel) โ†’ ReLU
    • MaxPooling2D (2x2)
    • Conv2D (128 filters, 3x3 kernel) โ†’ ReLU
    • Conv2D (128 filters, 3x3 kernel) โ†’ ReLU
    • MaxPooling2D (2x2)
    • Conv2D (256 filters, 3x3 kernel) โ†’ ReLU
    • Conv2D (256 filters, 3x3 kernel) โ†’ ReLU
    • MaxPooling2D (2x2)
    • Conv2D (512 filters, 3x3 kernel) โ†’ ReLU
    • Conv2D (512 filters, 3x3 kernel) โ†’ ReLU
    • MaxPooling2D (2x2)
    • Flatten
    • Dense (256 units) โ†’ ReLU โ†’ Dropout
    • Dense (64 units) โ†’ ReLU โ†’ Dropout
    • Dense (4 units) โ†’ Softmax

Training Details:

  • Optimizer: Adamax (learning rate: 0.001)
  • Loss Function: Categorical Crossentropy
  • Metrics: Accuracy
  • Output Classes: 4 (Glioma, Meningioma, No Tumor, Pituitary)

2. Segmentation Model (brain_tumor_segmentation.h5)

Architecture:

  • U-Net based architecture with encoder-decoder structure
  • Image Size: 256ร—256 pixels
  • Architecture components:
    • Encoder:
      • Multiple convolutional blocks with downsampling
      • Captures contextual information at different scales
    • Decoder:
      • Upsampling layers with skip connections
      • Reconstructs spatial resolution
    • Skip Connections:
      • Connects encoder and decoder layers
      • Preserves fine-grained details

Training Details:

  • Optimizer: Adam
  • Loss Function: Binary Crossentropy
  • Metrics: Accuracy
  • Input/Output Size: 256ร—256ร—3 โ†’ 256ร—256ร—1

๐Ÿ“ฆ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager
  • GPU (optional, but recommended for faster inference)

Step 1: Clone the Repository

git clone <repository-url>
cd brain-tumor-webapp

Step 2: Create Virtual Environment (Recommended)

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

Step 3: Install Dependencies

pip install flask
pip install tensorflow
pip install keras
pip install numpy
pip install opencv-python
pip install pillow
pip install werkzeug

Or create a requirements.txt file:

flask==3.0.0
tensorflow==2.15.0
keras==3.0.0
numpy==1.26.0
opencv-python==4.9.0
Pillow==10.2.0
werkzeug==3.0.1

Then install:

pip install -r requirements.txt

Step 4: Create Missing Module

โš ๏ธ Important: The models/predict.py file is missing. You need to create it with the following content:

# models/predict.py
import tensorflow as tf
import numpy as np
import cv2
from PIL import Image
import os

# Load models
classification_model = tf.keras.models.load_model('models/Model m2.h5')
segmentation_model = tf.keras.models.load_model('models/brain_tumor_segmentation.h5')

# Class names for classification
class_names = ['glioma', 'meningioma', 'notumor', 'pituitary']

def classify_tumor(image_path):
    """
    Classify the type of brain tumor from MRI image
    
    Args:
        image_path: Path to the MRI image
        
    Returns:
        tumor_type: String indicating the tumor type
    """
    # Load and preprocess image
    img = Image.open(image_path)
    img = img.resize((224, 224))  # Adjust size based on your model
    img_array = np.array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    
    # Predict
    predictions = classification_model.predict(img_array)
    predicted_class = np.argmax(predictions, axis=1)[0]
    
    return class_names[predicted_class]

def segment_tumor(image_path):
    """
    Segment tumor region from MRI image
    
    Args:
        image_path: Path to the MRI image
        
    Returns:
        segmented_path: Path to the saved segmented image
    """
    # Load and preprocess image
    img = cv2.imread(image_path)
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img_resized = cv2.resize(img_rgb, (256, 256))
    img_normalized = img_resized / 255.0
    img_input = np.expand_dims(img_normalized, axis=0)
    
    # Predict segmentation mask
    prediction = segmentation_model.predict(img_input)[0]
    mask = (prediction > 0.5).astype(np.uint8) * 255
    
    # Apply mask to original image
    mask_3channel = cv2.merge([mask[:,:,0], mask[:,:,0], mask[:,:,0]])
    segmented = cv2.bitwise_and(img_resized, mask_3channel)
    
    # Save segmented image
    filename = os.path.basename(image_path)
    save_dir = os.path.dirname(image_path)
    segmented_filename = f"segmented_{filename}"
    segmented_path = os.path.join(save_dir, segmented_filename)
    
    cv2.imwrite(segmented_path, cv2.cvtColor(segmented, cv2.COLOR_RGB2BGR))
    
    return segmented_path

๐Ÿš€ Usage

Running the Application

  1. Start the Flask server:
python app.py
  1. Open your browser and navigate to:
http://localhost:5000
  1. Using the Application:
    • Enter patient information (name, age, gender)
    • Upload an MRI brain scan image (JPEG, PNG formats supported)
    • Click "Analyze" button
    • View results:
      • Original MRI image
      • Predicted tumor type
      • Segmented tumor visualization

API Endpoints

  • GET /: Displays the main web interface
  • POST /: Handles image upload and returns prediction results

๐Ÿ“Š Dataset

The models were trained on a comprehensive brain tumor MRI dataset containing:

  • Total Images: 5,732+ MRI scans
  • Classes:
    • Glioma
    • Meningioma
    • Pituitary Tumor
    • No Tumor

Data Split:

  • Training Set: ~70%
  • Validation Set: ~15%
  • Test Set: ~15%

Data Source: The dataset was sourced from publicly available medical imaging repositories, including Kaggle and medical imaging databases.

Data Augmentation

Training utilized data augmentation techniques:

  • Rotation
  • Width/Height shifts
  • Zoom
  • Horizontal flip
  • Brightness adjustment

๐Ÿ“ˆ Model Performance

Classification Model

  • Training Accuracy: ~95%+
  • Validation Accuracy: ~93%+
  • Test Accuracy: ~92%+

Segmentation Model

  • Training Accuracy: ~96%+
  • Loss: Binary Crossentropy
  • Epochs: 20-50 (varies based on training configuration)

Note: Exact metrics can be found in the training notebooks in the training_scripts/ directory.

๐Ÿ“ธ Screenshots

Web Interface

The application features a clean, Bootstrap-based interface with:

  • Patient information form
  • File upload button
  • Result display area with original and segmented images
  • Tumor classification label

๐Ÿ”ฎ Future Enhancements

  • Add confidence scores for predictions
  • Support for DICOM format medical images
  • Batch processing of multiple MRI scans
  • 3D visualization of tumor regions
  • Integration with hospital information systems (HIS)
  • Export reports in PDF format
  • User authentication and patient record management
  • Support for additional tumor types
  • Model explainability (Grad-CAM visualization)
  • REST API for integration with other systems
  • Mobile application version
  • Real-time streaming analysis

๐Ÿ”ง Troubleshooting

Common Issues

Issue 1: Model file not found

Solution: Ensure model files are in the models/ directory:
- brain_tumor_segmentation.h5
- Model m2.h5

Issue 2: Import errors

Solution: Install all required dependencies:
pip install -r requirements.txt

Issue 3: Prediction errors

Solution: Check that predict.py exists in models/ directory
Verify image format is supported (JPEG, PNG)

Issue 4: Upload folder errors

Solution: Create static/uploads/ directory if it doesn't exist
Ensure write permissions are set correctly

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/improvement)
  3. Make your changes
  4. Commit your changes (git commit -am 'Add new feature')
  5. Push to the branch (git push origin feature/improvement)
  6. Create a Pull Request

๐Ÿ“„ License

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

โš ๏ธ Disclaimer

This application is designed for educational and research purposes only. It should NOT be used as a replacement for professional medical diagnosis. Always consult qualified healthcare professionals for medical advice and diagnosis.

๐Ÿ™ Acknowledgments

  • TensorFlow/Keras team for the deep learning framework
  • Medical imaging community for providing datasets
  • Flask framework developers
  • Bootstrap team for the UI framework
  • All contributors and researchers in medical AI

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages