HiDiffusion

repository·main·Indexed 21 days ago

https://github.com/megvii-research/hidiffusion

A training-free, plug-and-play method that enables pretrained diffusion models to generate high-resolution images more efficiently. It integrates with the Hugging Face Diffusers library via the apply_hidiffusion(pipe) function and supports tasks including text-to-image, ControlNet (text-to-image and image-to-image), and inpainting. Compatible with models such as Stable Diffusion XL, SDXL Turbo, Stable Diffusion v2-1, Stable Diffusion v1-5, Ghibli-Diffusion, and Playground.

Tokens
5.8K
Snippets
14
Records
16
Agent score
24%

What's inside HiDiffusion

  1. How HiDiffusion works with Diffusers pipelines

    main

    HiDiffusion is a training-free, plug-and-play method designed to increase the resolution and speed of pretrained diffusion models. It can be integrated into existing Hugging Face diffusers pipelines by adding a single line of code: apply_hidiffusion(pipe).

    It supports various tasks including text-to-image, ControlNet (text-to-image and image-to-image), and inpainting. To revert the pipeline to its original state, use remove_hidiffusion(pipe).

  2. Integrate HiDiffusion with custom models (Ghibli-Diffusion & Playground)

    main

    HiDiffusion is compatible with various specialized models. You can wrap any standard diffusers pipeline with apply_hidiffusion to enable high-resolution generation.

    Ghibli-Diffusion Example

    from diffusers import StableDiffusionPipeline
    from hidiffusion import apply_hidiffusion
    
    model_id = "nitrosocke/Ghibli-Diffusion"
    pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
    apply_hidiffusion(pipe)
    
    image = pipe("ghibli style magical princess with golden hair", height=1024, width=1024, eta=1.0).images[0]

    Playground Example

    from diffusers import DiffusionPipeline
    from hidiffusion import apply_hidiffusion
    
    pipe = DiffusionPipeline.from_pretrained(
        "playgroundai/playground-v2-1024px-aesthetic",
        torch_dtype=torch.float16,
        use_safetensors=True,
        add_watermarker=False,
        variant="fp16"
    ).to("cuda")
    
    pipe.enable_xformers_memory_efficient_attention()
    apply_hidiffusion(pipe)
    
    # Note: For Playground, you may need to adjust guidance_scale (e.g., 3.0 to 5.0)
    image = pipe(
        prompt="The little girl riding a bike...", 
        guidance_scale=3.0, 
        height=2048, 
        width=2048, 
        negative_prompt="blurry, ugly, duplicate, poorly drawn, deformed, mosaic"
    ).images[0]
  3. Install HiDiffusion via pip or source

    main

    HiDiffusion requires Python 3.8, torch>=1.13.1, diffusers>=0.25.0, transformers, accelerate, and xformers.

    You can install it directly via pip:

    pip3 install hidiffusion

    Alternatively, install from the GitHub source:

    git clone https://github.com/megvii-model/HiDiffusion.git
    cd HiDiffusion
    python3 setup.py install
    pip3 install hidiffusion
  4. Install HiDiffusion and dependencies

    main

    Install the hidiffusion package along with specific versions of diffusers, transformers, accelerate, and xformers to ensure compatibility with the high-resolution generation features.

    !pip install -q hidiffusion
    !pip install -q diffusers==0.25.0 transformers==4.27.4 accelerate==0.18.0 xformers==0.0.16rc425
  5. Text-to-image generation with Stable Diffusion XL Turbo

    main

    For SDXL Turbo, use AutoPipelineForText2Image. Note that Turbo models typically require fewer inference steps (e.g., num_inference_steps=4) and a guidance_scale of 0.0.

    from hidiffusion import apply_hidiffusion, remove_hidiffusion
    from diffusers import AutoPipelineForText2Image
    import torch
    
    pretrain_model = "stabilityai/sdxl-turbo"
    pipe = AutoPipelineForText2Image.from_pretrained(pretrain_model, torch_dtype=torch.float16, variant="fp16").to('cuda')
    
    # Apply hidiffusion with a single line of code.
    apply_hidiffusion(pipe)
    
    prompt = "In the depths of a mystical forest, a robotic owl with night vision lenses for eyes watches over the nocturnal creatures."
    image = pipe(prompt, num_inference_steps=4, height=1024, width=1024, guidance_scale=0.0).images[0]
    image.save(f"./owl.jpg")
  6. Text-to-image generation with Stable Diffusion XL

    main

    To use HiDiffusion with Stable Diffusion XL, initialize a StableDiffusionXLPipeline and call apply_hidiffusion(pipe). You can generate high-resolution images (e.g., 2048x2048 or even 4096x4096) by setting the height and width parameters in the pipeline call.

    from hidiffusion import apply_hidiffusion, remove_hidiffusion
    from diffusers import StableDiffusionXLPipeline, DDIMScheduler
    import torch
    
    pretrain_model = "stabilityai/stable-diffusion-xl-base-1.0"
    scheduler = DDIMScheduler.from_pretrained(pretrain_model, subfolder="scheduler")
    pipe = StableDiffusionXLPipeline.from_pretrained(pretrain_model, scheduler = scheduler, torch_dtype=torch.float16, variant="fp16").to("cuda")
    
    # Apply hidiffusion with a single line of code.
    apply_hidiffusion(pipe)
    
    prompt = "Standing tall amidst the ruins, a stone golem awakens, vines and flowers sprouting from the crevices in its body."
    negative_prompt = "blurry, ugly, duplicate, poorly drawn face, deformed, mosaic, artifacts, bad limbs"
    image = pipe(prompt, guidance_scale=7.5, height=2048, width=2048, eta=1.0, negative_prompt=negative_prompt).images[0]
    image.save(f"golem.jpg")
  7. Perform Image-to-Image generation with ControlNet (SDXL)

    main

    Use StableDiffusionXLControlNetImg2ImgPipeline combined with HiDiffusion to transform an existing image while maintaining structural control via ControlNet.

    Note: When using high resolutions, ensure you set height and width appropriately in the pipeline call.

    from diffusers import ControlNetModel, StableDiffusionXLControlNetImg2ImgPipeline, DDIMScheduler
    import torch
    from PIL import Image
    from hidiffusion import apply_hidiffusion
    
    # ... (setup controlnet and scheduler) ...
    
    pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0",
        controlnet=controlnet,
        scheduler=scheduler,
        torch_dtype=torch.float16,
    ).to("cuda")
    
    apply_hidiffusion(pipe)
    
    # ... (prepare ori_image and canny_image) ...
    
    image = pipe(
        prompt="Lara Croft with brown hair...",
        image=ori_image,
        control_image=canny_image,
        height=1536,
        width=2048,
        strength=0.99,
        num_inference_steps=50,
        controlnet_conditioning_scale=0.5,
        guidance_scale=12.5,
        negative_prompt="underexposed, poorly drawn hands...",
        eta=1.0
    ).images[0]
    
    image.save("lara.jpg")
  8. Text-to-image generation with Stable Diffusion v1-5

    main

    Use DiffusionPipeline with the runwayml/stable-diffusion-v1-5 model. HiDiffusion enables high-resolution generation (e.g., 2048x2048) for this version as well.

    from hidiffusion import apply_hidiffusion, remove_hidiffusion
    from diffusers import DiffusionPipeline, DDIMScheduler
    import torch
    
    pretrain_model = "runwayml/stable-diffusion-v1-5"
    scheduler = DDIMScheduler.from_pretrained(pretrain_model, subfolder="scheduler")
    pipe = DiffusionPipeline.from_pretrained(pretrain_model, scheduler = scheduler, torch_dtype=torch.float16).to("cuda")
    
    # Apply hidiffusion with a single line of code.
    apply_hidiffusion(pipe)
    
    prompt = "thick strokes, bright colors, an exotic fox, cute, chibi kawaii. detailed fur, hyperdetailed , big reflective eyes, fairytale, artstation,centered composition, perfect composition, centered, vibrant colors, muted colors, high detailed, 8k."
    negative_prompt = "ugly, tiling, poorly drawn face, out of frame, disfigured, deformed, blurry, bad anatomy, blurred."
    image = pipe(prompt, guidance_scale=7.5, height=1024, width=1024, eta=1.0, negative_prompt=negative_prompt).images[0]
    image.save(f"fox.jpg")
  9. Perform Inpainting with HiDiffusion

    main

    HiDiffusion can be applied to AutoPipelineForInpainting to generate high-resolution details within a masked area of an image.

    Usage:

    1. Load an inpainting pipeline (e.g., diffusers/stable-diffusion-xl-1.0-inpainting-0.1).
    2. Apply apply_hidiffusion(pipeline).
    3. Provide the image (base image) and mask_image to the pipeline call along with high height and width values.
    import torch
    from diffusers import AutoPipelineForInpainting, DDIMScheduler
    from diffusers.utils import load_image
    from hidiffusion import apply_hidiffusion
    from PIL import Image
    
    scheduler = DDIMScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
    pipeline = AutoPipelineForInpainting.from_pretrained(
        "diffusers/stable-diffusion-xl-1.0-inpainting-0.1", 
        torch_dtype=torch.float16, 
        variant="fp16", 
        scheduler=scheduler
    )
    
    apply_hidiffusion(pipeline)
    
    pipeline.enable_model_cpu_offload()
    pipeline.enable_xformers_memory_efficient_attention()
    
    img_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-text2img.png"
    init_image = load_image(img_url)
    mask_image = Image.open("./assets/mask_image.png")
    
    image = pipeline(
        prompt="A steampunk explorer...",
        image=init_image, 
        mask_image=mask_image, 
        height=2048, 
        width=2048, 
        strength=0.85, 
        guidance_scale=12.5, 
        negative_prompt="blurry, ugly, duplicate, poorly drawn, deformed, mosaic",
        eta=1.0
    ).images[0]
    
    image.save('steampunk_explorer.jpg')
  10. Text-to-image generation with Stable Diffusion v2-1

    main

    Use DiffusionPipeline with the stabilityai/stable-diffusion-2-1-base model. Applying HiDiffusion allows for higher resolutions like 2048x2048.

    from hidiffusion import apply_hidiffusion, remove_hidiffusion
    from diffusers import DiffusionPipeline, DDIMScheduler
    import torch
    
    pretrain_model = "stabilityai/stable-diffusion-2-1-base"
    scheduler = DDIMScheduler.from_pretrained(pretrain_model, subfolder="scheduler")
    pipe = DiffusionPipeline.from_pretrained(pretrain_model, scheduler = scheduler, torch_dtype=torch.float16).to("cuda")
    
    # Apply hidiffusion with a single line of code.
    apply_hidiffusion(pipe)
    
    prompt = "An adorable happy brown border collie sitting on a bed, high detail."
    negative_prompt = "ugly, tiling, out of frame, poorly drawn face, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, artifacts, bad proportions."
    image = pipe(prompt, guidance_scale=7.5, height=1024, width=1024, eta=1.0, negative_prompt=negative_prompt).images[0]
    image.save(f"collie.jpg")
  11. Perform Text-to-Image generation with ControlNet (SDXL)

    main

    You can use HiDiffusion with StableDiffusionXLControlNetPipeline to generate high-resolution images guided by a ControlNet (e.g., Canny edge detection).

    Key steps:

    1. Initialize the ControlNetModel and StableDiffusionXLControlNetPipeline.
    2. Call apply_hidiffusion(pipe).
    3. Pass parameters like height and width (e.g., 2048x2048) to the pipeline to leverage high-resolution capabilities.
    from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel, DDIMScheduler
    import torch
    from PIL import Image
    import cv2
    import numpy as np
    from hidiffusion import apply_hidiffusion
    
    # ... (setup controlnet and scheduler) ...
    
    pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0", 
        controlnet=controlnet, 
        torch_dtype=torch.float16,
        scheduler=scheduler
    )
    
    # Apply hidiffusion
    apply_hidiffusion(pipe)
    
    # Generate high-res image
    image = pipe(
        prompt="The Joker, high face detail, high detail, muted color, 8k",
        controlnet_conditioning_scale=0.5,
        image=canny_image,
        height=2048,
        width=2048,
        guidance_scale=7.5,
        negative_prompt="blurry, ugly, duplicate, poorly drawn, deformed, mosaic.",
        eta=1.0
    ).images[0]
    
    image.save('joker.jpg')
  12. Apply HiDiffusion to a Diffusers pipeline

    main

    To enable high-resolution generation in a pretrained diffusion model, use the apply_hidiffusion function. This function integrates HiDiffusion's capabilities into standard Hugging Face diffusers pipelines with a single line of code. You can also use remove_hidiffusion to revert the pipeline to its original state.

    from hidiffusion import apply_hidiffusion, remove_hidiffusion
    
    # Apply to a pipeline
    apply_hidiffusion(pipe)
    
    # Revert the pipeline
    remove_hidiffusion(pipe)