Skip to content

wangjia184/diffusion_model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5ea111b · Dec 25, 2024

History

57 Commits
Jul 14, 2024
Dec 25, 2024
Dec 25, 2024
Feb 24, 2023
Jul 15, 2024
Dec 24, 2023
Mar 14, 2023
Feb 26, 2023
Feb 24, 2023
Mar 27, 2023
Feb 26, 2023
Jul 14, 2024

Repository files navigation

Diffusion model in web browser

Online Demo! Run it in your web browser

Youtube

UPDATE:

  • 2024-07-14 : Update online sample to use WebGPU if possible
  • 2024-07-15 : Added DDIM sampling method

1. DDPM Introduction

  • q - a fixed (or predefined) forward diffusion process of adding Gaussian noise to an image gradually, until ending up with pure noise
  • p θ - a learned reverse denoising diffusion process, where a neural network is trained to gradually denoise an image starting from pure noise, until ending up with an actual image.

Both the forward and reverse process indexed by t happen for some number of finite time steps T (the DDPM authors use T =1000). You start with t = 0 where you sample a real image x 0 from your data distribution, and the forward process samples some noise from a Gaussian distribution at each time step t , which is added to the image of the previous time step. Given a sufficiently large T and a well behaved schedule for adding noise at each time step, you end up with what is called an isotropic Gaussian distribution at t = T via a gradual process

2. Forward Process q

x 0 q ( x 1 | x 0 ) x 1 q ( x 2 | x 1 ) x 2 x T 1 q ( x t | x t 1 ) x T

This process is a markov chain, x t only depends on x t 1 . q ( x t | x t 1 ) adds Gaussian noise at each time step t , according to a known variance schedule β t

x t = 1 β t × x t 1 + β t × ϵ t

  • β t is not constant at each time step t . In fact one defines a so-called "variance schedule", which can be linear, quadratic, cosine, etc.

0 < β 1 < β 2 < β 3 < < β T < 1

  • ϵ t Gaussian noise, sampled from standard normal distribution.

x t = 1 β t × x t 1 + β t × ϵ t

Define a t = 1 β t

x t = a t × x t 1 + 1 a t × ϵ t

2.1 Relationship between x t and x t 2

x t 1 = a t 1 × x t 2 + 1 a t 1 × ϵ t 1

x t = a t ( a t 1 × x t 2 + 1 a t 1 ϵ t 1 ) + 1 a t × ϵ t

x t = a t a t 1 × x t 2 + a t ( 1 a t 1 ) ϵ t 1 + 1 a t × ϵ t

Because N ( μ 1 , σ 1 2 ) + N ( μ 2 , σ 2 2 ) = N ( μ 1 + μ 2 , σ 1 2 + σ 2 2 )

Proof

x t = a t a t 1 × x t 2 + a t ( 1 a t 1 ) + 1 a t × ϵ

x t = a t a t 1 × x t 2 + 1 a t a t 1 × ϵ

2.2 Relationship between x t and x t 3

x t 2 = a t 2 × x t 3 + 1 a t 2 × ϵ t 2

x t = a t a t 1 ( a t 2 × x t 3 + 1 a t 2 ϵ t 2 ) + 1 a t a t 1 × ϵ

x t = a t a t 1 a t 2 × x t 3 + a t a t 1 ( 1 a t 2 ) ϵ t 2 + 1 a t a t 1 × ϵ

x t = a t a t 1 a t 2 × x t 3 + a t a t 1 a t a t 1 a t 2 ϵ t 2 + 1 a t a t 1 × ϵ

x t = a t a t 1 a t 2 × x t 3 + ( a t a t 1 a t a t 1 a t 2 ) + 1 a t a t 1 × ϵ

x t = a t a t 1 a t 2 × x t 3 + 1 a t a t 1 a t 2 × ϵ

2.3 Relationship between x t and x 0

  • x t = a t a t 1 × x t 2 + 1 a t a t 1 × ϵ
  • x t = a t a t 1 a t 2 × x t 3 + 1 a t a t 1 a t 2 × ϵ
  • x t = a t a t 1 a t 2 a t 3 . . . a t ( k 2 ) a t ( k 1 ) × x t k + 1 a t a t 1 a t 2 a t 3 . . . a t ( k 2 ) a t ( k 1 ) × ϵ
  • x t = a t a t 1 a t 2 a t 3 . . . a 2 a 1 × x 0 + 1 a t a t 1 a t 2 a t 3 . . . a 2 a 1 × ϵ

$$\bar{a}{t} := a{t}a_{t-1}a_{t-2}a_{t-3}...a_{2}a_{1}$$

x t = a ¯ t × x 0 + 1 a ¯ t × ϵ , ϵ N ( 0 , I )

q ( x t | x 0 ) = 1 2 π 1 a ¯ t e ( 1 2 ( x t a ¯ t x 0 ) 2 1 a ¯ t )

3.Reverse Process p

Because P ( A | B ) = P ( B | A ) P ( A ) P ( B )

p ( x t 1 | x t , x 0 ) = q ( x t | x t 1 , x 0 ) × q ( x t 1 | x 0 ) q ( x t | x 0 )

x t = a t x t 1 + 1 a t × ϵ ~ N ( a t x t 1 , 1 a t )
x t 1 = a ¯ t 1 x 0 + 1 a ¯ t 1 × ϵ ~ N ( a ¯ t 1 x 0 , 1 a ¯ t 1 )
x t = a ¯ t x 0 + 1 a ¯ t × ϵ ~ N ( a ¯ t x 0 , 1 a ¯ t )

q ( x t | x t 1 , x 0 ) = 1 2 π 1 a t e ( 1 2 ( x t a t x t 1 ) 2 1 a t )

q ( x t 1 | x 0 ) = 1 2 π 1 a ¯ t 1 e ( 1 2 ( x t 1 a ¯ t 1 x 0 ) 2 1 a ¯ t 1 )

q ( x t | x 0 ) = 1 2 π 1 a ¯ t e ( 1 2 ( x t a ¯ t x 0 ) 2 1 a ¯ t )

q ( x t | x t 1 , x 0 ) × q ( x t 1 | x 0 ) q ( x t | x 0 ) = [ 1 2 π 1 a t e ( 1 2 ( x t a t x t 1 ) 2 1 a t ) ] [ 1 2 π 1 a ¯ t 1 e ( 1 2 ( x t 1 a ¯ t 1 x 0 ) 2 1 a ¯ t 1 ) ] ÷ [ 1 2 π 1 a ¯ t e ( 1 2 ( x t a ¯ t x 0 ) 2 1 a ¯ t ) ]

2 π 1 a ¯ t 2 π 1 a t 2 π 1 a ¯ t 1 e [ 1 2 ( ( x t a t x t 1 ) 2 1 a t + ( x t 1 a ¯ t 1 x 0 ) 2 1 a ¯ t 1 ( x t a ¯ t x 0 ) 2 1 a ¯ t ) ]

1 2 π ( 1 a t 1 a ¯ t 1 1 a ¯ t ) exp [ 1 2 ( ( x t a t x t 1 ) 2 1 a t + ( x t 1 a ¯ t 1 x 0 ) 2 1 a ¯ t 1 ( x t a ¯ t x 0 ) 2 1 a ¯ t ) ]

1 2 π ( 1 a t 1 a ¯ t 1 1 a ¯ t ) exp [ 1 2 ( x t 2 2 a t x t x t 1 + a t x t 1 2 1 a t + x t 1 2 2 a ¯ t 1 x 0 x t 1 + a ¯ t 1 x 0 2 1 a ¯ t 1 ( x t a ¯ t x 0 ) 2 1 a ¯ t ) ]

1 2 π ( 1 a t 1 a ¯ t 1 1 a ¯ t ) exp [ 1 2 ( x t 1 ( a t ( 1 a ¯ t 1 ) 1 a ¯ t x t + a ¯ t 1 ( 1 a t ) 1 a ¯ t x 0 ) ) 2 ( 1 a t 1 a ¯ t 1 1 a ¯ t ) 2 ]

p ( x t 1 | x t ) N ( a t ( 1 a ¯ t 1 ) 1 a ¯ t x t + a ¯ t 1 ( 1 a t ) 1 a ¯ t x 0 , ( 1 a t 1 a ¯ t 1 1 a ¯ t ) 2 )

Because x t = a ¯ t × x 0 + 1 a ¯ t × ϵ , x 0 = x t 1 a ¯ t × ϵ a ¯ t . Substitute x 0 with this formula.

p ( x t 1 | x t ) N ( a t ( 1 a ¯ t 1 ) 1 a ¯ t x t + a ¯ t 1 ( 1 a t ) 1 a ¯ t × x t 1 a ¯ t × ϵ a ¯ t , β t ( 1 a ¯ t 1 ) 1 a ¯ t )

p ( x t 1 | x t ) N ( a t ( 1 a ¯ t 1 ) 1 a ¯ t x t + a ¯ t 1 ( 1 a t ) 1 a ¯ t × x t 1 a ¯ t × ϵ a ¯ t , β t ( 1 a ¯ t 1 ) 1 a ¯ t )

Note: This README.md is intended solely for previewing on the Github page. If you wish to view the rendered page locally, please consult README.raw.md.