How to Use ControlNet v1.1 – Canny Version for Image Processing

May 6, 2023 | Educational

ControlNet v1.1 is an advanced model built upon its predecessor, ControlNet v1.0, allowing users to provide additional input conditions for enhanced image generation using Stable Diffusion. In this guide, we will walk you through the installation and usage of ControlNet v1.1, helping you transform your image processing experience.

What You Will Need

  • Python installed on your computer.
  • Access to the internet for downloading packages and dependencies.
  • A basic understanding of using command-line interfaces.

Setting Up ControlNet v1.1

To get started, you’ll need to follow these steps:

1. Install Required Packages

Open your command line interface (CLI) and run the following commands to install the necessary packages:

pip install opencv-contrib-python
pip install diffusers transformers accelerate

2. Python Code Implementation

Now that you have installed the required packages, let’s look at the code you’ll need to use ControlNet v1.1. Imagine that coding is like baking a cake; each line of code is an ingredient that contributes to the final delicious product. When mixed and executed correctly, your cake (or in this case, the image output) will look just right!


import torch
import os
from huggingface_hub import HfApi
from pathlib import Path
from diffusers.utils import load_image
import numpy as np
import cv2
from PIL import Image
from diffusers import (
    ControlNetModel,
    StableDiffusionControlNetPipeline,
    UniPCMultistepScheduler,
)

checkpoint = "lllyasviel/control_v11p_sd15_canny"
image = load_image("https://huggingface.co/lllyasviel/control_v11p_sd15_canny/resolve/main/images/input.png")

image = np.array(image)
low_threshold = 100
high_threshold = 200
image = cv2.Canny(image, low_threshold, high_threshold)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
control_image = Image.fromarray(image)
control_image.save("images/control.png")

controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()

generator = torch.manual_seed(33)
image = pipe("a blue paradise bird in the jungle", num_inference_steps=20, generator=generator, image=control_image).images[0]
image.save("images/image_out.png")

In this code, we start by importing our necessary libraries—like gathering all our baking tools. We set up the image preprocessing steps by applying Canny edge detection, which can be compared to providing the right texture to your cake batter. Next, we load the ControlNet model and generate the final image based on the provided conditions.

Troubleshooting Common Issues

While the setup and code execution should work smoothly, you may encounter a few issues. Here are some tips to troubleshoot:

  • If you receive a “ModuleNotFoundError,” ensure that all required packages are properly installed.
  • Check your internet connection, as the scripts require access to download models and images.
  • Make sure that you have adequate permissions to save files in your specified directories.
  • If you have any performance issues, experiment with reducing the resolution of the input image or tweaking the parameters within the code.

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

Conclusion

ControlNet v1.1 provides a robust method for conditional control of diffusion models to enhance your image processing skills. With proper setup and execution, you will be able to explore a world of creative possibilities. 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