Two Ways to Fade an Image

Diffusion ModelsMedium+20 XP+20

Two Ways to Fade an Image

A noise schedule decides how fast the forward process destroys an image. The number to watch is the signal remaining after step ttt:

αˉt=(1−β1)(1−β2)⋯(1−βt)\bar\alpha_t = (1-\beta_1)(1-\beta_2)\cdots(1-\beta_t)αˉt​=(1−β1​)(1−β2​)⋯(1−βt​)

where βs\beta_sβs​ is the amount of noise added at step sss. Before any noise is added it is 1, and it falls toward 0 as the steps go on. You will build two schedules with TTT steps each and compare their curves.

Linear. The betas rise in equal increments, from beta_start at step 1 to beta_end at step TTT:

βt=βstart+t−1T−1 (βend−βstart)\beta_t = \beta_{\text{start}} + \frac{t-1}{T-1}\,\big(\beta_{\text{end}} - \beta_{\text{start}}\big)βt​=βstart​+T−1t−1​(βend​−βstart​)

Cosine. Start from the curve

f(t)=cos⁡2 ⁣(t/T+s1+s⋅π2)f(t) = \cos^2\!\left(\frac{t/T + s}{1 + s}\cdot\frac{\pi}{2}\right)f(t)=cos2(1+st/T+s​⋅2π​)

where sss is a small offset. Each step's beta measures how much that curve drops compared with the step before. It is capped at 0.9990.9990.999 so that no single step wipes the image out completely:

βt=min⁡ ⁣(1−f(t)f(t−1), 0.999)\beta_t = \min\!\left(1 - \frac{f(t)}{f(t-1)},\ 0.999\right)βt​=min(1−f(t−1)f(t)​, 0.999)

For both schedules, the signal remaining is the running product of (1−βt)(1 - \beta_t)(1−βt​) over these betas. For the cosine schedule, that means the capped betas.

Task: write compare_schedules(T, beta_start, beta_end, s, checkpoints). It returns a tuple (linear, cosine). Each list has one entry per step number in checkpoints, in the order given, holding αˉt\bar\alpha_tαˉt​ for that schedule at that step.

  • TTT is at least 2, and every checkpoint is a step number from 1 to TTT.
  • Round each value to 4 decimal places, only at the end.

Once your function passes, put the first example's two lists side by side and look at step 500.

Examples

T = 1000
beta_start = 0.0001
beta_end = 0.02
s = 0.008
checkpoints = [100, 250, 500, 750, 1000]
returns ([0.897, 0.5241, 0.0786, 0.0034, 0.0], [0.9721, 0.847, 0.4938, 0.1443, 0.0])
T = 4
beta_start = 0.05
beta_end = 0.4
s = 0.0
checkpoints = [1, 2, 3, 4]
returns ([0.95, 0.7917, 0.5674, 0.3404], [0.8536, 0.5, 0.1464, 0.0001])

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run