Update model_pipelines.py
Browse files- model_pipelines.py +41 -21
model_pipelines.py
CHANGED
@@ -1,28 +1,48 @@
|
|
|
|
1 |
import torch
|
2 |
-
from
|
|
|
3 |
|
4 |
-
device = "
|
|
|
5 |
|
6 |
def load_pipelines():
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
}
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
pipe = pipe.to(device)
|
19 |
-
pipe.enable_attention_slicing()
|
20 |
-
pipes[name] = pipe
|
21 |
-
return pipes
|
22 |
|
23 |
-
def generate_all(
|
24 |
results = {}
|
25 |
-
for name, pipe in
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import StableDiffusionPipeline, DiffusionPipeline
|
2 |
import torch
|
3 |
+
from PIL import Image
|
4 |
+
import time
|
5 |
|
6 |
+
device = "cpu"
|
7 |
+
torch_dtype = torch.float32 # CPU上使用float32更稳定
|
8 |
|
9 |
def load_pipelines():
|
10 |
+
print("正在加载模型(CPU模式)...")
|
11 |
+
|
12 |
+
# 使用较小的模型或优化配置
|
13 |
+
models = {
|
14 |
+
"sd_v1_5": StableDiffusionPipeline.from_pretrained(
|
15 |
+
"runwayml/stable-diffusion-v1-5",
|
16 |
+
torch_dtype=torch_dtype,
|
17 |
+
safety_checker=None,
|
18 |
+
requires_safety_checker=False
|
19 |
+
).to(device),
|
20 |
+
|
21 |
+
"openjourney_v4": DiffusionPipeline.from_pretrained(
|
22 |
+
"prompthero/openjourney-v4",
|
23 |
+
torch_dtype=torch_dtype,
|
24 |
+
safety_checker=None
|
25 |
+
).to(device),
|
26 |
+
|
27 |
+
"ldm_256": DiffusionPipeline.from_pretrained(
|
28 |
+
"CompVis/ldm-text2im-large-256",
|
29 |
+
torch_dtype=torch_dtype
|
30 |
+
).to(device)
|
31 |
}
|
32 |
+
|
33 |
+
# 启用内存优化
|
34 |
+
for model in models.values():
|
35 |
+
model.enable_attention_slicing()
|
36 |
+
|
37 |
+
return models
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
def generate_all(pipelines, prompt, steps=20):
|
40 |
results = {}
|
41 |
+
for name, pipe in pipelines.items():
|
42 |
+
print(f"正在用 {name} 生成图像...")
|
43 |
+
start = time.time()
|
44 |
+
result = pipe(prompt, num_inference_steps=steps).images[0]
|
45 |
+
gen_time = time.time() - start
|
46 |
+
print(f"{name} 生成完成,耗时 {gen_time:.2f}秒")
|
47 |
+
results[name] = result
|
48 |
+
return results
|