Intermediate18 min read

Generate and compare images on your computer

Use a local diffusion model to make an illustration and run a controlled comparison of seed, prompt, and device.

On this page

Prepare a small run

A text-to-image diffusion pipeline starts from noise and repeatedly updates a representation of the image using the text prompt. It decodes the result into pixels. This lab uses SD-Turbo, which was trained to work with a small number of inference steps.

Use a Python 3 virtual environment. The first run downloads about 2.6 GB of model weights, plus the Python packages. Leave additional disk space for the cache and use the model card to check the license for your intended use. The commands below were tested with Python 3.13.

python -m pip install "torch==2.15.0" "diffusers==0.39.0" "transformers==4.57.6" "accelerate==1.15.0" pillow

Start on CPU or use --device mps with a working Apple silicon PyTorch installation. For a supported NVIDIA GPU, use the PyTorch installer’s matching CUDA build and select --device cuda. The exercise was verified on CPU and Apple Metal. Runtime memory and speed vary by device.

Generate and check the result

Download diffusion_card.py
View diffusion_card.py
import argparse
from pathlib import Path
from time import perf_counter

from diffusers import AutoPipelineForText2Image
from diffusers.utils import logging
import torch

MODEL = "stabilityai/sd-turbo"
REVISION = "b261bac6fd2cf515557d5d0707481eafa0485ec2"
PROMPT = "Editorial still life of a green notebook and a wooden pencil on a cream desk, soft window light"

parser = argparse.ArgumentParser(description="Generate a small image with a local diffusion model.")
parser.add_argument("output", type=Path)
parser.add_argument("--device", choices=("cpu", "mps", "cuda"), default="cpu")
parser.add_argument("--prompt", default=PROMPT)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--steps", type=int, choices=(1, 2, 3, 4), default=2)
parser.add_argument("--offline", action="store_true")
args = parser.parse_args()

if args.output.suffix.lower() != ".png":
    parser.error("Use a .png output filename.")
if args.output.exists():
    parser.error("Choose an output filename that does not exist.")
if args.device == "mps" and not torch.backends.mps.is_available():
    parser.error("This PyTorch installation does not have a working MPS device.")
if args.device == "cuda" and not torch.cuda.is_available():
    parser.error("This PyTorch installation does not have a working CUDA device.")

logging.set_verbosity_error()
try:
    pipe = AutoPipelineForText2Image.from_pretrained(
        MODEL,
        revision=REVISION,
        variant="fp16",
        torch_dtype=torch.float32 if args.device == "cpu" else torch.float16,
        use_safetensors=True,
        local_files_only=args.offline,
    ).to(args.device)
    pipe.set_progress_bar_config(disable=True)
    generator = torch.Generator(device="cpu").manual_seed(args.seed)
    start = perf_counter()
    image = pipe(
        args.prompt,
        height=512,
        width=512,
        num_inference_steps=args.steps,
        guidance_scale=0.0,
        generator=generator,
    ).images[0]
    elapsed = perf_counter() - start
    with args.output.open("xb") as output:
        image.save(output, format="PNG")
except (OSError, RuntimeError, ValueError) as error:
    raise SystemExit(f"Could not generate the image: {error}") from None

print(f"Saved {image.width} × {image.height} to {args.output} in {elapsed:.2f} s after loading.")
python diffusion_card.py notebook.png --device cpu
You’re ready when

The command saves a 512 × 512 PNG and reports time spent generating it after model loading. Open the image and look for the green notebook, wooden pencil, and soft light. Check each requested feature against the output.

Change one setting

python diffusion_card.py notebook-seed43.png --device cpu --seed 43 --offline
python diffusion_card.py notebook-blue.png --device cpu --prompt "Editorial still life of a blue notebook and a wooden pencil on a cream desk, soft window light" --offline

The first comparison changes the starting noise seed and keeps the prompt. The second returns to seed 42 and changes one word in the prompt. Compare the objects and colors. A model can ignore part of the request. Record the model revision, device, prompt, seed, and number of steps so you can repeat the run. Exact pixels can differ across devices and software versions.

The --offline option uses cached model files. If loading fails, finish one online run with the same Python environment. If the process runs out of memory, close other memory-heavy applications or choose a smaller model that fits your computer.

Check your understanding

Which setting should you hold fixed if you want to compare “green notebook” and “blue notebook” under matching starting conditions?

Keep the seed, model, device, and steps the same. Change the notebook color in the prompt.

Sources

Search the guide