Cosine Noise Schedule
Implementation of a cosine noise schedule for diffusion models.
This module provides a cosine-based noise scheduling as proposed in "Improved Denoising Diffusion Probabilistic Models" (Nichol & Dhariwal, 2021). The schedule offers smoother transitions between noise levels.
CosineNoiseSchedule
¶
Bases: BaseNoiseSchedule
Cosine noise schedule implementation for diffusion models.
This noise schedule uses a cosine function to define the noise levels over time, which typically results in better sample quality compared to linear schedules.
Source code in image_gen\noise\cosine.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
__call__(t, *_, **__)
¶
Compute beta(t) at timestep t.
For cosine schedule, beta(t) is derived from the derivative of alpha_bar(t): beta(t) = -d(log(alpha_bar))/dt = -d(alpha_bar)/dt / alpha_bar
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Tensor
|
Tensor of timesteps in [0, 1] range. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Beta values at specified timesteps. |
Source code in image_gen\noise\cosine.py
__init__(*_, s=0.008, beta_min=0.0001, beta_max=20.0, **__)
¶
Initialize the cosine noise schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
float
|
Small offset to prevent alpha_bar(t) from being too small near t=1. Defaults to 0.008. |
0.008
|
beta_min
|
float
|
Minimum noise level for numerical stability. Defaults to 0.0001. |
0.0001
|
beta_max
|
float
|
Maximum noise level for numerical stability. Defaults to 20.0. |
20.0
|
Source code in image_gen\noise\cosine.py
alpha_bar(t)
¶
Compute the cumulative product of (1-beta) up to time t.
Uses the cosine formula: alpha_bar(t) = cos^2((t/T + s)/(1 + s) * π/2)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Tensor
|
Tensor of timesteps in [0, 1] range. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
alpha_bar values at the specified timesteps. |
Source code in image_gen\noise\cosine.py
config()
¶
Get the configuration parameters of the noise schedule.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Dictionary containing the configuration parameters. |
Source code in image_gen\noise\cosine.py
integral_beta(t, *_, **__)
¶
Compute the integral of beta from 0 to t.
For cosine schedule, this equals -log(alpha_bar(t)) which represents the total amount of noise added up to time t.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Tensor
|
Tensor of timesteps in [0, 1] range. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Integrated beta values from 0 to t. |