🚀 二维码条件控制网络模型,适用于Stable Diffusion 2.1
本项目为Stable Diffusion 2.1提供了基于二维码条件的ControlNet模型,可实现图像到图像的转换,为图像生成带来更多创意与可能。

🚀 快速开始
安装依赖
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="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 v2.1的二维码条件控制网络(ControlNet)的safetensors和diffusers版本。
- Stable Diffusion 2.1版本效果略好,它是为满足特定需求而开发的。不过,也在相同数据集上训练了1.5版本的模型,以满足使用旧版本的用户需求。
📦 安装指南
将 .safetensors
模型及其 .yaml
配置文件放置在其他ControlNet模型的安装文件夹中,具体文件夹因应用而异。
- 若要在auto1111中使用,可将它们放置在
webui/models/ControlNet
文件夹中。
- 可通过webui的扩展选项卡安装ControlNet的webui扩展(https://github.com/Mikubill/sd-webui-controlnet)来加载这些模型。
- 确保启用ControlNet单元,并将输入图像设置为二维码。根据基础的Stable Diffusion模型,选择SD2.1或1.5版本的模型,否则会出错。
- 无需预处理器,但可使用反转预处理器以获得不同的结果。建议以768的分辨率进行生成,这样可以使最终作品具有更高的细节水平。
📚 详细文档
性能与局限性
这些模型在大多数情况下表现良好,但请注意,它们并非100%准确。在某些情况下,二维码的形状可能无法如预期那样呈现。你可以增加ControlNet的权重来突出二维码的形状,但需谨慎操作,因为这可能会对输出的风格产生负面影响。为了优化扫描效果,请使用纠错模式 'H' (30%) 生成二维码。
为了在风格和形状之间取得平衡,可能需要根据具体输入、期望输出以及正确的提示词对控制权重进行微调。有些提示词在大幅增加权重后才会生效。找到这些因素之间的最佳平衡既需要技巧也需要经验。为了获得最佳效果,建议以768的分辨率生成作品,这样可以使最终产品具有更高的细节水平,提升基于二维码的艺术作品的质量和效果。
📄 许可证
本项目采用OpenRAIL++许可证。