第 16 屆 iThome 鐵人賽 (2023)
{%hackmd BJrTq20hE %}
Scribble作為我滿喜歡的Controlnet模組,用於控制預訓練的大型擴散模型,以支持附加的輸入條件。ControlNet以端到端的方式學習特定任務的條件,即使在訓練數據集很小的情況下(<50k),學習也是穩定的,是一個用於控制擴散模型(Diffusion Models)的神經網絡結構,通過添加額外的條件來實現對模型的控制。這個版本的ControlNet被設計為基於Scribble圖像的條件,
還記得我們前面教的Controlnet的圖片嗎?,我們要嘛是摘取圖片的特徵,要嘛是取下景深,或是取下其姿勢,那麼Scribble會做甚麼呢,話不多說 直接看結果。
沒錯!就是這張畫的草圖,是不是超ㄎ一ㄤ超可愛的呀 > w <,不過認真說其中使用到的特徵擷取採最簡化模式儲存一張圖片,麻 算是Controlnet比較少用但很用了會很驚喜的功能呢,如同Canny一樣,畢竟都是特徵擷取,也能夠丟回Stable diffusion執行再生成。
from PIL import Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
import torch
from controlnet_aux import HEDdetector
from diffusers.utils import load_image
hed = HEDdetector.from_pretrained('lllyasviel/ControlNet')
image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-scribble/resolve/main/images/bag.png")
image = hed(image, scribble=True)
controlnet = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-scribble", torch_dtype=torch.float16
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
# Remove if you do not have xformers installed
# see https://huggingface.co/docs/diffusers/v0.13.0/en/optimization/xformers#installing-xformers
# for installation instructions
pipe.enable_xformers_memory_efficient_attention()
pipe.enable_model_cpu_offload()
image = pipe("bag", image, num_inference_steps=20).images[0]
image.save('images/bag_scribble_out.png')
https://huggingface.co/lllyasviel/sd-controlnet-scribble