How to Utilize the T2I-Adapter for Stable Diffusion with Canny Edge Detection

Sep 8, 2023 | Educational

In this article, we’ll explore how to effectively use the T2I-Adapter designed specifically for conditioning images using canny edge detection with the Stable Diffusion XL model. This advanced tool, a product of collaboration between Tencent ARC and Hugging Face, allows you to add an extra layer of control to your text-to-image generation.

What is T2I-Adapter?

The T2I-Adapter acts as a bridge between textual prompts and the Stable Diffusion model, allowing for enhanced conditioning based on specific inputs. Each adapter checkpoint is tailored for different types of conditioning. This particular model utilizes canny edge detection, enabling precise control over image generation.

Getting Started

To get started, follow these steps meticulously:

  1. Set up your environment by installing the required dependencies:
  2. pip install -U git+https://github.com/huggingface/diffusers.git
    pip install -U controlnet_aux==0.0.7 # for conditioning models and detectors
    pip install transformers accelerate safetensors
  3. Download the control images that you will use for conditioning.
  4. Pass the control images and prompts to the StableDiffusionXLAdapterPipeline.

Example Code

Let’s dive into a practical example using the Canny Adapter:

from diffusers import StableDiffusionXLAdapterPipeline, T2IAdapter, EulerAncestralDiscreteScheduler, AutoencoderKL
from diffusers.utils import load_image
from controlnet_aux.canny import CannyDetector
import torch

# Load adapter
adapter = T2IAdapter.from_pretrained("TencentARC/t2i-adapter-canny-sdxl-1.0", torch_dtype=torch.float16).to("cuda")

# Load scheduler and model
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
euler_a = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
vae = AutoencoderKL.from_pretrained("madebyol/llins-dxl-vae-fp16-fix", torch_dtype=torch.float16)
pipe = StableDiffusionXLAdapterPipeline.from_pretrained(model_id, vae=vae, adapter=adapter, scheduler=euler_a, torch_dtype=torch.float16).to("cuda")
pipe.enable_xformers_memory_efficient_attention()

# Setup Canny Detector
canny_detector = CannyDetector()

# Load control image
url = "https://huggingface.co/Adapter/t2i_adapter_resolve/main/figs/SDXL_V1.0/org_canny.jpg"
image = load_image(url)
image = canny_detector(image, detect_resolution=384, image_resolution=1024)

# Generation
prompt = "Mystical fairy in real, magic, 4k picture, high quality"
negative_prompt = "extra digit, fewer digits, cropped, worst quality"
gen_images = pipe(prompt=prompt, negative_prompt=negative_prompt, image=image, num_inference_steps=30, guidance_scale=7.5).images[0]
gen_images.save("out_canny.png")

Code Explanation via Analogy

Think of the T2I-Adapter as a master chef in a large kitchen (the Stable Diffusion model). The ingredients (text prompts) need specific preparations (canny edge detection) to create a dish (the final image). The T2I-Adapter applies the appropriate recipes (checkpoints) for different cuisines (types of conditioning) to ensure the best results in the end product. Just as a chef might use a cutting tool to prepare vegetables, the Canny Detector prepares the image, making the kitchen work more smoothly.

Troubleshooting

If you encounter issues while using the T2I-Adapter or Stable Diffusion model, try the following solutions:

  • Ensure that all dependencies are correctly installed; sometimes, library versions may conflict.
  • Check that the model and adapter paths are accurate and correctly formatted.
  • If the images are not generating as expected, validate your prompt and the conditioning image involved.
  • Memory allocation issues? Consider reducing the batch size or using more efficient data handling techniques.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

By integrating the T2I-Adapter with the Stable Diffusion model, you can harness advanced capabilities in generating images from text prompts. The Canny edge detection enhances your control, allowing for stunning results in your generative art projects. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox