标签:
- 稳定扩散
- 控制网络
- 图像到图像
许可证: openrail++
语言:
- 英文
库名称: diffusers
管道标签: 图像到图像
复制来源: DionTimmer/controlnet_qrcode-control_v1p_sd15
适用于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单元后,将二维码图像设为输入图像,并根据您的基础Stable Diffusion模型选择SD2.1或1.5版本(否则会报错)。虽然不需要预处理器,但使用反转预处理器可获得不同效果变体。推荐生成分辨率设为768以获得更精细的细节。若遇到问题,建议查阅更多关于controlnet的使用资料——只要webui正常运行,安装controlnet扩展其实非常简单。