《Stable Diffusion绘画完全指南:从入门到精通的Prompt设计艺术》-配套代码示例

第一章:模型加载与基础生成

1.1 基础模型加载
复制代码
from diffusers import StableDiffusionPipeline
import torch

# 加载SD 1.5基础模型(FP32精度)
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float32
).to("cuda")

# 生成第一张图片
image = pipe("a cat wearing sunglasses").images[0]
image.save("basic_cat.png")
1.2 半精度优化加载
复制代码
# 加载SDXL模型(FP16精度 + xformers加速)
pipe = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    use_xformers=True
).to("cuda")

# 生成高分辨率图片
image = pipe("cyberpunk city at night, 8k").images[0]
image.save("cyber_city.png")
1.3 低显存设备适配
复制代码
# CPU卸载模式(显存<6GB适用)
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()  # 动态加载模型到显存

image = pipe("watercolor landscape").images[0]
image.save("low_vram_image.png")

第二章:Prompt工程优化

2.1 结构化Prompt模板
复制代码
template = """
(cute corgi:1.3) wearing {glasses|hat|scarf},  # 主体
Studio Ghibli style, soft shading,            # 风格
in a flower field at sunset,                   # 环境
4k resolution, bokeh effect                    # 画质
[blurry, low quality]                          # 负面提示
"""

image = pipe(template.format("sunglasses")).images[0]
image.save("styled_corgi.png")
2.2 动态权重控制
复制代码
# 使用数值权重调整元素重要性
prompt = """
(a beautiful castle:1.5) on a cliff,
(medieval style:0.8) with (futuristic elements:0.6),
intricate details, 8k cinematic lighting
"""

image = pipe(prompt, guidance_scale=7).images[0]
image.save("hybrid_castle.png")
2.3 多语言Prompt融合
复制代码
# 中英混合Prompt(需CLIP多语言支持)
prompt = """
A girl in 汉服 (hanfu:1.2) standing by 西湖 (West Lake),
水墨画风格 (ink wash painting style), ultra detailed
"""

image = pipe(prompt).images[0]
image.save("hanfu_girl.png")

第三章:高级控制技术

3.1 ControlNet姿势控制
复制代码
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
from PIL import Image

# 加载OpenPose ControlNet
controlnet = ControlNetModel.from_pretrained(
    "lllyasviel/sd-controlnet-openpose",
    torch_dtype=torch.float16
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=controlnet,
    torch_dtype=torch.float16
).to("cuda")

# 输入姿势图
pose_image = Image.open("pose_ref.png")
image = pipe("dancing woman", image=pose_image).images[0]
image.save("controlled_dance.png")
3.2 LoRA风格叠加
复制代码
from diffusers import StableDiffusionXLPipeline
import torch

# 加载基础模型
pipe = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16
).to("cuda")

# 加载动漫LoRA
pipe.load_lora_weights("lora/anime_style_xl.safetensors")

image = pipe("a warrior in armor").images[0]
image.save("anime_warrior.png")
3.3 多ControlNet联合控制
复制代码
# 同时使用Canny边缘和深度图控制
controlnets = [
    ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16),
    ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-depth", torch_dtype=torch.float16)
]

pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=controlnets,
    torch_dtype=torch.float16
).to("cuda")

# 输入多控制图
canny_img = Image.open("edge.png")
depth_img = Image.open("depth.png")
image = pipe("futuristic car", image=[canny_img, depth_img]).images[0]
image.save("multi_control_car.png")

第四章:性能优化

4.1 TensorRT加速
复制代码
from diffusers import DiffusionPipeline
import torch_tensorrt

# 转换模型为TensorRT格式
pipe = DiffusionPipeline.from_pretrained(...)
trt_unet = torch_tensorrt.compile(
    pipe.unet,
    inputs=[torch.randn(1,4,64,64).to("cuda")],
    enabled_precisions={torch.float16}
)
pipe.unet = trt_unet

# 加速生成
image = pipe("speed test image").images[0]
4.2 批处理生成
复制代码
# 一次生成4张不同提示的图片
prompts = [
    "a red rose",
    "a blue rose", 
    "a golden rose",
    "a black rose"
]

images = pipe(prompt=prompts, num_images_per_prompt=1).images
for idx, img in enumerate(images):
    img.save(f"rose_{idx}.png")
4.3 缓存优化
复制代码
from diffusers import StableDiffusionPipeline
import torch

# 启用KV缓存加速
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
    enable_sequential_cpu_offload=True,
    enable_kv_caching=True  # 关键优化
).to("cuda")

# 第一次生成较慢(编译缓存)
image = pipe("warmup image").images[0]

# 后续生成加速30%
image = pipe("optimized image").images[0]

第五章:商业化生产

5.1 批量产品图生成
复制代码
product_data = [
    {"name": "sneakers", "color": "neon green"},
    {"name": "backpack", "color": "matte black"},
    {"name": "watch", "color": "rose gold"}
]

for product in product_data:
    prompt = f"""
    Professional product photo of {product['color']} {product['name']},
    studio lighting, 8k resolution, product design award winner
    """
    image = pipe(prompt).images[0]
    image.save(f"{product['name']}_{product['color']}.png")
5.2 自动质量检测
复制代码
from transformers import CLIPModel, CLIPProcessor

# 加载CLIP模型
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

def quality_check(image, target_prompt):
    inputs = clip_processor(text=target_prompt, images=image, return_tensors="pt")
    outputs = clip_model(**inputs)
    similarity = outputs.logits_per_image.item()
    return similarity > 25  # 阈值根据实际情况调整

if quality_check(image, prompt):
    image.save("approved.png")
else:
    print("Quality check failed!")
5.3 多尺寸适配生成
复制代码
resolutions = [(512,512), (768,768), (1024,1024)]

for w, h in resolutions:
    pipe = StableDiffusionPipeline.from_pretrained(...)
    image = pipe(
        prompt, 
        width=w,
        height=h,
        target_size=(w,h)
    ).images[0]
    image.save(f"output_{w}x{h}.png")

第六章:故障排查

6.1 显存监控
复制代码
import nvidia_smi

nvidia_smi.nvmlInit()
handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)

def print_mem_usage():
    info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
    print(f"Used VRAM: {info.used//1024**2} MB")

print_mem_usage()  # 生成前后调用检测
6.2 异常处理
复制代码
try:
    image = pipe("problematic prompt").images[0]
except torch.cuda.OutOfMemoryError:
    print("显存不足!尝试启用--medvram")
    pipe.enable_model_cpu_offload()
    image = pipe("problematic prompt").images[0]
6.3 采样调试
复制代码
# 记录采样过程
pipe = StableDiffusionPipeline.from_pretrained(...)
pipe.set_progress_bar_config(leave=True)  # 显示详细进度

# 生成并保存中间步骤
for i in range(pipe.scheduler.config.num_train_timesteps):
    image = pipe(
        prompt, 
        callback_on_step_end=lambda step, t, latents: 
            latents.save(f"step_{step}.pt")
    )
相关推荐
云和数据.ChenGuang1 天前
Python 重写(Override)深度解析:从 MRO 到 super() 的完整机制
java·开发语言·人工智能·python·langchain·1024程序员节
西西木科技丨Shopify开发机构1 天前
CalDigit TS4案例:AI产品图的Shopify整站接入与验收
人工智能·shopify·独立站
2601_965448871 天前
AI视频制作:做系列节气民俗视频音色不统一,AI 音色克隆省事的方法
人工智能·机器学习·音视频
美狐美颜SDK开放平台1 天前
视频美颜SDK试用DEMO阶段应该测试什么?直播APP接入实操指南
开发语言·人工智能·深度学习·音视频·美颜sdk·直播美颜sdk·第三方美颜sdk
HRaitest1 天前
秋招技术岗招聘提效:三类 AI 招聘方案的技术底层与选型逻辑
大数据·人工智能·求职招聘·ai-native
天远数科1 天前
零信任架构实战:基于天远企业股权变更构建自动化商业银行对公授信存续期股权异动与资产抽逃预警网关
运维·人工智能·架构·自动化
车软学堂1 天前
从测试转开发,到底要补什么
网络·人工智能·python·功能测试·单片机·车载系统·自动化
海盗12341 天前
AI 新闻日报 10-28:Atria Dawn 完整版开源、Cursor Agent SDK 转正、Genie 4 实时世界模型
人工智能·机器人·开源·人工智能aigc
志尊宝1 天前
AI原生前端开发(035):AI时代的工程化最佳实践总结
人工智能·ai-native
冬奇Lab1 天前
一天一个开源项目(第251篇):Claude SEO —— 装进 Claude Code 的 SEO 审计团队,26 个子技能 + 19 个专家 Agent 并行体检网站
人工智能·开源