Update test_lora.py
Browse files- test_lora.py +52 -36
test_lora.py
CHANGED
@@ -2,39 +2,55 @@ from diffusers import DiffusionPipeline
|
|
2 |
import torch
|
3 |
from PIL import Image
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
|
5 |
+
|
6 |
+
def test_lora(lcm_speedup=Flase):
|
7 |
+
# 加载 Stable Diffusion 模型
|
8 |
+
pipe = DiffusionPipeline.from_pretrained("your_sd_dir/stable-diffusion-v1-5", torch_dtype=torch.float16, safety_checker = None, requires_safety_checker=False)
|
9 |
+
pipe.to("cuda")
|
10 |
+
|
11 |
+
# 加载 LoRA 权重
|
12 |
+
lora_path = "your_lora_dir"
|
13 |
+
pipe.load_lora_weights(pretrained_model_name_or_path_or_dict=lora_path, weight_name="1epoch_lora.safetensors", adapter_name="pattern")
|
14 |
+
if lcm_speedup:
|
15 |
+
pipe.load_lora_weights(pretrained_model_name_or_path_or_dict=lora_path, weight_name="lcm_lora.safetensors", adapter_name="lcm")
|
16 |
+
pipe.set_adapters(["pattern", "lcm"], adapter_weights=[1.0, 1.0])
|
17 |
+
|
18 |
+
# 定义 prompt 列表
|
19 |
+
prompts = [
|
20 |
+
"Tang Dynasty Phoenix bird pattern, multi-integrated color complex figurative embroidery animal pattern, white background, asymmetry, meaning good weather, good luck, happy life. A symbol of good peace, abundance of children, supreme power and dominion. Worship of auspicious gods",
|
21 |
+
"Tang Dynasty Treasure Flower Pattern,flower,rotational,flower,rotational, radioactive arrangement,symmetry, solo, yellow theme"
|
22 |
+
]
|
23 |
+
|
24 |
+
# 设置生成参数
|
25 |
+
if lcm_speedup:
|
26 |
+
num_inference_steps = 8
|
27 |
+
guidance_scale = 2
|
28 |
+
else:
|
29 |
+
num_inference_steps = 30
|
30 |
+
guidance_scale = 7.5
|
31 |
+
|
32 |
+
num_samples_per_prompt = 3
|
33 |
+
|
34 |
+
# 创建一个空的图像列表
|
35 |
+
all_images = []
|
36 |
+
|
37 |
+
# 为每个 prompt 生成 num_samples_per_prompt 张图片
|
38 |
+
for prompt in prompts:
|
39 |
+
images = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, num_images_per_prompt=num_samples_per_prompt).images
|
40 |
+
all_images.extend(images)
|
41 |
+
|
42 |
+
# 创建一个 2x3 的网格图
|
43 |
+
grid_image = Image.new('RGB', (3 * 512, 2 * 512)) # 假设每张图片大小为 512x512
|
44 |
+
for idx, img in enumerate(all_images):
|
45 |
+
x = (idx % 3) * 512
|
46 |
+
y = (idx // 3) * 512
|
47 |
+
grid_image.paste(img, (x, y))
|
48 |
+
|
49 |
+
# 保存网格图
|
50 |
+
n = 4 if lcm_speedup else 30
|
51 |
+
grid_image.save(f"test_lora_grid_{n}_steps.png")
|
52 |
+
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
test_lora()
|
56 |
+
test_lora(lcm_speedup=True)
|