How nihalgupta84/rectallitenet works
Purpose and Entry Points
RectalLiteNet is a Python/PyTorch 2.5D CT segmentation pipeline for three classes: background, normal rectum, and tumor. Main executable boundaries are training (train.py), labeled evaluation (test.py), and mask generation for CARE-style NPZ inputs (inference.py). Project behavior is centrally parameterized by configs/default.json.
Dataset Preparation and Split Boundary
External CARE data is intentionally absent and must be placed below data/CARE. Slice inventories are converted to CSV manifests, then split patient-disjointly to prevent adjacent slices from a patient crossing train/validation boundaries.
• scripts/prepare_manifest.py
• scripts/split_manifest.py
• datasets/care_dataset.py
Manifests carry NPZ paths, patient IDs, slice IDs, and dimensions; they are the contract between data preparation and all model workflows.
2.5D Dataset and Preprocessing
The CARE dataset loader maps each center slice to previous/center/next slices, using the center image at volume edges. It loads NPZ image/optional label, performs fixed intensity processing, crop, resize, normalization, and training-only synchronized spatial augmentation. Labels above 2 are consolidated to tumor class 2.
• datasets/care_dataset.py
• configs/default.json
This preprocessing contract must match checkpoint metadata/configuration at evaluation and inference.
Model Architecture
The segmentation network accepts the three-slice stack as channels and returns per-pixel logits for the center slice. Its architecture is ConvNeXt-Tiny encoding with a lightweight U-Net-like decoder, encoder skip joins, refinement, and SCSE attention.
• models/rectal_lite_net.py
• models/__init__.py
Training may obtain ConvNeXt ImageNet initialization weights, creating an external model-weight download dependency unless disabled.
Training and Experiment Lifecycle
train.py builds datasets/loaders, model, composite CE plus foreground-Dice loss, optimizer, warmup/cosine schedule, mixed-precision execution, and gradient accumulation. It validates by macro Dice and writes resumable/best checkpoints plus a resolved configuration and metric history to a required new run directory.
• train.py
• utils/losses.py
• utils/checkpoint.py
Reproducibility spans CLI seed handling, split hashes, resolved settings, and checkpoint state.
Evaluation, Inference, and Ensembles
test.py evaluates labeled manifests and emits aggregate/per-patient segmentation metrics. inference.py consumes a directory of CARE-named NPZ files, produces exact class-ID arrays and visual masks, and records a summary. Both support flip test-time augmentation and repeated checkpoints; ensembles average compatible model predictions.
• test.py
• inference.py
• utils/inference.py
• utils/metrics.py
Artifacts, Tracking, and Runtime Services
The runtime is Python 3.10/3.11 with PyTorch and optional CUDA/AMP. Included Git-LFS checkpoints enable standalone prediction and are integrity-recorded.
• checkpoints/rectallitenet_seed42.pt
• checkpoints/checksums.json
Training persists local durable events/metrics and optionally mirrors them to Weights & Biases; W&B failures degrade to local tracking rather than stopping execution.
• utils/tracking.py
• utils/config.py
Main entry points: train.py Training entry point · test.py Labeled evaluation · inference.py CARE NPZ inference · utils/inference.py Ensemble prediction runtime
Components 19
Data pipeline
CARE inputs become patient-disjoint manifests and 2.5D samples.
- CARE NPZ data · Externally supplied CARE files under data/CARE.
- Manifest preparation · Builds slice inventories with NPZ, patient, slice, and dimension fields.
scripts/prepare_manifest.py - Patient-disjoint split · Prevents slices from one patient crossing train and validation sets.
scripts/split_manifest.py - CSV manifests · Shared input contract for training, evaluation, and inference workflows.
- CARE 2.5D dataset · Builds neighboring-slice stacks and synchronized labeled samples.
datasets/care_dataset.py
Model
Three-slice CT segmentation network.
- RectalLiteNet · ConvNeXt-Tiny encoder with U-Net-like skip decoder and SCSE refinement.
models/rectal_lite_net.py - Model exports
models/__init__.py - ImageNet weights · Optional ConvNeXt initialization download.
Training runtime
Experiment creation, optimization, and durable state.
- Training entry point · Creates a new resumable experiment run.
train.py - Composite segmentation loss · Cross-entropy plus foreground Dice loss.
utils/losses.py - Checkpoint lifecycle · Writes and resumes best and latest model state.
utils/checkpoint.py - Local and W&B tracking · W&B mirroring is optional and failures fall back to local tracking.
utils/tracking.py - Resolved settings · Persists effective settings alongside run state.
utils/config.py - Packaged checkpoints · Git-LFS standalone prediction checkpoint with recorded integrity.
checkpoints/rectallitenet_seed42.pt
Evaluation & prediction
Checkpoint-driven labeled evaluation and NPZ mask generation.
- Labeled evaluation · Reports aggregate and per-patient segmentation metrics.
test.py - CARE NPZ inference · Writes class-ID arrays, visual masks, and a prediction summary.
inference.py - Ensemble prediction runtime · Averages compatible checkpoints and supports flip TTA.
utils/inference.py - Segmentation metrics · Computes macro Dice and evaluation reports.
utils/metrics.py
Other components
- Default configuration · Central preprocessing and runtime parameter contract.
configs/default.json
Connections 24
- CARE NPZ data → Manifest preparation: source slices
- Manifest preparation → Patient-disjoint split: inventory
- Patient-disjoint split → CSV manifests: train/validation CSVs
- CSV manifests → CARE 2.5D dataset: slice records
- Default configuration → CARE 2.5D dataset: preprocessing contract
- CARE 2.5D dataset → Training entry point: 2.5D training batches
- Model exports → RectalLiteNet: exports
- ImageNet weights → RectalLiteNet: optional initialization
- RectalLiteNet → Training entry point: logits model
- Composite segmentation loss → Training entry point: optimization objective
- Default configuration → Training entry point: runtime settings
- Training entry point → Checkpoint lifecycle: model and optimizer state
- Training entry point → Local and W&B tracking: metrics and events
- Training entry point → Resolved settings: resolved run config
- Checkpoint lifecycle → Labeled evaluation: trained checkpoints
- Packaged checkpoints → Labeled evaluation: standalone checkpoint
- Checkpoint lifecycle → CARE NPZ inference: trained checkpoints
- Packaged checkpoints → CARE NPZ inference: standalone checkpoint
- CSV manifests → Labeled evaluation: labeled evaluation records
- CARE NPZ data → CARE NPZ inference: CARE-named NPZ directory
- Labeled evaluation → Ensemble prediction runtime: checkpoint predictions
- CARE NPZ inference → Ensemble prediction runtime: checkpoint predictions
- Ensemble prediction runtime → Segmentation metrics: labeled masks and logits
- Default configuration → Ensemble prediction runtime: compatible preprocessing