关键词:Stable Diffusion、Diffusers、AIGC、10行代码、GPU、CPU
00 先上结果
把下面 10 行代码粘进 Colab / Jupyter / VS Code,回车即出图(示例耗时 8.3 秒,RTX 3060 12 GB):
from diffusers import StableDiffusionPipeline
import torch
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16,
variant="fp16"
).to("cuda")
prompt = "a cat wearing steampunk goggles, cyberpunk city, 8k, ultra-realistic"
image = pipe(prompt, num_inference_steps=20, guidance_scale=7.5).images[0]
image.save("cat.png")
输出:
https://i.imgur.com/SteampunkCat.png (示例图,仅示意)
01 环境 3 步搞定
| 场景 | 命令 |
|---|---|
| Colab | 直接选 GPU 运行时,无需额外安装 |
| 本地 | pip install diffusers==0.30.0 transformers accelerate safetensors |
| CPU | 把 .to("cuda") 换成 .to("cpu"),速度约慢 20×,但能跑 |
02 10 行代码逐行拆解
| 行 | 作用 | 小白翻译 |
|---|---|---|
| 1-2 | 导入库 | 工具箱 |
| 4-7 | 下载并加载模型 | 把「画家大脑」搬到本地 |
| 9 | 写提示词 | 告诉画家画啥 |
| 10 | 生成 | 真正开始画画 |
| 11 | 保存 | 把画存成 PNG |
03 3 个超参数 30 秒上手
| 参数 | 取值示例 | 解释 |
|---|---|---|
num_inference_steps | 20–50 | 步数越多细节越好,时间线性增加 |
guidance_scale | 7.5–12 | 越大越「听话」,但过高会失真 |
negative_prompt | "blurry, lowres" | 负面提示词,告诉画家 不要 画什么 |
04 CPU 党福利:一行代码提速 4×
pipe.enable_model_cpu_offload() # 显存不足也能跑大图
05 Mac / AMD 也能跑
pipe = pipe.to("mps") # Apple Silicon 专用
06 4 个常见报错 & 秒解方案
| 报错信息 | 原因 | 解决 |
|---|---|---|
CUDA out of memory | 显存不够 | 加 pipe.enable_attention_slicing() |
ImportError: libGL.so.1 | Linux 缺库 | apt-get install libgl1-mesa-glx |
PIL.UnidentifiedImageError | 下载中断 | 手动删缓存 ~/.cache/huggingface |
RuntimeError: Expected mps | macOS 版本低 | 升级 macOS ≥ 12.3 |
07 30 秒变体:不同风格一句话切换
prompt = "portrait of an astronaut, art by greg rutkowski, soft lighting, trending on artstation"
→ 直接出油画质感
prompt = "pixel art cat, 16-bit, game sprite, black background"
→ 直接出像素风
08 进阶:30 行以内做「图生图」
from diffusers import StableDiffusionImg2ImgPipeline
from PIL import Image
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")
init_img = Image.open("sketch.png").convert("RGB").resize((512, 512))
image = pipe(prompt="a detailed color version", image=init_img, strength=0.75).images[0]
image.save("colorized.png")
09 一键打包 Gradio 网页 Demo
import gradio as gr
def gen(prompt):
return pipe(prompt).images[0]
gr.Interface(fn=gen, inputs="text", outputs="image").launch()

1802

被折叠的 条评论
为什么被折叠?



