> ## Documentation Index
> Fetch the complete documentation index at: https://instabox.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Generate images

> Run an open image model on a GPU machine, turn prompts into pictures in a fraction of a second each, and copy the pictures to your computer.

Run an open image model on a GPU machine and generate pictures from your own prompts. This guide
runs
[SD-Turbo](https://huggingface.co/stabilityai/sd-turbo) with
[Diffusers](https://huggingface.co/docs/diffusers) on one `l4` GPU. After the model is loaded, each
512 by 512 picture takes about a quarter of a second.

![Three generated pictures side by side: an oil painting of a lighthouse on a rock at sunset, a photo-like red fox in snow, and an isometric drawing of a room of server racks.](https://mintcdn.com/instacloud/fhQEKre-5PMsCqy7/images/gpu/generate-images.webp?fit=max\&auto=format\&n=fhQEKre-5PMsCqy7\&q=85\&s=1fba73938a6d482e6993bcbfd8d7f90c)

**1. Create a GPU machine**

```bash theme={null}
instabox create --name images --gpu l4
```

**2. Install Diffusers**

The GPU template already has PyTorch in `/opt/venv`, so you only add the image libraries.

```bash theme={null}
instabox exec images --timeout 300 -- \
  /opt/venv/bin/pip install diffusers transformers accelerate
```

**3. Copy the script**

Save this script as `generate.py` on your computer. It loads the model once, with a safety
checker that blanks a picture it flags, and then draws one picture for each pair of a prompt and
a file name.

```python generate.py theme={null}
import os, sys, time, warnings

# only errors from the libraries, and no progress bars
warnings.filterwarnings("ignore")
for name in ("TRANSFORMERS", "DIFFUSERS", "HF_HUB"):
    os.environ[name + "_VERBOSITY"] = "error"
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"

import torch
from diffusers import AutoPipelineForText2Image
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
from diffusers.utils import logging
from transformers import CLIPImageProcessor

logging.disable_progress_bar()
t = time.time()
checker = "CompVis/stable-diffusion-safety-checker"
pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/sd-turbo", dtype=torch.float16, variant="fp16",
    safety_checker=StableDiffusionSafetyChecker.from_pretrained(checker, dtype=torch.float16),
    feature_extractor=CLIPImageProcessor.from_pretrained(checker),
).to("cuda")
pipe.set_progress_bar_config(disable=True)
print(f"model loaded in {time.time() - t:.1f}s")

# arguments: PROMPT OUTPUT.png [PROMPT OUTPUT.png ...]
for prompt, path in zip(sys.argv[1::2], sys.argv[2::2]):
    t = time.time()
    pipe(prompt, num_inference_steps=1, guidance_scale=0.0).images[0].save(path)
    print(f"saved {path} in {time.time() - t:.2f}s")
```

Make a folder for the pictures on the machine, and copy the script into it.

```bash theme={null}
instabox exec images -- mkdir -p /data/home/images
```

```bash theme={null}
instabox cp generate.py images:/data/home/images/generate.py
```

**4. Generate the pictures**

Run the script with three prompts. The first run downloads the model, which is about 2.5 GB,
into the machine's Hugging Face cache.

```bash theme={null}
instabox exec images --cwd /data/home/images --timeout 600 -- \
  /opt/venv/bin/python generate.py \
  'a lighthouse on a rocky coast at sunset, oil painting' gen1.png \
  'a red fox sitting in fresh snow, photograph' gen2.png \
  'an isometric illustration of a tiny server room with glowing racks' gen3.png
```

The script prints how long the model took to load and how long each picture took. This output
is from a run with the model already on the machine's disk.

```text theme={null}
model loaded in 1.9s
saved gen1.png in 2.21s
saved gen2.png in 0.25s
saved gen3.png in 0.23s
```

The first picture of a run takes longer than the ones after it.

**5. Copy the pictures to your computer**

```bash theme={null}
instabox cp images:/data/home/images/gen1.png gen1.png
```

Repeat the command for `gen2.png` and `gen3.png`. The picture at the top of this page shows the
three results of this run.

**6. Stop or delete the machine**

Stop the machine when you are done for now. The model and your pictures stay on its disk, and a
stopped machine does not pay for its GPU.

```bash theme={null}
instabox stop images
```

Delete the machine when you no longer need it.

```bash theme={null}
instabox rm images --yes
```

## What it uses

SD-Turbo is a small model, so it leaves most of the 24 GiB of an `l4` free. A larger model, such as
SDXL or FLUX, needs more memory, so check its size before you pick a GPU type.

To generate many pictures, pass all the prompts to one run, so that the model loads once.

> [card] [Serve an open model](https://instabox.sh/docs/use-cases/gpu/run-a-model.md)
>
> Keep a model loaded behind an HTTPS endpoint.

> [card] [GPU machines](https://instabox.sh/docs/machines/gpu.md)
>
> GPU types and counts, and what the template includes.
