Create test_lora.py
Browse files- test_lora.py +40 -0
test_lora.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import DiffusionPipeline
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# 加载 Stable Diffusion 模型
|
6 |
+
pipe = DiffusionPipeline.from_pretrained("your_sd_dir/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
7 |
+
pipe.to("cuda")
|
8 |
+
|
9 |
+
# 加载 LoRA 权重
|
10 |
+
lora_path = "your_lora_dir"
|
11 |
+
pipe.load_lora_weights(pretrained_model_name_or_path_or_dict=lora_path, weight_name="1epoch_lora.safetensors")
|
12 |
+
|
13 |
+
# 定义 prompt 列表
|
14 |
+
prompts = [
|
15 |
+
"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",
|
16 |
+
"Tang Dynasty Treasure Flower Pattern,flower,rotational,flower,rotational, radioactive arrangement,symmetry, solo, yellow theme"
|
17 |
+
]
|
18 |
+
|
19 |
+
# 设置生成参数
|
20 |
+
num_inference_steps = 30
|
21 |
+
guidance_scale = 7.5
|
22 |
+
num_samples_per_prompt = 3
|
23 |
+
|
24 |
+
# 创建一个空的图像列表
|
25 |
+
all_images = []
|
26 |
+
|
27 |
+
# 为每个 prompt 生成 num_samples_per_prompt 张图片
|
28 |
+
for prompt in prompts:
|
29 |
+
images = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, num_images_per_prompt=num_samples_per_prompt).images
|
30 |
+
all_images.extend(images)
|
31 |
+
|
32 |
+
# 创建一个 2x3 的网格图
|
33 |
+
grid_image = Image.new('RGB', (3 * 512, 2 * 512)) # 假设每张图片大小为 512x512
|
34 |
+
for idx, img in enumerate(all_images):
|
35 |
+
x = (idx % 3) * 512
|
36 |
+
y = (idx // 3) * 512
|
37 |
+
grid_image.paste(img, (x, y))
|
38 |
+
|
39 |
+
# 保存网格图
|
40 |
+
grid_image.save("test_lora_grid.png")
|