Skip to content

Repository files navigation

Brain Hemorrhage Segmentation with NIfTI

A deep learning-based medical imaging project for automated brain hemorrhage detection and segmentation using 3D NIfTI CT scans. This project includes a complete pipeline from data preprocessing to an interactive web interface for clinical visualization.

Dataset

Brain Hemorrhage Segmentation 3D Data is publicly available on Kaggle:

πŸ”— Download Dataset on Kaggle

The dataset contains 3D CT scan volumes in NIfTI format (.nii.gz) with corresponding hemorrhage segmentation masks for training deep learning models.


Sustainable Development Goals

  • SDG 3 (Good Health and Well-being): This project aims to improve early diagnosis and treatment outcomes for brain hemorrhage patients by assisting radiologists with automated, high-precision segmentation.
  • SDG 9 (Industry, Innovation and Infrastructure): We leverage state-of-the-art deep learning architectures like SwinUNETR to foster innovation in medical imaging technology and digital health infrastructure.
  • SDG 10 (Reduced Inequalities): By providing an open-source, automated diagnostic tool, we contribute to reducing disparities in healthcare access, potentially aiding regions with limited specialized radiological expertise.

Project Overview

This repository contains:

  1. Data Processing Scripts - Convert DICOM to NIfTI, preprocessing, visualization
  2. Model Training - SwinUNETR-based 3D segmentation model
  3. Interactive Web Interface - Real-time 3D visualization and analysis
  4. Backend API - FastAPI server for model inference

Project Structure

Brain_Hemorrhage_Segmentation_With_NIFTI/
β”œβ”€β”€ Brain Hemorrhage Interface/       # Web application
β”‚   β”œβ”€β”€ backend/
β”‚   β”‚   β”œβ”€β”€ main.py                   # FastAPI server
β”‚   β”‚   └── best_metric_model.zip     # Trained model
β”‚   └── Brain_Hemorrhage_Interface/   # React frontend
β”‚       β”œβ”€β”€ node_modules/
β”‚       β”œβ”€β”€ public/
β”‚       β”œβ”€β”€ src/
β”‚       └── README.md                  
β”œβ”€β”€ Scripts/                          # SwinUNet Codes
β”‚   β”œβ”€β”€ divide_dicoms.py
β”‚   β”œβ”€β”€ preprocessing_and_visualizing_data.py
β”‚   β”œβ”€β”€ find_dims_and_Swin-Unet_params.py
β”‚   β”œβ”€β”€ dicom_to_nifti.py
β”‚   β”œβ”€β”€ training_with_new_model.py
β”‚   β”œβ”€β”€ find_nobleed_patient.py
β”‚   β”œβ”€β”€ utilities.py
β”‚   └── visualize_testing_data.py
β”œβ”€β”€ Dataset/
β”‚   └── nifti_files_last_data         #Dataset
β”œβ”€β”€ datasetcard.md                    # Dataset documentation
β”œβ”€β”€ modelcard.md                      # Model documentation
β”œβ”€β”€ README.md                         #ReadMe
β”œβ”€β”€ Testing Area/
β”‚   β”œβ”€β”€ TestDataset.md 
β”‚       β”œβ”€β”€ id_0a01....gz
β”‚   β”œβ”€β”€ inference.py 
β”œβ”€β”€ model_results 
β”‚   β”œβ”€β”€ metric_test.npy
β”‚   β”œβ”€β”€ metric_train.npy
β”‚   β”œβ”€β”€ loss_test.npy
β”‚   β”œβ”€β”€ loss_train.npy    
└── Consept Note Muhammed Kerem Demirbent.pdf


Model

Architecture: SwinUNETR (Swin Transformer + U-Net)

The trained model (best_metric_model.pth) was extracted from model_results/ and placed in the backend/ directory for deployment. The model is:

  • Compressed: FP16 quantization + ZIP compression (244 MB β†’ 74 MB)
  • Ready for deployment: Included in this repository

Model Features

  • 3D volumetric segmentation
  • Multi-scale feature extraction using Swin Transformer
  • Trained on brain CT scans from the Kaggle dataset

Quick Start

Prerequisites

  • Python 3.10+
  • Node.js 18+
  • CUDA-capable GPU

Installation

  1. Clone the repository
git clone https://github.com/MuhammedKeremDemirbent/Brain_Hemorrhage_Segmentation_With_NIFTI.git
cd Brain_Hemorrhage_Segmentation_With_NIFTI
  1. Backend Setup
# Navigate to project root (where requirements.txt is located)
# then install dependencies:
pip install -r requirements.txt

# Run the backend
cd "Brain Hemorrhage Interface/backend"
python main.py
  1. Frontend Setup
cd "../Brain_Hemorrhage_Interface"
npm install

Note: All frontend dependencies are listed in package.json and will be installed automatically with npm install.

Running the Application

  1. Start Backend API
cd "Brain Hemorrhage Interface/backend"
python main.py
# Server runs on http://127.0.0.1:8000
  1. Start Frontend
cd "../Brain_Hemorrhage_Interface"
npm run dev
# Application runs on http://localhost:5173
  1. Open in Browser Navigate to http://localhost:5173 and upload a NIfTI file (.nii.gz) for analysis.

Scripts

Data Processing & Analysis

dicom_to_nifti.py - Convert DICOM files to NIfTI format using dicom2nifti

divide_dicoms.py - Organize and divide DICOM files into patient folders

preprocessing_and_visualizing_data.py - Preprocess CT scans with MONAI transforms and data augmentation

  • Includes normalization, resizing, rotation, flipping, and Gaussian noise
  • Creates DataLoaders for training

find_dims_and_SWINUNETR_params.py - Analyze dataset dimensions and SwinUNETR model parameters

training_with_new_models.py - Train SwinUNETR model with DiceFocalLoss

visualize_testing_data.py - Visualize training metrics (Dice score & Loss) using matplotlib

utilities.py - Helper functions for training and validation

Visualization Tools

  • 3D Slicer - Used for 3D medical image visualization
  • Matplotlib - Used for plotting Dice scores and loss curves

How to Use Interface

This interface allows clinicians and researchers to upload, analyze, and visualize brain hemorrhage segmentation results in 3D.

1. Main Screen - Upload Data

The landing page provides a simple drag-and-drop interface or file selection for NIfTI (.nii, .nii.gz) files.

Main Interface

2. Analysis Page

Once the file is uploaded, it is listed in the patient data section. Click the Analyze button to send the data to the deep learning model for processing.

Analysis Page

3. Visualizing Results

After analysis, the application displays the comprehensive results dashboard:

  • 3D Volumetric Render: Interactive 3D view of the brain and detected hemorrhage (red).
  • Slice Viewer: Scroll through 2D axial slices.
  • Bleeding Detected: Immediate alert with confidence score and voxel volume.
  • Tools: Generate Grad-CAM heatmaps, view uncertainty maps, or download the full report as PDF.

Results Dashboard

4. Settings

Configure application preferences, including theme (Dark/Light), language, and report customization (Hospital Name, Doctor Name).

Settings Page


Features

Web Interface

  • 3D Brain Visualization - Interactive Three.js rendering
  • Hemorrhage Detection - Real-time segmentation overlay
  • Slice Viewer - Browse axial planes
  • Grad-CAM Heatmap - Model attention visualization
  • Uncertainty Map - Confidence analysis
  • Export Reports - PDF generation with results
  • Multi-language Support - EN, TR, DE, KO

Backend API

  • /predict - Hemorrhage detection with confidence scores
  • /visualize_layers - 3D mesh generation (brain + lesion)
  • /gradcam - Attention heatmap visualization
  • /uncertainty - Uncertainty quantification
  • /get_slice - 2D slice extraction

Documentation


Model Optimization

The model file was optimized for GitHub deployment:

Stage Size Compression
Original (FP32) 244 MB -
FP16 Quantization 126 MB 48%
ZIP Compression 74 MB 70% total

The backend automatically extracts and converts the model during runtime.



License

Please check the dataset license on Kaggle and respect the terms of use for medical imaging data.


Author

Muhammed Kerem Demirbent


Acknowledgments

  • MONAI - Medical imaging deep learning framework
  • Swin Transformer - Vision transformer architecture
  • React Three Fiber - 3D visualization in React
  • FastAPI - Modern Python web framework

Disclaimer

This software is intended for research and educational purposes only. It is not approved for clinical use or medical decision-making. Always consult qualified healthcare professionals for medical diagnosis and treatment.

About

3D Brain Hemorrhage Segmentation pipeline with SwinUNETR, FastAPI, and a React-based 3D visualization interface.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages