## Image generation

*Images, video and audio*

*Last edited: 13 November 2026*

A diffusion model starts from random noise and improves the whole image at once over a series of steps, guided by the prompt at every step. One real Stable Diffusion run shows how the prompt shapes the image, which areas of the image each prompt word influences, and how a new prompt changes a finished image.

**In plain words:** Like an illustrator who keeps a written description on the desk and looks at it again on every pass over the sketch. First they decide where everything goes, then refine the shapes and add the details. The more you insist that they follow the description, the more faithfully they do so, but the stiffer and more garish the drawing becomes.

*Interactive widget on the page: From noise to an image in 20 steps.*

### The denoising loop and how the generator is trained

*Interactive widget on the page: What happens to the prompt and the noise.*

- Stable Diffusion 1.5 (2022) is an old model. It is the example here because it is small enough for the whole run to be computed on an ordinary CPU and shown step by step. Newer open models such as SD3 and FLUX have the same loop and differ in three places: the generator, the training target and the text encoder.
- Generator: newer models use a transformer in place of the U-Net, an older convolutional network that processes the grid through small windows and uses attention in only some of its layers. Training target: they predict the direction in which the grid should change, not the noise (flow matching, described below). Text encoder: a larger language model (T5 in SD3 and FLUX.1) works alongside CLIP and its 77 positions, so the model accepts longer prompts, up to 512 tokens for FLUX.1 dev in the diffusers library. FLUX.2 (2025) replaces both encoders with a single Mistral model, also limited to 512 tokens. OpenAI has not published the architecture of GPT Image, nor Google that of Nano Banana Pro; OpenAI has said only that image generation in GPT-4o is autoregressive.

*Interactive widget on the page: Which parts of the image the model ties each prompt word to.*

- During training a model predicts a known target: in an LLM it is the next token, and here it is noise. The prediction error is used to adjust the weights (see “Training”). Noise at a random level is added to the grid of a real image, and the Stable Diffusion 1.5 generator learns to predict which noise was added. Each training example covers one noise level, and after hundreds of millions of examples the generator can denoise from any level, so at generation time it can be called step by step starting from pure noise.

*Interactive widget on the page: Training the generator: a real image and its caption.*

- The newer SD3, FLUX and most video models use flow matching, a variant of the same training with noise. In training the noisy grid is a linear interpolation: (1 − t) × image grid + t × noise, for a random t between 0 and 1. The generator learns to predict the difference noise − image grid, which for every number in the grid is the direction from the image to the noise. At generation time the sampler (also called a solver in these models) moves the grid a small step the opposite way, towards the image, and calls the generator again; the diffusers library does this 28 times by default for FLUX.1 dev. *Flow matching: the noisy grid lies on the line segment between the image grid and the noise. The generator predicts the direction towards the noise, and the sampler moves the grid a small step the opposite way.*
- The training data consists of images paired with captions. Increasingly, another model writes those captions: 95% of the captions used to train DALL·E 3 (2023) were model-generated, because internet captions are short and leave out which object is where. Training usually starts on small images and their resolution is then raised in stages, for example from 256 to 1024 pixels. After that, as with an LLM, the model is tuned on selected examples and on human preferences (see “Training”).

### Guidance and seed

- In a diffusion model the guidance scale controls how strongly the prompt influences the image. In the classic version, classifier-free guidance (CFG), which Stable Diffusion 1.5 uses, the generator predicts the noise twice at every step, without the prompt and with it, and the sampler amplifies the difference between the two predictions.

*Interactive widget on the page: Guidance: two predictions and their amplified difference.*

- For the generator to be able to predict without a prompt, the caption is dropped from some of the training examples. The method’s authors got their best results when dropping the condition from 10–20% of examples (2022, an ImageNet 64×64 model, where the condition is a class label), and Stable Diffusion 1.5 was trained with captions dropped from 10% of examples. A higher guidance scale gives less varied results, and one that is too high ruins the colours, as in the pictures under “Same prompt, different settings” at the top. The effect resembles lowering the temperature (see “The next token”), but the mechanism is different and doubles the number of generator calls, so it almost doubles generation time. Models with distilled guidance, such as FLUX.1 dev, learn this effect without the second prediction. Scale values do not carry over between models: FLUX.1 dev defaults to 3.5 in diffusers.
- The seed is the number that determines the starting noise. With the other settings unchanged, the seed decides the layout of the image. Changing the prompt or the guidance scale changes the layout too, because the prompt affects it from the first steps. An LLM samples at every token (see “The next token”), while the DDIM sampler shown here draws only once, at the start. The same prompt, seed and settings on the same model version usually give the same result; after a version change they no longer do, so save the finished files.

### Editing and reference images

*Interactive widget on the page: Same image, new prompt.*

- Editing a whole image (image-to-image) and inpainting also need the autoencoder’s encoder, which turns the original into a grid. In image-to-image, noise is added to the original image’s grid at the level selected by the strength setting, and the grid is then denoised with the new prompt. In inpainting the generator also receives the mask and the original’s grid with the masked area hidden, and creates new content inside the mask (in the example above, from pure noise) with the rest of the image as context. Cells outside the mask go through the loop and the decoder as well, so pixels outside the mask can change slightly, as the example above shows. In GPT Image the mask is only a hint, and OpenAI notes that the model does not always follow it exactly. When pixels outside the mask must stay untouched, paste the generated area into the original in your own code.
- In FLUX.1 Kontext (2025) a reference image, for example a face or a product, goes through the same autoencoder as the generated image, and its tokens are placed next to the tokens of the generated image. It is context for one call, not training. Unintended changes accumulate over successive edits. The Kontext authors tracked face similarity to the original across successive edits; similarity declined more slowly for Kontext than for the other models they tested. Pass the original as the reference in every edit and compare the result with it, and don’t keep correcting only the previous version.

### Resolution, errors and cost

- The counterpart of the context window is the number of image tokens, and it follows from the size fixed before the start; an image can later be extended past its edge, but that is a separate generation (outpainting). Doubling the side means four times as many tokens and, for FLUX.1, an estimated 5.6 times as much computation per step: at 1024 pixels attention is about 13% of a step’s computation and grows 16 times, while the rest grows 4 times (see the calculator in “How models generate images, video and audio”). APIs have limits: the GPT Image models generate images with a side of up to 3840 pixels and at most 8.3 million pixels in total (as of November 2026).
- Fitting a larger grid into GPU memory is not enough: a model generates well only at sizes close to those it was trained on. Stable Diffusion 1.5, trained on 512×512 images, often repeats objects and breaks their structure when asked for 1024×1024. A large image can be assembled from overlapping tiles that are denoised in the same steps and averaged where they overlap: seams usually do not show, but each tile sees only the prompt and its neighbours, so the whole can be inconsistent, for example with a repeating landscape. Generate the image first at a size the model handles, then upscale it and run it through image-to-image in tiles at a low strength. This order is more reliable: the layout is already fixed, so the tiles repeat objects less often. A low strength limits changes to the content but does not rule them out.
- Hands and the number of objects: the model learns what parts of photos look like, but nothing forces a generated hand to have five fingers. Newer models get this wrong less often.
- Text in images was the weakest point for a long time: Stable Diffusion 1.5 got letters wrong, and the autoencoder distorted small text. Models from 2025–2026 such as GPT Image and Nano Banana Pro usually render short text correctly, but OpenAI still warns of problems with the exact placement and legibility of text. When text, a logo or a chart has to be exact to the character, add it in code after generation.
- Generate images at the smallest size the model handles well, using several different seeds. Pick a layout, then upscale the chosen image. Using the same seed at a larger size produces different starting noise and therefore a different layout. Add the rejected attempts to the cost of every accepted image.

### Check yourself

**Question:** How does a diffusion model turn a prompt into an image, and how does that differ from an LLM generating text?

**Short answer:** A text encoder turns the prompt into vectors. The model works on a smaller grid of numbers that an autoencoder can turn into an image, and generation starts from random noise in that grid. At every step the generator predicts the noise (or the direction of change) for the whole grid at once and the sampler updates the grid; at the end the decoder turns it into pixels. An LLM appends one token from its vocabulary at a time, while here the generator improves the whole grid at once; the grid’s size depends on the resolution, not on the content. Randomness comes mainly from the starting noise, which the seed determines (some samplers add noise at every step), not from a draw at every token. A separate parameter, the guidance scale, controls how strongly the prompt influences the image.

### Follow-up questions

- **After you raise the guidance scale, the image has oversaturated colours and harsh contrast. What do you change?** Lower the guidance scale and leave the seed and the other settings alone, so that you see the effect of one parameter. A scale that is too high pushes the result outside what the model has learned. If the image drifts from the prompt at the lower scale, fix the prompt or pass a reference image, and don’t raise the guidance scale again.
- **You need a 4096×4096 poster and the model handles 1024×1024. How do you do it?** Generate several versions at 1024×1024 with different seeds and pick a layout. Then upscale the chosen version and run it through image-to-image in tiles at a low strength, which adds detail. Generating directly at 4096×4096 means sixteen times as many tokens and a size the model never saw in training, so it often produces repeated objects. Add text and logos in code at the end.
- **The same character has to appear in ten illustrations. How do you set that up?** Don’t describe the character in words alone, because one description fits many different faces and outfits. If the model supports it, pass the same reference image of the character with every illustration. Generate from that image, not from the previous illustration, and compare the character in every result with it. If that is not enough, you can fine-tune the model on images of the character, for example using LoRA (see “Fine-tuning and LoRA”).
- **You set the same seed and prompt, and the image from the API came out different from a month ago. Why?** The seed reproduces the starting noise, but the result also depends on the model version, the sampler, the libraries and the hardware. The provider may have changed any of these. Many services rewrite the prompt with an LLM before generating, and the way they rewrite it can change as well. If you will need the image later, save the file, not just the settings.

### Sources

- [Rombach et al.: High-Resolution Image Synthesis with Latent Diffusion Models (2021)](https://arxiv.org/abs/2112.10752)
- [Ho, Salimans: Classifier-Free Diffusion Guidance (2022)](https://arxiv.org/abs/2207.12598)
- [Esser et al.: Scaling Rectified Flow Transformers for High-Resolution Image Synthesis, SD3 (2024)](https://arxiv.org/abs/2403.03206)
- [Black Forest Labs: FLUX.1 Kontext (2025)](https://arxiv.org/abs/2506.15742)
- [Betker et al.: Improving Image Generation with Better Captions, DALL·E 3 (2023)](https://cdn.openai.com/papers/dall-e-3.pdf)
- [OpenAI: Image generation, API guide (as of November 2026)](https://developers.openai.com/api/docs/guides/image-generation)

Interactive page: https://howaiworks.dev/image-generation/
