본문 바로가기

vision/generate model

DDIM: Denoising Diffusion Implicit Models

[2010.02502] Denoising Diffusion Implicit Models (arxiv.org)

 

Denoising Diffusion Implicit Models

Denoising diffusion probabilistic models (DDPMs) have achieved high quality image generation without adversarial training, yet they require simulating a Markov chain for many steps to produce a sample. To accelerate sampling, we present denoising diffusion

arxiv.org

2021 ICLR

1. Introduction

high quality sample을 생성하는데 너무 많은 iteration이 필요 - 수천번의 step

⇒ GAN보다 훨씬 많은 시간 소요(한 단계만 거치면됨)

이 논문에서 GAN과의 이 gap을 해결하고자 함 ⇒ DDIM(denoising diffusion implicit models)

Loss function같은 구조는 거의 DDPM과 동일함

 

Markovian, non-Markovian 이용

forward ⇒ non-Markov chain, reverse ⇒ Markov chain

non-Markovian diffusion process, Markovian diffusion process

이러한 방식으로 sample quality를 약간 감소시키더라도 sample efficiency를 훨씬 증가시킬 수 있었음

2. Background

여기서 다루는 $\alpha_t$ 는 이전 논문에서 다루던 $\bar \alpha_t$ $\bar \alpha_t = \alpha_t * \alpha_{t-1} * ... * \alpha_1 , \alpha_t = 1-\beta_t$

나머지 내용들은 모두 전 논문들에 나온 내용이니 따로 다루지는 않겠음

논문에서 $\alpha$로 바꿔 쓰는 걸 그냥 이전 term 처럼 $\bar\alpha$로 작성

3. Variational Inference for Non-Markovian Forward Process

  • inference process에서 iteration 수를 줄이는 것이 목표
  • 기존 DDPM에서는 marginal distribution(주변분포)인 $q(x_t, x_0)$에만 의존
  • joint distribution(결합분포)인 $q(x_{1:T}|x_0)$에는 의존하지 않음
  • 이 저자들이 새로 제시하는 것은 non-Markovian process
  • objective function의 변화는 없기에 굳이 다른 objective function으로 학습할 필요 x
  • ⇒ 위의 그림을 통한 전개할 샘플링 방법만 사용하면됨

3.1 Non-Markovian Forward Process

forward process에서 $x_t$를 생성할 때 $x_{t-1}$만 보고 생성하는 것이 아닌 $x_0$까지 참고하는것

⇒ $q_\sigma(x_t|x_{t-1}, x_0)$

bayes rule을 이용해 간단하게 구할 수 있음

                         $q_\\sigma(x_t|x_{t-1}, x_0) = { q_\\sigma (x_{t-1}|x_t, x_0)q_\\sigma(x_t|x_0)\\over q_\\sigma(x_{t-1}|x_0)}$ 

$q_\sigma(x_t|x_0)$ or $q_\sigma(x_{t-1}|x_0)$의 경우는 기존 markov 방식을 이용해 계산 가능

$q_\sigma(x_{t-1}|x_t, x_0)$의 경우 이전 DDPM Loss 유도 하면서 구할 수 있었음

⇒ $q_\sigma(x_{t-1}|x_t, x_0) = \mathcal {N} (\sqrt{\bar\alpha_{t-1}}x_0 + \sqrt{1-\bar\alpha_{t-1}-\sigma_t^2} \cdot {x_t-\sqrt{\alpha_t}x_0\over \sqrt{1-\bar\alpha_t}}, \sigma_t^2I)$

$q_\sigma(x_t | x_{t-1}, x_0)$은 정규분포를 따르지만 더 이상 markovian process라 할 수 없음

⇒ $x_t$는 더이상 $x_{t-1}$의 영향만을 받는 것이 아닌 $x_0$의 영향도 받기 때문!

여기서 $\sigma$의 크기는 forward process가 얼마나 확률적인지 제어…?(파파고 번역이라 좀 이상)

$\sigma$→0일 때, 어떤 t에 대해 $x_0$ 와 $x_t$를 알면 고정된 $x_{t-1}$을 알 수 있는 극단 적인 경우에 도달…?

The magnitude of $\sigma$ controls the how stochastic the forward process is; when $\sigma$→ 0, we reach an extreme case where as long as we observe $x_t$ and $x_0$ for some t, then $x_{t-1}$ become known and fixed.(원문 첨가 했습니다. 혹시 이 문장에 대해 제가 작성한 의견보다 좀더 좋은 의견 있으시면 달아주세요🙏)

⇒ 논문 표현자체가 어려워서 좀 생각해보면서 해봤는데. 여기서 $\sigma$는 우리가 설정하는 noise scheduling이라고 봐야할 것 같음! 이전에 linear scheduling, cosine scheduling에 따라 설정했는데 이 논문에서 만약에

$\sigma$ → 0 인 극단 적인 상황에서 $x_0$와 $x_t$를 알면 고정된 $x_{t-1}$을 알 수 있다는 것 같음!

기존의 $q_\sigma(x_{t-1}|x_t, x_0)$의 경우는 $\sigma$값에 따라 달라지기에 그냥 이 값이 고정 될 수도 있다 이런 말..?

규석) $\sigma$가 0보다 크면 확률적으로 제어하고 $\sigma$ → 0 (극단적인 상황) 이면 Stochastic하지 않고 Deterministic 하여 $x_0$와 $x_t$를 알면$x_0와 x_t$$x_{t-1}, x_{t-2}, ...$ 알 수 있다는 뜻인거같아요!

3.2 Generative Process And Unified Variational Interference Objective

직전에 forward process에서 markov chain을 non-markovian 으로 바꿨으니 이제 reverse process(이 논문에서는 generative process라는 term 이용)는 어떻게 될 지 논의

우선 직관적으로 생각, noisy observation인 $x_t$ 가 있고 이에 대응하는 $x_0$우선 예측

⇒ 이를 이용해 sample $x_{t-1}$얻음

논문에서 이렇게 표현되어 있는데 좀더 직관적으로 바라보았을 때는 $x_t$ 와 $x_0$를 이용해 sample $x_{t-1}$을 얻는다. 이렇게 바라보면 되는 듯 하다.

⇒ $x_{t-1}$ 얻기 위해 $q_\sigma(x_{t-1}|x_t, x_0)$ 이용하면 됨

우리가 이전에 사용했던 $x_t, x_0, \epsilon$식을 다시 이용할 예정

$x_t = \sqrt{\bar\alpha_t}x_0 + \sqrt{1-\bar\alpha_t}\epsilon$

이 식을 변형해 $x_t$가 주어진 상태에서 $x_0$를 예측하는 denoised observation 예측 식 작성 가능

$f_\theta^{(t)} (x_t) := (x_t - \sqrt{1-\alpha_t}\cdot \epsilon_\theta^{(t)} (x_t))/\sqrt{\alpha_t}$

여기서 $\epsilon_\theta^{(t)}$ 는 우리가 예측한 noise

$p_\theta(x_T) = \mathcal N(0, I)$를 이용해 generative process(reverse process)정의 가능

$p_\theta^{(t)}(x_{t-1}|x_t) =\begin{cases} \mathcal N(f_\theta^{(1)}(x_1), \sigma_1^2I )& \text {if}\ t=1 \newline q_\sigma(x_{t-1}|x_t, f_\theta^{(t)}(x_t)) & \text{Otherwise} \end{cases}$

$p_\theta^{(t)} (\mathbf x_{t-1}|\mathbf x_t) = \begin{cases} \mathcal N(f_\theta^{(1)}(\mathbf x_1), \sigma^2I) & \text{if} \ t = 1\newline q_\sigma(\mathbf x_{t-1}|\mathbf x_t, f_\theta^{(t)}(\mathbf x_t)) &\text{Otherwise}\end{cases}$

아래의 경우 위의 $q_\sigma(x_{t-1}|x_t, x_0)$에서 $x_0$를 $f_\theta^{(t)}(x_t)$로 바꾼 식이다

t = 1일 때는 단순 가우시안 noise.

$\sum_{t>1} D_{KL} (q_\sigma(x_{t-1}|x_t, x_0) || p_\theta(x_{t-1}|x_t))$ 기존 DDPM에서 q분포를 non-markovian 으로 바꿔주면 DDIM의 loss가 되는 것

여기서 $p_\theta$를 우리가 위에서 새로 정의한 함수를 이용해 표현해보자 $f_\theta^{(t)}(x_t)$를 $x_0(x_t, \epsilon_\theta)$로 바꿔 표현

⇒ $D_{KL}(q_\sigma(x_{t-1} | x_t, x_0) || q_\sigma(x_{t-1}|x_t, x_0(x_t, \epsilon_\theta))$

⇒ $\mathbb E_q({||x_0 - x_0(x_t, \epsilon_\theta)||^2_2\over 2\sigma_t^2})$

⇒ $\mathbb E_q({||(x_t - \sqrt{1-\bar\alpha_t}\epsilon / \sqrt{\bar\alpha_t} - (x_t - \sqrt{1-\bar\alpha_t}\epsilon_\theta^{(t)}(x_t))/\sqrt{\bar\alpha_t}||^2_2\over2\sigma_t^2})$

⇒ $\mathbb E_q((1-\bar\alpha_t)\cdot{||\epsilon-\epsilon_\theta^{(t)}(x_t)||^2\over2\sigma_t^2\bar\alpha_t} = \mathbb E_q({1-\bar\alpha_t\over2\sigma_t^2\bar\alpha_t}\cdot||\epsilon-\epsilon_\theta^{(t)}(x_t)||^2)$

결국 DDIM sampling을 하기 위해 설정한 non-markovian posterior가 실제로 DDPM loss로 수렴하는 것을 확인할 수 있음

4. Sampling from generalized generative process

DDIM에서 진행되는 sampling 방법. 이에 대한 논의

4.1 Denoising Diffusion Implicit Models

$p_\theta^{(t)}(x_{t-1}|x_t) =\begin{cases} \mathcal N(f_\theta^{(1)}(x_1), \sigma_1^2I )& \text {if}\ t=1 \newline q_\sigma(x_{t-1}|x_t, f_\theta^{(t)}(x_t)) & \text{Otherwise} \end{cases}$

$q_\sigma(x_{t-1}|x_t, x_0) = \mathcal {N} (\sqrt{\bar\alpha_{t-1}}x_0 + \sqrt{1-\bar\alpha_{t-1}-\sigma_t^2} \cdot {x_t-\sqrt{\alpha_t}x_0\over \sqrt{1-\bar\alpha_t}}, \sigma_t^2I)$

위에서 만들었던 식들을 이용해 $x_t$에서 $x_{t-1}$을 샘플링 해보자

⇒ $x_{t-1} = \sqrt{\bar\alpha_{t-1}}({x_t-\sqrt{1-\bar\alpha_t}\epsilon_\theta^{(t)}(x_t)\over\sqrt{\bar\alpha_t}}) + \sqrt{1-\bar\alpha_{t-1}-\sigma_t^2}\cdot\epsilon_\theta^{(t)}(x_t) + \sigma_t\epsilon_t$

왼쪽 괄호 부분이 $x_0$예측, 중간 항이 $x_t$, 마지막이 random noise 식을 보면 $x_0$를 예측하고 $x_t$를 다시 복구하는 형태를 가짐

$\sigma$값 설정을 어떻게 하냐에 따라 조금씩 달라진다.

만약 $\sigma_t = \eta\sqrt{(1-\alpha_t) \times({1-\bar\alpha_{t-1}\over1-\bar\alpha_t})}$ $\eta = 1$ 이라면 기존 DDPM과 같은 수식이 된다.

이 variance 유도하는 식 만약 세미나 발표하게 되면 살펴보기

Welcome to JunYoung's blog | DDIM(Denoising Diffusion Implicit Models) 이해하기

  • DDPM : $x_{t-1} = \sqrt{\bar\alpha_{t-1}}x_0 + \sqrt{1-\bar\alpha_{t-1}}\epsilon$

forward process가 markovian이 되고 generative process가 DDPM이 된다는 뜻

$\sigma_t = 0$ 일 때의 경우를 보자. 이 때는 random noise가 0가 되어 $x_t$ 에서 $x_0$로 sampling 되는 과정이 고정됨

⇒ Denoising Diffusion Implicit Models

train할 때 DDPM의 objective를 이용할 수 있으며 sampling 과정을 더 빠르게 할 수 있는 장점을 가진 모델이 DDIM

4.2 Accelerated generation process

DDPM에서는 forward process에 T step, reverse process에 T step 필요

그러나 $q_\sigma(x_t|x_0)$는 고정되어 있기에 T step보다 적은 forward process를 고려할 수 있을거라고 예상

⇒ 이를 통해 다른 model을 훈련시키지 않고도 reverse process(generate process)를 accelerate가능

forward process에서 latent variable $x_{1:T}$를 모두 정의하는 것이 아닌 sub set$\{x_{T1}, ... , x_{TS}\}$ 만 정의

여기서 T1 ~ TS는 기존 1..T의 subsequence

$q(x_{\tau_i}|x_0) = \mathcal N(\sqrt{\bar\alpha_{\tau_i}}x_0, (1-\bar\alpha_{\tau_i})I)$ 로 정의 가능 위에서 보였던 그림으로 확인 가능

연속 sequence에 의존할 필요가 없다는 것을 알 수 있음(굳이 t-1 번째 sample 에측할 필요없이, t-2번째 sample을 예측해도 괜찮다는 뜻) - 4.1에서 식에서 t-1을 t-2로 바꿔도 상관 없음

이 때 $\tau$로 정의된 sequence는 기존 sequence 길이인 T보다 훨씬 작다

⇒ sampling process에서 계산 효율을 훨씬 높일 수 잇다.

4.3 Relevence to neural ODEs

$x_{t-1} = \sqrt{\bar\alpha_{t-1}}({x_t-\sqrt{1-\bar\alpha_t}\epsilon_\theta^{(t)}(x_t)\over\sqrt{\bar\alpha_t}}) + \sqrt{1-\bar\alpha_{t-1}-\sigma_t^2}\cdot\epsilon_\theta^{(t)}(x_t) + \sigma_t\epsilon_t$

이 식을 다시 써보자

ODE(ordinary differential equations)를 위한 오일러 방법과 유사한 식 얻을 수 있음

${x_t - \Delta t\over \sqrt{\bar\alpha_t - \Delta t}} = {x_t\over\sqrt{\bar\alpha_t}} + (\sqrt{1-\bar\alpha_t-\Delta t \over \bar\alpha_t - \Delta t } - \sqrt{1-\bar\alpha_t \over \bar\alpha_t}) \epsilon_\theta^{(t)}(x_t)$

…..

여기 파트는 너무 개념이 동 떨어져 있는 느낌….?? 수식 자체가 어렵기도 하고 다른 리뷰들에선 잘 다루지 않는다… 후속 논문에서 비슷한 개념이 나오면 다시 이 파트 리뷰하겠습니다.

5. Experiments

DDPM과 다르게 DDIM에서는 한번 초기 latent variable $x_{tau}$가 고정되면 높은 수준의 image feature를 유지

또한 latent space에서 direct하게 sample을 뽑아오는 것이 가능하나 DDPM에서는 어려움(단계 일일이 거침)

DDPM과의 비교가 중점이므로 DDPM, DDIM 모두 T = 1000으로 각각 훈련 시킴

이 비교에서 둘의 차이는 오직 sampling 생성 방법의 차이이다. ($\tau, \sigma$)의 차이

$\sigma_{\tau_i}(\eta) = \eta\sqrt{(1-\bar\alpha_{\tau_{i-1}})/(1-\bar\alpha_{\tau_i})}\sqrt{1-\bar\alpha_{\tau_i}/\bar\alpha_{\tau_{i-1}}}$

$\eta = 1$ ⇒ DDPM, $\eta = 0$ ⇒ DDIM

step을 줄이면서 나타나는 성능을 보면 DDPM의 경우 성능이 크게 낮아지지만 DDIM은 눈에 띌 정도로 성능이 낮아지는 것을 방지할 수 있었음

figure 3 는 step을 다르게 해가면서 확인한 결과

step이 다르더라도 초기 $x_\tau$가 같을 경우 high level feature가 비슷하게 나오는 것을 보임

20 step으로만 만들어진 sample들이 1000step으로 이루어진 sample과 매우 닮은 현상

개인적인 discussion

  • 이전 diffusion 논문들 읽을 때 그냥 diffusion은 markov 고정 인줄 알았는데 $x_0$를 이용한 nonmarkovian이 굉장히 신박… 뭔가 후속 논문들에도 이 개념이 쓰일 것 같다
  • sampling 단계 줄이는게 DDIM 말고 improved ddpm에도 있었는데 이 논문에서는 딱히 그럴듯한 근거가 없었는데 여기서 제대로된 근거가 제시되어 흥미로웠음
  • 오일러 방법을 이용한 ODE….?? 이 부분 나중에 다시 공부 해볼만 한 것 같음
  • 논문 수식 표현 방법이 굉장히 이상하지만 나름 수식으로 근거도 많이 들었고 잘 쓴 논문인 것 같다..!