← back

First Frame Anchor Noise Injection

#460 · Deep Learning · Easy

⊣ Solve on deep-ml.com

Problem

Implement first-frame anchor noise injection for video diffusion. Given a clean first frame and a noise level, produce the noised anchor frame that will be used as the conditioning signal during diffusion-based video generation.

Solution

1
2
3
4
5
6
7
8
import random, math

def anchor_noise_injection(
    first_frame: list[list[float]], noise_level: float, seed: int = 0
) -> list[list[float]]:
    rng = random.Random(seed)
    scale = math.sqrt(1 - noise_level ** 2)
    return [[scale * v + noise_level * rng.gauss(0, 1) for v in row] for row in first_frame]

Explanation

  1. Generate Gaussian noise matching the shape of the first frame.
  2. Apply the standard diffusion noising formula: blend the clean frame with noise using the given noise level sigma.
  3. The factor sqrt(1 - sigma^2) on the clean signal ensures the output maintains unit variance when the input has unit variance.
  4. A small noise level preserves most of the first-frame content while giving the diffusion model room to adjust fine details for temporal consistency.

Complexity

  • Time: O(H W C) for the element-wise operations
  • Space: O(H W C) for the noise and output arrays