标签:
- 稳定扩散
- 控制网络
- 图像到图像
许可证: openrail++
语言:
- 英文
管道标签: 图像到图像
适用于Stable Diffusion 2.1的二维码条件控制网络模型

模型描述
本仓库包含针对Stable Diffusion v2.1的二维码条件控制网络的safetensors和diffusers版本。
Stable Diffusion 2.1版本效果略优,因其专为解决我的特定需求而开发。不过,我们也基于相同数据集训练了1.5版本模型,供使用旧版用户选择。
与diffusers的配合使用
pip -q install diffusers transformers accelerate torch xformers
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="纽约市带有二维码的广告牌",
negative_prompt="丑陋,畸形,低质量,模糊,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]
性能与局限
这些模型在多数情况下表现良好,但请注意其并非100%准确。某些情况下二维码形状可能不如预期。您可以通过增加ControlNet权重来强化二维码形状,但需注意这可能影响输出风格。为确保最佳扫描效果,请生成纠错等级为'H'(30%)的二维码。
要平衡风格与形状,可能需要根据具体输入和期望输出微调控件权重,并配合恰当提示词。某些提示词需大幅增加权重才能生效。这个过程兼具艺术性与科学性。建议以768分辨率生成作品,可获得更精细的细节表现,提升二维码艺术品的质量与效果。
安装指南
最简使用方式是将.safetensors模型及其.yaml配置文件放入您应用程序的ControlNet模型文件夹(路径因应用而异)。
在auto1111中可放置于webui/models/ControlNet目录。通过webui扩展选项卡安装controlnet扩展后即可加载(扩展地址:https://github.com/Mikubill/sd-webui-controlnet)。启用controlnet单元并将二维码设为输入图像,根据基础模型选择SD2.1或1.5版本(否则会报错)。虽无需预处理器,但使用反转预处理器可获得不同效果变体。推荐生成分辨率设为768以获取更多细节。
若遇困难,请查阅controlnet使用指南。只要webui运行正常,安装controlnet扩展非常简便。