How kushall-07/brain-tumor-segmentation works
Purpose and entry points
This is a local full-stack MRI brain-tumor segmentation system. Users upload four NIfTI modalities (T1, T1ce, T2, FLAIR); a Python inference backend produces a four-class segmentation and tumor measurements; a React client displays progress, results, and 3D volumes.
Runtime entry points are FastAPI at api/main.py (served via Uvicorn) and the Vite/React client at frontend/src/main.jsx. Offline training starts at training/train.py.
API orchestration boundary
The FastAPI layer owns HTTP contracts, validation, job lifecycle, and research metadata. Route handlers delegate work rather than exposing model internals directly.
• api/main.py
• api/routes.py
• api/jobs.py
• api/schemas.py
• api/services.py
The principal control flow is upload → modality/format validation → asynchronous or tracked prediction job → polling/progress → result and artifact retrieval. API errors are normalized through api/exceptions.py.
Inference pipeline
Inference accepts the four uploaded NIfTI volumes, loads the designated SwinUNETR checkpoint, applies the model pipeline, postprocesses class labels, and emits prediction artifacts plus derived analysis. The runtime checkpoint contract is under outputs/exp_swinunetr_4class_et_fixed/checkpoints/best_mean_dice.pt.
• inference/predict.py
• api/services.py
• utils/inference_utils.py
• utils/postprocess.py
• utils/postprocess_3d.py
This is the key boundary between request-scoped uploaded data and GPU/CPU PyTorch/MONAI execution. NiBabel, NumPy, and SciPy support NIfTI/image operations; NVIDIA CUDA is recommended but CPU execution is supported.
Model and data contracts
The learned model is SwinUNETR, selected through a model factory; a 3D U-Net implementation is also retained. Training and inference share BraTS-oriented multi-modal and label conventions.
• models/model_factory.py
• models/swinunetr.py
• models/unet3d.py
• datasets/brats_dataset.py
• datasets/brats_inference.py
The segmentation output has background plus NCR/NET, edema, and enhancing-tumor labels. Dataset loading is relevant for optional BraTS training, while runtime inference intentionally requires no local dataset.
Analysis, postprocessing, and persisted outputs
Postprocessing and evaluation utilities convert raw model output into clinically oriented regions and measurements, including per-class volume/dimensions and BraTS metrics. Generated artifacts are persisted beneath experiment/output directories rather than a database.
• utils/segmentation_evaluation.py
• utils/brats_metrics.py
• utils/analyze_3d_components.py
• outputs/exp_swinunetr_4class_et_fixed/predictions/
• outputs/exp_swinunetr_4class_et_fixed/metrics/
Existing experiment folders contain checkpoints, metrics, NIfTI predictions, and HTML visualizations. They are both model-artifact infrastructure and evidence/reproducibility outputs.
Frontend workflow and visualization
React Router separates upload/prediction, processing, result, and model-evaluation screens. Frontend service modules call the FastAPI API; local pending-upload state bridges navigation between upload and processing. NiiVue renders NIfTI scans and segmentation overlays interactively.
• frontend/src/router/AppRouter.jsx
• frontend/src/pages/PredictPage.jsx
• frontend/src/pages/Processing.jsx
• frontend/src/pages/Results.jsx
• frontend/src/services/predictionService.js
• frontend/src/components/NiiVueViewer.jsx
The client also presents research-oriented model details, timing, limitations, validation metrics, and optional ground-truth comparisons through frontend/src/pages/ModelEvaluation.jsx and frontend/src/components/research/.
Training and operational environment
Training includes preflight and dry-run stages before full model training, with checkpointing, metrics logging, VRAM checks, and qualitative visualization scripts.
• training/preflight_check.py
• training/dry_run.py
• training/train.py
• configs/config.py
• utils/checkpoint_utils.py
• utils/experiment_logger.py
Material runtimes are Python 3.11, PyTorch/MONAI, FastAPI/Uvicorn, Node.js 18+, React/Vite, and browser-side NiiVue. The architecture is local-process oriented: no cloud service, queue, database, or external API is indicated.
Main entry points: train.py Training pipeline · predict.py Prediction runner · AppRouter.jsx Workflow screens · Processing.jsx Progress and results
Components 19
React client
- Vite React entry
frontend/src/main.jsx - Workflow screens · Upload, processing, results, and evaluation flows.
frontend/src/router/AppRouter.jsx - Upload and prediction
frontend/src/pages/PredictPage.jsx - Progress and results
frontend/src/pages/Processing.jsx - API client · Submits jobs and polls progress/results.
frontend/src/services/predictionService.js - NIfTI volume viewer · Renders scans and segmentation overlays.
frontend/src/components/NiiVueViewer.jsx
API orchestration
- FastAPI application
api/main.py - HTTP routes · Upload, job polling, result, and artifact contracts.
api/routes.py - Tracked jobs · Owns asynchronous prediction state and progress.
api/jobs.py - Prediction service · Bridges request-scoped uploads to inference execution.
api/services.py
Inference pipeline
- Prediction runner · Loads four modalities and runs SwinUNETR.
inference/predict.py - Model selection · Selects SwinUNETR; retains 3D U-Net option.
models/model_factory.py - NIfTI preprocessing · Applies shared inference loading and transforms.
utils/inference_utils.py - Labels and measurements · Derives tumor regions, volumes, and dimensions.
utils/postprocess_3d.py
Experiment artifacts
- Best model checkpoint
outputs/exp_swinunetr_4class_et_fixed/checkpoints/best_mean_dice.pt - Predictions and metrics · NIfTI predictions, measurements, and visualizations.
Training
- Training pipeline · Runs preflight, dry-run, training, checkpointing, and logging.
training/train.py - BraTS dataset contract · Defines shared multimodal inputs and four-class labels.
datasets/brats_dataset.py - Training configuration
configs/config.py
Connections 22
- Vite React entry → Workflow screens: mounts
- Workflow screens → Upload and prediction: routes to
- Workflow screens → Progress and results: routes to
- Upload and prediction → API client: uploads four modalities
- Progress and results → API client: polls job and fetches results
- Progress and results → NIfTI volume viewer: displays volumes
- API client → FastAPI application: HTTP
- FastAPI application → HTTP routes: registers
- HTTP routes → Tracked jobs: creates and queries
- Tracked jobs → Prediction service: runs tracked prediction
- Prediction service → Prediction runner: invokes
- Prediction runner → NIfTI preprocessing: loads and prepares NIfTI
- Prediction runner → Model selection: constructs model
- Prediction runner → Best model checkpoint: loads weights
- Prediction runner → Labels and measurements: raw class output
- Labels and measurements → Predictions and metrics: persists predictions and analysis
- HTTP routes → Predictions and metrics: retrieves artifacts
- Training pipeline → BraTS dataset contract: trains on
- Training pipeline → Training configuration: uses
- Training pipeline → Model selection: trains selected model
- Training pipeline → Best model checkpoint: writes best checkpoint
- Training pipeline → Predictions and metrics: logs metrics and visualizations