-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2image06.py
More file actions
58 lines (48 loc) · 1.73 KB
/
Copy pathi2image06.py
File metadata and controls
58 lines (48 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# -*- coding utf-8 -*-
import torch
import matplotlib.pyplot as plt
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
from diffusers.utils import load_image
"""
以一个简单的简笔画为基础,生成精美的图片
"""
def draw_image_grids(images, rows, cols):
# Create a rows x cols grid for displaying the images
fig, axes = plt.subplots(2, 2, figsize=(10, 10))
for row in range(rows):
for col in range(cols):
axes[row, col].imshow(images[col + row * cols])
for ax in axes.flatten():
ax.axis('off')
# Display the grid
plt.show()
if __name__ == '__main__':
# 加载模型
controlnet = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-scribble", torch_dtype=torch.float16
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
torch_dtype=torch.float16,
)
# 显存不够用时,将模型从显存中移除
pipe.enable_model_cpu_offload()
# 通过xformers加速推理
pipe.enable_xformers_memory_efficient_attention()
# 加载狗狗简笔画
image_file = "./data/scribble_dog.png"
scribble_image = load_image(image_file)
# 生成四种狗狗图片
generator = [torch.Generator(device="cpu").manual_seed(2) for i in range(4)]
prompt = "dog"
prompt = [prompt + t for t in [" in a room", " near the lake", " on the street", " in the forest"]]
output = pipe(
prompt,
scribble_image,
negative_prompt=["lowres, bad anatomy, worst quality, low quality"] * 4,
generator=generator,
num_inference_steps=50,
)
draw_image_grids(output.images, 2, 2)