These are some notes I took while studying diffusion models. I already implemented this paper on PyTorch.
Introduction
The ideal for generating images would be to define a probabilistic distribution containing all image content, and after that generate the image through a random process based on weighted probability as LLM does. But this is a huge task, and at the moment it is not possible.
Diffusion models propose a process to achieve image generation thoug a similar method: to develop a neural network-based model that is able to gradually remove Gaussian noise from an image. So starting from any image generaeted through a Gaussian noise distribution N(0,1), converge to image space after a finite number of N(0,1) steps.
Noising Process
To train this type of model, the first step is to develop an algorithm that takes any image and converts it to white noise in a finite number of steps. This will act as a starting point for our model which we will train to do the reverse process.
Initial approach
One method that could be considered is to simply add white noise step by step:
xt=xt−1+β⋅ϵxt=x0+t⋅β⋅ϵ
Notation:
x0 refers to the original image
xt image adter applying t-steps of noising
β corresponds to the weight of noise
ϵ∼N(0,1) is the noise added each step generated throug a normal distribution
Noising process example on an image over 500 steps. For testing if each distribution was likely a N(0,1) distribution, it was applied Kolmogorov-Smirnov test.
The problem with this method is that the distribution diverges at:
t→∞limxt∼σ→∞limN(0,σ2)
This phenomenon is called variance exploitation.
Noising process example on an image over 500 steps. For testing if each distribution was likely a N(0,1) distribution, it was applied Kolmogorov-Smirnov test.
Diffusion method
An alternative method is proposed in the original paper:
Diffusion process example on an image over 500 steps. For testing if each distribution was likely a N(0,1) distribution, it was applied Kolmogorov-Smirnov test.
It could be shown that this distribution already converges to a normal distribution:
t→∞limxt∼N(0,1)
Diffusion process example on an image over 500 steps. For testing if each distribution was likely a N(0,1) distribution, it was applied Kolmogorov-Smirnov test.
We have already defined a successful method that converges to a standardized normal distribution.
Markov chain notation
The forward process or diffusion process is defined as a Markov chain:
Where q is the probability density function of obtaining the value xt from the value xt−1.
q(xt∣xt−1)=N(xt;1−βt⋅xt−1,βtI)
Notation:
N(x,μ,σ) refers to the density probability function of normal distribution having the value x. To obtain the probability it must be integrated over the entire image space.
Q(x1:T∣x0)=∫t=1∏Tq(xt∣xt−1)dx1:T
Denoising Process
The inverse process is defined as the denoising process that converts a denoised image xT into a functional image. The whole process can be defined as a Markov chain:
pθ(x0:T)=pθ(xT)t=1∏Tpθ(xt−1∣xt)
Since by definition it starts from white noise, probability of the initial noise state could be considered as:
pθ(xT)=N(xT,0,I)
Notation:
θ refers to the parameters of the neural network. If any element contains θ it means that it was calculated through NN and its parameters.
By definition pθ and q are inverse processes of themselves. It can be shown that the inverse process of a Gaussian is a Gaussian (reference to demonstration) so:
pθ(xt−1∣xt)=N(xt−1,μθ(xt,t),σθ(xt,t))
Loss Function
Now that we have defined the stochastic process, it is time to define the loss function, as we have a probability density function over images, higer the probability of a generated image it is, better the image it is. Taking this into account the loss function could be defined as:
L=E[−logpθ(x0)]
With E referred as the mean value over all the examples and pθ(x0) could be obtained by integrating:
pθ(x0)=∫pθ(x0:T)dx1:T
Since it is not computationally possible to integrate over all the values, an approximation based on Jensen’s inequality is made.
As xT is generated thoug a normal standard distribution and no apportation of NN weights to the computation of this loss and we consider as hyperparameters βt in terms of model optimizaation we consider LT as a constant loss function in terms of θ.
Noising process contribution
Lt−1=DKL(q(xt−1∣xt,x0)∣∣pθ(xt−1∣xt))
As working with normal distributions is easier, it is considered that both distributions are gaussians:
If we suppose that we are working with unit8 images, each pixel contains a number between 0 and 255, we are going to work with this values normalized into the interval [-1, 1]. If we consider a pixel discrete pixel value into a continues space we should consider:
x→[x−2551,x+2551]
This transformation fully transforms discrete pixel values into continous space. For completion into the real space, we consider in limits intervals: (+∞,x+2551] and [x−2551,+∞).
So the result of conditioned probability would be the integral over each pixel (and channel) D: