Stable Diffusion之模型篇

Stable Diffusion 模型资源

  1. Hugging Face
  2. Civitai

模型安装

Stable Diffusion Web Ui安装过程中会默认下载Stable Diffusion v1.5模型,名称为v1-5-pruned-emaonly。如果想用最新的Stable Diffusion v2.1,可以从Hugging Face上下载官方版本stabilityai/stable-diffusion-2-1。下载后将模型复制到models目录下的Stable-diffusion目录即可。完成后点击页面左上角的刷新按钮,即可在模型下拉列表中看到新加入的模型。

除了标准模型外,Stable Diffusion还有其他几种类型的模型,models目录下每一个子目录就是一种类型的模型,其中用的最多的是LoRA模型。

我们来实践几个模型

基础大模型

  • v1-5-pruned-emaonly.ckpt
    Stable Diffusion基础大模型,原则上所有其他大模型都在这个模型基础上再微调出来的

  • chilloutmix_NiPrunedFp32Fix.safetensors
    AI真人基础模型,一般搭配LoRA小模型

  • Anything、Waifu、novelai、Counterfeit
    二次元漫画型

LoRA示例

LoRA(Low-Rank Adaptation)模型是小型稳定扩散模型,可对标准模型进行微调。它通常比标准模型小10-100倍,这使得LoRA模型在文件大小和训练效果之间取得了很好平衡。LoRA无法单独使用,需要跟标准模型配合使用,这种组合使用方式也为Stable Diffusion带来了强大的灵活性。

下面我们使用 chilloutmix大模型 + LoRA的微调小模型koreanDollLikeness来试试

LoRA模型下载后需要放到Lora目录中,使用时在提示中加入LoRA语法,语法格式如下:

1
<lora:filename:multiplier>

filename是LoRA模型的文件名(不带文件后缀)

multiplier是LoRA 模型的权重,默认值为1,将其设置为 0 将禁用该模型。

Prompt
选择好模型后,我们开始设计prompt。首先我们引入LoRA

1
<lora:koreanDollLikeness:0.66>

然后定义生成图片的风格,我们希望超写实风,可以用如下关键词:

1
best quality, ultra high res, (photorealistic:1.4)

其中photorealistic我们赋予较高的权重1.4。

接着来定义图片的主体内容,这里我将希望图片中出现的元素都做个权重增强:

1
1girl, thighhighs, ((school uniform)),((pleated skirt)), ((black stockings)), (full body), (Kpop idol), (platinum blonde hair:1), ((puffy eyes))

最后,修饰一些表情、姿势的细节:

1
smiling, solo focus, looking at viewer, facing front

这样我们完整的promt是:

1
<lora:koreanDollLikeness:0.66>, best quality, ultra high res, (photorealistic:1.4), 1girl, thighhighs, ((school uniform)),((pleated skirt)), ((black stockings)), (full body), (Kpop idol), (platinum blonde hair:1), ((puffy eyes)), smiling, solo focus, looking at viewer, facing front

Negative prompt
我们还需要提供Negative prompt去除我们不想要的风格和元素:

1
paintings, sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, age spot, glan

这里主要剔除了绘画风、简笔画、低质量、灰度图,同时去除雀斑、痤疮等皮肤瑕疵。

参数设置
为了让图片生成得更加真实自然,我们需要对参数做一些调整,需要调整的参数如下:

Sampler: DPM++ SDE Karras
Sample Steps: 28
CFG scale: 8
Size: 512×768

dddd44d

常见问题

Q1:SD:2.1安装报错

1
NansException: A tensor with all NaNs was produced in VAE. This could be because there's not enough precision to represent the picture. Try adding --no-half-vae commandline argument to fix this.

解决

1
Settings > Stable Diffusion > Enable option "Upcast cross attention layer to float32" (at Stable Diffusion localhost server).