🚀 适用于Stable Diffusion 1.5的二维码条件ControlNet模型
本项目提供适用于Stable Diffusion 1.5的二维码条件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_v1p_sd15",
torch_dtype=torch.float16)
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
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 v1.5的二维码条件ControlNet的safetensors和diffusers版本。
- 虽然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++。