Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Generating images, audio and video

After this lesson: I can explain how a generator turns a text prompt into an image, a sound or a video.

60 min lesson3 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Type a sentence, get a picture

Text-to-image, text-to-speech and text-to-video generators turn a short prompt into media. Inside, most combine the pieces of lessons 2 and 4: an autoencoder, a diffusion model and a way for the words to steer them.

Knowing the pieces tells you why a picture takes seconds, why a video costs far more, and what the prompt can and cannot do.

By the end of this lesson

Note

I can explain how a generator turns a text prompt into an image, a sound or a video.

Key words

WordWhat it meansExample
Pixelone dot of an image, often three numbers
Latent diffusiondiffusion run on an autoencoder's small code
Conditioninggiving the denoiser extra input such as a prompt
Guidance scalehow strongly the prompt steers each step7
Spectrogramhow strong each pitch is in each short slice of sound
Frameone still picture of a video

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Work in a small code, not in pixels

A 512 by 512 colour picture has 786,432 numbers, three a pixel. Fifty denoising steps over all of them is slow.

So many generators use latent diffusion. An autoencoder, as in lesson 2, squeezes pictures to a code of 64 by 64 by 4 numbers. Diffusion runs on that code, and the decoder turns the final code into pixels once, at the end.

From a prompt to a picture

flowchart LR
  P["Prompt"] --> T["Text encoder: vectors"]
  N["Noise in the small code"] --> D["Denoiser, many steps"]
  T --> D
  D --> C["Clean code"]
  C --> X["Decoder"]
  X --> I["Picture in pixels"]

Count pixels against the code

Command
docker exec jupyter python3 -c "print(512 * 512 * 3, 64 * 64 * 4, 512 * 512 * 3 // (64 * 64 * 4))"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try it: count the numbers

Code: python
# How many numbers a generator has to produce.
def numbers_in(width, height, channels):
    return width * height * channels

pixels = numbers_in(512, 512, 3)    # red, green and blue
latent = numbers_in(64, 64, 4)      # the autoencoder's code
print("pixels:", pixels)
print("latent:", latent)
print("times fewer:", pixels // latent)
# Try a 1024 by 1024 picture with a 128 by 128 latent of 4.

Make a free account to save your work and track your progress.

The prompt steers every step

Conditioning gives the denoiser the prompt as vectors from a text encoder, as in modules 22 and 90. Then the model can guess the noise with the prompt or without it.

Guidance uses both guesses: without + scale × (with - without). A guidance scale of 1 is the plain prompt guess. A larger scale follows the prompt harder and gives less variety; too large looks overdone.

Worked example: two guesses, four scales

ScaleWithout + scale × (with - without)Guided guess
0(0.2, -0.1) + 0 × (0.3, 0.4)(0.2, -0.1), ignores the prompt
1(0.2, -0.1) + 1 × (0.3, 0.4)(0.5, 0.3), the prompt guess
3(0.2, -0.1) + 3 × (0.3, 0.4)(1.1, 1.1)
7(0.2, -0.1) + 7 × (0.3, 0.4)(2.3, 2.7), pushed far past it

The cells students fill in are left blank in this preview.

Guide a guess at four scales

Command
docker exec jupyter python3 -c "import numpy as np; u = np.array([0.2, -0.1]); c = np.array([0.5, 0.3]); print([(u + s * (c - u)).round(2).tolist() for s in (0, 1, 3, 7)])"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think a generator finds pictures matching the prompt and pastes them together. It starts from noise, and the prompt only steers each step through numbers. It can still reproduce an image it memorised, as lesson 4 showed.

Sound is samples, often drawn as a picture

Speech is often stored as 16,000 numbers a second, so ten seconds is 160,000. Some generators make a spectrogram first: the strength of each pitch in short slices of time, which a diffusion model can treat like an image.

Others cut sound into tokens and predict them one at a time, like the next-token model of module 90.

A spectrogram of a pure tone

Command
docker exec jupyter python3 -c "import numpy as np; sr = 16000; t = np.arange(8000) / sr; x = np.sin(2 * np.pi * 440 * t); s = abs(np.fft.rfft(x[:7680].reshape(30, 256), axis=1)); print(s.shape, s[0].argmax() * sr / 256)"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Video is frames that must agree

A video is a frame after a frame, often 24 a second. Ten seconds is 240 pictures, and a face that changes shape between two of them looks wrong at once.

So video models denoise many frames together and squeeze time as well as space. Even so, the numbers grow with every second, which is why video costs far more than a picture.

How much each kind of media holds

MediaNumbersWorking
One picture, pixels786,432512 × 512 × 3
One picture, small code16,38464 × 64 × 4
Ten seconds of speech160,00016,000 × 10
Ten seconds of video, codes per frame3,932,160240 × 16,384

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Match each idea to what it does

Have a go

Loading…

Your answers are marked here and are not kept. An account keeps your marks.

Assignment: sizes and guidance

Write four functions a team would use to plan a media generator.

numbers_in(width, height, channels) gives the count of numbers in one picture or code. squeeze_factor(pixels, code) takes two lists of [width, height, channels] and gives how many times fewer numbers the code holds, rounded to 1 place. guide(without, with_prompt, scale) takes two lists of guesses and gives, for each place, without + scale * (with_prompt - without) rounded to 2 places. video_numbers(seconds, fps, code) gives the numbers for a whole clip: the frames in it times the numbers in one frame's code, code being [width, height, channels].

Code: python
def numbers_in(width, height, channels):
    return 0


def squeeze_factor(pixels, code):
    return 1.0


def guide(without, with_prompt, scale):
    return without


def video_numbers(seconds, fps, code):
    return 0

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.