How xpixelgroup/hypir works
1. HYPIR is an image-restoration repo centered on a Stable Diffusion 2.1-based restoration model. The core idea is to apply LoRA weights on top of a pretrained diffusion backbone rather than training a full model from scratch.
2. The repo has three main execution paths:
- interactive inference via app.py
- batch/offline inference via test.py and predict.py
- training via train.py
3. Configuration is file-driven. The main runtime behavior is controlled by YAML files in configs/, especially sd2_gradio.yaml for the demo and sd2_train.yaml for training. This makes the architecture mostly “entrypoint + config + modular subsystems.”
4. The model subsystem is split into:
- HYPIR/model/backbone.py: diffusion backbone integration
- HYPIR/model/D.py: an auxiliary discriminator-like component or support module
- HYPIR/enhancer/: higher-level restoration wrappers, including the SD2-specific implementation in sd2.py
The enhancer layer is the user-facing abstraction over the underlying model pieces.
5. Training logic lives in HYPIR/trainer/. base.py likely defines shared training orchestration, while sd2.py specializes it for the Stable Diffusion 2 setup. The training loop is designed around LoRA fine-tuning and is launched with accelerate, indicating distributed/multi-GPU support.
6. Data loading and corruption synthesis are in HYPIR/dataset/. Key parts are:
- realesrgan.py: dataset class used for restoration training, matching the README’s parquet-based image/prompt metadata flow
- batch_transform.py, degradation.py, diffjpeg.py: synthetic low-quality generation and degradation pipelines
- file_backend.py: file access abstraction
This is the main data boundary: clean image/prompt records are transformed into paired low-quality/restoration training inputs.
7. The training data flow is: parquet metadata listing image paths and prompts -> dataset loader -> degradation pipeline -> model/trainer. The README’s example shows prompts can be empty, so text conditioning is optional in the data layer but part of the model interface.
8. The inference flow is: low-quality input image + optional prompt -> enhancer/model -> restored output. In the demo path, prompts can be user-supplied or generated automatically through HYPIR/utils/captioner.py, which can call an external GPT API using environment variables. This makes text conditioning a pluggable side path rather than a hard dependency.
9. Memory/performance support is handled by utility code, especially HYPIR/utils/tiled_vae/. That subsystem suggests tiled VAE encode/decode hooks to run large-resolution inference on limited VRAM. ema.py, common.py, and tabulate.py provide standard training/runtime helpers.
10. Deployment and packaging are first-class concerns. Besides the local Gradio demo, there are repo-specific deployment files for OpenXLab and Cog (app_openxlab.py, cog.yaml). The README also points to hosted model weights on HuggingFace/OpenXLab, so the runtime assumes external pretrained LoRA weights are downloaded separately.
11. The project’s main external dependencies that affect architecture are:
- Stable Diffusion 2.1 as the base generative backbone
- HuggingFace/OpenXLab for model weight distribution
- Gradio for interactive UI
- OpenAI-compatible chat APIs for optional caption generation
- Accelerate for training orchestration
12. From an architecture diagram perspective, the important boundaries are:
- user interfaces / deployment wrappers
- config-driven orchestration
- dataset/degradation pipeline
- diffusion backbone + LoRA enhancement layer
- auxiliary utilities for captions, EMA, and tiled VAE memory management
The repo is organized more like a model-serving and model-training system than a general application stack.
Main entry points: enhancer/sd2.py SD2 enhancer · trainer/sd2.py SD2 trainer · app.py App · realesrgan.py Restoration dataset
Components 20
Entry points
Interactive, batch, and training launchers plus deployment wrappers.
- App · Interactive restoration UI entrypoint.
app.py - OpenXLab app · Deployment wrapper for hosted OpenXLab execution.
app_openxlab.py - Test · Offline evaluation/inference entrypoint.
test.py - Predict · Batch or scripted prediction entrypoint.
predict.py - Train · Training entrypoint for LoRA fine-tuning.
train.py
Configuration
YAML-driven runtime settings for demo and training.
- Demo config · Runtime settings for the interactive SD2 demo.
configs/sd2_gradio.yaml - Train config · Runtime settings for SD2 training.
configs/sd2_train.yaml
Core model
Enhancement, backbone, and auxiliary model pieces.
- Enhancer base · Shared abstraction for restoration interfaces and orchestration.
HYPIR/enhancer/base.py - SD2 enhancer · Stable Diffusion 2-specific enhancement layer exposed to callers.
HYPIR/enhancer/sd2.py - Diffusion backbone · Integration with the pretrained Stable Diffusion 2.1 base model.
HYPIR/model/backbone.py - Aux module · Auxiliary discriminator-like or support component used by the model stack.
HYPIR/model/D.py
Data pipeline
Dataset loading, file access, and synthetic degradation.
- Restoration dataset · Parquet/image-prompt dataset loader for restoration training.
HYPIR/dataset/realesrgan.py - File backend · Abstracts file access for dataset loading.
HYPIR/dataset/file_backend.py - Degradation pipe · Batch transforms and synthetic low-quality generation.
HYPIR/dataset/batch_transform.py - Degradations · Image degradation operators used to form training pairs.
HYPIR/utils/degradation.py
Training
Orchestration for LoRA fine-tuning with Accelerate.
- Trainer base · Shared training loop, scheduling, and state management.
HYPIR/trainer/base.py - SD2 trainer · Stable Diffusion 2-specific training specialization.
HYPIR/trainer/sd2.py
Utilities
Shared helpers for captions, EMA, and tiled VAE memory handling.
- Captioner · Generates prompts via external chat APIs when needed.
HYPIR/utils/captioner.py - Tiled VAE · Tiled encode/decode hooks for large-resolution inference.
HYPIR/utils/tiled_vae/__init__.py - Training helpers · EMA, runtime helpers, and formatting utilities.
HYPIR/utils/ema.py
Connections 20
- App → Demo config: loads config · Interactive demo behavior is driven by YAML settings.
- App → SD2 enhancer: serves · The Gradio app invokes the SD2 restoration wrapper.
- OpenXLab app → SD2 enhancer: serves · Hosted deployment uses the same enhancement stack.
- Test → SD2 enhancer: runs inference · Offline testing uses the restoration model.
- Predict → SD2 enhancer: runs inference · Prediction entrypoint calls the enhancement layer.
- Train → Train config: loads config · Training runtime is configured from YAML.
- Train → SD2 trainer: starts · Training entrypoint dispatches to the SD2 trainer.
- SD2 enhancer → Enhancer base: extends · SD2-specific restoration logic builds on the shared enhancer API.
- SD2 enhancer → Diffusion backbone: uses · The enhancer wraps the pretrained diffusion backbone.
- SD2 enhancer → Aux module: uses · The enhancer depends on the auxiliary model component.
- SD2 trainer → Trainer base: extends · SD2 training specializes the shared trainer orchestration.
- SD2 trainer → SD2 enhancer: optimizes · Trainer updates the SD2 restoration model and LoRA weights.
- SD2 trainer → Restoration dataset: consumes · Training reads paired restoration samples from the dataset layer.
- Restoration dataset → File backend: reads via · Dataset loading is abstracted behind a file backend.
- Restoration dataset → Degradation pipe: applies · Samples pass through synthetic degradation transforms.
- Degradation pipe → Degradations: uses · Transform pipeline relies on corruption operators.
- App → Captioner: optional prompts · Demo can auto-generate text prompts when users do not provide them.
- SD2 enhancer → Captioner: accepts prompts · Text conditioning is optional but supported by the model interface.
- SD2 enhancer → Tiled VAE: memory optimize · Large-image inference can use tiled VAE hooks.
- SD2 trainer → Training helpers: uses helpers · Training relies on shared utilities such as EMA and common helpers.