Update models/model_manager.py
Browse files- models/model_manager.py +18 -26
models/model_manager.py
CHANGED
|
@@ -6,6 +6,7 @@ from diffusers import StableDiffusionPipeline, ControlNetModel, StableDiffusionC
|
|
| 6 |
import os
|
| 7 |
import logging
|
| 8 |
import time
|
|
|
|
| 9 |
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
| 11 |
logger = logging.getLogger(__name__)
|
|
@@ -129,10 +130,8 @@ class ModelManager:
|
|
| 129 |
if self.clip_model is None or self.clip_processor is None:
|
| 130 |
self.load_clip_model()
|
| 131 |
|
| 132 |
-
# 定义服装风格类别
|
| 133 |
styles = ["商务正装", "休闲风", "运动风", "时尚潮流", "复古风", "街头风", "优雅风"]
|
| 134 |
|
| 135 |
-
# 准备输入
|
| 136 |
inputs = self.clip_processor(
|
| 137 |
text=styles,
|
| 138 |
images=image,
|
|
@@ -140,13 +139,11 @@ class ModelManager:
|
|
| 140 |
padding=True
|
| 141 |
).to(self.device)
|
| 142 |
|
| 143 |
-
# 获取特征
|
| 144 |
with torch.no_grad():
|
| 145 |
outputs = self.clip_model(**inputs)
|
| 146 |
logits_per_image = outputs.logits_per_image
|
| 147 |
probs = logits_per_image.softmax(dim=1).cpu().numpy()[0]
|
| 148 |
|
| 149 |
-
# 转换为分数字典
|
| 150 |
style_scores = {style: float(prob) for style, prob in zip(styles, probs)}
|
| 151 |
return style_scores
|
| 152 |
|
|
@@ -158,7 +155,6 @@ class ModelManager:
|
|
| 158 |
logger.error("无法生成图像:Stable Diffusion 模型未加载")
|
| 159 |
return self.create_placeholder_image(width, height)
|
| 160 |
|
| 161 |
-
# 生成图像
|
| 162 |
result = self.sd_pipeline(
|
| 163 |
prompt=prompt,
|
| 164 |
negative_prompt=negative_prompt,
|
|
@@ -177,15 +173,12 @@ class ModelManager:
|
|
| 177 |
logger.error("无法生成3D试穿:ControlNet 模型未加载")
|
| 178 |
return self.create_placeholder_image(512, 768)
|
| 179 |
|
| 180 |
-
# 如果有参考图像,将其融入提示词
|
| 181 |
if reference_image is not None:
|
| 182 |
-
# 这里可以添加将参考图像融入提示词的逻辑
|
| 183 |
prompt = f"{prompt}, based on reference design"
|
| 184 |
|
| 185 |
-
# 生成图像
|
| 186 |
result = self.controlnet_pipeline(
|
| 187 |
prompt=prompt,
|
| 188 |
-
image=image,
|
| 189 |
negative_prompt=negative_prompt,
|
| 190 |
num_inference_steps=num_inference_steps,
|
| 191 |
guidance_scale=guidance_scale,
|
|
@@ -197,20 +190,19 @@ class ModelManager:
|
|
| 197 |
color = (random.randint(120, 200), random.randint(120, 200), random.randint(120, 200))
|
| 198 |
return Image.new('RGB', (width, height), color=color)
|
| 199 |
|
| 200 |
-
def cleanup(self):
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
logger.error(f"清理显存失败: {e}")
|
|
|
|
| 6 |
import os
|
| 7 |
import logging
|
| 8 |
import time
|
| 9 |
+
import random # 补充导入
|
| 10 |
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
|
|
|
| 130 |
if self.clip_model is None or self.clip_processor is None:
|
| 131 |
self.load_clip_model()
|
| 132 |
|
|
|
|
| 133 |
styles = ["商务正装", "休闲风", "运动风", "时尚潮流", "复古风", "街头风", "优雅风"]
|
| 134 |
|
|
|
|
| 135 |
inputs = self.clip_processor(
|
| 136 |
text=styles,
|
| 137 |
images=image,
|
|
|
|
| 139 |
padding=True
|
| 140 |
).to(self.device)
|
| 141 |
|
|
|
|
| 142 |
with torch.no_grad():
|
| 143 |
outputs = self.clip_model(**inputs)
|
| 144 |
logits_per_image = outputs.logits_per_image
|
| 145 |
probs = logits_per_image.softmax(dim=1).cpu().numpy()[0]
|
| 146 |
|
|
|
|
| 147 |
style_scores = {style: float(prob) for style, prob in zip(styles, probs)}
|
| 148 |
return style_scores
|
| 149 |
|
|
|
|
| 155 |
logger.error("无法生成图像:Stable Diffusion 模型未加载")
|
| 156 |
return self.create_placeholder_image(width, height)
|
| 157 |
|
|
|
|
| 158 |
result = self.sd_pipeline(
|
| 159 |
prompt=prompt,
|
| 160 |
negative_prompt=negative_prompt,
|
|
|
|
| 173 |
logger.error("无法生成3D试穿:ControlNet 模型未加载")
|
| 174 |
return self.create_placeholder_image(512, 768)
|
| 175 |
|
|
|
|
| 176 |
if reference_image is not None:
|
|
|
|
| 177 |
prompt = f"{prompt}, based on reference design"
|
| 178 |
|
|
|
|
| 179 |
result = self.controlnet_pipeline(
|
| 180 |
prompt=prompt,
|
| 181 |
+
image=image,
|
| 182 |
negative_prompt=negative_prompt,
|
| 183 |
num_inference_steps=num_inference_steps,
|
| 184 |
guidance_scale=guidance_scale,
|
|
|
|
| 190 |
color = (random.randint(120, 200), random.randint(120, 200), random.randint(120, 200))
|
| 191 |
return Image.new('RGB', (width, height), color=color)
|
| 192 |
|
| 193 |
+
def cleanup(self):
|
| 194 |
+
"""释放模型占用显存和缓存"""
|
| 195 |
+
logger.info("释放模型占用显存和缓存...")
|
| 196 |
+
try:
|
| 197 |
+
del self.caption_model
|
| 198 |
+
del self.clip_model
|
| 199 |
+
del self.sd_pipeline
|
| 200 |
+
del self.controlnet
|
| 201 |
+
del self.controlnet_pipeline
|
| 202 |
+
|
| 203 |
+
if torch.cuda.is_available():
|
| 204 |
+
torch.cuda.empty_cache()
|
| 205 |
+
|
| 206 |
+
logger.info("显存和缓存清理完成")
|
| 207 |
+
except Exception as e:
|
| 208 |
+
logger.error(f"清理显存失败: {e}")
|
|
|