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