Skip to content

Commit 6b7de97

Browse files
committed
feedback
1 parent 2e61465 commit 6b7de97

File tree

1 file changed

+144
-8
lines changed

1 file changed

+144
-8
lines changed

docs/source/en/using-diffusers/batched_inference.md

Lines changed: 144 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ Batch inference processes multiple prompts at a time to increase throughput. It
1616

1717
The downside is increased latency because you must wait for the entire batch to complete, and more GPU memory is required for large batches.
1818

19-
To generate a batch of images, pass a list of prompts or images to the pipeline.
20-
2119
<hfoptions id="usage">
2220
<hfoption id="text-to-image">
2321

22+
To generate a batch of images, pass a list of prompts to the pipeline.
23+
2424
```py
2525
import torch
2626
from diffusers import DiffusionPipeline
@@ -40,14 +40,72 @@ images = pipeline(
4040
prompt=prompts,
4141
).images
4242

43+
fig, axes = plt.subplots(2, 2, figsize=(12, 12))
44+
axes = axes.flatten()
45+
46+
for i, image in enumerate(images):
47+
axes[i].imshow(image)
48+
axes[i].set_title(f"Image {i+1}")
49+
axes[i].axis('off')
50+
51+
plt.tight_layout()
52+
plt.show()
53+
```
54+
55+
To generate multiple variations of one prompt, use the `num_images_per_prompt` argument.
56+
57+
```py
58+
import torch
59+
import matplotlib.pyplot as plt
60+
from diffusers import DiffusionPipeline
61+
62+
pipeline = DiffusionPipeline.from_pretrained(
63+
"stabilityai/stable-diffusion-xl-base-1.0",
64+
torch_dtype=torch.float16
65+
).to("cuda")
66+
67+
images = pipeline(
68+
prompt="pixel-art a cozy coffee shop interior, low-res, blocky, pixel art style, 8-bit graphics",
69+
num_images_per_prompt=4
70+
).images
71+
72+
fig, axes = plt.subplots(2, 2, figsize=(12, 12))
73+
axes = axes.flatten()
74+
75+
for i, image in enumerate(images):
76+
axes[i].imshow(image)
77+
axes[i].set_title(f"Image {i+1}")
78+
axes[i].axis('off')
79+
80+
plt.tight_layout()
81+
plt.show()
82+
```
83+
84+
Combine both approaches to generate different variations of different prompts.
85+
86+
```py
87+
images = pipeline(
88+
prompt=prompts,
89+
num_images_per_prompt=2,
90+
).images
91+
92+
fig, axes = plt.subplots(2, 2, figsize=(12, 12))
93+
axes = axes.flatten()
94+
4395
for i, image in enumerate(images):
44-
image.save(f"batch_image_{i}.png")
45-
print(f"Generated image {i+1} for prompt: {prompts[i]}")
96+
axes[i].imshow(image)
97+
axes[i].set_title(f"Image {i+1}")
98+
axes[i].axis('off')
99+
100+
plt.tight_layout()
101+
plt.show()
46102
```
47103

48104
</hfoption>
49105
<hfoption id="image-to-image">
50106

107+
To generate a batch of images, pass a list of images to the pipeline.
108+
51109
```py
52110
import torch
53111
from diffusers.utils import load_image
@@ -77,9 +135,80 @@ images = pipeline(
77135
strength=0.5
78136
).images
79137

138+
fig, axes = plt.subplots(2, 2, figsize=(12, 12))
139+
axes = axes.flatten()
140+
141+
for i, image in enumerate(images):
142+
axes[i].imshow(image)
143+
axes[i].set_title(f"Image {i+1}")
144+
axes[i].axis('off')
145+
146+
plt.tight_layout()
147+
plt.show()
148+
```
149+
150+
To generate multiple variations of one prompt, use the `num_images_per_prompt` argument.
151+
152+
```py
153+
import torch
154+
import matplotlib.pyplot as plt
155+
from diffusers.utils import load_image
156+
from diffusers import DiffusionPipeline
157+
158+
pipeline = DiffusionPipeline.from_pretrained(
159+
"stabilityai/stable-diffusion-xl-base-1.0",
160+
torch_dtype=torch.float16
161+
).to("cuda")
162+
163+
input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/detail-prompt.png")
164+
165+
images = pipeline(
166+
prompt="pixel-art a cozy coffee shop interior, low-res, blocky, pixel art style, 8-bit graphics",
167+
image=input_image,
168+
num_images_per_prompt=4
169+
).images
170+
171+
fig, axes = plt.subplots(2, 2, figsize=(12, 12))
172+
axes = axes.flatten()
173+
174+
for i, image in enumerate(images):
175+
axes[i].imshow(image)
176+
axes[i].set_title(f"Image {i+1}")
177+
axes[i].axis('off')
178+
179+
plt.tight_layout()
180+
plt.show()
181+
```
182+
183+
Combine both approaches to generate different variations of different prompts.
184+
185+
```py
186+
input_images = [
187+
load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png"),
188+
load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/detail-prompt.png")
189+
]
190+
191+
prompts = [
192+
"cinematic film still of a cat basking in the sun on a roof in Turkey, highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain",
193+
"pixel-art a cozy coffee shop interior, low-res, blocky, pixel art style, 8-bit graphics"
194+
]
195+
196+
images = pipeline(
197+
prompt=prompts,
198+
image=input_images,
199+
num_images_per_prompt=2,
200+
).images
201+
202+
fig, axes = plt.subplots(2, 2, figsize=(12, 12))
203+
axes = axes.flatten()
204+
80205
for i, image in enumerate(images):
81-
image.save(f"batch_image_{i}.png")
82-
print(f"Generated image {i+1} for prompt: {prompts[i]}")
206+
axes[i].imshow(image)
207+
axes[i].set_title(f"Image {i+1}")
208+
axes[i].axis('off')
209+
210+
plt.tight_layout()
211+
plt.show()
83212
```
84213

85214
</hfoption>
@@ -120,9 +249,16 @@ images = pipeline(
120249
generator=generator
121250
).images
122251

252+
fig, axes = plt.subplots(2, 2, figsize=(12, 12))
253+
axes = axes.flatten()
254+
123255
for i, image in enumerate(images):
124-
image.save(f"batch_image_{i}.png")
125-
print(f"Generated image {i+1} for prompt: {prompts[i]}")
256+
axes[i].imshow(image)
257+
axes[i].set_title(f"Image {i+1}")
258+
axes[i].axis('off')
259+
260+
plt.tight_layout()
261+
plt.show()
126262
```
127263

128264
You can use this to iteratively select an image associated with a seed and then improve on it by crafting a more detailed prompt.

0 commit comments

Comments
 (0)