# kail8/unsupdis-pytorch architecture

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

- Interactive diagram: https://gitdiagram.com/kail8/unsupdis-pytorch
- Repository: https://github.com/kail8/unsupdis-pytorch

## Overview

1. **Project purpose and overall shape**  
   This repository is a PyTorch reimplementation of the UnsupDIS deep image stitching pipeline. Architecturally it is a two-stage ML system: an **alignment stage** that predicts coarse geometric alignment between paired images, followed by a **fusion/reconstruction stage** that produces the final stitched output.

2. **Top-level workflow boundary**  
   The repository is organized around command-line entry points rather than a service runtime. The main executables are `train.py`, `test.py`, `inference_align.py`, and `inference_fuse.py`, with `main.sh` documenting the intended end-to-end pipeline. The data flow is: raw paired images -> alignment model -> warped/aligned pairs -> fusion model -> stitched result.

3. **Data layer and dataset contracts**  
   `data/*.yaml` defines dataset locations and splits for **UDIS-D** and **WarpedCOCO**. The expected directory structure is fixed around paired inputs such as `input1` and `input2`, with separate train/test splits. Text files like `train.txt` and `test.txt` act as split manifests. This is the primary boundary between the code and external data.

4. **Two-stage model architecture**  
   The `mode` flag in training/inference distinguishes the two subsystems: `align` and `fuse`. Alignment learns to register the two views; fusion consumes the aligned pair and reconstructs the final image. These are separate model families with separate YAML configs, checkpoints, loss behavior, and input resolution expectations.

5. **Model configuration and backbone selection**  
   `models/*.yaml` defines network layouts for alignment and fusion variants, including `origin`, `variant`, and YOLO-backed versions. The repo borrows the YOLOv5-style configuration system, so architecture is assembled from YAML and reused backbone components. The README notes that `depth_multiple` and `width_multiple` are used to select the backbone scale.

6. **Model implementation boundary**  
   Core neural network code lives in `models/` and is split into reusable modules and model definitions. `models/common.py`, `models/yolo.py`, and `models/experimental.py` provide the YOLO-style building blocks and parsing logic; `models/torchsummary.py` is for inspection. The repo’s alignment and fusion networks are configured on top of this shared model framework.

7. **Training pipeline**  
   `train.py` is the orchestration layer for optimization. It reads a dataset config, a hyperparameter file, a model YAML, and optional pretrained weights. The repository uses different hyperparameter files for scratch training and fine-tuning, reflecting the stage-specific optimization strategy. Training artifacts are written under `runs/train/` and later moved into `weights/`.

8. **Inference and evaluation split**  
   `test.py` handles batched evaluation metrics, while `inference_align.py` and `inference_fuse.py` generate visual outputs and intermediate artifacts. Alignment inference can produce metrics like RMSE and also save warped pairs for the next stage. Fusion inference takes the aligned data and emits the final stitched images.

9. **Losses and geometry-aware utilities**  
   `utils/loss.py` contains the training objectives, while `utils/stitching.py` and `utils/general.py` support image composition, geometric handling, and evaluation helpers. These utilities are part of the algorithmic core rather than generic plumbing because stitching quality depends on both alignment geometry and reconstruction losses.

10. **Data loading and preprocessing**  
   `utils/datasets.py` is the bridge from the dataset manifests to minibatches. It likely handles paired-image loading, resizing/cropping, and augmentation consistent with the stitch-pair task. This is where raw file structure is converted into tensors consumed by the training and inference code.

11. **Runtime and dependency profile**  
   The project is implemented in **Python with PyTorch**, and it relies on the YOLOv5 ecosystem for model structure and pretrained backbones. The README’s emphasis on pretrained `yolov5*.pt` weights shows that external checkpoints are part of the model initialization path, especially for the YOLO-based variants.

12. **Artifacts and checkpoint layout**  
   Checkpoints are organized under `weights/align/...` and `weights/fuse/...`, with training outputs copied from `runs/train/exp`. The pipeline explicitly uses one stage’s checkpoint as the initialization for the next stage, so checkpoint flow is a first-class architectural dependency.

13. **Key external dependencies**  
   The main external inputs are the datasets **UDIS-D** and **WarpedCOCO**, plus pretrained YOLOv5 backbones. The project also depends on the standard PyTorch training stack and image I/O libraries listed in `requirements.txt`. No server, database, or online API boundary appears in the architecture.

14. **Important architectural tradeoff**  
   The repo deliberately simplifies the original TensorFlow implementation by replacing heavier alignment components such as cost volume with lighter concatenation-based design choices. That tradeoff affects the diagram: the architecture is optimized for practical training speed and inference speed, not for maximum model fidelity to the original paper.

15. **How to draw the system**  
   A correct architecture diagram should show: external datasets and pretrained weights feeding a dataset/config layer; that layer feeding the alignment training/inference pipeline; alignment outputs generating warped pairs; warped pairs feeding the reconstruction training/inference pipeline; and utility modules supporting both stages for losses, stitching, and evaluation.

Main entry points: [`train.py`](https://github.com/kail8/unsupdis-pytorch/blob/main/train.py) (Train), [`inference_align.py`](https://github.com/kail8/unsupdis-pytorch/blob/main/inference_align.py) (Align infer), [`inference_fuse.py`](https://github.com/kail8/unsupdis-pytorch/blob/main/inference_fuse.py) (Fuse infer), [`utils/general.py`](https://github.com/kail8/unsupdis-pytorch/blob/main/utils/general.py) (General utils)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external["External inputs"]
  node_udis_d[("UDIS-D<br/>dataset")]
  node_warped_coco[("WarpedCOCO<br/>dataset")]
  node_yolo_weights["YOLO weights<br/>checkpoint"]
end

subgraph group_entry["Entry points"]
  node_train_py["Train<br/>orchestrator<br/>[train.py]"]
  node_test_py["Test<br/>evaluation<br/>[test.py]"]
  node_infer_align_py["Align infer<br/>inference<br/>[inference_align.py]"]
  node_infer_fuse_py["Fuse infer<br/>inference<br/>[inference_fuse.py]"]
  node_main_sh["Pipeline<br/>workflow<br/>[main.sh]"]
end

subgraph group_data["Data layer"]
  node_data_yaml["Data cfgs<br/>dataset-config<br/>[udis.yaml]"]
  node_hyp_yaml["Hyperparams<br/>train-config"]
end

subgraph group_model["Model stack"]
  node_align_yaml["Align nets<br/>model-config<br/>[align_yolo.yaml]"]
  node_fuse_yaml["Fuse nets<br/>model-config<br/>[fuse_yolo.yaml]"]
  node_model_core["Model core<br/>framework<br/>[yolo.py]"]
  node_model_common["Blocks<br/>modules<br/>[common.py]"]
  node_model_experimental["Experimental<br/>modules<br/>[experimental.py]"]
end

subgraph group_utils["Utility core"]
  node_losses["Losses<br/>objective<br/>[loss.py]"]
  node_datasets_py["Dataloader<br/>data-loader<br/>[datasets.py]"]
  node_stitching["Stitching<br/>geometry<br/>[stitching.py]"]
  node_general["General utils<br/>helpers<br/>[general.py]"]
  node_torch_utils["Torch utils<br/>runtime<br/>[torch_utils.py]"]
end

node_udis_d -->|"dataset paths"| node_data_yaml
node_warped_coco -->|"dataset paths"| node_data_yaml
node_yolo_weights -->|"init"| node_train_py
node_data_yaml -->|"load"| node_datasets_py
node_hyp_yaml -->|"optimize"| node_train_py
node_align_yaml -->|"align model"| node_train_py
node_fuse_yaml -->|"fuse model"| node_train_py
node_datasets_py -->|"batches"| node_train_py
node_model_core -->|"assemble"| node_align_yaml
node_model_core -->|"assemble"| node_fuse_yaml
node_model_common -->|"blocks"| node_model_core
node_model_experimental -->|"variants"| node_model_core
node_losses -->|"objective"| node_train_py
node_stitching -->|"warp"| node_infer_align_py
node_stitching -->|"compose"| node_infer_fuse_py
node_general -->|"metrics"| node_test_py
node_general -->|"eval"| node_infer_align_py
node_general -->|"eval"| node_infer_fuse_py
node_torch_utils -->|"runtime"| node_train_py
node_main_sh -->|"stage 1"| node_train_py
node_main_sh -->|"stage 2"| node_infer_align_py
node_main_sh -->|"stage 3"| node_infer_fuse_py
node_train_py -->|"checkpoint flow"| node_infer_align_py
node_infer_align_py -->|"warped pairs"| node_train_py
node_infer_align_py -->|"input pairs"| node_infer_fuse_py
node_train_py -->|"checkpoint flow"| node_infer_fuse_py
node_test_py -->|"reporting"| node_general

click node_udis_d "https://github.com/kail8/unsupdis-pytorch/tree/main/data/UDIS-D"
click node_warped_coco "https://github.com/kail8/unsupdis-pytorch/tree/main/data/WarpedCOCO"
click node_train_py "https://github.com/kail8/unsupdis-pytorch/blob/main/train.py"
click node_test_py "https://github.com/kail8/unsupdis-pytorch/blob/main/test.py"
click node_infer_align_py "https://github.com/kail8/unsupdis-pytorch/blob/main/inference_align.py"
click node_infer_fuse_py "https://github.com/kail8/unsupdis-pytorch/blob/main/inference_fuse.py"
click node_main_sh "https://github.com/kail8/unsupdis-pytorch/blob/main/main.sh"
click node_data_yaml "https://github.com/kail8/unsupdis-pytorch/blob/main/data/udis.yaml"
click node_hyp_yaml "https://github.com/kail8/unsupdis-pytorch/blob/main/data/hyp.align.scratch.yaml"
click node_align_yaml "https://github.com/kail8/unsupdis-pytorch/blob/main/models/align_yolo.yaml"
click node_fuse_yaml "https://github.com/kail8/unsupdis-pytorch/blob/main/models/fuse_yolo.yaml"
click node_model_core "https://github.com/kail8/unsupdis-pytorch/blob/main/models/yolo.py"
click node_model_common "https://github.com/kail8/unsupdis-pytorch/blob/main/models/common.py"
click node_model_experimental "https://github.com/kail8/unsupdis-pytorch/blob/main/models/experimental.py"
click node_losses "https://github.com/kail8/unsupdis-pytorch/blob/main/utils/loss.py"
click node_datasets_py "https://github.com/kail8/unsupdis-pytorch/blob/main/utils/datasets.py"
click node_stitching "https://github.com/kail8/unsupdis-pytorch/blob/main/utils/stitching.py"
click node_general "https://github.com/kail8/unsupdis-pytorch/blob/main/utils/general.py"
click node_torch_utils "https://github.com/kail8/unsupdis-pytorch/blob/main/utils/torch_utils.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_udis_d,node_warped_coco,node_yolo_weights toneBlue
class node_train_py,node_test_py,node_infer_align_py,node_infer_fuse_py,node_main_sh toneAmber
class node_data_yaml,node_hyp_yaml toneMint
class node_align_yaml,node_fuse_yaml,node_model_core,node_model_common,node_model_experimental toneRose
class node_losses,node_datasets_py,node_stitching,node_general,node_torch_utils toneIndigo
```

## Components

### External inputs

Datasets and pretrained checkpoints consumed by the pipeline.

- [UDIS-D](https://github.com/kail8/unsupdis-pytorch/tree/main/data/UDIS-D): dataset · Paired-image stitching dataset used for training and evaluation. · `data/UDIS-D`
- [WarpedCOCO](https://github.com/kail8/unsupdis-pytorch/tree/main/data/WarpedCOCO): dataset · Secondary paired-image dataset for training and testing. · `data/WarpedCOCO`
- **YOLO weights**: checkpoint · Pretrained backbones used to initialize YOLO-backed variants.

### Entry points

Command-line workflows for training, testing, and inference.

- [Train](https://github.com/kail8/unsupdis-pytorch/blob/main/train.py): orchestrator · Stage-aware optimization entry point for alignment and fusion. · `train.py`
- [Test](https://github.com/kail8/unsupdis-pytorch/blob/main/test.py): evaluation · Batched evaluation and metric reporting entry point. · `test.py`
- [Align infer](https://github.com/kail8/unsupdis-pytorch/blob/main/inference_align.py): inference · Produces warped/aligned pairs and alignment metrics. · `inference_align.py`
- [Fuse infer](https://github.com/kail8/unsupdis-pytorch/blob/main/inference_fuse.py): inference · Consumes aligned pairs and emits stitched outputs. · `inference_fuse.py`
- [Pipeline](https://github.com/kail8/unsupdis-pytorch/blob/main/main.sh): workflow · Documents the intended end-to-end two-stage workflow. · `main.sh`

### Data layer

Dataset contracts, manifests, and hyperparameter configs.

- [Data cfgs](https://github.com/kail8/unsupdis-pytorch/blob/main/data/udis.yaml): dataset-config · Dataset locations, splits, and manifests for the supported corpora. · `data/udis.yaml`
- [Hyperparams](https://github.com/kail8/unsupdis-pytorch/blob/main/data/hyp.align.scratch.yaml): train-config · Stage-specific optimization settings for scratch and finetune runs. · `data/hyp.align.scratch.yaml`

### Model stack

Shared model framework and stage-specific network definitions.

- [Align nets](https://github.com/kail8/unsupdis-pytorch/blob/main/models/align_yolo.yaml): model-config · Alignment model variants and backbone-scale settings. · `models/align_yolo.yaml`
- [Fuse nets](https://github.com/kail8/unsupdis-pytorch/blob/main/models/fuse_yolo.yaml): model-config · Fusion model variants and backbone-scale settings. · `models/fuse_yolo.yaml`
- [Model core](https://github.com/kail8/unsupdis-pytorch/blob/main/models/yolo.py): framework · Shared YOLO-style modules and model parsing logic. · `models/yolo.py`
- [Blocks](https://github.com/kail8/unsupdis-pytorch/blob/main/models/common.py): modules · Reusable neural network building blocks shared across variants. · `models/common.py`
- [Experimental](https://github.com/kail8/unsupdis-pytorch/blob/main/models/experimental.py): modules · Auxiliary model components and variant layers. · `models/experimental.py`

### Utility core

Losses, stitching, geometry, and runtime helpers.

- [Losses](https://github.com/kail8/unsupdis-pytorch/blob/main/utils/loss.py): objective · Training objectives for alignment and reconstruction. · `utils/loss.py`
- [Dataloader](https://github.com/kail8/unsupdis-pytorch/blob/main/utils/datasets.py): data-loader · Loads paired images, applies preprocessing, and builds minibatches. · `utils/datasets.py`
- [Stitching](https://github.com/kail8/unsupdis-pytorch/blob/main/utils/stitching.py): geometry · Alignment, warping, and composition helpers. · `utils/stitching.py`
- [General utils](https://github.com/kail8/unsupdis-pytorch/blob/main/utils/general.py): helpers · Shared evaluation and image-processing utilities. · `utils/general.py`
- [Torch utils](https://github.com/kail8/unsupdis-pytorch/blob/main/utils/torch_utils.py): runtime · PyTorch device, precision, and execution helpers. · `utils/torch_utils.py`

## Connections

- UDIS-D → Data cfgs: dataset paths · Dataset manifests point to UDIS-D splits.
- WarpedCOCO → Data cfgs: dataset paths · Dataset manifests point to WarpedCOCO splits.
- YOLO weights → Train: init · Pretrained weights initialize YOLO-backed variants.
- Data cfgs → Dataloader: load · Dataset config is converted into minibatches.
- Hyperparams → Train: optimize · Training uses stage-specific hyperparameters.
- Align nets → Train: align model · Alignment architecture is selected from YAML.
- Fuse nets → Train: fuse model · Fusion architecture is selected from YAML.
- Dataloader → Train: batches · Dataloader feeds training and validation batches.
- Model core → Align nets: assemble · Shared framework assembles alignment networks.
- Model core → Fuse nets: assemble · Shared framework assembles fusion networks.
- Blocks → Model core: blocks · Common modules provide reusable layers.
- Experimental → Model core: variants · Experimental components extend model definitions.
- Losses → Train: objective · Losses drive optimization in both stages.
- Stitching → Align infer: warp · Alignment inference produces warped pairs.
- Stitching → Fuse infer: compose · Fusion inference builds the final stitched image.
- General utils → Test: metrics · Evaluation utilities support metric computation.
- General utils → Align infer: eval · General helpers support alignment evaluation outputs.
- General utils → Fuse infer: eval · General helpers support fusion output handling.
- Torch utils → Train: runtime · PyTorch runtime helpers support training execution.
- Pipeline → Train: stage 1 · Workflow starts with alignment training.
- Pipeline → Align infer: stage 2 · Workflow runs alignment inference to produce warped pairs.
- Pipeline → Fuse infer: stage 3 · Workflow runs fusion on aligned pairs.
- Train → Align infer: checkpoint flow · Trained alignment weights are reused for inference.
- Align infer → Train: warped pairs · Aligned outputs feed the fusion training path.
- Align infer → Fuse infer: input pairs · Warped/aligned pairs become fusion inputs.
- Train → Fuse infer: checkpoint flow · Fusion training consumes stage-specific weights and aligned data.
- Test → General utils: reporting · Testing uses shared helpers for metrics and output handling.
