🤖 AI/ML

ControlNet

A neural network architecture for controlling image generation

📖 Detailed Explanation

ControlNet is a neural network architecture that adds additional conditional control to image generation models such as Stable Diffusion. By precisely guiding the generation process based on a user-provided reference image (edges, pose, depth map, etc.), it enables control over composition and shape that is difficult to achieve with text prompts alone.

ControlNet, published by Stanford University researchers in February 2023, dramatically increased the controllability of image generation AI. Shortly after the paper was released, it spread rapidly on GitHub and has become one of the most widely used Stable Diffusion extensions today.

The core ideas of ControlNet are "Zero Convolution" and copying the encoder of a pretrained Diffusion model. The weights of the original model are frozen, and only the copied network is trained to fit the conditional input. With Zero Convolution initialization, training starts out identical to the original output, enabling stable training.

In practice, ControlNet is used for maintaining character pose consistency, specifying composition in architectural rendering, converting product photo backgrounds, and animation frame interpolation. It is combined with various preprocessing modules such as Canny Edge, OpenPose, Depth Map, and Scribble, and multiple ControlNets can be used simultaneously for complex conditional control.

💻 Code Example

from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
from diffusers.utils import load_image
import torch
from PIL import Image
import cv2
import numpy as np

# Canny Edge ControlNet 모델 로드
controlnet = ControlNetModel.from_pretrained(
    "lllyasviel/sd-controlnet-canny",
    torch_dtype=torch.float16
)

# SD 1.5 + ControlNet 파이프라인 구성
pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=controlnet,
    torch_dtype=torch.float16
).to("cuda")

# 입력 이미지에서 Canny Edge 추출
def get_canny_edge(image_path, low_threshold=100, high_threshold=200):
    image = cv2.imread(image_path)
    image = cv2.Canny(image, low_threshold, high_threshold)
    image = image[:, :, None]
    image = np.concatenate([image, image, image], axis=2)
    return Image.fromarray(image)

# 참조 이미지로 엣지맵 생성
control_image = get_canny_edge("reference_pose.png")

# ControlNet으로 이미지 생성
result = pipe(
    prompt="a professional photo of a woman in red dress, studio lighting",
    negative_prompt="blurry, low quality, distorted",
    image=control_image,  # Canny edge를 조건으로 사용
    num_inference_steps=30,
    guidance_scale=7.5,
    controlnet_conditioning_scale=1.0  # ControlNet 영향력 (0-2)
).images[0]

result.save("controlled_output.png")

# 여러 ControlNet 동시 사용 (Multi-ControlNet)
from diffusers import MultiControlNetModel

controlnets = MultiControlNetModel([
    ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny"),
    ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-openpose"),
])
# conditioning_scale를 리스트로: [canny_scale, pose_scale]

🗣️ Examples of Practical Conversations

At an AI art production meeting

"To keep a character's pose consistent, OpenPose ControlNet is the best. Extract the skeleton from a reference photo, then just change the outfit or background with the prompt, and you can produce many versions with the same pose."

On a tech blog

"ControlNet's conditioning_scale is the key. At 1.0 it follows the condition image strongly, and at 0.5 it is influenced half by the prompt and half by the condition image. For creative variation, between 0.3-0.7 is good, and if you need exact reproduction, use 1.0 or higher."

In a product photo automation discussion

"We're using Depth ControlNet to keep the product silhouette while swapping only the background with AI. We replaced what used to be 20 studio shots with 1 sample + ControlNet, cutting costs by 70%."

⚠️ Cautions

1
Preprocessing quality determines the result

The quality of the ControlNet input (Canny, OpenPose, etc.) determines the output. Blurry or noisy preprocessing results also degrade generation quality.

2
Check model compatibility

ControlNet models are trained for a specific SD version. A ControlNet for SD 1.5 will not work with SDXL. Be sure to check the version.

3
Increased VRAM usage

Because ControlNet is an additional network, it increases VRAM usage. It increases even more when using Multi-ControlNet, so consider the --medvram option on an 8GB GPU.

🔗 Related Terms

📚 Learn More