InvSR Documentation

repository·master·Indexed 23 days ago

https://github.com/zsyoaoa/invsr

InvSR is an image super-resolution technique based on diffusion inversion (CVPR 2025). It utilizes a deep noise predictor to estimate optimal noise maps, enabling efficient super-resolution with 1-5 sampling steps. The repository includes tools for inference via inference_invsr.py, a Gradio-based demo, and instructions for training and reproducing results on datasets such as Imagenet-Test and RealSRV3.

Tokens
17.7K
Snippets
21
Records
94
Agent score
80%

What's inside InvSR

  1. What are Diffusers Pipelines?

    master

    Pipelines provide a unified, high-level API to run complex diffusion models in inference. Diffusion systems typically consist of multiple independently trained components (e.g., Autoencoders, Unet, Text Encoders, Schedulers, and Safety Checkers) that must work together. Pipelines encapsulate these components, handling the pre-processing, model forwarding, and post-processing required for an end-to-end workflow.

    Key Characteristics:

    • Fidelity: They load officially published weights to yield outputs consistent with original research papers.
    • Simplicity: They offer a simple user interface for inference.
    • Inference Only: Pipelines are designed for inference and have PyTorch's autograd disabled via torch.no_grad. They are not intended for training functionality.
  2. Use Stable Diffusion without Hugging Face Hub authentication

    master

    To avoid requiring a Hugging Face authentication token during runtime, you can download the model weights locally using Git LFS and pass the local directory path to the from_pretrained method instead of a Hub model ID.

    1. Install Git LFS and clone the repository:
    git lfs install
    git clone https://huggingface.co/runwayml/stable-diffusion-v1-5
    1. Load the pipeline from the local path:
    from diffusers import StableDiffusionPipeline
    
    pipe = StableDiffusionPipeline.from_pretrained("./stable-diffusion-v1-5")
  3. Install InvSR via Conda

    master

    To set up the InvSR environment, create a conda environment named invsr and install the required dependencies including Python 3.10, PyTorch 2.4.0, and xformers 0.0.27.post2. Ensure you use the correct index URL for CUDA 12.1 support as specified in the installation steps.

    conda create -n invsr python=3.10
    conda activate invsr
    pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 --index-url https://download.pytorch.org/whl/cu121
    pip install -U xformers==0.0.27.post2 --index-url https://download.pytorch.org/whl/cu121
    pip install -e ".[torch]"
    pip install -r requirements.txt
  4. Reproduce Paper Results

    master

    To reproduce the quantitative results for Imagenet-Test and RealSRV3, use the following datasets and configuration:

    • Datasets:
      • Synthetic ImageNet-Test: [Google Drive link provided in repo]
      • RealSRV3: [External link]
      • RealSet80: Located in testdata/RealSet80
    • Color Fixing: When reproducing results, add the --color_fix wavelet option to the inference command.
  5. Prepare for Training

    master

    Before starting training, complete these steps:

    1. Download Weights: Download the finetuned LPIPS model (vgg16_sdturbo_lpips.pth) and place it in the weights folder.
    2. Configure configs/sd-turbo-sr-ldis.yaml:
      • configs.sd_pipe.params.cache_dir: Path to SD-Turbo.
      • data.train.params.data_source: Path to training data.
      • data.val.params.dir_path: Path to low-quality validation images.
      • data.val.params.extra_dir_path: Path to high-quality validation images.
      • configs.train.batch and configs.train.microbatch: Set these to control total batch size (Total = microbatch * #GPUS * num_grad_accumulation).
  6. Sample from the latent distribution

    master

    The encode method returns an OobleckDiagonalGaussianDistribution. You can interact with this distribution to either get the most likely latent (the mode) or sample from it stochastically.

    Methods:

    • mode(): Returns the mean of the distribution (the most likely latent).
    • sample(generator=None): Returns a sample drawn from the distribution using the reparameterization trick.
  7. Run invsr via Docker Compose

    master

    You can deploy the invsr Gradio interface using Docker Compose. The configuration sets up a service named gradio that exposes the web interface on port 7860.

    Key deployment details:

    • GPU Support: The configuration requires the nvidia driver and is configured to use all available GPUs (count: all).
    • Weights Persistence: Model weights are stored in a named volume invsr_weights, which is bound to the local ./weights directory on the host machine.
    • Networking: The service runs on a custom network named invsr_net.
    • Inter-Process Communication: Uses ipc: host to ensure sufficient shared memory (shm) for GPU operations.
    services:
      gradio:
        container_name: invsr-gradio
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - 7860:7860
        volumes:
          - invsr_weights:/invsr/weights/
        ipc: host
        deploy:
          resources:
            reservations:
              devices:
                - driver: nvidia
                  count: all
                  capabilities:
                    - gpu
    
    volumes:
      invsr_weights:
        name: invsr_weights
        driver: local
        driver_opts:
          type: none
          o: bind
          device: ./weights
  8. Manage memory for Kandinsky combined pipelines

    master

    To reduce VRAM usage when using Kandinsky combined pipelines, you can use the following methods:

    1. enable_model_cpu_offload(): Moves one whole model at a time to the GPU when its forward method is called. This has a lower impact on performance than sequential offloading.
    2. enable_sequential_cpu_offload(gpu_id=None, device="cuda"): Offloads all models to CPU using accelerate. It saves state dicts to CPU and loads submodules to GPU only when needed. This provides higher memory savings but lower performance.
    3. enable_xformers_memory_efficient_attention(attention_op=None): Enables xformers memory-efficient attention in the decoder pipe.
  9. Use the CLI to run training

    master

    The main.py script serves as the entrypoint for the INVSR training pipeline. It uses a combination of a YAML configuration file (via OmegaConf) and command-line arguments to initialize a trainer. Command-line arguments can be used to override specific loss coefficients and training parameters defined in the YAML config.

    To run training, execute main.py with the desired configuration path and optional overrides.