### Launch InvSR Online Demo Source: https://github.com/zsyoaoa/invsr/blob/master/README.md Start the web-based demo for the InvSR method. This command launches a local server, typically accessible via a web browser. ```python python app.py ``` -------------------------------- ### Deploy InvSR with Docker Source: https://github.com/zsyoaoa/invsr/blob/master/README.md Deploy the InvSR project using Docker Compose. This command starts the necessary services in detached mode and provides the URL to access the application. ```bash docker compose up -d # Access the demo at: http://127.0.0.1:7860/ ``` -------------------------------- ### Paint by Example Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Implements the Paint by Example pipeline for image generation based on example images. ```python from diffusers import PaintByExamplePipeline pipe = PaintByExamplePipeline.from_pretrained("diffusers/paint_by_example_128") ``` -------------------------------- ### Image-to-Image Text-Guided Generation with Stable Diffusion (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers/pipelines/README.md Generates new images based on a text prompt and an initial image using Stable Diffusion. This pipeline allows for image transformation guided by text. Requires 'diffusers', 'requests', and 'Pillow'. Outputs a PNG image file. ```python import requests from PIL import Image from io import BytesIO from diffusers import StableDiffusionImg2ImgPipeline # load the pipeline device = "cuda" pipe = StableDiffusionImg2ImgPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, ).to(device) # let's download an initial image url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg" response = requests.get(url) init_image = Image.open(BytesIO(response.content)).convert("RGB") init_image = init_image.resize((768, 512)) prompt = "A fantasy landscape, trending on artstation" images = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images images[0].save("fantasy_landscape.png") ``` -------------------------------- ### Text-to-Image Generation with DDIM Scheduler (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers/pipelines/stable_diffusion/README.md Illustrates how to perform text-to-image generation using Stable Diffusion with a custom DDIM scheduler. The example shows loading a specific scheduler configuration and then initializing the pipeline with it, followed by image generation and saving. ```python # make sure you're logged in with `huggingface-cli login` from diffusers import StableDiffusionPipeline, DDIMScheduler scheduler = DDIMScheduler.from_pretrained("CompVis/stable-diffusion-v1-4", subfolder="scheduler") pipe = StableDiffusionPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", scheduler=scheduler, ).to("cuda") prompt = "a photo of an astronaut riding a horse on mars" image = pipe(prompt).images[0] image.save("astronaut_rides_horse.png") ``` -------------------------------- ### Text-to-Image Generation with K-LMS Scheduler (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers/pipelines/stable_diffusion/README.md This snippet demonstrates text-to-image generation using Stable Diffusion with the K-LMS scheduler. It follows a similar pattern to other scheduler examples, involving loading the scheduler and then the pipeline before generating and saving the image. ```python # make sure you're logged in with `huggingface-cli login` from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler scheduler = LMSDiscreteScheduler.from_pretrained("CompVis/stable-diffusion-v1-4", subfolder="scheduler") pipe = StableDiffusionPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", scheduler=scheduler, ).to("cuda") prompt = "a photo of an astronaut riding a horse on mars" image = pipe(prompt).images[0] image.save("astronaut_rides_horse.png") ``` -------------------------------- ### Text-to-Image Generation with PLMS Scheduler (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers/pipelines/stable_diffusion/README.md Provides a Python code example for generating an image from a text prompt using Stable Diffusion with the default PLMS scheduler. It includes moving the pipeline to a CUDA-enabled GPU for faster inference and saving the generated image. ```python # make sure you're logged in with `huggingface-cli login` from diffusers import StableDiffusionPipeline pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5") pipe = pipe.to("cuda") prompt = "a photo of an astronaut riding a horse on mars" image = pipe(prompt).images[0] image.save("astronaut_rides_horse.png") ``` -------------------------------- ### Version Utility Functions Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Utilities for checking and managing library versions. Ensures compatibility and provides information about the installed Diffusers version. ```python from diffusers.utils.versions import VERSION # Example usage: print(f"Diffusers version: {VERSION}") ``` -------------------------------- ### Text-to-Video Synthesis Pipeline (Image-to-Video) Implementation Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This Python file implements the image-to-video functionality for text-to-video synthesis. It allows users to generate a video based on an initial image and a text prompt, providing more control over the video's starting point. Dependencies include PyTorch and Diffusers. ```python from diffusers import TextToVideoPipeline from diffusers.utils import load_image import torch pipe = TextToVideoPipeline.from_pretrained("cerspense/distil-video-diffusion-2-1-ms-sdxl-text-to-video", torch_dtype=torch.float16) pipe.to("cuda") image = load_image("path/to/your/image.png") image = image.resize((1024, 576)) prompt = "A dog running in a park" output = pipe(prompt, image=image, num_inference_steps=1, guidance_scale=1.0) video = output.videos[0] # Save the video # ... (code to save video) ``` -------------------------------- ### Versatile Diffusion Dual Guided Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This pipeline supports dual guidance for Versatile Diffusion, allowing for generation conditioned on two different inputs, such as text and an image. ```python from diffusers import VersatileDiffusionDualGuidedPipeline pipeline = VersatileDiffusionDualGuidedPipeline.from_pretrained("path/to/model") result = pipeline("prompt", image="input image path").images[0] ``` -------------------------------- ### Import Utility Functions Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Helper functions for managing imports and checking for the availability of optional dependencies. Ensures smooth operation even when certain libraries are not installed. ```python from diffusers.utils.import_utils import is_flax_available, is_onnx_available # Example usage: if is_flax_available(): print("Flax is available") ``` -------------------------------- ### Begin Training InvSR Source: https://github.com/zsyoaoa/invsr/blob/master/README.md Initiate the training process for the InvSR model using PyTorch's distributed training utility (`torchrun`). This command requires specifying the GPUs to use, the number of processes per node, and the logging directory. Configuration for SD-Turbo path, training/validation data paths, and batch sizes should be set in the YAML config file. ```bash CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --standalone --nproc_per_node=4 --nnodes=1 main.py --save_dir [Logging Folder] ``` -------------------------------- ### In-painting with Stable Diffusion Pipeline (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers/pipelines/README.md This Python code snippet demonstrates how to use the `StableDiffusionInpaintPipeline` to perform image in-painting. It involves downloading an initial image and a mask, initializing the pipeline, and then generating a new image based on a text prompt and the provided mask. Dependencies include `PIL`, `requests`, `torch`, and `diffusers`. The input is an image URL, a mask URL, and a text prompt. The output is an in-painted image. ```python import PIL import requests import torch from io import BytesIO from diffusers import StableDiffusionInpaintPipeline def download_image(url): response = requests.get(url) return PIL.Image.open(BytesIO(response.content)).convert("RGB") img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" init_image = download_image(img_url).resize((512, 512)) mask_image = download_image(mask_url).resize((512, 512)) pipe = StableDiffusionInpaintPipeline.from_pretrained( "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16, ) pipe = pipe.to("cuda") prompt = "Face of a yellow cat, high resolution, sitting on a park bench" image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0] ``` -------------------------------- ### Text-to-Image Generation with Stable Diffusion (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers/pipelines/README.md Generates an image from a text prompt using the Stable Diffusion pipeline. Requires the 'diffusers' library and authentication with 'huggingface-cli login'. Outputs a PNG image file. ```python from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler # make sure you're logged in with `huggingface-cli login` pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5") pipe = pipe.to("cuda") prompt = "a photo of an astronaut riding a horse on mars" image = pipe(prompt).images[0] image.save("astronaut_rides_horse.png") ``` -------------------------------- ### Launch and Use InvSR Gradio Web Interface for Super-Resolution Source: https://context7.com/zsyoaoa/invsr/llms.txt This section details how to launch the Gradio web interface for InvSR, enabling interactive super-resolution tasks. It also provides instructions for using Docker for deployment and shows programmatic usage of the prediction functions for single image and batch processing. The interface allows configuration of sampling steps, chopping size, and random seeds. ```python # Start Gradio demo python app.py # Opens at http://127.0.0.1:7860 # Or use Docker docker compose up -d # Access at http://127.0.0.1:7860 # Programmatic usage of prediction functions from app import predict_single, process_batch # Single image processing output_image, output_path = predict_single( in_path="./low_res_image.png", num_steps=1, # 1-5 sampling steps chopping_size=128, # Patch size (128 or 256) seed=12345 # Random seed for reproducibility ) # output_image: numpy array (H, W, 3), uint8 RGB # output_path: str, path to saved PNG file # Batch processing status_message = process_batch( input_dir="./image_folder", num_steps=1, chopping_size=128, seed=12345 ) # Results saved to input_dir/invsr_output/ ``` -------------------------------- ### Kandinsky 2.2 Prior Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt The prior model pipeline for Kandinsky 2.2, responsible for generating initial representations to guide image synthesis. ```python from diffusers import KandinskyV22PriorPipeline pipeline = KandinskyV22PriorPipeline.from_pretrained("path/to/model") result = pipeline("prompt").images[0] ``` -------------------------------- ### Kandinsky 2.2 ControlNet Image-to-Image Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Combines ControlNet conditioning with image-to-image translation for Kandinsky 2.2. This enables guided image transformations. ```python from diffusers import KandinskyV22ControlnetImg2ImgPipeline pipeline = KandinskyV22ControlnetImg2ImgPipeline.from_pretrained("path/to/model") result = pipeline(prompt="prompt", image="input image path", control_image="control image path").images[0] ``` -------------------------------- ### PAG ControlNet SD Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt PAG pipeline integrated with ControlNet for Stable Diffusion. This allows for guided image generation based on control signals. ```python from diffusers import PAGControlNetPipediffusionPipeline pipeline = PAGControlNetPipediffusionPipeline.from_pretrained("path/to/model") result = pipeline(prompt="prompt", image="control image path").images[0] ``` -------------------------------- ### Create Dataset (Python) Source: https://context7.com/zsyoaoa/invsr/llms.txt This snippet shows how to create a dataset using the provided `create_dataset` function from the `datapipe.datasets` module. It implies the existence of a `BaseData` class for dataset implementation. ```python from datapipe.datasets import create_dataset, BaseData ``` -------------------------------- ### Kandinsky Prior Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt The prior model pipeline for Kandinsky. This component is responsible for generating embeddings or initial representations that guide the main diffusion process. ```python from diffusers import KandinskyPriorPipeline pipeline = KandinskyPriorPipeline.from_pretrained("path/to/model") result = pipeline("prompt").images[0] ``` -------------------------------- ### Calculate Image Quality Metrics (Bash) Source: https://context7.com/zsyoaoa/invsr/llms.txt Command-line scripts for calculating reference and non-reference image quality metrics. Requires specifying directories for predicted and ground truth images, and the desired metrics. ```bash # Reference metrics (PSNR, SSIM, LPIPS) - requires ground truth python scripts/cal_metrics_ref.py \ --pred_dir ./results \ --gt_dir ./ground_truth \ --metric psnr ssim lpips # Non-reference metrics (NIQE, BRISQUE, etc.) python scripts/cal_metrics_nonref.py \ --pred_dir ./results \ --metric niqe brisque ``` -------------------------------- ### Create Datasets from Configuration (Python) Source: https://context7.com/zsyoaoa/invsr/llms.txt Defines configurations for creating image datasets, including base datasets and RealESRGAN-style degradation datasets. Supports paired low-quality/high-quality data and specifies transformations and data access parameters. ```python dataset_config = { 'type': 'base', 'params': { 'dir_path': './train_images', 'transform_type': 'default', 'transform_kwargs': {'mean': 0.0, 'std': 1.0}, 'need_path': True, 'recursive': False, 'length': None, # None for all images 'im_exts': ['png', 'jpg', 'jpeg', 'JPEG', 'bmp'] } } dataset = create_dataset(dataset_config) # With paired LQ/HQ data for validation val_config = { 'type': 'base', 'params': { 'dir_path': './val_lq', # Low-quality images 'extra_dir_path': './val_hq', # High-quality ground truth 'transform_type': 'default', 'transform_kwargs': {'mean': 0.0, 'std': 1.0}, 'extra_transform_type': 'default', 'extra_transform_kwargs': {'mean': 0.5, 'std': 0.5}, 'need_path': True } } val_dataset = create_dataset(val_config) # Access data sample = dataset[0] # sample['lq']: Transformed input tensor (c x h x w) # sample['gt']: Ground truth tensor (if extra_dir_path provided) # sample['path']: Original file path (if need_path=True) # RealESRGAN-style degradation dataset for training train_config = { 'type': 'realesrgan', 'params': { 'gt_size': 512, 'gt_path': './train_hq_images', 'use_hflip': True, 'use_rot': True, # ... degradation parameters } } train_dataset = create_dataset(train_config) ``` -------------------------------- ### Convert Kandinsky 3 UNet Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Utility script for converting a Kandinsky 3 UNet model. This is useful for adapting models or preparing them for specific inference setups. ```python import torch from diffusers.models import UNet2DConditionModel # Load original model original_model = UNet2DConditionModel.from_pretrained("path/to/original/unet") # Save converted model converted_model = UNet2DConditionModel.from_config(original_model.config) converted_model.load_state_dict(original_model.state_dict()) converted_model.save_pretrained("path/to/converted/unet") ``` -------------------------------- ### Stable Diffusion Model Editing Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This pipeline facilitates editing of Stable Diffusion models. It allows for targeted modifications to the model's output based on specific instructions or examples. ```python from diffusers import StableDiffusionModelEditingPipeline pipeline = StableDiffusionModelEditingPipeline.from_pretrained("path/to/model") result = pipeline("prompt", target_image=image, editing_prompt="editing instructions").images[0] ``` -------------------------------- ### Reproduce Paper Results with Color Fixing Source: https://github.com/zsyoaoa/invsr/blob/master/README.md Instructions to reproduce quantitative results from the paper using specific datasets and applying color fixing. The `--color_fix wavelet` option is crucial for accurate reproduction. ```python python inference_invsr.py -i [image folder/image path] -o [result folder] --color_fix wavelet ``` -------------------------------- ### Text-to-Video Zero SDXL Pipeline Implementation Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This Python file implements the Text-to-Video Zero pipeline specifically leveraging the Stable Diffusion XL (SDXL) model. This aims to provide high-quality video generation from text prompts using the advanced capabilities of SDXL. Dependencies include PyTorch and Diffusers. ```python from diffusers import TextToVideoZeroPipeline import torch pipe = TextToVideoZeroPipeline.from_pretrained("cerspense/distil-video-diffusion-2-1-ms-sdxl-text-to-video", torch_dtype=torch.float16) pipe.to("cuda") prompt = "A futuristic cityscape at night." output = pipe(prompt, num_inference_steps=1, guidance_scale=1.0) video = output.videos[0] # Save the video # ... (code to save video) ``` -------------------------------- ### Unclip Image Variation Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This Python file implements the image variation functionality for the Unclip pipeline. It allows users to generate variations of an existing image, potentially guided by text prompts or other conditioning. Dependencies include PyTorch and Diffusers. ```python from diffusers import UnCLIPImageVariationPipeline from diffusers.utils import load_image import torch pipe = UnCLIPImageVariationPipeline.from_pretrained("google/ddpm-ema-kl-128", torch_dtype=torch.float16) pipe.to("cuda") image = load_image("path/to/your/image.png") prompt = "A painting of a landscape" image_variation = pipe(prompt, image=image, guidance_scale=1.0).images[0] image_variation.save("unclip_variation.png") ``` -------------------------------- ### Resume InvSR Training from Interruption Source: https://github.com/zsyoaoa/invsr/blob/master/README.md Continue a previously interrupted training session for the InvSR model. This command uses `torchrun` and requires specifying the GPUs, process count, logging directory, and the path to the checkpoint file to resume from. ```bash CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --standalone --nproc_per_node=4 --nnodes=1 main.py --save_dir [Logging Folder] --resume save_dir/ckpts/model_xx.pth ``` -------------------------------- ### Initialize and Use InvSamplerSR for Super-Resolution Inference Source: https://context7.com/zsyoaoa/invsr/llms.txt This Python code demonstrates how to initialize and use the InvSamplerSR class for image super-resolution. It involves loading configurations, setting up timesteps based on the desired number of steps, configuring image chopping parameters, and then performing inference on single images or directories. The sampler handles the diffusion pipeline and noise predictor for generating high-resolution outputs. ```python from omegaconf import OmegaConf from sampler_invsr import InvSamplerSR from utils import util_image # Load configuration configs = OmegaConf.load("./configs/sample-sd-turbo.yaml") # Configure timesteps based on number of steps num_steps = 1 if num_steps == 1: configs.timesteps = [200] elif num_steps == 2: configs.timesteps = [200, 100] elif num_steps == 3: configs.timesteps = [200, 100, 50] elif num_steps == 4: configs.timesteps = [200, 150, 100, 50] elif num_steps == 5: configs.timesteps = [250, 200, 150, 100, 50] # Set paths configs.sd_pipe.params.cache_dir = "./weights" configs.model_start.ckpt_path = "./weights/noise_predictor_sd_turbo_v5.pth" # Configure chopping for large images configs.basesr.chopping.pch_size = 256 # Use 256 for 4K images configs.basesr.chopping.extra_bs = 4 # Batch size for patches # Initialize sampler sampler = InvSamplerSR(configs) # Process single image im_lq = util_image.imread("./input.png", chn='rgb', dtype='float32') im_tensor = util_image.img2tensor(im_lq).cuda() result = sampler.sample_func(im_tensor) # Returns h x w x c numpy array [0,1] util_image.imwrite(result.squeeze(0), "./output.png", dtype_in='float32') # Process directory sampler.inference( in_path="./input_folder", out_path="./output_folder", bs=1 # Batch size for loading images ) ``` -------------------------------- ### Run Inference with InvSR Source: https://github.com/zsyoaoa/invsr/blob/master/README.md Execute the inference script for image super-resolution. Supports specifying input/output paths, handling large images with chopping size, and using pre-downloaded models for SD Turbo and noise predictors. Options for number of sampling steps and GPU memory optimization are also available. ```python python inference_invsr.py -i [image folder/image path] -o [result folder] --num_steps 1 # For large images (e.g., 1k -> 4k): python inference_invsr.py -i [image folder/image path] -o [result folder] --num_steps 1 --chopping_size 256 # Specify pre-downloaded SD Turbo model: python inference_invsr.py -i [image folder/image path] -o [result folder] --num_steps 1 --sd_path /path/to/sd-turbo # Specify pre-downloaded noise predictor: python inference_invsr.py -i [image folder/image path] -o [result folder] --num_steps 1 --started_ckpt_path /path/to/noise_predictor # Optimize for limited GPU memory: python inference_invsr.py -i [image folder/image path] -o [result folder] --num_steps 1 --chopping_bs 1 ``` -------------------------------- ### Gradio Web Interface Source: https://context7.com/zsyoaoa/invsr/llms.txt Launch an interactive web demo for single image or batch processing with configurable parameters via Gradio. Also provides programmatic access to prediction functions. ```APIDOC ## Web Interface & Programmatic Access ### Description Access the InvSR functionality through an interactive Gradio web interface or use the provided prediction functions (`predict_single`, `process_batch`) directly in your Python code for streamlined integration. ### Method CLI Command / Docker / Python Function Call ### Endpoint - Web UI: `http://127.0.0.1:7860` (default) - Docker: `http://127.0.0.1:7860` (default) ### Parameters #### CLI / Docker Arguments (refer to Core Inference API for details) - **-i, --in_path**: Input image or folder path - **-o, --out_path**: Output folder for results - **--num_steps**: Number of inference steps (1-5, default: 1) - **--chopping_size**: Patch size for large images (default: 128, use 256 for large images) - **--chopping_bs**: Batch size for chopped patches (default: 8, use 1 for limited GPU memory) - **--sd_path**: Path to pre-downloaded SD Turbo model - **--started_ckpt_path**: Path to noise predictor checkpoint - **--tiled_vae**: Enable tiled VAE (default: true) - **--color_fix**: Color fixing method ('wavelet' or 'ycbcr') - **-t, --timesteps**: Custom timesteps list (e.g., -t 200 100 50) #### Programmatic Function Parameters (`predict_single`, `process_batch`) - **in_path** (string) - Required - Path to the input image or directory. - **num_steps** (integer) - Optional - Number of sampling steps (1-5, default: 1). - **chopping_size** (integer) - Optional - Patch size (128 or 256, default: 128). - **seed** (integer) - Optional - Random seed for reproducibility (default: None). - **output_path** (string) - Optional - Specific output path for `predict_single` (default: None, saves to input directory). ### Request Example #### Launching Web UI ```bash python app.py ``` #### Using Docker ```bash docker compose up -d ``` #### Programmatic Usage ```python from app import predict_single, process_batch # Single image processing output_image, output_path = predict_single( in_path="./low_res_image.png", num_steps=1, chopping_size=128, seed=12345 ) # output_image: numpy array (H, W, 3), uint8 RGB # output_path: str, path to saved PNG file # Batch processing status_message = process_batch( input_dir="./image_folder", num_steps=1, chopping_size=128, seed=12345 ) # Results saved to input_dir/invsr_output/ ``` ### Response #### Success Response - **Web UI**: Renders an interactive interface in the browser. - **Docker**: Starts the web service, accessible via the specified URL. - **`predict_single`**: Returns a tuple containing the super-resolved image as a NumPy array and the path where it was saved. - **`process_batch`**: Returns a status message string indicating the completion of batch processing. #### Response Example (`predict_single`) ```json { "output_image": "", "output_path": "./low_res_image_invsr.png" } ``` #### Response Example (`process_batch`) ```json { "status_message": "Batch processing completed successfully. Results saved to ./image_folder/invsr_output/" } ``` ``` -------------------------------- ### Wuerstchen Prior Modeling Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This snippet likely contains the implementation for the 'prior' model within the Wuerstchen framework. The prior model often guides the diffusion process, potentially by modeling the distribution of latent representations or providing conditioning information. Dependencies would include PyTorch. ```python import torch from torch import nn class WuerstchenPrior(nn.Module): def __init__(self, config): super().__init__() # ... (Prior model architecture) ... def forward(self, class_labels=None, conditioning_tensor=None): # ... (logic to generate prior distribution or embeddings) ... return prior_representation # Example usage: # config = {...} # prior_model = WuerstchenPrior(config) # prior_output = prior_model(class_labels=torch.tensor([1])) ``` -------------------------------- ### Text-to-Video Zero Pipeline Implementation Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This snippet implements the Text-to-Video Zero pipeline. It focuses on generating videos from text prompts with potentially fewer parameters or a different approach compared to other text-to-video models. Dependencies include PyTorch and Diffusers. ```python from diffusers import TextToVideoZeroPipeline import torch pipe = TextToVideoZeroPipeline.from_pretrained("cerspense/distil-video-diffusion-2-1-ms-sdxl-text-to-video", torch_dtype=torch.float16) pipe.to("cuda") prompt = "A person walking on the moon." output = pipe(prompt, num_inference_steps=1, guidance_scale=1.0) video = output.videos[0] # Save the video # ... (code to save video) ``` -------------------------------- ### InvSamplerSR Class Usage Source: https://context7.com/zsyoaoa/invsr/llms.txt Demonstrates how to use the InvSamplerSR class programmatically for super-resolution inference, including configuration loading, sampler initialization, and processing single images or directories. ```APIDOC ## Python API: InvSamplerSR ### Description Use the `InvSamplerSR` class to perform super-resolution inference programmatically within your Python applications. This involves loading configurations, initializing the sampler, and then calling its inference methods. ### Method Python Class Method ### Endpoint N/A (Python Class) ### Parameters #### Initialization Parameters (for `InvSamplerSR` constructor) - **configs** (OmegaConf object) - Configuration object loaded from a YAML file, specifying model paths, timesteps, and other inference parameters. #### Method Parameters (`sampler.sample_func`) - **im_tensor** (torch.Tensor) - Input low-resolution image tensor (e.g., C x H x W), moved to CUDA. #### Method Parameters (`sampler.inference`) - **in_path** (string) - Path to the input image or directory. - **out_path** (string) - Path to the output directory. - **bs** (integer) - Batch size for loading images from the input directory (default: 1). ### Request Example ```python from omegaconf import OmegaConf from sampler_invsr import InvSamplerSR from utils import util_image # Load configuration configs = OmegaConf.load("./configs/sample-sd-turbo.yaml") # Configure timesteps based on number of steps num_steps = 3 if num_steps == 1: configs.timesteps = [200] elif num_steps == 2: configs.timesteps = [200, 100] elif num_steps == 3: configs.timesteps = [200, 100, 50] elif num_steps == 4: configs.timesteps = [200, 150, 100, 50] elif num_steps == 5: configs.timesteps = [250, 200, 150, 100, 50] # Set paths configs.sd_pipe.params.cache_dir = "./weights" configs.model_start.ckpt_path = "./weights/noise_predictor_sd_turbo_v5.pth" # Configure chopping for large images configs.basesr.chopping.pch_size = 256 # Use 256 for 4K images configs.basesr.chopping.extra_bs = 4 # Batch size for patches # Initialize sampler sampler = InvSamplerSR(configs) # Process single image im_lq = util_image.imread("./input.png", chn='rgb', dtype='float32') im_tensor = util_image.img2tensor(im_lq).cuda() result = sampler.sample_func(im_tensor) # Returns h x w x c numpy array [0,1] util_image.imwrite(result.squeeze(0), "./output.png", dtype_in='float32') # Process directory sampler.inference( in_path="./input_folder", out_path="./output_folder", bs=1 # Batch size for loading images ) ``` ### Response #### Success Response - **result** (numpy.ndarray) - For `sample_func`, returns the super-resolved image as a NumPy array (H x W x C) with values in the range [0, 1]. - **None** - For `inference`, the method saves results to disk and does not return a value. #### Response Example (for `sample_func`) ```python # result is a numpy array representing the high-resolution image ``` ``` -------------------------------- ### Run InvSR Super-Resolution via Command-Line Interface Source: https://context7.com/zsyoaoa/invsr/llms.txt Execute image super-resolution using the InvSR CLI. This script supports specifying input/output paths, the number of inference steps, and options for handling large images and custom model checkpoints. It allows for detailed configuration including color fixing methods and batch sizes for chopped image patches. ```python python inference_invsr.py -i ./input_images -o ./output_results --num_steps 1 python inference_invsr.py -i ./large_image.png -o ./results --num_steps 1 --chopping_size 256 python inference_invsr.py \ -i ./testdata/RealSet80 \ -o ./results \ --num_steps 3 \ --sd_path ./weights \ --started_ckpt_path ./weights/noise_predictor_sd_turbo_v5.pth \ --color_fix wavelet \ --chopping_bs 1 ``` -------------------------------- ### Load Stable Diffusion Model from Local Path (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers/pipelines/stable_diffusion/README.md Shows how to load Stable Diffusion model weights from a local directory after cloning the repository. This approach avoids the need for Hugging Face Hub authentication and is useful for offline use or custom deployments. ```bash git lfs install git clone https://huggingface.co/runwayml/stable-diffusion-v1-5 ``` ```python from diffusers import StableDiffusionPipeline pipe = StableDiffusionPipeline.from_pretrained("./stable-diffusion-v1-5") ``` -------------------------------- ### Core Inference API Source: https://context7.com/zsyoaoa/invsr/llms.txt The main inference entry point for running super-resolution on images or directories. Supports configurable sampling steps, chopping for large images, and color correction. ```APIDOC ## POST /inference ### Description Run super-resolution on input images or directories using the InvSR model. This endpoint supports configurable sampling steps, chopping for large images, and color correction. ### Method POST ### Endpoint /inference ### Parameters #### Query Parameters - **in_path** (string) - Required - Input image or folder path - **out_path** (string) - Required - Output folder for results - **num_steps** (integer) - Optional - Number of inference steps (1-5, default: 1) - **chopping_size** (integer) - Optional - Patch size for large images (default: 128, use 256 for large images) - **chopping_bs** (integer) - Optional - Batch size for chopped patches (default: 8, use 1 for limited GPU memory) - **sd_path** (string) - Optional - Path to pre-downloaded SD Turbo model - **started_ckpt_path** (string) - Optional - Path to noise predictor checkpoint - **tiled_vae** (boolean) - Optional - Enable tiled VAE (default: true) - **color_fix** (string) - Optional - Color fixing method ('wavelet' or 'ycbcr') - **timesteps** (list of integers) - Optional - Custom timesteps list (e.g., [200, 100, 50]) ### Request Example ```json { "in_path": "./input_images", "out_path": "./output_results", "num_steps": 1, "chopping_size": 256, "color_fix": "wavelet" } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message indicating the inference process has started or completed. - **output_path** (string) - Path to the directory where the results are saved. #### Response Example ```json { "message": "Super-resolution inference completed successfully.", "output_path": "./output_results" } ``` ``` -------------------------------- ### Dummy Object Creation Utilities Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Functions for creating dummy objects required for testing and compatibility checks with various libraries like Flax, Transformers, ONNX, PyTorch, etc. These avoid actual imports during certain test scenarios. ```python from diffusers.utils.dummy_flax_objects import FlaxUNet2DConditionModel from diffusers.utils.dummy_pt_objects import PyTorchUNet2DConditionModel # Example usage: flax_unet = FlaxUNet2DConditionModel() pt_unet = PyTorchUNet2DConditionModel() ``` -------------------------------- ### Inference Configuration (YAML) Source: https://context7.com/zsyoaoa/invsr/llms.txt This YAML configuration file defines settings for Stable Diffusion Turbo inference, including scale factor, VAE tiling, classifier-free guidance, and model parameters for the base model and noise predictor. ```yaml # configs/sample-sd-turbo.yaml - Inference configuration seed: 12345 # Super-resolution settings basesr: sf: 4 # Scale factor (4x upscaling) chopping: pch_size: 128 # Patch size for processing weight_type: Gaussian # Blending weight type # VAE settings for memory efficiency tiled_vae: True latent_tiled_size: 128 sample_tiled_size: 1024 gradient_checkpointing_vae: True sliced_vae: False # Classifier-free guidance (1.0 = disabled) cfg_scale: 1.0 # Starting timestep for diffusion start_timesteps: 200 # Color correction (null, 'wavelet', or 'ycbcr') color_fix: ~ # Stable Diffusion pipeline base_model: sd-turbo sd_pipe: target: diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline enable_grad_checkpoint: True params: pretrained_model_name_or_path: stabilityai/sd-turbo cache_dir: ./weights use_safetensors: True torch_dtype: torch.float16 # Noise predictor architecture model_start: target: diffusers.models.autoencoders.NoisePredictor ckpt_path: ~ params: in_channels: 3 down_block_types: [AttnDownBlock2D, AttnDownBlock2D] up_block_types: [AttnUpBlock2D, AttnUpBlock2D] block_out_channels: [256, 512] layers_per_block: [3, 3] act_fn: silu latent_channels: 4 norm_num_groups: 32 sample_size: 128 mid_block_add_attention: True double_z: True ``` -------------------------------- ### Text-to-Video Synthesis Pipeline (Basic) Implementation Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This Python snippet implements a basic text-to-video synthesis pipeline. It takes a text prompt and generates a corresponding video clip. Dependencies include PyTorch and Diffusers. ```python from diffusers import TextToVideoPipeline pipe = TextToVideoPipeline.from_pretrained("cerspense/distil-video-diffusion-2-1-ms-sdxl-text-to-video", torch_dtype=torch.float16) pipe.to("cuda") prompt = "A cat playing with a ball of yarn." output = pipe(prompt, num_inference_steps=1, guidance_scale=1.0) video = output.videos[0] # Save the video # ... (code to save video) ``` -------------------------------- ### Load Stable Diffusion Model from Hugging Face Hub (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers/pipelines/stable_diffusion/README.md Demonstrates how to load the Stable Diffusion model weights directly from the Hugging Face Hub using the `DiffusionPipeline` class. This method requires authentication if accessing private models or for certain usage quotas. ```python from diffusers import DiffusionPipeline pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5") ``` -------------------------------- ### Train Noise Predictor Model (Python) Source: https://context7.com/zsyoaoa/invsr/llms.txt This snippet demonstrates how to train a noise predictor model. It covers single GPU, multi-GPU using torchrun, and resuming training from a checkpoint. Key parameters for diffusion loss coefficients are also shown. ```python # Single GPU training python main.py --save_dir ./experiments/exp1 # Multi-GPU training with torchrun CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun \ --standalone \ --nproc_per_node=4 \ --nnodes=1 \ main.py \ --save_dir ./experiments/exp1 \ --cfg_path ./configs/sd-turbo-sr-ldis.yaml \ --ldif 1.0 \ --llpips 2.0 \ --ldis 0.1 # Resume training from checkpoint CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun \ --standalone \ --nproc_per_node=4 \ --nnodes=1 \ main.py \ --save_dir ./experiments/exp1 \ --resume ./experiments/exp1/ckpts/model_10000.pth # Training configuration (configs/sd-turbo-sr-ldis.yaml) # Key parameters: # - sd_pipe.params.cache_dir: Path to SD-Turbo model # - data.train.params.data_source: Training data path # - data.val.params.dir_path: Validation LQ images # - data.val.params.extra_dir_path: Validation HQ images # - train.batch: Total batch size # - train.microbatch: Micro batch (total = microbatch * GPUs * grad_accumulation) ``` -------------------------------- ### Programmatic Metric Calculation (Python) Source: https://context7.com/zsyoaoa/invsr/llms.txt Python code for calculating image quality metrics programmatically using the pyiqa library. Supports creating various metric calculators (e.g., PSNR, LPIPS) and computing scores between prediction and ground truth tensors. ```python import pyiqa import torch # Create metrics psnr_metric = pyiqa.create_metric( 'psnr', test_y_channel=True, color_space='ycbcr', device=torch.device("cuda") ) lpips_metric = pyiqa.create_metric( 'lpips-vgg', device=torch.device("cuda"), as_loss=False ) # Calculate metrics psnr_score = psnr_metric(pred_tensor, gt_tensor) # Higher is better lpips_score = lpips_metric(pred_tensor, gt_tensor) # Lower is better ``` -------------------------------- ### Documentation Utility Functions Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Utilities for generating and managing documentation strings for Diffusers components. Aids in maintaining consistent and informative documentation. ```python from diffusers.utils.doc_utils import DocStringProcessor # Example usage: # ... use DocStringProcessor ... ``` -------------------------------- ### T2I Adapter Pipeline (Stable Diffusion XL) Implementation Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This Python file implements the T2I-Adapter pipeline for Stable Diffusion XL. It extends the capabilities of SDXL by allowing conditioning on various inputs like depth maps, enabling more precise control over image generation. Dependencies include PyTorch and Diffusers. ```python from diffusers import StableDiffusionXLAdapterPipeline from diffusers.utils import load_image import torch # Load the pipeline with the adapter pipe = StableDiffusionXLAdapterPipeline.from_pretrained("TencentARC/t2i-adapter-depth-xl", torch_dtype=torch.float16) pipe.to("cuda") # Load an input image and condition it with depth map # ... (code to load image and generate depth map) prompt = "a majestic castle" image = pipe(prompt, image=depth_image, num_inference_steps=20, guidance_scale=7.0).images[0] image.save("adapter_castle.png") ``` -------------------------------- ### Stable Diffusion XL Instruct Pix2Pix Pipeline Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This file implements the InstructPix2Pix pipeline for Stable Diffusion XL. It allows for instruction-based image editing, where users can provide text instructions to modify an input image using the SDXL model. Dependencies include PyTorch and Diffusers. ```python from diffusers import StableDiffusionXLInstructPix2PixPipeline from PIL import Image import torch # Load the pipeline pipe = StableDiffusionXLInstructPix2PixPipeline.from_pretrained("diffusers/stable-diffusion-xl-instructpix2pix", torch_dtype=torch.float16, variant="fp16", use_safetensors=True) pipe.to("cuda") # Load an input image # ... (code to load input image) image = pipe(prompt="turn the sky into a sunset", image=image, num_inference_steps=1, guidance_scale=1.0).images[0] image.save("sunset_sky.png") ``` -------------------------------- ### Cosine DPM-Solver Multistep Scheduler Implementation Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt This Python file implements the Cosine DPM-Solver Multistep scheduler. This scheduler combines the DPM-Solver algorithm with a cosine noise schedule, aiming for efficient and high-quality sampling in diffusion models. Dependencies include PyTorch and Diffusers. ```python from diffusers import CosineDPM фаразларыScheduler scheduler = CosineDPM фаразларыScheduler.from_config(scheduler_config) # Example usage within a diffusion pipeline: # ... (code demonstrating the use of CosineDPM фаразларыScheduler) ``` -------------------------------- ### StableDiffusionInvEnhancePipeline for Image Enhancement (Python) Source: https://context7.com/zsyoaoa/invsr/llms.txt Custom diffusion pipeline for image enhancement using diffusion inversion. Loads a base pipeline, configures memory optimizations for the VAE, attaches a noise predictor, and runs inference with specified prompts and parameters. ```python from diffusers import StableDiffusionInvEnhancePipeline, AutoencoderKL import torch # Load base pipeline and convert base_pipe = StableDiffusionPipeline.from_pretrained( "stabilityai/sd-turbo", torch_dtype=torch.float16, use_safetensors=True ) sd_pipe = StableDiffusionInvEnhancePipeline.from_pipe(base_pipe) sd_pipe.to("cuda") # Enable memory optimizations sd_pipe.vae.enable_slicing() sd_pipe.vae.enable_tiling() sd_pipe.vae.tile_latent_min_size = 128 sd_pipe.vae.tile_sample_min_size = 1024 # Attach noise predictor sd_pipe.start_noise_predictor = noise_predictor_model # Run inference positive_prompt = "Cinematic, high-contrast, photo-realistic, 8k, ultra HD" negative_prompt = "Low quality, blurring, jpeg artifacts, deformed" result = sd_pipe( image=input_tensor, # b x c x h x w, [0,1], float16 prompt=[positive_prompt] * batch_size, negative_prompt=[negative_prompt] * batch_size, target_size=(512, 512), # Output resolution timesteps=[200], # Diffusion timesteps guidance_scale=1.0, # CFG scale (1.0 = disabled) output_type="pt" # Return torch tensor ).images # result: b x c x h x w tensor, [0,1] ``` -------------------------------- ### Consistency Models Pipeline Implementations (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Contains the implementation for pipelines utilizing Consistency Models. These models offer efficient sampling for diffusion tasks, potentially leading to faster generation times. ```python from diffusers import ConsistencyModelsPipeline # Example usage for Consistency Models pipeline pipe = ConsistencyModelsPipeline.from_pretrained("path/to/consistency_models/model") image = pipe("a fantasy landscape", num_inference_steps=10).images[0] ``` -------------------------------- ### ControlNet XS Pipeline Implementations (Python) Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Offers implementations for ControlNet XS pipelines, likely a variant for efficient or specialized control. Includes base and SDXL compatible versions. ```python from diffusers import ControlNetXsPipeline # Example usage for ControlNet XS pipeline pipe = ControlNetXsPipeline.from_pretrained("path/to/controlnet_xs/model") # ... ControlNet XS generation logic ... # Example usage for ControlNet XS with SDXL from diffusers import ControlNetXsPipelineSDXL pipe_sdxl = ControlNetXsPipelineSDXL.from_pretrained("path/to/controlnet_xs_sdxl/model") # ... ControlNet XS SDXL generation logic ... ``` -------------------------------- ### Loading Utility Functions Source: https://github.com/zsyoaoa/invsr/blob/master/src/diffusers.egg-info/SOURCES.txt Utilities for loading models and configurations from various sources, including local paths and the Hugging Face Hub. Simplifies model retrieval. ```python from diffusers.utils.loading_utils import load_image # Example usage: image = load_image("path/to/image.png") ```