stable diffusion(一)——Dreambooth原理与实践


这个系列会分享下stable diffusion中比较常用的几种训练方式,分别是Dreambooth、textual inversion、LORA和Hypernetworks。在https://civitai.com/选择模型时也能看到它们的身影。本文该系列的第一篇

  • Dreambooth

Dreambooth

论文链接:https://arxiv.org/abs/2208.12242

代码链接:https://github.com/XavierXiao/Dreambooth-Stable-Diffusion

https://dreambooth.github.io/
dreambooth是2022年谷歌发布的,该方式通过向模型注入自定义的主题来fine-tune diffusion model的技术。为什么叫这个名字,google团队给出了解释:

It’s like a photo booth, but once the subject is captured, it can be synthesized wherever your dreams take you.

先来看看效果,仅使用三张图片作为输入,就能在不同的prompt下生成对应的图片。下面的例子用一只狗做示例,生成的图片确实就像狗在不同场景下的照片,Dreambooth模型就像一个照相亭一样。

How does Dreambooth work?

要训练自己数据最直观的方法,就是把自己的图片加入模型迭代时一起训练。但会带来两个问题,一个是过拟合,另一个是语言漂移(language drift)。
而Dreambooth的优势就在于能避免上述的两个问题

  • 可以用一个罕见的词来代表图片的含义,保证新加入的图片对应的词在模型中没有太多的意义
  • 为了保留类别的含义,例如上图中的“狗”,模型会在狗的类别基础上微调,并保留对应词的语义,例如给这只狗取名为”Devora”, 那么生成的”Devora”就会特指这只狗。

区别于textual inversion方法,Dreambooth使用的是一个罕见的词,而textual inversion使用的是新词。Dreambooth会对整个模型做微调,而textual inversion只会对text embedding部分调整

训练dreambooth

数据

  • 3-10张图片, 最好是不同角度,且背景有变化的图片
  • 独特的标识符(unique identifier)
  • 类的名字(class name)
    先了解下几个prompt:
    instance prompt: a photo of [unique identifier] [class name]
    class prompt: a photo of [class name]

这里我用无敌帅气的“Halley”的照片

那么在这个例子里,instance prompt: a photo of Halley Dog; class prompt: a photo of Dog

对图片进行裁剪到512*512的,这里推荐一个网站https://www.birme.net/?target_width=512&target_height=512可以很方便的做裁剪

安装

在webui上依次点击”Extensions”->”Available”->”Load from”。会搜索出非常多的插件,选择”Dreambooth”并”Install”

安装完成后依次点击”Extensions”->”Installed”->”Apply and restart UI”

可能会遇到些坑,例如我在重新启动UI时,页面崩溃了,发现时代码中也有检测extension并安装的逻辑,注释掉相应的代码就行了

训练

接下来就是Dreambooth的训练页面

首先创建一个空的模型,设定好Name和原始的Checkpoint就行

然后点击”Dreambooth”->”Train”,有如下几个地方需要设置,大多数填的内容在上面的篇幅都有介绍到。需要注意的是有两种风格可以选,分别是”Person”和”Object/Style”,根据自己的需求选择对应的就ok

然后”Settings”,选择训练时候的一些设置,这里推荐一些参数,”use LORA”需要勾选上,混合精度选上fp16。”Saving”中选上”Generate lora weights when saving during training.”,”Generate”中”Image Generation Scheduler”选择”DDIM”

模型训练:

效果

prompt: (4k,masterpiece,best quality), a photo of Halley Dog, black pupils brown whites, Sleeping in a blanket in the snow

prompt:(best quality), a photo of Halley Dog, black pupils brown whites, eat dog biscuits

prompt:(best quality), a photo of Halley Dog, black pupils brown whites, eat dog biscuits

prompt:(best quality), a photo of Halley Dog, black pupils brown whites, A chef Halley Dog cooking scallops, wearing a chef hat, in a kitchen, professional

-------------本文结束感谢您的阅读-------------