Denoising Diffusion Probabilistic Models
์์ฆ Hot๐ฅย ํ diffusion model์ ์์กฐ๊ฒฉ์ธ ๋ ผ๋ฌธ์ด๋ค. Novel ai ๋ผ๋ ๊ณณ์์ ์์ฉํํ๊ธฐ๋ ํ ๋ชจ๋ธ์ด๋ค. ( Stable Diffusion์ด๋ผ๋ ๋ ๋ฐ์ ๋ ๋ชจ๋ธ ) ์์์ด ์ ๋ง์ ๋ง์ ๋ง ๋ง๊ธฐ ๋๋ฌธ์ ์์ ํ๋ํ๋๋ฅผ ๋ค ์ดํดํ๊ธฐ ๋ณด๋ค๋ ์ด๋ค flow๋ก ํ์ต์ด ์งํ๋๋์ง, ๊ธฐ๋ฐ์ด ๋๋ ์ปจ์ ์ด ๋ฌด์์ธ์ง, ๊ตฌํ์ ์ด๋ค์์ผ๋ก ๋์ด์๋์ง ์ ๋๋ฅผ ์ดํด๋ณด๋ ค๊ณ ํ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก diffusion model์ โ์์ฑโ๋ชจ๋ธ์ด๋ค. ์ธ์์ ์๋ ์ด๋ค ์๋ก์ด ๊ฒ์ ์์ฑํด๋ด๋ ๊ฒ์ด ๋ชฉ์ ์ด๋ค.
๊ธฐ์กด์ ๋ง์ด ์ฌ์ฉ๋๋ ์์ฑ๋ชจ๋ธ์ ์ด๋ ๊ฒ 4๊ฐ์ง ์ ๋๋ก ์ถ๋ ค๋ณผ ์ ์๋ค.

https://lilianweng.github.io/posts/2021-07-11-diffusion-models/

๋ ผ๋ฌธ์ ์ฒจ๋ถ๋์ด ์๋ figure๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก $\bold{x}_0 โ \bold{x}_T$ ๊ณผ์ ์ forward process, $\bold{x}_T โ \bold{x}_0$ ๊ณผ์ ์ reverse process๋ผ๊ณ ์ ์ํ๋ค. ์ฃผ์ํ ์ ์ ์ด ๊ณผ์ ์ ์ ์๊ฐ โtrainingโ๊ณผ ๊ด๋ จ๋ ๊ฒ์ ์๋๋ผ๋ ๊ฒ์ด๋ค. ๊ทธ๋ฅ noise๋ฅผ ์ ์ ๋ํด์ฃผ๋ ๊ณผ์ ๊ณผ ์ ์ ์์ ์ฃผ๋ ๊ณผ์ ์ โ์ฉ์ด๋ก ์ ์โ๋ง ํ ๊ฒ ๋ฟ์ด๋ค.
timesteps๋ ๋ณดํต 1000์ผ๋ก ์ ์ํ๋ค. (github๊ตฌํ๋ค)
Forward process : noise๋ฅผ ์์ฐจ์ ์ผ๋ก ์ ์ ๋ํด์ฃผ์ด ์์ ํ noise( ์ฌ๋ ๊ทธ๋ฆผ์ ์๋ฏธ๊ฐ ์์ ํ ๊นจ์ง ๋ ๊น์ง ) ๋ก ๋ง๋๋ ๊ณผ์ . $q$๋ก ์ ์๋์ด ์๋ค.


## pseudo code
noise = torch.randn_like(x_0) # x_0์ size์ ๊ฐ์ gaussian ๋ถํฌ ์์ฑ
p_sample = sqrt(alpha_t)* x_0 + sqrt(1-alpha_t) * noise
์ด๋ ์ฌ์ค reparameterization trick์ ์ฌ์ฉํด $\bold{x}_0$์ผ๋ก๋ถํฐ $\bold{x}_T$๋ฅผ ์์ฑํด๋ธ ๊ฒ์ด๋ค.
$\alpha_t = 1-\beta_t , \; \;\;\overline{\alpha}t = \prod{t=1}^t \alpha_i$

betas = torch.linspace(beta_start, beta_end, timesteps, dtype = torch.float64) # start๊ฐ๊ณผ end๊ฐ์ ๊ธฐ์ค์ผ๋ก step๋งํผ์ ๊ฐ์ ๊ฐ์ ๊ฐ๊ฒฉ์ผ๋ก return
alphas = 1. - betas
alphas_cumprod = torch.cumprod(alphas, dim=0) # ๋์ ๊ณฑ์ return
Backward process : ์์ ํ noise ๋ก๋ถํฐ ์์ฐจ์ ์ผ๋ก ์ ์ original image๋ก ๋ณต์ํด๊ฐ๋ ๊ณผ์ . $p_\theta$ ๋ก ์ ์ํ๋ค. $q(\bold{x}_{t-1}|\bold{x}t)$ ๋ฅผ ์์ธกํ๋ ๊ฒ์ด๋ค. ๊ทธ๋ฌ๋ ์ด๊ฑด ์ฝ์ง ์๊ธฐ ๋๋ฌธ์ $p\theta$ ๋ฅผ ํตํด conditional probabilities๋ฅผ ๋ฐฐ์์ผ ํ๋ ๊ฒ์ด๋ค.
โ noise๋ก๋ถํฐ ํน์ ์์ t์ noise $\bold{x}_t$๋ฅผ ์์ธก

โ ๏ธย reverse conditional probability๋ $\bold{x}0$ ์ด ์ฃผ์ด์ง๋ฉด ๋ค๋ฃจ๊ธฐ ์ฝ๋ค โ $q(\bold{x}{t-1}|\bold{x}_t, \bold{x}0) = \mathcal{N}(\bold{x}{t-1} ; \tilde{\mu}(\bold{x}_t, \bold{x}_0), \tilde{\beta}_t\bold{I})$
$p(\bold{x}T)=\mathcal{N}(\bold{x}T;0, \bold{I})$ ์์ ์์ํ๋ Markov chain with learned Gaussian transition์ผ๋ก ์ ์๋ joint distribution $p\theta(\bold{x}{0:T})$๊ฐ reverse process๋ก ์ ์๋๋ค. โ forward process์์ time step์ ๋ฐ๋ผ ์ถ๊ฐ๋ gaussian noise์ ํ๊ท ๊ณผ ๋ถ์ฐ์ ์๊ฒ ๋๋ค๋ฉด ์๋์ ์ด๋ฏธ์ง ๋ถํฌ๋ก ๋๋์๊ฐ ์ ์์ง ์์๊น?๋ผ๋ ๊ฐ๋ ์ธ ๋ฏ ํ๋ค.
ํ์ตํ๋ ๊ณผ์ ์ ๋ณด๋ฉด ์ง๊ด์ ์ผ๋ก ์ดํด๊ฐ ๊ฐ ๊ฒ์ด๋ค.