StegaStampWatermarker<T>

Models & Types · AiDotNet.Safety.Watermarking

StegaStamp: a learned image watermark. A U-Net encoder adds a residual that hides a bit string; a decoder with a spatial transformer reads it back after the printing-and-photographing distortions it was trained against.

Inheritance: NeuralNetworkBase<T> → StegaStampWatermarker<T>

Implements: IImageWatermarker<T>, IKeyedWatermarker

Known derived types: SynthIdImageWatermarker<T>

For Beginners

two small neural networks learn together. One hides a message in an image by changing it almost invisibly; the other learns to read the message back even after the image is warped, blurred, recoloured and compressed. The network has to be trained before it hides anything: call Train with images and random bit strings, or load a trained model.

How It Works

Architecture and training follow Tancik, Mildenhall and Ng (2020). Encoder: the secret through a dense layer to an (S/8) x (S/8) x 3 map, upsampled x8 and concatenated with the image, then a U-Net (32 at stride 1; 32, 64, 128, 256 at stride 2; four upsampling stages with skip connections; two 32-channel convolutions) to a 3-channel residual that is added to the image. Decoder: a spatial transformer (three stride-2 convolutions 32/64/128, dense 128, dense 6 initialised to the identity warp) followed by convolutions 32 s2, 32, 64 s2, 64, 64 s2, 128 s2, 128 s2, dense 512 and one logit per secret bit. A WGAN critic (convolutions 8, 16, 32, 64 at stride 2 and a 1-channel output, weights clipped) scores the encoded image.

Each training step encodes random secrets into the images, applies the paper’s differentiable distortions, and trains the encoder and decoder on the secret’s cross-entropy, a border-weighted YUV L2 loss on the residual and the critic’s score, with the image losses held off and then ramped in as in the paper. The distortions: a random perspective warp, Gaussian or motion blur, Gaussian noise, contrast, brightness, per-channel hue offsets and desaturation, and an 8x8-block JPEG with the cubic rounding approximation (Shin and Song 2017), each ramped in. The paper’s LPIPS term needs pretrained AlexNet features, which this library does not ship, so it is not used.

As a keyed watermarker, EmbedWatermark hides the key’s payload: 56 bits derived from the key, protected by the paper’s BCH code (corrects 5 bit errors), then zero padding to SecretBits. Detect correlates the decoder’s per-bit logits with the key codeword’s signs, a z that is exactly standard normal given the logits when the image does not carry the key’s payload. EncodeSecret and DecodeSecret embed and read arbitrary bits.

Constructors

Constructor Summary
StegaStampWatermarker() Creates a StegaStamp watermarker with the paper’s 400 x 400 images and 100-bit secrets.
StegaStampWatermarker(StegaStampOptions options, string key) Creates a StegaStamp watermarker.

Properties

Property Summary
bool IsReady { get; } True once the networks have taken a training step (here, or in the model whose state this one copied or loaded).
bool SupportsTraining { get; }
bool UsesPublicKey { get; }
int TrainingStep { get; } Training steps taken so far, which drive the paper’s loss and distortion ramps.
string ModuleName { get; }

Methods

Method Summary
IReadOnlyList<SafetyFinding> Evaluate(Vector<T> content)
IReadOnlyList<SafetyFinding> EvaluateImage(Tensor<T> image)
ModelMetadata<T> GetModelMetadata()
StegaStampWatermarker<T> WithKey(string key) A watermarker with the same trained networks that hides and detects key’s payload: the networks are trained once and serve every key.
Tensor<T> EmbedWatermark(Tensor<T> image)
Tensor<T> EncodeSecret(Tensor<T> image, IReadOnlyList<bool> secret) Hides secret (one 0/1 value per bit) in image.
ValueTuple<bool[], int>? DecodePayload(Tensor<T> image) The key’s payload recovered from image by BCH decoding, with the number of bits corrected; null when error correction is off or the decoded bits hold more errors than the code corrects.
WatermarkResult Detect(Tensor<T> image) Tests image for this key’s payload with a soft-decision score: the correlation of the decoder’s logits with the key codeword’s signs, z = sum(s_i l_i) / sqrt(sum(l_i^2)).
bool[] DecodeSecret(Tensor<T> image) The decoded bits of image (resized to the model’s size if needed).
bool[] KeySecret() The bit string EmbedWatermark hides for this key.
double DetectWatermark(Tensor<T> image)