🚀 用于Stable Diffusion 2.1的二维码条件ControlNet模型
本项目提供了适用于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的扩展标签(https://github.com/Mikubill/sd-webui-controlnet)安装ControlNet的webui扩展来加载这些模型。
- 确保启用ControlNet单元,并将输入图像设置为二维码。根据你的基础Stable Diffusion模型,将模型设置为SD2.1或1.5版本,否则会报错。
- 无需预处理器,但你可以使用反转变换预处理器以获得不同的结果。为了获得更多细节,建议以768的分辨率生成图像。
📚 详细文档
性能与局限性
这些模型在大多数情况下表现良好,但请注意,它们并非100%准确。在某些情况下,二维码形状可能无法如预期般呈现。你可以增加ControlNet的权重以突出二维码形状,但要注意这可能会对输出风格产生负面影响。为了优化扫描效果,请使用纠错模式为 'H' (30%) 生成二维码。
为了在风格和形状之间取得平衡,可能需要根据具体输入、期望输出以及正确的提示词对控制权重进行微调。有些提示词在大幅增加权重后才会生效。找到这些因素之间的最佳平衡既是一门艺术,也是一门科学。为了获得最佳效果,建议以768的分辨率生成你的艺术作品,这样可以在最终产品中实现更高的细节水平,提升基于二维码的艺术作品的质量和效果。
📄 许可证
本项目采用openrail++许可证。
属性 |
详情 |
模型类型 |
用于Stable Diffusion 2.1的二维码条件ControlNet模型 |
训练数据 |
未提及 |
⚠️ 重要提示
这些模型并非100%准确,在某些情况下二维码形状可能无法如预期呈现。为优化扫描效果,请使用纠错模式 'H' (30%) 生成二维码。
💡 使用建议
- 为了在风格和形状之间取得平衡,可能需要根据具体输入、期望输出以及正确的提示词对控制权重进行微调。
- 有些提示词在大幅增加权重后才会生效。
- 为获得最佳效果,建议以768的分辨率生成艺术作品。