画像生成AI「Stable Diffusion」を使いこなすために知っておくと理解が進む「どうやって絵を描いているのか」をわかりやすく図解

2022年8月に無料で一般公開された画像生成AI「Stable Diffusion」は、NVIDIA製GPUを搭載したCPUあるいはGoogle Colaboratoryのようなオンライン実行環境を整えれば、任意の文字列や誰でも画像を生成することができます。そんなStable Diffusionがどのようにして画像を生成しているのかについて、AIについてTwitterで解説を行うAI Pubが説明しています。
// Stable Diffusion, Explained //
— AI Pub (@ai__pub) August 21, 2022
You've seen the Stable Diffusion AI art all over Twitter.
But how does Stable Diffusion _work_?
A thread explaining diffusion models, latent space representations, and context injection:
1/15 pic.twitter.com/VX9UVmUaKJ
Stable Diffusionがどのように画像を生成しているのかについては、以下の画像をクリックすると見ることができるGIFアニメーションを見ると、おおまかに理解できます。

そもそもStable Diffusionの「Diffusion」とは「拡散」という意味です。この拡散とは、画像にランダムで小さなノイズを繰り返し追加するプロセスのことで、以下の画像だと左から右に進める処理です。Stable Diffusionでは、さらにこの逆となる右から左、つまりノイズを画像に変換する処理も行います。
First, a one-tweet summary of diffusion models (DMs).
— AI Pub (@ai__pub) August 21, 2022
Diffusion is the process of adding small, random noise to an image, repeatedly. (Left-to-right)
Diffusion models reverse this process, turning noise into images, bit-by-bit. (Right-to-left)
Photo credit: @AssemblyAI
2/15 pic.twitter.com/Jk34utlZxE
そして、このノイズを画像に変換する工程を、学習済みのニューラルネットワークが担当します。ニュートラルネットワークが学習するのは関数f(x,t)で、これはxのノイズをわずかに除去し、t-1回目でどのように見えるかを生成するものです。
How do DMs turn noise into images?
— AI Pub (@ai__pub) August 21, 2022
By training a neural network to do so gradually.
With the sequence of noised images = x_1, x_2, ... x_T,
The neural net learns a function f(x,t) that denoises x "a little bit", producing what x would look like at time step t-1.
3/15 pic.twitter.com/QZJX89X6Rk
純粋なノイズをキレイな画像に変化させるには、この関数を何度も適用すればいいというわけです。Stable Diffusionの処理はいわばf(f(f(f(....f(N, T), T-1), T-2)..., 2, 1)というように、関数の入れ子状態になっています。このNは純粋なノイズ、Tはステップ数になります。
To turn pure noise into an HD image, just apply f several times!
— AI Pub (@ai__pub) August 21, 2022
The output of a diffusion model really is just
f(f(f(f(....f(N, T), T-1), T-2)..., 2, 1)
where N is pure noise, and T is the number of diffusion steps.
The neural net f is typically implemented as a U-net.
4/15 pic.twitter.com/lxYvucaCGt
もちろん一連の作業を512×512ピクセルで行うのは計算負荷が非常に高く、コストもかかります。
The key idea behind Stable Diffusion:
— AI Pub (@ai__pub) August 21, 2022
Training and computing a diffusion model on large 512 x 512 images is _incredibly_ slow and expensive.
Instead, let's do the computation on _embeddings_ of images, rather than on images themselves.
5/15 pic.twitter.com/h8qRyxeqY1
そのため、この計算負荷を下げるため、実際のピクセル空間を使うのではなく、より低い次元である潜在空間を利用します。具体的には、エンコーダーを使って画像Xを潜在空間表現であるz(x)に圧縮し、xではなくz(x)で拡散(Diffusion Process)とノイズ除去(Denoising U-Net)を実行する流れ。以下の図のεがエンコーダーで、Dがデコーダーです。
So, Stable Diffusion works in two steps.
— AI Pub (@ai__pub) August 21, 2022
Step 1: Use an encoder to compress an image "x" into a lower-dimensional, latent-space representation "z(x)"
Step 2: run diffusion and denoising on z(x), rather than x.
Diagram below!
6/15 pic.twitter.com/SPHCyZDOSD
ニューラルネットワークがどのように画像を理解しているのかについては、以下の記事でも解説しています。
ニューラルネットワークはどのように画像を理解しているのか - GIGAZINE

Stable Diffusionは、さらに関数の変数として文字列(プロンプト)を入力できます。プロンプトを入力することで、ノイズ除去の方向がある程度定まることになります。
But where does the text prompt come in?
— AI Pub (@ai__pub) August 21, 2022
I lied! SD does NOT learn a function f(x,t) to denoise x a "little bit" back in time.
It actually learns a function f(x, t, y), with y the "context" to guide the denoising of x.
Below, y is the image label "arctic fox".
8/15 pic.twitter.com/z4WVWJ8NVu
Stable Diffusionはプロンプトという「コンテクスト」を、ノイズ除去プロセスで行われる単純結合(Simple concatenation)とデコード直前のCross-attentionで介入させます。
But how does SD process context?
— AI Pub (@ai__pub) August 21, 2022
The "context" y, alongside the time step t, can be injected into the latent space representation z(x) either by:
1) Simple concatenation
2) Cross-attention
Stable diffusion uses both.
10/15 pic.twitter.com/WaDmy0RyIB
また、コンテクストとして文字列のほかに画像を扱うことができるというのもStable Diffusionの大きな特徴です。Stable Diffusionは、画像データから画像修復と画像合成の同時を行います。
The cool part not talked about on Twitter: the context mechanism is incredibly flexible.
— AI Pub (@ai__pub) August 21, 2022
Instead of y = an image label,
Let y = a masked image, or y = a scene segmentation.
SD trained on this different data, can now do image inpainting and semantic image synthesis!
11/15 pic.twitter.com/wzCF8OOV0p
また、以下の記事でもStable Diffusionの仕組みについて詳しくまとめられています。
アーティストの権利侵害やポルノ生成などの問題も浮上する画像生成AI「Stable Diffusion」の仕組みとは? - GIGAZINE

・関連記事
言語処理のニューラルネットワークモデルが脳の働きと同じ構造をしているという仮説 - GIGAZINE
「ロシアのGoogle」とも呼ばれるYandexが1000億ものパラメーターを持つ言語モデル「YaLM 100B」をオープンソースで公開 - GIGAZINE
ロボットに炒め料理のテクニックを教えるコツとは? - GIGAZINE
「AIが生成したイラストの投稿禁止」をイラスト投稿サイトが次々に決定し始めている - GIGAZINE
まるで人間のアーティストが描いたような画像を生成するAIが「アーティストの権利を侵害している」と批判される - GIGAZINE
画像生成AI「Midjourney」の描いた絵が美術品評会で1位を取ってしまい人間のアーティストが激怒 - GIGAZINE
・関連コンテンツ
in ソフトウェア, Posted by log1i_yk
You can read the machine translated English article An easy-to-understand illustration of ``….