# wwangcece/crefdiff architecture

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

- Interactive diagram: https://gitdiagram.com/wwangcece/crefdiff
- Repository: https://github.com/wwangcece/crefdiff

## Overview

1. **Project purpose and overall shape**
   - This is a PyTorch-based research repo for **reference-guided remote-sensing image super-resolution** using a **latent diffusion** backbone.
   - The architecture is split into: **dataset preparation**, **image/degradation utilities**, **diffusion/latent model code**, **reference-conditioning adapters**, and **training/inference scripts**.

2. **Primary data flow**
   - Input data is organized as **paired LR/HR samples plus a reference image** for guidance.
   - The pipeline typically goes: dataset files → preprocessing/degradation and LR preparation → dataset loaders → model conditioning inputs → diffusion-based SR generation → saved outputs / metric evaluation.

3. **Dataset subsystem**
   - `dataset/` contains the project-specific data pipeline:
     - `LRHRRef.py` for paired LR/HR/reference samples.
     - `batch_transform.py` for batch-level augmentations/transforms.
     - `data_module.py` suggests a Lightning-style wrapper around train/val/test loaders.
     - `prepare_lr.py` is an offline utility to create or resize LR images to the expected resolution.
   - Configs under `configs/dataset/` define the train/val/test dataset wiring.

4. **Image processing and degradation utilities**
   - `img_utils/` is a shared support layer for image I/O, resizing, metric computation, and degradation simulation.
   - Important responsibilities include:
     - blur/noise/JPEG-like degradation,
     - sharpening and alignment/color correction,
     - restoration helpers,
     - image metrics and file utilities.
   - This layer is the bridge between raw remote-sensing imagery and the tensors consumed by the model.

5. **Core diffusion model stack**
   - `ldm/` contains the latent diffusion machinery, likely inherited/adapted from DiffBIR-style code.
   - Key pieces:
     - `ldm/models/diffusion/` for DDPM/DDIM/PLMS and DPM-Solver sampling.
     - `ldm/models/autoencoder.py` for latent-space encoding/decoding.
     - `ldm/modules/diffusionmodules/` for UNet-like building blocks, upscaling, and utility layers.
   - This is the generative engine that performs SR in latent space rather than directly in pixel space.

6. **Reference conditioning and fusion**
   - `model/refsr_ldm_adapter_dino.py` is the central project-specific model definition.
   - `model/adapters.py`, `model/mixins.py`, `model/spade.py`, and `model/modules.py` indicate that the base diffusion model is extended with **conditioning adapters** and **feature fusion blocks**.
   - The README’s “local–global fusion” maps to this layer: local details from the low-res input are combined with global semantics from the reference branch.

7. **Reference encoder / DINOv2 integration**
   - `ldm/modules/dinov2/` vendors a full DINOv2 implementation/config stack.
   - This strongly suggests the reference image is encoded with a **vision transformer backbone** to provide high-level guidance features.
   - The project-specific config `configs/model/refsr_dino.yaml` likely wires DINO features into the diffusion adapter path.

8. **Sampling and controllability**
   - `model/spaced_sampler.py` and diffusion samplers under `ldm/models/diffusion/` show multiple inference-time sampling strategies.
   - `model/cond_fn.py` and `model/callbacks.py` indicate controllable generation hooks and training/inference callbacks.
   - The `global_ref_scale` argument mentioned in the README is a control knob for reference influence at inference time.

9. **Training and evaluation workflow**
   - `configs/train_refsr.yaml` appears to be the top-level training recipe, tying together dataset, model, optimizer, and runtime settings.
   - `scripts/evaluate_img.py` and `scripts/evaluate_pfid.py` are evaluation entry points, likely for image-quality and perceptual metrics.
   - `scripts/get_latent.py` and `scripts/init_weight_refsr.py` are utility scripts for preprocessing/initialization, supporting the model’s latent-based setup.

10. **Inference entry point**
   - `inference_refsr_batch.py` is the main batch inference driver.
   - It reads a checkpoint, loads the test dataset config, runs reference-guided diffusion sampling, and writes SR results to disk.
   - This is the main “production-like” path for the repo: config → checkpoint → dataloader → conditioned sampler → output images.

11. **Supporting subsystems inherited from the parent project**
   - The repo includes `ldm/modules/midas/` and `img_utils/realesrgan/`, which are auxiliary perception/restoration components carried over from the DiffBIR ecosystem.
   - They are not the architectural center here, but they support depth/feature estimation and restoration-related preprocessing when needed.

12. **External dependencies and packaging**
   - Runtime is Python 3.9 with a PyTorch-oriented dependency stack from `requirements.txt`.
   - Pretrained weights and datasets are pulled from Hugging Face, then mounted locally under `checkpoints/` and dataset paths configured in YAML.
   - The repo is config-driven rather than hardcoded, so the architecture diagram should show **YAML configs as the orchestration layer** between data, model, and inference.

Main entry points: [`model/refsr_ldm_adapter_dino.py`](https://github.com/wwangcece/crefdiff/blob/main/model/refsr_ldm_adapter_dino.py) (RefSR adapter), [`inference_refsr_batch.py`](https://github.com/wwangcece/crefdiff/blob/main/inference_refsr_batch.py) (Batch inference), [`dataset/LRHRRef.py`](https://github.com/wwangcece/crefdiff/blob/main/dataset/LRHRRef.py) (LR/HR/Ref loader), [`dataset/data_module.py`](https://github.com/wwangcece/crefdiff/blob/main/dataset/data_module.py) (Data module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_configs["Configs"]
  node_configs_train["Train recipe<br/>yaml config<br/>[train_refsr.yaml]"]
  node_configs_model["Model config<br/>yaml config<br/>[refsr_dino.yaml]"]
  node_configs_data["Dataset configs<br/>yaml configs"]
end

subgraph group_data["Data Pipeline"]
  node_dataset_loader["LR/HR/Ref loader<br/>dataset loader<br/>[LRHRRef.py]"]
  node_dataset_module["Data module<br/>loader wrapper<br/>[data_module.py]"]
  node_dataset_transform["Batch transforms<br/>augmentation<br/>[batch_transform.py]"]
  node_dataset_prepare["LR prep<br/>offline preprocess<br/>[prepare_lr.py]"]
end

subgraph group_utils["Image Utils"]
  node_img_degrade["Degradation<br/>image ops<br/>[degradation.py]"]
  node_img_common["Image common<br/>image io<br/>[common.py]"]
  node_img_metrics["Metrics<br/>evaluation utils<br/>[metrics.py]"]
end

subgraph group_ldm["Latent Diffusion"]
  node_ldm_autoenc["Autoencoder<br/>latent codec<br/>[autoencoder.py]"]
  node_ldm_ddpm["DDPM core<br/>diffusion core<br/>[ddpm.py]"]
  node_ldm_samplers["Samplers<br/>sampling<br/>[ddim.py]"]
  node_ldm_unet["UNet blocks<br/>diffusion blocks<br/>[model.py]"]
  node_ldm_dino["DINOv2<br/>vision backbone"]
end

subgraph group_model["RefSR Model"]
  node_model_refsr["RefSR adapter<br/>project model"]
  node_model_adapters["Adapters<br/>conditioning<br/>[adapters.py]"]
  node_model_fusion["Fusion blocks<br/>conditioning ops<br/>[modules.py]"]
  node_model_sampler["Spaced sampler<br/>inference control<br/>[spaced_sampler.py]"]
end

subgraph group_scripts["Entry Points"]
  node_scripts_infer["Batch inference<br/>entry point"]
  node_scripts_eval["Evaluation<br/>entry point<br/>[evaluate_img.py]"]
  node_scripts_setup["Setup tools<br/>utility scripts"]
end

node_configs_train -->|"selects"| node_configs_data
node_configs_train -->|"selects"| node_configs_model
node_configs_data -->|"builds"| node_dataset_module
node_dataset_module -->|"loads"| node_dataset_loader
node_dataset_module -->|"applies"| node_dataset_transform
node_dataset_prepare -->|"prepares"| node_dataset_loader
node_dataset_loader -->|"uses"| node_img_common
node_dataset_loader -->|"uses"| node_img_degrade
node_img_degrade -->|"extends"| node_img_common
node_img_common -->|"supports"| node_img_metrics
node_configs_model -->|"wires"| node_model_refsr
node_model_refsr -->|"encodes"| node_ldm_autoenc
node_model_refsr -->|"drives"| node_ldm_ddpm
node_model_refsr -->|"conditions"| node_model_adapters
node_model_refsr -->|"fuses"| node_model_fusion
node_model_refsr -->|"references"| node_ldm_dino
node_model_refsr -->|"samples with"| node_model_sampler
node_ldm_ddpm -->|"uses"| node_ldm_unet
node_ldm_ddpm -->|"served by"| node_ldm_samplers
node_scripts_infer -->|"reads"| node_configs_train
node_scripts_infer -->|"loads"| node_model_refsr
node_scripts_infer -->|"streams"| node_dataset_module
node_scripts_infer -->|"writes"| node_img_common
node_scripts_eval -->|"computes"| node_img_metrics
node_scripts_setup -->|"initializes"| node_ldm_autoenc
node_scripts_setup -->|"prepares"| node_model_refsr

click node_configs_train "https://github.com/wwangcece/crefdiff/blob/main/configs/train_refsr.yaml"
click node_configs_model "https://github.com/wwangcece/crefdiff/blob/main/configs/model/refsr_dino.yaml"
click node_configs_data "https://github.com/wwangcece/crefdiff/blob/main/configs/dataset/reference_sr_train.yaml"
click node_dataset_loader "https://github.com/wwangcece/crefdiff/blob/main/dataset/LRHRRef.py"
click node_dataset_module "https://github.com/wwangcece/crefdiff/blob/main/dataset/data_module.py"
click node_dataset_transform "https://github.com/wwangcece/crefdiff/blob/main/dataset/batch_transform.py"
click node_dataset_prepare "https://github.com/wwangcece/crefdiff/blob/main/dataset/prepare_lr.py"
click node_img_degrade "https://github.com/wwangcece/crefdiff/blob/main/img_utils/degradation.py"
click node_img_common "https://github.com/wwangcece/crefdiff/blob/main/img_utils/common.py"
click node_img_metrics "https://github.com/wwangcece/crefdiff/blob/main/img_utils/metrics.py"
click node_ldm_autoenc "https://github.com/wwangcece/crefdiff/blob/main/ldm/models/autoencoder.py"
click node_ldm_ddpm "https://github.com/wwangcece/crefdiff/blob/main/ldm/models/diffusion/ddpm.py"
click node_ldm_samplers "https://github.com/wwangcece/crefdiff/blob/main/ldm/models/diffusion/ddim.py"
click node_ldm_unet "https://github.com/wwangcece/crefdiff/blob/main/ldm/modules/diffusionmodules/model.py"
click node_ldm_dino "https://github.com/wwangcece/crefdiff/blob/main/ldm/modules/dinov2/models/vision_transformer.py"
click node_model_refsr "https://github.com/wwangcece/crefdiff/blob/main/model/refsr_ldm_adapter_dino.py"
click node_model_adapters "https://github.com/wwangcece/crefdiff/blob/main/model/adapters.py"
click node_model_fusion "https://github.com/wwangcece/crefdiff/blob/main/model/modules.py"
click node_model_sampler "https://github.com/wwangcece/crefdiff/blob/main/model/spaced_sampler.py"
click node_scripts_infer "https://github.com/wwangcece/crefdiff/blob/main/inference_refsr_batch.py"
click node_scripts_eval "https://github.com/wwangcece/crefdiff/blob/main/scripts/evaluate_img.py"
click node_scripts_setup "https://github.com/wwangcece/crefdiff/blob/main/scripts/init_weight_refsr.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_configs_train,node_configs_model,node_configs_data toneBlue
class node_dataset_loader,node_dataset_module,node_dataset_transform,node_dataset_prepare toneAmber
class node_img_degrade,node_img_common,node_img_metrics toneMint
class node_ldm_autoenc,node_ldm_ddpm,node_ldm_samplers,node_ldm_unet,node_ldm_dino toneRose
class node_model_refsr,node_model_adapters,node_model_fusion,node_model_sampler toneIndigo
class node_scripts_infer,node_scripts_eval,node_scripts_setup toneTeal
```

## Components

### Configs

YAML orchestration for data, model, and training

- [Train recipe](https://github.com/wwangcece/crefdiff/blob/main/configs/train_refsr.yaml): yaml config · Top-level training configuration wiring dataset, model, optimizer, and runtime · `configs/train_refsr.yaml`
- [Model config](https://github.com/wwangcece/crefdiff/blob/main/configs/model/refsr_dino.yaml): yaml config · Reference-guided latent diffusion model configuration · `configs/model/refsr_dino.yaml`
- [Dataset configs](https://github.com/wwangcece/crefdiff/blob/main/configs/dataset/reference_sr_train.yaml): yaml configs · Train/val/test dataset wiring for reference-super-resolution · `configs/dataset/reference_sr_train.yaml`

### Data Pipeline

Dataset loading and preprocessing for reference-guided SR

- [LR/HR/Ref loader](https://github.com/wwangcece/crefdiff/blob/main/dataset/LRHRRef.py): dataset loader · Paired low-res, high-res, and reference sample dataset · `dataset/LRHRRef.py`
- [Data module](https://github.com/wwangcece/crefdiff/blob/main/dataset/data_module.py): loader wrapper · Lightning-style train/val/test dataloader orchestration · `dataset/data_module.py`
- [Batch transforms](https://github.com/wwangcece/crefdiff/blob/main/dataset/batch_transform.py): augmentation · Batch-level augmentation and transform pipeline · `dataset/batch_transform.py`
- [LR prep](https://github.com/wwangcece/crefdiff/blob/main/dataset/prepare_lr.py): offline preprocess · Offline low-resolution preparation and resizing utility · `dataset/prepare_lr.py`

### Image Utils

I/O, degradation, restoration, and metrics helpers

- [Degradation](https://github.com/wwangcece/crefdiff/blob/main/img_utils/degradation.py): image ops · Blur, noise, JPEG, sharpening, and alignment utilities · `img_utils/degradation.py`
- [Image common](https://github.com/wwangcece/crefdiff/blob/main/img_utils/common.py): image io · Shared image processing, file, and restoration helpers · `img_utils/common.py`
- [Metrics](https://github.com/wwangcece/crefdiff/blob/main/img_utils/metrics.py): evaluation utils · Image quality and comparison metrics · `img_utils/metrics.py`

### Latent Diffusion

Core latent generative backbone and sampled diffusion stack

- [Autoencoder](https://github.com/wwangcece/crefdiff/blob/main/ldm/models/autoencoder.py): latent codec · Latent-space encoder and decoder · `ldm/models/autoencoder.py`
- [DDPM core](https://github.com/wwangcece/crefdiff/blob/main/ldm/models/diffusion/ddpm.py): diffusion core · Training and reverse-diffusion process implementation · `ldm/models/diffusion/ddpm.py`
- [Samplers](https://github.com/wwangcece/crefdiff/blob/main/ldm/models/diffusion/ddim.py): sampling · DDIM, PLMS, and DPM-Solver inference strategies · `ldm/models/diffusion/ddim.py`
- [UNet blocks](https://github.com/wwangcece/crefdiff/blob/main/ldm/modules/diffusionmodules/model.py): diffusion blocks · Diffusion model building blocks and upscaling layers · `ldm/modules/diffusionmodules/model.py`
- [DINOv2](https://github.com/wwangcece/crefdiff/blob/main/ldm/modules/dinov2/models/vision_transformer.py): vision backbone · Reference-image transformer encoder stack for global guidance · `ldm/modules/dinov2/models/vision_transformer.py`

### RefSR Model

Project-specific conditioning, fusion, and sampling extensions

- [RefSR adapter](https://github.com/wwangcece/crefdiff/blob/main/model/refsr_ldm_adapter_dino.py): project model · Central reference-guided latent diffusion model definition · `model/refsr_ldm_adapter_dino.py`
- [Adapters](https://github.com/wwangcece/crefdiff/blob/main/model/adapters.py): conditioning · Feature adapters and fusion helpers for reference conditioning · `model/adapters.py`
- [Fusion blocks](https://github.com/wwangcece/crefdiff/blob/main/model/modules.py): conditioning ops · Local-global fusion and normalization helpers · `model/modules.py`
- [Spaced sampler](https://github.com/wwangcece/crefdiff/blob/main/model/spaced_sampler.py): inference control · Project-specific sampler and controllable inference wrapper · `model/spaced_sampler.py`

### Entry Points

Training, inference, evaluation, and setup scripts

- [Batch inference](https://github.com/wwangcece/crefdiff/blob/main/inference_refsr_batch.py): entry point · Checkpoint-driven batch super-resolution generation · `inference_refsr_batch.py`
- [Evaluation](https://github.com/wwangcece/crefdiff/blob/main/scripts/evaluate_img.py): entry point · Image-quality and PFID evaluation scripts · `scripts/evaluate_img.py`
- [Setup tools](https://github.com/wwangcece/crefdiff/blob/main/scripts/init_weight_refsr.py): utility scripts · Latent extraction and weight initialization utilities · `scripts/init_weight_refsr.py`

## Connections

- Train recipe → Dataset configs: selects · Training recipe wires dataset splits and loaders
- Train recipe → Model config: selects · Training recipe chooses the model definition
- Dataset configs → Data module: builds · Dataset configuration instantiates the data module
- Data module → LR/HR/Ref loader: loads · Data module wraps the paired LR/HR/reference dataset
- Data module → Batch transforms: applies · Training and validation batches are augmented/transformed
- LR prep → LR/HR/Ref loader: prepares · Offline LR preparation feeds the paired dataset
- LR/HR/Ref loader → Image common: uses · Dataset pipeline relies on shared image I/O utilities
- LR/HR/Ref loader → Degradation: uses · Dataset samples may be degraded or resized for supervision
- Degradation → Image common: extends · Degradation utilities build on common image helpers
- Image common → Metrics: supports · Shared image utilities back metric and restoration workflows
- Model config → RefSR adapter: wires · Model config instantiates the project-specific RefSR model
- RefSR adapter → Autoencoder: encodes · RefSR model uses the latent autoencoder
- RefSR adapter → DDPM core: drives · RefSR model is built on the diffusion core
- RefSR adapter → Adapters: conditions · Adapters inject reference guidance into the base model
- RefSR adapter → Fusion blocks: fuses · Local and global features are merged through fusion blocks
- RefSR adapter → DINOv2: references · DINOv2 encodes the guidance image for semantic features
- RefSR adapter → Spaced sampler: samples with · Project model uses a controllable sampling wrapper
- DDPM core → UNet blocks: uses · Diffusion core is implemented with UNet-style modules
- DDPM core → Samplers: served by · Inference uses DDIM/PLMS/DPM-Solver samplers
- Batch inference → Train recipe: reads · Inference follows config-driven runtime wiring
- Batch inference → RefSR adapter: loads · Batch inference restores the trained RefSR checkpoint
- Batch inference → Data module: streams · Inference iterates over configured test data
- Batch inference → Image common: writes · Outputs are saved through shared image utilities
- Evaluation → Metrics: computes · Evaluation scripts compute image-quality metrics
- Setup tools → Autoencoder: initializes · Utility scripts support latent setup and weight initialization
- Setup tools → RefSR adapter: prepares · Project-specific initialization targets the RefSR model
