在 ChatGPT 出圈不久,ControlNet 的横空出世很快在英文和中文互联网收获了众多开发者和普通用户,甚至有用户宣传 ControlNet 的出现将 AI 创作带入了直立行走的时代。不夸张地说,包括 ControlNet 在内,同期的 T2I-Adapter、Composer, 以及 LoRA 训练技巧,可控生成作为 AI 创作最后一道高墙,极有可能在可预见的时间内有进一步突破,从而极大地降低用户的创作成本,提高创作的可玩性。距离 ControlNet 开源仅仅过去两周,其官方 Star 就已经超过 1 万,这种热度无疑是空前的。 与此同时,开源社区也极大地降低了用户的使用门槛,如 Hugging Face 平台提供了基础模型权重以及通用的模型训练框架 diffusers,stable-diffusion-webui 开发了完善的一套 Demo 平台,Civitai 贡献了海量风格化 LoRA 权重。本文从实际存在的问题出发,对代码框架不兼容、模型加载受限等问题率先提出了自研解决方案,快速帮助开发者更容易地开发。


python ./scripts/convert_original_stable_diffusion_to_diffusers.py --checkpoint_path xxx.safetensors --dump_path save_dir --from_safetensors
如果 full model 是 ckpt 格式,可以通过以下 diffusers 脚本转换
python ./scripts/convert_original_stable_diffusion_to_diffusers.py --checkpoint_path xxx.ckpt --dump_path save_dir
转换完成后,可直接利用 diffusers 的 API 进行模型加载
from diffusers import StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_pretrained (save_dir,torch_dtype=torch.float32)
(2)LoRA only (仅包含 LoRA 模块)
目前 diffusers 官方无法支持仅加载 LoRA 权重,而开源平台上的 LoRA 权重基本以这种形式存储。本质上是完成 LoRA 权重中 key-value 的重新映射,使其适配到 diffusers 模型中。为此,我们自行支持这个功能,提供了转换脚本。
pipeline = StableDiffusionPipeline.from_pretrained (model_id,torch_dtype=torch.float32)
model_path = "onePieceWanoSagaStyle_v2Offset.safetensors"
state_dict = load_file (model_path)
只需要指定 diffusers 格式的模型,以及存储为 safetensors 格式的 LoRA 权重。我们提供了一个转换示例。
# the default mergering ratio is 0.75, you can manually set it
python convert_lora_safetensor_to_diffusers.py
此外,LoRA 本身由于其轻量化,可以在小数据情况下快速完成训练,并能够嵌入到其他网络中。为了不局限于已有 LoRA 权重,我们在 diffusers 框架中支持了 LoRA 的多模块(UNet+text encoder)训练,并已经在官方代码库提交 PR(https://github.com/huggingface/diffusers/pull/2479),并支持了 ColossalAI 中训练 LoRA。
代码开源在:https://github.com/haofanwang/Lora-for-Diffusers
ControlNet for diffusers





审核编辑 :李倩
全部0条评论
快来发表一下你的评论吧 !