PaintByExampleModel<T>
Models & Types · AiDotNet.Diffusion.ImageEditing
Paint-by-Example model for exemplar-based inpainting using reference images.
Inheritance: DiffusionModelBase<T> → LatentDiffusionModelBase<T> → PaintByExampleModel<T>
For Beginners
Paint-by-Example fills in parts of an image using another image as a guide.
How it works:
- You provide a source image, a mask indicating the region to fill, and a reference image
- The reference image is encoded by CLIP into a visual embedding
- The U-Net takes 9 input channels (latent + masked image + mask) and denoises
- The filled region matches the style and content of the reference image
Key characteristics:
- Based on Stable Diffusion 1.5 (512x512)
- U-Net has 9 input channels (4 latent + 4 masked image + 1 mask)
- Uses exemplar images instead of text for conditioning
- Uses DDIM scheduler for deterministic inpainting results
How It Works
Paint-by-Example fills masked image regions using exemplar images as visual references instead of text prompts. The U-Net receives 9 input channels: 4 latent channels from the noisy latent, 4 channels from the masked source image latent, and 1 channel for the binary mask. The exemplar image is encoded via CLIP and injected through cross-attention.
Technical specifications:
- Architecture: SD 1.5 U-Net modified with 9 input channels for inpainting
- Conditioning: CLIP image encoder (768-dim) via cross-attention
- Input channels: 9 (4 noisy latent + 4 masked source latent + 1 binary mask)
- Output channels: 4 (latent space prediction)
- 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
Reference: Yang et al., “Paint by Example: Exemplar-based Image Editing with Diffusion Models”, CVPR 2023
Example
var options = new DiffusionModelOptions<float> { LatentChannels = 4, DefaultInferenceSteps = 30 };
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 PaintByExampleModel<float>(options: options))
.Build(trainX, trainY);
var prediction = result.Predict(input);
Constructors
| Constructor | Summary |
|---|---|
PaintByExampleModel(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 PaintByExampleModel 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 image encoder). |
int InputChannels { get; } |
Gets the number of U-Net input channels (9: 4 latent + 4 masked + 1 mask). |
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 Paint-by-Example (SD 1.5 native resolution). |
int DefaultWidth |
Default image width for Paint-by-Example (SD 1.5 native resolution). |