Stable Diffusion 环境准备
安装Python 3.10或更高版本,并配置虚拟环境。推荐使用Anaconda管理依赖:
conda create -n sd python=3.10
conda activate sd
安装PyTorch(需匹配CUDA版本):
pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu118
模型下载与配置
从Hugging Face下载Stable Diffusion模型(如stable-diffusion-2-1),保存至本地目录。需注册并同意许可协议:
from diffusers import StableDiffusionPipeline
model_path = "./stable-diffusion-2-1"
pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
pipe.to("cuda")
生成参数调优
关键参数包括:
prompt: 正向提示词(如"a cyberpunk cityscape, neon lights, 4k detailed")negative_prompt: 反向提示词(如"blurry, low resolution")num_inference_steps: 迭代步数(20-50步平衡质量与速度)guidance_scale: 提示词权重(7-15范围效果较佳)
示例生成代码:
image = pipe(
prompt="portrait of a wizard, intricate cloak, magical aura",
negative_prompt="deformed, bad anatomy",
num_inference_steps=30,
guidance_scale=10
).images[0]
image.save("output.png")
后期处理技巧
使用ADetailer等扩展工具修复面部细节,或通过Img2Img功能局部重绘。对于分辨率提升,可结合Real-ESRGAN超分模型:
pip install realesrgan
调用命令:
from realesrgan import RealESRGAN
upscaler = RealESRGAN(device="cuda", scale=4)
sr_image = upscaler.enhance(image)
性能优化建议
启用xFormers加速注意力计算:
pipe.enable_xformers_memory_efficient_attention()
对于低显存设备(<8GB),使用--medvram参数或启用模型分片加载。
注:商业用途需遵守模型许可证(如CreativeML OpenRAIL-M),部分衍生模型可能有额外限制。
653

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



