How wwangcece/crefdiff works
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: refsr_ldm_adapter_dino.py RefSR adapter · inference_refsr_batch.py Batch inference · LRHRRef.py LR/HR/Ref loader · data_module.py Data module
Components 22
Configs
YAML orchestration for data, model, and training
- Train recipe · Top-level training configuration wiring dataset, model, optimizer, and runtime
configs/train_refsr.yaml - Model config · Reference-guided latent diffusion model configuration
configs/model/refsr_dino.yaml - Dataset 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 · Paired low-res, high-res, and reference sample dataset
dataset/LRHRRef.py - Data module · Lightning-style train/val/test dataloader orchestration
dataset/data_module.py - Batch transforms · Batch-level augmentation and transform pipeline
dataset/batch_transform.py - LR prep · Offline low-resolution preparation and resizing utility
dataset/prepare_lr.py
Image Utils
I/O, degradation, restoration, and metrics helpers
- Degradation · Blur, noise, JPEG, sharpening, and alignment utilities
img_utils/degradation.py - Image common · Shared image processing, file, and restoration helpers
img_utils/common.py - Metrics · Image quality and comparison metrics
img_utils/metrics.py
Latent Diffusion
Core latent generative backbone and sampled diffusion stack
- Autoencoder · Latent-space encoder and decoder
ldm/models/autoencoder.py - DDPM core · Training and reverse-diffusion process implementation
ldm/models/diffusion/ddpm.py - Samplers · DDIM, PLMS, and DPM-Solver inference strategies
ldm/models/diffusion/ddim.py - UNet blocks · Diffusion model building blocks and upscaling layers
ldm/modules/diffusionmodules/model.py - DINOv2 · 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 · Central reference-guided latent diffusion model definition
model/refsr_ldm_adapter_dino.py - Adapters · Feature adapters and fusion helpers for reference conditioning
model/adapters.py - Fusion blocks · Local-global fusion and normalization helpers
model/modules.py - Spaced sampler · Project-specific sampler and controllable inference wrapper
model/spaced_sampler.py
Entry Points
Training, inference, evaluation, and setup scripts
- Batch inference · Checkpoint-driven batch super-resolution generation
inference_refsr_batch.py - Evaluation · Image-quality and PFID evaluation scripts
scripts/evaluate_img.py - Setup tools · Latent extraction and weight initialization utilities
scripts/init_weight_refsr.py
Connections 26
- 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