- Explain the text encoder, latent denoiser and decoder stages of a text-to-image pipeline and the failure modes of each.
- Choose among image-to-image, inpainting, ControlNet-style conditioning and LoRA personalization for a given control problem.
- Compute and interpret FID and CLIP score, and design a human-preference evaluation.
- Explain how spacetime latents and diffusion transformers achieve temporal consistency, and estimate their token and compute cost.
- Implement text-to-image and image-to-image generation with Hugging Face diffusers.
- Identify copyright, licensing and consent risks before deploying generated media.
Before this: Diffusion Models and Flow Matching, Computer Vision: Tasks, Metrics, Models and Deployment
Text-to-image and text-to-video systems are now ordinary tools in design, marketing, film pre-visualization, games and product photography. Building with them professionally means understanding the pipeline well enough to control it, measure it, budget it and use it lawfully. This lesson assumes you know how diffusion and flow-matching models work and focuses on the full systems built around them.
Intuition: a translator, an artist and a printer
Picture a studio with three specialists. A translator reads your brief and turns it into precise internal notes (the text encoder). An artist works on a small, abstract sketch pad, starting from random scribbles and refining them over many passes while re-reading the notes (the diffusion or flow model in latent space). A printer turns the finished sketch into a full-resolution picture (the autoencoder's decoder). Each specialist can be swapped or tuned separately, and most control techniques are ways of slipping extra instructions to the artist.
The text-to-image pipeline
- Text encoding. The prompt is tokenized and embedded by a pretrained text encoder. Stable Diffusion 1.x used the text tower of CLIP ViT-L/14; Google's Imagen (Saharia et al., 2022) showed that scaling a large frozen language-model encoder (T5-XXL) improved prompt fidelity more than scaling the image model; Stable Diffusion 3 (Esser et al., 2024) combines two CLIP text encoders with T5-XXL. Newer systems increasingly use decoder-only language models as text encoders. Output: a sequence of embedding vectors (for cross-attention) and often a pooled vector (for global conditioning).
- Latent generation. Start from Gaussian noise of latent shape, for example for a 1024-pixel image with an 8× downsampling autoencoder and 16 channels. A U-Net or diffusion transformer denoises it over steps, conditioned on the text embeddings, timestep and sometimes size or crop metadata, typically with classifier-free guidance.
- Decoding. The autoencoder's decoder maps the final latent to pixels. Optional stages: a super-resolution or refiner model, safety classifiers on the prompt and output, and provenance marking (watermarks or C2PA metadata, covered in the deepfakes lesson).
The modular design means three different models with three different failure modes: misread prompts (text encoder), poor composition (denoiser), smeared faces or text (decoder).
Control beyond the prompt
Text is a weak control signal for layout, pose and identity. Professionals rely on these techniques:
Image-to-image (SDEdit)
Instead of starting from pure noise, take an input image, encode it to a latent, noise it to an intermediate timestep, and denoise from there with the new prompt (Meng et al., 2022, "SDEdit"). The strength parameter sets how far into the noise schedule you go: with 50 steps and strength 0.6, about denoising steps run, starting from a moderately noised version of your image. Low strength keeps structure and color; high strength keeps only rough composition.
Inpainting and outpainting
Regenerate only a masked region. Dedicated inpainting models take the masked image latent and the mask as extra input channels; a training-free alternative re-imposes the known region (suitably noised) at every step. Outpainting is inpainting beyond the original borders.
Structural conditioning (ControlNet-style)
ControlNet (Zhang, Rao and Agrawala, 2023) copies the encoder blocks of a frozen pretrained U-Net into a trainable branch that reads a conditioning image (edges, depth, human pose skeletons, segmentation maps). The branch's outputs are added back into the frozen network through zero-initialized convolutions, so at the start of training the model behaves exactly like the original and the control is learned without damaging it. Lighter alternatives include T2I-Adapter. For identity or style from a reference image, IP-Adapter (Ye et al., 2023) adds image-prompt embeddings through decoupled cross-attention. Diffusion-transformer models use analogous adapters.
Personalization and style: LoRA and friends
LoRA (Hu et al., 2021) freezes a weight matrix and learns a low-rank update:
where is the rank and a scaling constant. For a attention projection with , LoRA trains parameters instead of , 2.5% of the matrix. Style and subject LoRAs are typically a few to a few hundred megabytes and can be mixed at inference with per-LoRA weights. DreamBooth (Ruiz et al., 2023) fine-tunes on 3 to 5 images of a subject bound to a rare token, with a prior-preservation loss to avoid forgetting the class; textual inversion (Gal et al., 2022) learns only a new token embedding. In practice, DreamBooth-style training with LoRA is the common combination.
Evaluation
There is no single number for "good image." Use complementary measures.
FID
The Fréchet Inception Distance (Heusel et al., 2017) embeds real and generated images with an Inception-v3 network, fits a Gaussian to each set of 2048-dimensional features, and computes the Fréchet distance between the Gaussians:
where and are the feature means and covariances of real and generated images. Lower is better; it rewards both fidelity and diversity.
Worked example (1D features). In one dimension FID reduces to . Real features: , . Model A: , , so . Model B: , , so . Model B's samples sit near the real mean but have collapsed diversity, and FID penalizes that.
FID's caveats are serious: it is biased with small sample counts (use the same number, commonly 10k to 50k, for all compared models), sensitive to image resizing and compression implementation details, based on ImageNet features that ignore text and fine detail, and blind to prompt alignment. Variants such as features from self-supervised encoders (DINOv2) address some of this.
CLIP score
CLIPScore (Hessel et al., 2021) measures prompt-image alignment as the cosine similarity of CLIP embeddings of the image and caption : with in the original paper (many libraries instead report ). If , the score is . It is cheap but inherits CLIP's weaknesses: poor at counting, spatial relations and attribute binding ("a red cube on a blue sphere").
Human preference and compositional benchmarks
Because automatic metrics miss so much, serious evaluation uses pairwise human preference (show two images for one prompt, ask which is better on alignment and quality, aggregate into win rates or Elo-style ratings). Learned preference models trained on such data, such as ImageReward, PickScore and HPS, approximate it cheaply. Object-focused benchmarks like GenEval use detectors to check counts, colors and positions. Always also check text rendering, hands, and demographic diversity across a fixed prompt set.
Video generation
Video adds the time axis, and with it the hardest requirement: temporal consistency. Objects must keep identity, shape and texture across frames; motion must obey plausible physics; camera movement must be coherent.
Early approaches inflated image U-Nets with temporal layers (Ho et al., 2022, "Video Diffusion Models"; Make-A-Video; Stable Video Diffusion). The dominant design described publicly since 2024 treats video as spacetime latents: a video autoencoder compresses space and time together (for example 8× spatially and 4× temporally), the latent is cut into spacetime patches, and a diffusion transformer with full or factorized attention over all patches denoises them jointly. OpenAI's 2024 Sora report and Meta's Movie Gen paper (2024) describe this pattern, and open models follow it.
Worked token-count example. A 5-second clip at 24 fps and 1024×1024 resolution has 120 frames. With 4× temporal compression that is about 30 latent frames (many encoders keep the first frame separately, giving 31). Each latent frame is ; with patches it becomes tokens. Total: tokens per denoising pass. Full self-attention scales with the square of that, about token pairs per layer, versus for a single 4,096-token image: roughly 900 times more attention work, before multiplying by steps and guidance. This is why video generation is expensive, why clips are short, and why systems generate at lower resolution and upsample.
Other video techniques: image-to-video (animate a given first frame), keyframe interpolation, camera-control conditioning, joint audio-video generation, and extending clips by conditioning on previous latents (which accumulates drift over long durations).
Notable systems (as of November 2026)
The commercial landscape changes monthly, so treat this as a snapshot and check vendor documentation. Google's text-to-image and video models, Imagen and Veo, embed Google's SynthID watermark in their outputs, according to reporting on SynthID. Stability AI's Stable Diffusion line (latent diffusion; SD3 uses rectified-flow transformers) is an open-weight family with published technical papers, and Meta's Movie Gen paper documents a large video model in detail even though its weights were not openly released. Evaluate any system on your own prompts; public leaderboards measure someone else's priorities.
Compute costs
Generation cost is driven by: (forward passes) = steps × (2 if CFG else 1); (cost per pass) which grows with token count and model size; plus text-encoder and decoder overhead (the video decoder can be a large share for long clips). Practical levers: fewer steps with a better solver, distilled few-step models, guidance distillation, lower resolution plus upscaling, reduced precision (fp16/bf16, sometimes 8-bit), caching repeated computations across steps, and batching. For budgeting, measure seconds per image on your hardware at your settings; vendor prices for hosted APIs are usually per image or per second of video and change frequently.
Code: text-to-image and image-to-image with diffusers
The Hugging Face diffusers library wraps the full pipeline. This uses the openly available SDXL base model and requires a GPU for reasonable speed.
import torch
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
from diffusers import DPMSolverMultistepScheduler
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipe = AutoPipelineForText2Image.from_pretrained(model_id, torch_dtype=dtype).to(device)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
gen = torch.Generator(device=device).manual_seed(42) # reproducible
image = pipe(
prompt="studio photo of a ceramic teapot shaped like a snail, soft light",
negative_prompt="blurry, low quality, text, watermark",
num_inference_steps=25,
guidance_scale=6.0,
generator=gen,
).images[0]
image.save("teapot.png")
# Image-to-image: reuse the loaded components, keep composition, change style
i2i = AutoPipelineForImage2Image.from_pipe(pipe)
edited = i2i(prompt="watercolor illustration of a snail teapot",
image=image, strength=0.55, guidance_scale=6.0,
generator=torch.Generator(device=device).manual_seed(42)).images[0]
edited.save("teapot_watercolor.png")
Key points: swapping the scheduler changes the sampler without retraining; fixing the generator seed makes runs reproducible for A/B tests; from_pipe shares weights so the second pipeline costs no extra memory; and strength=0.55 runs roughly 55% of the 25 steps on a noised copy of the first image. Loading a style LoRA is one line, pipe.load_lora_weights(path_or_repo), assuming you have the rights to that LoRA.
Copyright, consent and responsible use
These are legal and ethical questions that vary by jurisdiction and are actively changing; this section is general information, not legal advice.
- Training data. Most large image models were trained on web-scraped image-text pairs. Whether and when that is lawful is being contested in courts and legislatures in several countries, and some jurisdictions offer rights holders text-and-data-mining opt-outs. Prefer models with documented, licensed or opt-out-respecting training data for commercial work, and read the license (some restrict commercial use or require attribution).
- Output ownership. Copyright offices, including the US Copyright Office, have taken the position that copyright protects human authorship; how much human control over prompts, selection and editing is enough remains fact-specific.
- Memorization. Models can reproduce near-copies of training images, logos and characters, especially heavily duplicated ones. Avoid prompts that name living artists or trademarked characters for commercial outputs, and screen outputs.
- Likeness and consent. Generating identifiable real people, especially in sexual, defamatory or deceptive contexts, raises right-of-publicity, privacy and criminal issues. Several jurisdictions now require labelling of synthetic media (see the deepfakes lesson). Obtain written consent for any person whose likeness you train or generate.
Common pitfalls
- Prompting where control is needed. Layout, pose and brand-exact colors are better solved with ControlNet-style conditioning, inpainting or reference adapters than with ever-longer prompts.
- Over-training personalization. DreamBooth or LoRA runs that are too long or too high-rank make every output look like the training photos and destroy prompt following. Hold out prompts and check.
- Mismatched components. LoRAs, ControlNets and VAEs are tied to a base model family; loading an SD 1.5 LoRA into SDXL fails or produces garbage.
- Trusting one metric. Optimizing FID or CLIP score alone produces images that score well and look worse. Combine automatic metrics with human review on a fixed prompt set.
- Ignoring licenses and consent. Model licenses, LoRA sources and people's likeness rights are real constraints; check them before deployment, not after.
In practice
Production image pipelines usually look like: prompt templating and safety filtering, a licensed base model with a few tuned LoRAs or adapters, structural control from sketches or 3D renders, inpainting for fixes, upscaling, provenance marking, and human review for anything public. Teams track a fixed evaluation prompt suite with seeds, measure latency per image on target hardware, and re-run the suite whenever the model, sampler or LoRA set changes. For video, expect to generate many short candidates and select, to fix the first frame with an image model for consistency, and to budget compute far above image work.
Key takeaways
- A text-to-image system is three swappable models: a text encoder, a latent denoiser and an autoencoder decoder, each with its own failure modes.
- Structural control (ControlNet-style adapters, inpainting, image-to-image) and lightweight personalization (LoRA, DreamBooth) beat prompt engineering for layout, pose and identity.
- FID measures distribution match and diversity, CLIP score measures prompt alignment, and human preference remains the reference standard; use them together.
- Video models compress space and time into spacetime latents and denoise them jointly with transformers, which makes token counts and compute grow dramatically.
- Cost scales with steps, guidance passes, token count and model size; solvers, distillation and lower precision are the main levers.
- Training-data legality, output ownership, memorization and likeness consent must be checked before commercial deployment.
Terms in this lesson
- Image-to-image (SDEdit)
- Generating by noising an existing image's latent to an intermediate step and denoising it with a new prompt, controlled by a strength parameter.
- Inpainting
- Regenerating only a masked region of an image while keeping the rest consistent.
- ControlNet
- A trainable copy of a diffusion model's encoder that injects a structural condition such as edges, depth or pose through zero-initialized layers.
- LoRA
- Low-rank adaptation: fine-tuning by learning a small low-rank update BA added to frozen weight matrices.
- FID
- Fréchet Inception Distance, the Fréchet distance between Gaussians fitted to Inception features of real and generated images; lower is better.
- CLIP score
- A prompt-alignment metric based on the cosine similarity between CLIP embeddings of an image and its text prompt.
- Spacetime latent
- A compressed representation of a video in which an autoencoder has reduced both spatial and temporal resolution, later split into patches for a transformer.
- Temporal consistency
- The property that objects, identities and textures remain stable and move plausibly across video frames.
Check your understanding
- 1.
Real-image features have mean 0 and standard deviation 1 (1D). Generated features have mean 0.3 and standard deviation 0.5. What is the FID?
- 2.
A client needs product shots where the product's silhouette exactly matches a CAD render, but with new photorealistic backgrounds. What is the best first tool?
- 3.
With LoRA rank r = 8 on a 1024 x 1024 weight matrix, how many trainable parameters are added for that matrix?
- 4.
Why is CLIP score a poor sole metric for a prompt like 'three red cubes left of a blue sphere'?
- 5.
A video model uses 8x spatial and 4x temporal compression with 2x2 patches. Roughly how many tokens does a 4-second, 24 fps, 512x512 clip produce?
Exercises
Work each problem before opening the solution.
- Exercise 1Core
Compute the FID between two 2D Gaussian feature distributions with diagonal covariances: real , ; generated , .
Show solution
For diagonal covariances the matrices commute, so .
- Mean term: .
- Trace term: .
FID . Equivalently, per dimension : $1 + 0$ and .
- Exercise 2Core
You run image-to-image with 40 inference steps. How many denoising steps actually run at strength 0.25, 0.5 and 0.9, and what visual result would you expect from each?
Show solution
Steps run : 10, 20 and 36.
- 0.25: light noise, so colors, layout and most detail are kept; good for subtle restyling or cleanup.
- 0.5: composition and major shapes kept, textures and style substantially changed.
- 0.9: only rough layout and color masses survive; the result is close to a fresh generation guided loosely by the input.
- Exercise 3Core
Write a diffusers snippet that generates the same prompt with guidance scales 2, 5, 8 and 12 using a fixed seed, and saves a labelled file for each, so you can compare adherence and saturation.
Show solution
import torch from diffusers import AutoPipelineForText2Image device = "cuda" if torch.cuda.is_available() else "cpu" dtype = torch.float16 if device == "cuda" else torch.float32 pipe = AutoPipelineForText2Image.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=dtype).to(device) prompt = "a lighthouse on a cliff at dusk, oil painting" for s in [2, 5, 8, 12]: g = torch.Generator(device=device).manual_seed(7) # same seed each time img = pipe(prompt, guidance_scale=s, num_inference_steps=25, generator=g).images[0] img.save(f"lighthouse_cfg{s}.png")Recreating the generator inside the loop ensures every run starts from identical noise, so differences come only from the guidance scale. Expect weak adherence at 2, a good balance around 5 to 8, and oversaturation or harsh contrast at 12.
- Exercise 4Challenge
Estimate the relative self-attention cost of generating (a) one 1024×1024 image and (b) a 10-second, 24 fps, 1024×1024 video, both with 8× spatial compression, 2×2 patches, and (for video) 4× temporal compression. Then propose three changes that would reduce the video cost by at least 10× and state the quality trade-off of each.
Show solution
(a) Latent , patches tokens; attention pairs .
(b) 240 frames / 4 = 60 latent frames; tokens; pairs , about the image.
Reductions:
- Generate at 512×512 and upsample: tokens drop 4×, attention 16×; trade-off: fine detail depends on the upscaler.
- Factorized or windowed attention (spatial within frame, temporal across frames): cost per layer falls from to roughly with , , about , roughly 60× less; trade-off: weaker long-range spatiotemporal interactions and possible consistency loss.
- Generate a 5 s clip and extend it autoregressively: halves tokens per pass (4× less attention per pass, 2× more passes); trade-off: drift and seams between segments.
Any one of the first two exceeds 10×; combinations go further. Step count and CFG multiply both scenarios equally, so they do not change the ratio but do change absolute cost.
Further reading
- paperZhang, Rao & Agrawala, Adding Conditional Control to Text-to-Image Diffusion Models (2023)
- paperHu et al., LoRA: Low-Rank Adaptation of Large Language Models (2021)
- paperHeusel et al., GANs Trained by a Two Time-Scale Update Rule Converge to a Local Nash Equilibrium (FID) (2017)
- paperHessel et al., CLIPScore: A Reference-free Evaluation Metric for Image Captioning (2021)
- docsHugging Face, Diffusers documentation
- paperPolyak et al. (Meta), Movie Gen: A Cast of Media Foundation Models (2024)
Go further
Sources
- Saharia et al., Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding (Imagen)
- Esser et al., Scaling Rectified Flow Transformers for High-Resolution Image Synthesis
- Meng et al., SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations
- Ye et al., IP-Adapter
- Ruiz et al., DreamBooth
- Gal et al., An Image is Worth One Word (Textual Inversion)
- Ho et al., Video Diffusion Models
- Ghosh et al., GenEval
- TechCabal, What is SynthID and how does it detect AI-generated images? (June 2026)
- OpenAI, Video generation models as world simulators (Sora technical report, February 2024)
Fact-checked and last reviewed 2026-11-05