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

模型描述
此仓库包含针对Stable Diffusion v1.5的二维码条件控制网络的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_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="纽约市带有二维码的广告牌",
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后扩展安装过程非常简单。