Skip to main content

Continuous Latent Diffusion for Image Super-Resolution (CCSR) packaged as an installable library

Project description

CCSR: Improving the Stability and Efficiency of Diffusion Models for Content Consistent Super-Resolution

This repository contains CCSR-v2 (pruned), packaged as a standard, installable Python library (ccsr-pruned).


🛠️ Installation

You can install this library using either pip or uv:

Option A: From PyPI (Recommended)

pip install ccsr-pruned
# or using uv
uv add ccsr-pruned

Option B: Directly from GitHub (Latest)

pip install git+https://github.com/AI-Wrappers/ccsr-v2-pruned.git
# or using uv
uv add git+https://github.com/AI-Wrappers/ccsr-v2-pruned.git

📦 Model Weights

All required model weights (including pre-trained Stable Diffusion 2.1 Base, custom VAE, and ControlNet) can be downloaded here: 👉 HuggingFace Hub: kharma1/ccsr_v2_repost

Make sure to download:

  • stable-diffusion-2-1-base (standard SD 2.1 components).
  • controlnet weights (CCSR-specific ControlNet model).
  • vae weights (CCSR-specific fine-tuned Stage 2 VAE).

🚀 Quick Start / Usage

Here is a clean, simplified example of how to import the library and run super-resolution (SR) using the high-level CCSRUpscaler class, which automatically handles VAE tiling, image resizing, pipeline execution, and post-processing color fixes.

import torch
from PIL import Image
from ccsr import CCSRUpscaler

# 1. Setup model repository path
model_repo = "kharma1/ccsr_v2_repost"

# 2. Instantiate the upscaler (loads all subfolders directly from HF Hub)
upscaler = CCSRUpscaler(
    controlnet=(model_repo, "controlnet"),
    vae=(model_repo, "vae"),
    unet=(model_repo, "unet"),
    text_encoder=(model_repo, "text_encoder"),
    tokenizer=(model_repo, "tokenizer"),
    feature_extractor=(model_repo, "feature_extractor"),
    scheduler=(model_repo, "scheduler"),
    sample_method="ddpm",
    mixed_precision="fp16",
    tile_vae=True
)

# 4. Load low-quality (LQ) image and upscale
lq_image = Image.open("input_lq.png").convert("RGB")

sr_image = upscaler.upscale(
    image=lq_image,
    upscale=4,                        # Target upscale factor (e.g. 4x)
    num_inference_steps=6,            # Denoising steps
    t_max=0.6666,                     # Start noise level
    t_min=0.5,                        # Stop noise level (e.g. 0.5 for fast 1-step sampling)
    align_method="adain"              # Color fixing algorithm ('adain', 'wavelet', or 'nofix')
)

# 5. Save the result
sr_image.save("output_sr.png")

⚙️ CCSRUpscaler API Reference

Constructor Parameters (CCSRUpscaler(...))

The CCSRUpscaler class constructor sets up the accelerator and loads all model components.

Parameter Type Default Description
controlnet PathOrRepoParam Required Path or (repo_id, subfolder) to load custom ControlNet.
vae PathOrRepoParam Required Path or (repo_id, subfolder) to load fine-tuned Stage 2 VAE.
unet PathOrRepoParam Required Path or (repo_id, subfolder) to load UNet model.
text_encoder PathOrRepoParam Required Path or (repo_id, subfolder) to load Text Encoder.
tokenizer PathOrRepoParam Required Path or (repo_id, subfolder) to load Tokenizer.
feature_extractor PathOrRepoParam Required Path or (repo_id, subfolder) to load Feature Extractor.
scheduler PathOrRepoParam Required Path or (repo_id, subfolder) to load Scheduler.
sample_method str "ddpm" Diffusion sampler. Choices: "ddpm", "unipcmultistep", "dpmmultistep".
mixed_precision str "fp16" Mixed precision mode. Choices: "no", "fp16", "bf16".
tile_vae bool True Enable tiling inside the custom VAE (saves VRAM).
vae_encoder_tile_size int 1024 Tile size for VAE encoder.
vae_decoder_tile_size int 224 Tile size for VAE decoder.
accelerator Accelerator None Optional pre-configured HF Accelerator object.

Note: PathOrRepoParam is defined as Union[str, Tuple[str, Optional[str]]], permitting a simple local path string or a repo tuple.

Upscaling Parameters (upscaler.upscale(...))

The upscale() method executes the super-resolution pipeline.

Parameter Type Default Description
image PIL.Image Required The low-resolution PIL Image.
prompt str "clean, ..." Text prompt to guide details generation.
negative_prompt str "blurry, ..." Negative prompt to avoid artifacts.
num_inference_steps int 6 Total timeline steps configured for the scheduler.
guidance_scale float 1.0 Classifier-Free Guidance (CFG). Set higher to guide more via prompt.
conditioning_scale float 1.0 Strength of ControlNet alignment with input structure.
upscale int 4 Target upscaling factor multiplier.
process_size int 512 Minimum image dimension threshold (pre-rescales if too small).
t_max float 0.6666 Start noise scale along the scheduler timeline.
t_min float 0.5 Stop noise scale along the scheduler timeline.
start_steps int 999 Timestep index threshold when noising input.
start_point str "lr" Initialization method. "lr" (low-res latents) or "noise".
align_method str "adain" Post-processing color profile correction. "adain", "wavelet", or "nofix".
tile_diffusion bool False Enable sliding window tiling for UNet diffusion loops (saves VRAM).
tile_diffusion_size int 512 Tile size for diffusion loop.
tile_diffusion_stride int 256 Overlap stride for diffusion tiles.
use_vae_encode_condition bool True Encodes low-res condition via VAE. Must be True for optimal CCSR v2 quality.
seed int None Optional seed for reproducibility.
sample_times int 1 Number of samples to generate. Returns a list if > 1.

🛠️ Advanced Usage (Low-level Pipeline API)

If you prefer to directly interact with the underlying pipeline, you can import and call StableDiffusionControlNetCCSRPipeline directly:

from ccsr import StableDiffusionControlNetCCSRPipeline, ControlNetCCSRModel

# Load components
controlnet = ControlNetCCSRModel.from_pretrained("/workspace/models", subfolder="controlnet")
pipeline = StableDiffusionControlNetCCSRPipeline.from_pretrained("isometricneko/stable-diffusion-v2.1-clone", controlnet=controlnet)

# Run raw pipeline (requires manual resizing beforehand)
output = pipeline(
    t_max=0.6667,
    t_min=0.5,
    prompt="clean, high resolution",
    image=resized_image,
    num_inference_steps=6,
)

🎓 Citation

If you find CCSR helpful in your research or projects, please cite the original paper:

@article{sun2024improving,
  title={Improving the stability and efficiency of diffusion models for content consistent super-resolution},
  author={Sun, Lingchen and Wu, Rongyuan and Liang, Jie and Zhang, Zhengqiang and Yong, Hongwei and Zhang, Lei},
  journal={arXiv preprint arXiv:2401.00877},
  year={2024}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ccsr_pruned-2.2.0.tar.gz (118.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ccsr_pruned-2.2.0-py3-none-any.whl (67.4 kB view details)

Uploaded Python 3

File details

Details for the file ccsr_pruned-2.2.0.tar.gz.

File metadata

  • Download URL: ccsr_pruned-2.2.0.tar.gz
  • Upload date:
  • Size: 118.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for ccsr_pruned-2.2.0.tar.gz
Algorithm Hash digest
SHA256 de065bd794ed2040db7e580fd2eed39c457c80b2f20c3b245ae2c688c890a7eb
MD5 1c1f44e0fa5dbdcc3ef40d4b7ba1594c
BLAKE2b-256 5627216306fabbe70042d6df869e53487f36513dd5f1048c4a5833769de749c1

See more details on using hashes here.

File details

Details for the file ccsr_pruned-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: ccsr_pruned-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 67.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for ccsr_pruned-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c66f3024f6b13263ad6b46bd12b971ceac2aaf6054a003d7527b2d108a80427
MD5 f23f1c5f690ec0ac40007593bdf85a6b
BLAKE2b-256 ca0aad2f47726f174b38e21ce2975ec3f17e75591c9e5524becc7ffd8e0dc786

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page