前言
2023年第一篇博客,大家新年好呀~
這次來(lái)關(guān)注一下Stable Diffusion背后的原理,即 High-Resolution Image Synthesis with Latent Diffusion Models 這篇論文。
之前關(guān)注的那些工作只能工作到
256
×
256
256 \times 256
256×256 像素(resize成這個(gè)后才輸入模型),甚至更低。
然而這篇 Latent Diffusion Models 可以到
512
×
512
512 \times 512
512×512 了,生成的質(zhì)量也更好。
本文與之前的文章一樣,會(huì)從論文和代碼兩個(gè)角度來(lái)分析. 本文會(huì)不斷更新中…
DDPM原理與代碼剖析
IDDPM原理和代碼剖析
DDIM原理及代碼(Denoising diffusion implicit models)
Classifier Guided Diffusion
理論
摘要
(1) 在摘要部分,作者就說(shuō)啊,之前的diffusion模型,也可以實(shí)現(xiàn)SOTA,但需要耗費(fèi)巨大算力。
“However, since these models typically operate directly in pixel space, optimization of powerful DMs often consumes hundreds of GPU days and inference is expensive due to sequential evaluations.”
(2) 作者就想了個(gè)辦法,這也是模型名字 latent 的由來(lái),我們不要在原始像素上推導(dǎo)了,我們讓擴(kuò)散模型在 latent space(可以理解為一個(gè)feature map的空間中)進(jìn)行學(xué)習(xí)。
“we apply them in the latent space of powerful pretrained autoencoders.”
具體的,可以是圖片經(jīng)過(guò)encoder(可以是CNN) 后,得到一個(gè)feature map, 然后在這個(gè)feature map上進(jìn)行標(biāo)準(zhǔn)的擴(kuò)散過(guò)程,最后來(lái)個(gè)decoder映射回圖片像素空間。
(3) 優(yōu)勢(shì)很顯然
Our latent diffusion models (LDMs) achieve new state-of-the-art scores for image inpainting and class-conditional image synthesis and highly competitive performance on various tasks, including text-to-image synthesis, unconditional image generation and super-resolution, while significantly reducing computational requirements compared to pixel-based DMs.
Introduction
(1) 在introduction那里, 作者分析了一下 概率密度 相關(guān)模型可以分為兩個(gè)階段, 一個(gè)是 perceptual上的,就是圖像紋理細(xì)節(jié),另一個(gè)是語(yǔ)義上的,例如帥哥變成了美女。
As with any likelihood-based model, learning can be roughly divided into two stages: First is a perceptual compression stage which removes high-frequency details but still learns little semantic variation. In the second stage, the actual generative model learns the semantic and conceptual composition of the data (semantic compression).

所以呢,作者想先找到perceptual上的點(diǎn),犧牲一點(diǎn)紋理的精度,換取生成高清圖像( 512 × 512 512\times 512 512×512)的能力。
“Compared to pixel-based diffusion approaches, we also significantly decrease inference costs.”
Method
(1) 圖片經(jīng)過(guò)一個(gè)編碼器,得到特征
z
z
z, 即
z
=
E
(
x
)
z = E(x)
z=E(x)
中途就是常規(guī)的DDPM,只是denoise的是 z, 而不是 x。

最后通過(guò)decoder返回預(yù)測(cè)的
x
^
\hat{x}
x^。
x
^
=
D
(
z
^
)
\hat{x} = D(\hat{z})
x^=D(z^)

(2) 若是需要條件 (Conditioning Mechanisms) 的話, 則可以輸入相關(guān)條件的 feature
?
θ
(
z
t
,
t
,
y
)
\epsilon_θ(zt, t, y)
?θ?(zt,t,y), 這里
y
=
E
c
(
x
c
)
y=E_c(x_c)
y=Ec?(xc?)
例如,如果需要輸入文本的話,先通過(guò)文本編碼器,得到文本特征,再輸入到Unet網(wǎng)絡(luò)的condition embedding即可, 通過(guò)是和 step embedding相加或拼接等。這是一般的condition ddpm操作。
但是作者認(rèn)為這樣不好, “however, combining the generative power of DMs with other types of conditionings beyond class-labels [15] or blurred variants of the input image [72] is so far an under-explored area of research.”
本文引入了一種 cross-attention mechanism ,文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-800868.html

這里的 τ θ \tau_\theta τθ? 就是處理prompt y y y 的編碼器,例如文本 y y y 對(duì)應(yīng)的 τ θ \tau_\theta τθ? 就是文本編碼器。最后 ? θ \epsilon_\theta ?θ? 和 τ θ \tau_\theta τθ? 靠下列式子更新:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-800868.html

到了這里,關(guān)于Stable Diffusion背后原理(Latent Diffusion Models)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!