k12apana/dlpfc-segmentation
Lightweight 3D U-Net for DLPFC Segmentation in T1-Weighted MRI
  
๐ค Live demo โ upload an MRI and get an instant segmentation: <https://huggingface.co/spaces/k12apana/dlpfc-segmentation>
Automated segmentation and spatial localization of the dorsolateral prefrontal cortex (DLPFC) from structural brain MRI, intended to support target definition for repetitive transcranial magnetic stimulation (rTMS) neuronavigation.
This repository accompanies the doctoral dissertation of K. A. Apana (St. Petersburg Electrotechnical University "LETI"). It contains the preprocessing, model, training, inference and evaluation code, the trained leave-one-subject-out (LOSO) models, and the figures reported in the thesis.
Research objectives
- Segment the DLPFC from T1-weighted MRI under a small-data regime (N = 10) without transfer learning or large-scale pretraining.
- Constrain model capacity (a lightweight 3D U-Net, ~1.4M parameters) as a structural regularizer to mitigate overfitting on limited data.
- Recover a single physical target coordinate (mask centre of mass) suitable for TMS neuronavigation, and quantify spatial targeting error.
Methodology summary
T1 volumes are resampled to 1 mm isotropic spacing, centre cropped/padded to a 128ร128ร128 grid, and intensity-normalised (z-score over brain voxels). A lightweight 3D U-Net is trained on class-balanced 96ร96ร96 patches with a combined binary cross-entropy + soft-Dice loss. Models are evaluated by leave-one-subject-out cross-validation; full volumes are segmented by overlapping sliding-window inference and assessed with the Dice similarity coefficient (DSC) and centroid (centre-of-mass) error. See METHODS.md for details.
Repository structure
.
โโโ src/ Pipeline package (importable as `src`)
โ โโโ config.py Paths and hyperparameters (single source of truth)
โ โโโ preprocessing.py Resampling, crop/pad, normalization
โ โโโ model.py 3D U-Net + BCE/Dice loss
โ โโโ data.py .npz loading + class-balanced patch generator
โ โโโ inference.py Sliding-window full-volume inference
โ โโโ evaluate.py DSC + centroid-error metrics, LOSO evaluation
โ โโโ train.py LOSO training driver (CLI)
โโโ tests/ Pure-NumPy unit tests (+ optional TF model tests)
โโโ app/ Gradio web demo (upload MRI -> segmentation)
โโโ notebooks/
โ โโโ full_pipeline_colab.py Original Google Colab export (reference record)
โโโ data/
โ โโโ raw/{images,labels}/ T1 volumes and expert masks (.nii) [not in Git]
โ โโโ preprocessed/ 128ยณ .npz volumes [not in Git]
โโโ models/ LOSO model weights, best_model_caseN.h5 [not in Git]
โโโ results/figures/ Per-subject overlays and analysis figures (.png)
โโโ requirements.txt, environment.yml
โโโ *.md Documentation (see below)Large binary artifacts (*.nii, *.npz, *.h5) are excluded from version control; see DATASET.md and REPRODUCIBILITY.md.
Dataset
Ten T1-weighted MRI subjects with expert DLPFC masks delineated from anatomical landmarks (middle frontal gyrus, inferior frontal sulcus, precentral sulcus). Provenance, acquisition and ethics are documented in DATASET.md.
Installation
conda env create -f environment.yml
conda activate dlpfc-seg
# or, with pip in a Python 3.10 environment:
pip install -r requirements.txtUsage
# 1. Preprocess raw NIfTI volumes -> data/preprocessed/*.npz
python -m src.preprocessing
# 2. Leave-one-subject-out training -> models/best_model_<id>.h5
python -m src.train --epochs 80
# 3. Evaluate all folds (Dice + centroid error)
python -m src.evaluatePaths and hyperparameters are centralised in src/config.py and can be overridden with environment variables (e.g. DLPFC_DATA_ROOT).
Web demo
An interactive Gradio app accepts a T1 volume and returns the predicted DLPFC segmentation:
pip install -r app/requirements.txt
git lfs pull # fetch the model weights (models/*.h5)
python app/app.py # then open the printed local URLIt produces an overlay, the target centroid, and a downloadable mask. See app/README.md for configuration, a temporary public link, and Hugging Face Spaces deployment. Research/educational use only โ not a medical device.
Experimental workflow
preprocess โ LOSO train (N folds) โ sliding-window inference โ metrics. The full workflow is described in PIPELINE_DIAGRAM.md and reproduction is covered in REPRODUCIBILITY.md.
Results overview
Leave-one-subject-out cross-validation (N = 10), volumetric DSC per subject:
Two subjects (cases 2 and 4) show reduced overlap attributable to annotation convention rather than model failure; this is analysed in the dissertation and summarised in VALIDATION_REPORT.md. Per-subject overlay figures are in results/figures/.
Documentation
- METHODS.md โ methodology, architecture, metrics
- DATASET.md โ data origin, structure, ethics
- REPRODUCIBILITY.md โ environment and reproduction steps
- PIPELINE_DIAGRAM.md โ end-to-end workflow
- PROJECT_AUDIT.md โ repository audit and issue tracking
- VALIDATION_REPORT.md โ verification of code vs. results
- STRUCTURAL_CHANGES.md โ reorganization record
- RELEASE_CHECKLIST.md โ pre-publication checks
Citation
If you use this code, please cite the dissertation:
@phdthesis{apana2026dlpfc,
author = {Apana, Kenneth Ayinbuno},
title = {Development of artificial intelligence methods for precise spatial
localization in volumetric neuroimaging data},
school = {St. Petersburg Electrotechnical University (LETI)},
year = {2026}
}License
Released under the MIT License, covering the code, documentation and figures in this repository. Any separately distributed dataset or model weights remain subject to the data-use and ethics terms described in DATASET.md.
