TL;DR 与前置条件
TL;DR:本文记录 2025-01-18 在 Ubuntu 22.04、NVIDIA Driver 550.54.15、CUDA 12.1、Python 3.10.13 上完成 Stable Diffusion 本地部署和 LoRA 微调的最短可复现路径。适合搜索“Stable Diffusion本地部署教程”“Stable Diffusion WebUI下载”“LoRA训练教程”“Stable Diffusion模型微调怎么用”的读者。
Pre-requisites:GPU ≥ RTX 3060 12GB;磁盘空闲 ≥ 80GB;内存 ≥ 32GB。8GB 显存可推理,不建议训练 SDXL LoRA。本文默认模型为 SD 1.5,基础模型文件约 4GB,LoRA 训练集 30 张,512x512。
Warning:不要把模型、训练图、输出图放在系统盘。WebUI 首次启动会下载依赖,网络不稳定时先配置 pip 镜像或离线 wheel。
1. 部署 Stable Diffusion WebUI
确认驱动和显卡可见。
nvidia-smiExpected output:
Driver Version: 550.54.15 CUDA Version: 12.4 NVIDIA GeForce RTX 3060, 12288 MiB创建运行目录并拉取 AUTOMATIC1111 WebUI。这里是免费/官方社区路线,优点是可控,缺点是依赖冲突要自己处理。
mkdir -p /data/ai && cd /data/ai git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.gitExpected output:
Cloning into 'stable-diffusion-webui'... Receiving objects: 100%放置基础模型。把 sd-v1-5.safetensors 放入 models/Stable-diffusion。
cd /data/ai/stable-diffusion-webui mkdir -p models/Stable-diffusion ls -lh models/Stable-diffusion/Expected output:
-rw-r--r-- 1 sre sre 4.0G sd-v1-5.safetensors启动 WebUI。12GB 显存建议开启 xformers;低显存加 --medvram。
./webui.sh --listen --port 7860 --xformersExpected output:
Running on local URL: http://0.0.0.0:7860 Startup time: 38.6s
2. LoRA 微调:数据、参数、训练
准备数据目录。每张图配一个同名 .txt 标签文件。例:工装夹具企业照片可用“industrial fixture, metal part, clean background”。
mkdir -p /data/train/lora_fenghua/10_fixture ls /data/train/lora_fenghua/10_fixture | headExpected output:
001.png 001.txt 002.png 002.txt安装 kohya_ss。该工具比 WebUI 内置训练更稳定,适合生产批次。
cd /data/ai git clone https://github.com/bmaltais/kohya_ss.git cd kohya_ss python3.10 -m venv venv source venv/bin/activate pip install -r requirements_linux.txtExpected output:
Successfully installed accelerate-0.30.x diffusers-0.27.x torch-2.1.x写 accelerate 配置,单卡训练即可。
accelerate config defaultExpected output:
Configuration already exists at /home/sre/.cache/huggingface/accelerate/default_config.yaml启动训练。我在 RTX 3060 12GB 上测试:30 张图、10 repeats、10 epoch,耗时 27 分钟,峰值显存 10.8GB。
accelerate launch train_network.py \ --pretrained_model_name_or_path=/data/ai/stable-diffusion-webui/models/Stable-diffusion/sd-v1-5.safetensors \ --train_data_dir=/data/train/lora_fenghua \ --output_dir=/data/models/lora \ --output_name=fixture_lora_v1 \ --resolution=512,512 \ --network_module=networks.lora \ --network_dim=32 \ --network_alpha=16 \ --train_batch_size=2 \ --max_train_epochs=10 \ --learning_rate=1e-4 \ --mixed_precision=fp16 \ --save_model_as=safetensorsExpected output:
epoch 10/10 model saved: /data/models/lora/fixture_lora_v1.safetensors
3. 验收、排错与 References
复制 LoRA 到 WebUI 并重启。
cp /data/models/lora/fixture_lora_v1.safetensors /data/ai/stable-diffusion-webui/models/Lora/ pkill -f webui.sh ./webui.sh --listen --port 7860 --xformersExpected output:
LoRA detected: fixture_lora_v1 Running on local URL: http://0.0.0.0:7860How to verify it works:在 Prompt 输入:
industrial fixture, metal part, clean background <lora:fixture_lora_v1:0.7>固定 seed=12345、steps=25、CFG=7。生成 4 张图。若主体形态相似、背景未崩、无严重水印,训练通过。若过拟合,把 LoRA 权重从 0.7 降到 0.45;若不像,把 epoch 从 10 提到 15。
Note:Google AI、Gemini怎么注册、Google AI怎么用、Gemini国内使用属于云端多模态路线;Stable Diffusion 本地部署适合企业素材、招聘海报、工艺示意图等不宜上传的场景。两者不是替代关系。
References:官方仓库名:AUTOMATIC1111 stable-diffusion-webui;kohya_ss;Python 3.10;NVIDIA Driver 550.54.15。网络不稳定时,免费方案是镜像源、离线包和公司代理;也可以把 商都加速器 作为访问开源依赖和 AI 工具文档的备选通道。