NullTextInversionModel<T>

Models & Types · AiDotNet.Diffusion.ImageEditing

Null-text Inversion model for editing real images by optimizing unconditional embeddings.

Inheritance: DiffusionModelBase<T> → LatentDiffusionModelBase<T> → NullTextInversionModel<T>

For Beginners

Null-text Inversion lets you edit real photos with text prompts.

How it works:

  1. DDIM inversion maps your real image back to noise (latent code)
  2. The null-text embedding is optimized per-timestep for accurate reconstruction
  3. Editing is performed by swapping the text prompt while keeping the optimized embeddings

Key characteristics:

  • Based on Stable Diffusion 1.5 (512x512, CLIP ViT-L/14)
  • Enables Prompt-to-Prompt editing on real images
  • Requires per-image optimization (~50 optimization steps per timestep)
  • Uses DDIM scheduler for deterministic inversion and generation

How It Works

Null-text Inversion enables precise editing of real photographs by first inverting them into latent space using DDIM inversion, then optimizing the unconditional (null-text) embedding at each timestep to ensure faithful reconstruction. Once inverted, standard text-guided editing techniques like Prompt-to-Prompt can modify the image.

Technical specifications:

  • Architecture: SD 1.5 U-Net with per-timestep null-text optimization
  • Text encoder: CLIP ViT-L/14 (768-dim, 77 max tokens)
  • Cross-attention dimension: 768
  • VAE: 4 latent channels, scale factor 0.18215
  • Noise schedule: Scaled linear beta [0.00085, 0.012], 1000 timesteps
  • Scheduler: DDIM (required for deterministic inversion)

Reference: Mokady et al., “Null-text Inversion for Editing Real Images using Guided Diffusion Models”, CVPR 2023

Example

var options = new DiffusionModelOptions<float> { LatentChannels = 4, DefaultInferenceSteps = 50 };
var input = Tensor<float>.CreateRandom(new[] { 1, 4, 64, 64 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new NullTextInversionModel<float>(options: options))
    .Build(trainX, trainY);
var edited = result.Predict(input);

Constructors

Constructor Summary
NullTextInversionModel(NeuralNetworkArchitecture<T> architecture, DiffusionModelOptions<T> options, INoiseScheduler<T> scheduler, UNetNoisePredictor<T> unet, StandardVAE<T> vae, IConditioningModule<T> conditioner, int? seed) Initializes a new instance of NullTextInversionModel with full customization support.

Properties

Property Summary
IConditioningModule<T> Conditioner { get; }
INoisePredictor<T> NoisePredictor { get; }
IVAEModel<T> VAE { get; }
int CrossAttentionDim { get; } Gets the cross-attention dimension (768 for CLIP ViT-L/14).
int LatentChannels { get; }

Methods

Method Summary
ModelMetadata<T> GetModelMetadata()
Tensor<T> GenerateFromText(string prompt, string negativePrompt, int width, int height, int numInferenceSteps, double? guidanceScale, int? seed)
Tensor<T> ImageToImage(Tensor<T> inputImage, string prompt, string negativePrompt, double strength, int numInferenceSteps, double? guidanceScale, int? seed)

Fields

Field Summary
int DefaultHeight Default image height for Null-text Inversion (SD 1.5 native resolution).
int DefaultWidth Default image width for Null-text Inversion (SD 1.5 native resolution).