参考资料:
相应的github和huggingface
LDM [github]
StableDiffusion v1.1 ~ v1.4 [github] [huggingface]
StableDiffusion v1.5 [huggingface] [github]
StableDiffusion v2 v2.1 [github] [huggingface]
首先说一下,这篇文章的目的是让你清晰地了解StableDffusion这个模型的发展脉络,由于目前开源AIGC模型基本上都是基于SD的,因此了解它的发展历史是非常有必要的,毕竟它是进行再创作的根基,不了解这个base而盲目地搞一些finetune,虽然可能也会出效果,但有事倍功半的危险。
1. LDM
3. StableDiffusion v1.5
上面提到Compvis团队不仅了Stablility-AI团队合作,还和Runway团队有合作。而SD1.5这个火遍大江南北的模型就是由RunwayML团队发布在hugging face上的。值得注意的是这次模型的发布就不再是Compvis了(可能是利益相关的问题吧),看看它是怎么做的:
The Stable-Diffusion-v1-5 checkpoint was initialized with the weights of the Stable-Diffusion-v1-2 checkpoint and subsequently fine-tuned on 595k steps at resolution 512x512 on "laion-aesthetics v2 5+" and 10% dropping of the text-conditioning to improve classifier-free guidance sampling.
似乎没有什么fancy的... 仅仅就是在一个美学打分较高的LAION子集上猛猛训...步数超过了v1-4而已。但是这里有两个需要注意的地方,先看一下runway在hugging face上发布的模型:
一个是pruned,这个pruned是什么意思?reddit上有一段很好的解释: https://www.reddit.com/r/StableDiffusion/comments/xymibu/what_does_it_mean_to_prune_a_model/
A neural network is just a bunch of math operations. The "neurons" are connected by various "weights," which is to say, the output of a neuron is multipled by a weight (just a number) and gets added into another neuron, along with lots of other connections to that other neuron.
When the neural network learns, these weights get modified. Often, many of them become zero (or real close to it). And since anything time zero is zero, we can skip this part of the math when using the network to predict something. Also, when a set of data has a lot of zeros, it can be compressed to be much smaller.
Pruning finds the nearly zero connections, makes them exactly zero, and then let's you save a smaller, compressed network. Moreover, when you use the network to predict/create something, an optimized neural network solution (i.e. the code that does all of the math specified by the network) can do so faster by intelligently skipping the unneeded calculations involving zero.
这下就知道了,模型的pruning就是剪掉不需要的部分。pruned知道了,ema又是什么意思呢?这个其实我可以解释一下:EMA stands for Exponential Moving Average, and it refers to a technique used to smooth out noise in the training data. 即EMA是一种训练策略,在训练的时候模型会有一个主模型例如Unet,同时也会保存一个copy即EMA_Unet,这个EMA_Unet可以简单地看作是Unet的一个权值平均化的版本,可以使得训练更加稳定。一般认为EMA_Unet能够降噪,因此load ema版本的权重就可以了,但是如果你想接着finetune,那么不妨同时load EMA_Unet和真实的Unet,继续用ema的策略训练下去。hugging face上有一段话:
可知v1-5-pruned.ckpt包含的信息是比v1-5-pruned-emaonly.ckpt的信息“绝对”多的,然后就按自己需求有选择地下载就好了。
4. StableDiffusion v2 v2.1
上面说到之前的模型发布都是Compvis和Runway完成的,现在老大Stability-AI也坐不住了。我猜它应该是想挣钱的,因此发布Stalediffusion v2的一个重要举措是删除NSFW的东西。这个也可以理解,因为做产品就要考虑风控啊。SD v2同样有一个一句话定义:Stable Diffusion v2 refers to a specific configuration of the model architecture that uses a downsampling-factor 8 autoencoder with an 865M UNet and OpenCLIP ViT-H/14 text encoder for the diffusion model. The SD 2-v model produces 768x768 px outputs. 有三个变化,一个是text-encoder变了,变这个东西意味着什么?意味着和StableDiffusion v1割席,必须重新从零去训练了。一个是分辨率加大了,这个东西似乎没有什么技术壁垒,因为卷积这个操作好像天生就能够兼容不同分辨率的方图。
然后,StableDiffusion v2引入了一个叫v-prediction的概念,这导致模型出现了v2, v2-base, v2.1, v2.1-base,v2和v2.1是SD主推的产品(我的理解),而v2-base和v2.1-base则是原始的noise-prediction的模型。逻辑是这样的,v2-base是从零开始训的,并且屏蔽了NSFW,v2基于v2-base继续finetune。v2.1-base是基于v2-base finetune的,v2.1基于v2.1-base继续finetune。下面只展示v2-base的训练介绍,其他的模型训练信息可以到hugging face上自己去看: