🚀 二維碼條件控制網絡模型,適用於Stable Diffusion 2.1
本項目提供了適用於Stable Diffusion v2.1的二維碼條件控制網絡(ControlNet)的safetensors和diffusers版本。該模型能基於二維碼條件生成圖像,為圖像生成領域帶來新的可能性。

🚀 快速開始
環境準備
使用diffusers庫調用模型前,需要安裝必要的依賴:
pip -q install diffusers transformers accelerate torch xformers
代碼示例
以下是使用Python代碼調用模型的示例:
import torch
from PIL import Image
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler
from diffusers.utils import load_image
controlnet = ControlNetModel.from_pretrained("DionTimmer/controlnet_qrcode-control_v11p_sd21",
torch_dtype=torch.float16)
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1",
controlnet=controlnet,
safety_checker=None,
torch_dtype=torch.float16
)
pipe.enable_xformers_memory_efficient_attention()
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
def resize_for_condition_image(input_image: Image, resolution: int):
input_image = input_image.convert("RGB")
W, H = input_image.size
k = float(resolution) / min(H, W)
H *= k
W *= k
H = int(round(H / 64.0)) * 64
W = int(round(W / 64.0)) * 64
img = input_image.resize((W, H), resample=Image.LANCZOS)
return img
source_image = load_image("https://s3.amazonaws.com/moonup/production/uploads/6064e095abd8d3692e3e2ed6/A_RqHaAM6YHBodPLwqtjn.png")
init_image = load_image("https://s3.amazonaws.com/moonup/production/uploads/noauth/KfMBABpOwIuNolv1pe3qX.jpeg")
condition_image = resize_for_condition_image(source_image, 768)
init_image = resize_for_condition_image(init_image, 768)
generator = torch.manual_seed(123121231)
image = pipe(prompt="a bilboard in NYC with a qrcode",
negative_prompt="ugly, disfigured, low quality, blurry, nsfw",
image=init_image,
control_image=condition_image,
width=768,
height=768,
guidance_scale=20,
controlnet_conditioning_scale=1.5,
generator=generator,
strength=0.9,
num_inference_steps=150,
)
image.images[0]
✨ 主要特性
- 版本適配:提供了適用於Stable Diffusion 2.1和1.5版本的模型,滿足不同用戶的需求。
- 效果良好:在大多數情況下,模型能夠生成質量較高的圖像,但仍存在一定的侷限性。
- 可調節性:可以通過調整控制網絡的權重、引導比例等參數,平衡圖像的風格和二維碼的形狀。
📦 安裝指南
最簡單的使用方法是將.safetensors
模型文件及其對應的.yaml
配置文件放置在你其他ControlNet模型的安裝文件夾中,具體文件夾位置因應用而異。
- 在auto1111中使用:可以將模型文件和配置文件放置在
webui/models/ControlNet
文件夾中。你可以通過webui的擴展選項卡安裝ControlNet擴展(https://github.com/Mikubill/sd-webui-controlnet )來加載模型。
- 確保啟用ControlNet單元,並將輸入圖像設置為二維碼。
- 根據你的基礎Stable Diffusion模型選擇SD2.1或1.5版本的模型,否則會報錯。
- 不需要預處理器,但你可以使用反轉預處理器來獲得不同的結果。
- 推薦使用768的分辨率進行圖像生成,以獲得更豐富的細節。
🔧 技術細節
性能與侷限性
這些模型在大多數情況下表現良好,但請注意,它們並非100%準確。在某些情況下,二維碼的形狀可能無法如預期般呈現。你可以增加ControlNet的權重來強調二維碼的形狀,但要注意這可能會對輸出圖像的風格產生負面影響。為了優化二維碼的掃描效果,請使用糾錯模式為'H'(30%)生成二維碼。
為了在風格和形狀之間取得平衡,可能需要根據具體的輸入、期望的輸出以及合適的提示詞,對控制權重進行微調。有些提示詞在大幅增加權重之前可能無法正常工作。找到這些因素之間的正確平衡既是一門藝術,也是一門科學。為了獲得最佳效果,建議以768的分辨率生成你的藝術作品,這樣可以在最終產品中實現更高的細節水平,提升基於二維碼的藝術作品的質量和效果。
📄 許可證
本項目採用OpenRAIL++許可證。