# nihalgupta84/rectallitenet architecture

> Architecture diagram of the GitHub repository nihalgupta84/rectallitenet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nihalgupta84/rectallitenet
- Repository: https://github.com/nihalgupta84/rectallitenet

## Overview

#### 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`](https://github.com/nihalgupta84/rectallitenet/blob/main/train.py) (Training entry point), [`test.py`](https://github.com/nihalgupta84/rectallitenet/blob/main/test.py) (Labeled evaluation), [`inference.py`](https://github.com/nihalgupta84/rectallitenet/blob/main/inference.py) (CARE NPZ inference), [`utils/inference.py`](https://github.com/nihalgupta84/rectallitenet/blob/main/utils/inference.py) (Ensemble prediction runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data pipeline"]
  node_care_data[("CARE NPZ data<br/>external dataset")]
  node_prepare_manifest["Manifest preparation<br/>data script"]
  node_split_manifest["Patient-disjoint split<br/>data script<br/>[split_manifest.py]"]
  node_manifests["CSV manifests<br/>data contract"]
  node_care_dataset["CARE 2.5D dataset<br/>PyTorch dataset<br/>[care_dataset.py]"]
end

subgraph group_model["Model"]
  node_rectal_lite_net["RectalLiteNet<br/>segmentation model<br/>[rectal_lite_net.py]"]
  node_model_exports["Model exports<br/>package boundary<br/>[__init__.py]"]
  node_imagenet_weights["ImageNet weights<br/>external model weights"]
end

subgraph group_runtime["Training runtime"]
  node_train{{"Training entry point<br/>Python executable<br/>[train.py]"}}
  node_losses["Composite segmentation loss<br/>training component<br/>[losses.py]"]
  node_checkpoint_io["Checkpoint lifecycle<br/>artifact manager<br/>[checkpoint.py]"]
  node_tracking["Local and W&amp;B tracking<br/>runtime service<br/>[tracking.py]"]
  node_config_resolution["Resolved settings<br/>config utility<br/>[config.py]"]
  node_checkpoints[("Packaged checkpoints<br/>model artifact")]
end

subgraph group_serving["Evaluation &amp; prediction"]
  node_test{{"Labeled evaluation<br/>Python executable<br/>[test.py]"}}
  node_inference{{"CARE NPZ inference<br/>Python executable<br/>[inference.py]"}}
  node_prediction_runtime["Ensemble prediction runtime<br/>inference component<br/>[inference.py]"]
  node_metrics["Segmentation metrics<br/>metrics component<br/>[metrics.py]"]
end

node_default_config["Default configuration<br/>JSON config<br/>[default.json]"]

node_care_data -->|"source slices"| node_prepare_manifest
node_prepare_manifest -->|"inventory"| node_split_manifest
node_split_manifest -->|"train/validation CSVs"| node_manifests
node_manifests -->|"slice records"| node_care_dataset
node_default_config -->|"preprocessing contract"| node_care_dataset
node_care_dataset -->|"2.5D training batches"| node_train
node_model_exports -->|"exports"| node_rectal_lite_net
node_imagenet_weights -.->|"optional initialization"| node_rectal_lite_net
node_rectal_lite_net -->|"logits model"| node_train
node_losses -->|"optimization objective"| node_train
node_default_config -->|"runtime settings"| node_train
node_train -->|"model and optimizer state"| node_checkpoint_io
node_train -->|"metrics and events"| node_tracking
node_train -->|"resolved run config"| node_config_resolution
node_checkpoint_io -->|"trained checkpoints"| node_test
node_checkpoints -->|"standalone checkpoint"| node_test
node_checkpoint_io -->|"trained checkpoints"| node_inference
node_checkpoints -->|"standalone checkpoint"| node_inference
node_manifests -->|"labeled evaluation records"| node_test
node_care_data -->|"CARE-named NPZ directory"| node_inference
node_test -->|"checkpoint predictions"| node_prediction_runtime
node_inference -->|"checkpoint predictions"| node_prediction_runtime
node_prediction_runtime -->|"labeled masks and logits"| node_metrics
node_default_config -->|"compatible preprocessing"| node_prediction_runtime

click node_prepare_manifest "https://github.com/nihalgupta84/rectallitenet/blob/main/scripts/prepare_manifest.py"
click node_split_manifest "https://github.com/nihalgupta84/rectallitenet/blob/main/scripts/split_manifest.py"
click node_care_dataset "https://github.com/nihalgupta84/rectallitenet/blob/main/datasets/care_dataset.py"
click node_default_config "https://github.com/nihalgupta84/rectallitenet/blob/main/configs/default.json"
click node_rectal_lite_net "https://github.com/nihalgupta84/rectallitenet/blob/main/models/rectal_lite_net.py"
click node_model_exports "https://github.com/nihalgupta84/rectallitenet/blob/main/models/__init__.py"
click node_train "https://github.com/nihalgupta84/rectallitenet/blob/main/train.py"
click node_losses "https://github.com/nihalgupta84/rectallitenet/blob/main/utils/losses.py"
click node_checkpoint_io "https://github.com/nihalgupta84/rectallitenet/blob/main/utils/checkpoint.py"
click node_tracking "https://github.com/nihalgupta84/rectallitenet/blob/main/utils/tracking.py"
click node_config_resolution "https://github.com/nihalgupta84/rectallitenet/blob/main/utils/config.py"
click node_checkpoints "https://github.com/nihalgupta84/rectallitenet/blob/main/checkpoints/rectallitenet_seed42.pt"
click node_test "https://github.com/nihalgupta84/rectallitenet/blob/main/test.py"
click node_inference "https://github.com/nihalgupta84/rectallitenet/blob/main/inference.py"
click node_prediction_runtime "https://github.com/nihalgupta84/rectallitenet/blob/main/utils/inference.py"
click node_metrics "https://github.com/nihalgupta84/rectallitenet/blob/main/utils/metrics.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_care_data,node_prepare_manifest,node_split_manifest,node_manifests,node_care_dataset toneBlue
class node_rectal_lite_net,node_model_exports,node_imagenet_weights toneAmber
class node_train,node_losses,node_checkpoint_io,node_tracking,node_config_resolution,node_checkpoints toneMint
class node_test,node_inference,node_prediction_runtime,node_metrics toneRose
class node_default_config toneNeutral
```

## Components

### Data pipeline

CARE inputs become patient-disjoint manifests and 2.5D samples.

- **CARE NPZ data**: external dataset · Externally supplied CARE files under data/CARE.
- [Manifest preparation](https://github.com/nihalgupta84/rectallitenet/blob/main/scripts/prepare_manifest.py): data script · Builds slice inventories with NPZ, patient, slice, and dimension fields. · `scripts/prepare_manifest.py`
- [Patient-disjoint split](https://github.com/nihalgupta84/rectallitenet/blob/main/scripts/split_manifest.py): data script · Prevents slices from one patient crossing train and validation sets. · `scripts/split_manifest.py`
- **CSV manifests**: data contract · Shared input contract for training, evaluation, and inference workflows.
- [CARE 2.5D dataset](https://github.com/nihalgupta84/rectallitenet/blob/main/datasets/care_dataset.py): PyTorch dataset · Builds neighboring-slice stacks and synchronized labeled samples. · `datasets/care_dataset.py`

### Model

Three-slice CT segmentation network.

- [RectalLiteNet](https://github.com/nihalgupta84/rectallitenet/blob/main/models/rectal_lite_net.py): segmentation model · ConvNeXt-Tiny encoder with U-Net-like skip decoder and SCSE refinement. · `models/rectal_lite_net.py`
- [Model exports](https://github.com/nihalgupta84/rectallitenet/blob/main/models/__init__.py): package boundary · `models/__init__.py`
- **ImageNet weights**: external model weights · Optional ConvNeXt initialization download.

### Training runtime

Experiment creation, optimization, and durable state.

- [Training entry point](https://github.com/nihalgupta84/rectallitenet/blob/main/train.py): Python executable · Creates a new resumable experiment run. · `train.py`
- [Composite segmentation loss](https://github.com/nihalgupta84/rectallitenet/blob/main/utils/losses.py): training component · Cross-entropy plus foreground Dice loss. · `utils/losses.py`
- [Checkpoint lifecycle](https://github.com/nihalgupta84/rectallitenet/blob/main/utils/checkpoint.py): artifact manager · Writes and resumes best and latest model state. · `utils/checkpoint.py`
- [Local and W&B tracking](https://github.com/nihalgupta84/rectallitenet/blob/main/utils/tracking.py): runtime service · W&B mirroring is optional and failures fall back to local tracking. · `utils/tracking.py`
- [Resolved settings](https://github.com/nihalgupta84/rectallitenet/blob/main/utils/config.py): config utility · Persists effective settings alongside run state. · `utils/config.py`
- [Packaged checkpoints](https://github.com/nihalgupta84/rectallitenet/blob/main/checkpoints/rectallitenet_seed42.pt): model artifact · Git-LFS standalone prediction checkpoint with recorded integrity. · `checkpoints/rectallitenet_seed42.pt`

### Evaluation & prediction

Checkpoint-driven labeled evaluation and NPZ mask generation.

- [Labeled evaluation](https://github.com/nihalgupta84/rectallitenet/blob/main/test.py): Python executable · Reports aggregate and per-patient segmentation metrics. · `test.py`
- [CARE NPZ inference](https://github.com/nihalgupta84/rectallitenet/blob/main/inference.py): Python executable · Writes class-ID arrays, visual masks, and a prediction summary. · `inference.py`
- [Ensemble prediction runtime](https://github.com/nihalgupta84/rectallitenet/blob/main/utils/inference.py): inference component · Averages compatible checkpoints and supports flip TTA. · `utils/inference.py`
- [Segmentation metrics](https://github.com/nihalgupta84/rectallitenet/blob/main/utils/metrics.py): metrics component · Computes macro Dice and evaluation reports. · `utils/metrics.py`

### Other components

- [Default configuration](https://github.com/nihalgupta84/rectallitenet/blob/main/configs/default.json): JSON config · Central preprocessing and runtime parameter contract. · `configs/default.json`

## Connections

- 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
