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

模型描述
此仓库包含适用于Stable Diffusion v2.1的二维码条件控制网络的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_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="纽约市的一个带有二维码的广告牌",
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 webui扩展(https://github.com/Mikubill/sd-webui-controlnet)来加载它们。确保启用controlnet单元并将输入图像设置为二维码。根据你的基础Stable Diffusion模型选择SD2.1或1.5版本的模型,否则会报错。不需要预处理器,但你可以使用反转预处理器来获得不同的结果变体。768是首选的生成分辨率,因为它允许更多的细节。
如果遇到困难,请查阅有关如何使用controlnet的更多信息。一旦webui运行起来,安装controlnet扩展也非常容易。