TreeRingWatermarker<T>
Models & Types · AiDotNet.Safety.Watermarking
Tree-Ring watermarking for diffusion models: a keyed ring pattern written into the Fourier transform of the initial noise, which the sampler then turns into an image, detected by inverting the sampler back to noise.
Inheritance: ModelBase<T, Vector<T>, Vector<T>> → SafetyModuleBase<T> → ImageSafetyModuleBase<T> → TreeRingWatermarker<T>
Implements: IGeneratedParameterRegistrar<T>, IKeyedWatermarker
For Beginners
a diffusion model turns random noise into a picture. This watermarker chooses that starting noise so that its low-frequency “fingerprint” spells out a secret ring pattern. The finished picture shows nothing, but running the model backwards from the picture recovers the noise, and the rings are still there. Only someone with the same key and the same model can check for them.
How It Works
Following Wen et al. (2023): the key is a set of concentric rings in the centred 2D Fourier transform of one
channel of the starting noise x_T, a constant value along each ring and the rings’ values drawn from a Gaussian.
Inside the circular mask of radius r the transform is replaced by the key (Eq. 2); the rest of the noise is
untouched, so the sampler sees ordinary noise outside a few low-frequency modes. Generation runs deterministic
DDIM from that noise. Detection encodes the image (through the VAE for latent models), runs DDIM inversion with
the unconditional noise prediction (the paper’s empty prompt) to recover x’_T, and tests y = F(x’_T) on the mask
for the key.
Three refinements of the published construction:
- The ring values are real. A ring is symmetric under f -> -f, so a real value makes the imprinted spectrum
Hermitian and the watermarked noise exactly real. The reference implementation draws complex values and keeps the real part of the inverse transform, which silently discards each ring’s imaginary part, so its detector compares against values that were never embedded.
- Only the independent half of the mask is scored. Under H0 the transform of real noise has y(-f) =
conj(y(f)), so the paper’s sum over the whole mask counts every mode twice and its chi-squared degrees of freedom are wrong. Scoring one of each conjugate pair, each with two real degrees of freedom (one for the DC term), makes eta exactly noncentral chi-squared under H0.
- The reported z is the matched filter of the key against y (whitened by the estimated mode variance), not the
paper’s distance eta = (1/sigma^2) sum_M |k* - y|^2 with its noncentral chi-squared p-value (Eqs. 4-6). The paper estimates sigma^2 from the same modes it scores, which pins |y|^2 / sigma^2 to a constant, so eta’s p-value is not uniform when an image carries another key’s mark: across 100 foreign keys its z averaged 0.42 instead of 0. The matched filter is N(0, 1) both for unmarked images and, given the image, for any other key, and it is the most powerful test for a known Gaussian signal. The paper’s statistic remains available for comparison.
Constructors
| Constructor | Summary |
|---|---|
TreeRingWatermarker(IDiffusionModel<T> model, string key, int radius, int channel, int inferenceSteps, TreeRingKeyPattern pattern, double reportThreshold) |
Creates a Tree-Ring watermarker for model. |
Properties
| Property | Summary |
|---|---|
TreeRingKeyPattern Pattern { get; } |
The key pattern written into the mask. |
bool UsesPublicKey { get; } |
|
int Channel { get; } |
The noise channel that carries the rings. |
int InferenceSteps { get; } |
DDIM steps used for both generation and inversion (the paper uses 50 for each). |
int Radius { get; } |
The ring mask radius in Fourier modes (the paper uses 10; its ablation also reports 16). |
string ModuleName { get; } |
Methods
| Method | Summary |
|---|---|
IReadOnlyList<SafetyFinding> EvaluateImage(Tensor<T> image) |
|
Tensor<T> Generate(int[] noiseShape, int seed, Func<Tensor<T>, int, Tensor<T>> noisePredictor) |
Generates a watermarked sample: keyed initial noise, then deterministic DDIM. |
Tensor<T> GenerateWithoutWatermark(int[] noiseShape, int seed, Func<Tensor<T>, int, Tensor<T>> noisePredictor) |
Generates the same way as Generate but from unmodified Gaussian noise: the unwatermarked counterpart, for calibrating false-positive rates. |
Tensor<T> InvertToNoise(Tensor<T> image) |
Recovers the initial noise of image by DDIM inversion with the unconditional noise prediction. |
Tensor<T> WatermarkedNoise(int[] noiseShape, int seed) |
The keyed initial noise Generate starts from: Gaussian noise with the rings imprinted. |
WatermarkResult Detect(Tensor<T> image) |
Tests an image for this key’s rings: DDIM inversion, then the matched-filter z of DetectFromNoise (not the paper’s chi-squared statistic; see the class remarks). |
WatermarkResult DetectFromNoise(Tensor<T> noise) |
Tests recovered initial noise (see InvertToNoise) for this key’s rings. |
double DetectWatermark(Tensor<T> image) |
The detection confidence in [0, 1] for image (see Detect). |