TL;DR 与前置条件
TL;DR:本文记录 2025-02-15 在 Ubuntu 24.04、NVIDIA Driver 550.120、CUDA 12.4、RTX 4070 12GB 上完成 SDXL 本地推理和 LoRA 微调。目标是生成“奉化市先进制造业招聘海报”风格图,不依赖云端 API。适合搜索 Stable Diffusion本地部署教程、SDXL模型下载、LoRA微调怎么用、kohya_ss教程 的读者。
Pre-requisites:Ubuntu 24.04 LTS;Python 3.10;Git 2.43;显存最低 8GB,建议 12GB;磁盘空闲 80GB;已安装 NVIDIA 驱动。
Warning: SDXL base 模型单文件约 6.5GB,训练数据和缓存会快速占用磁盘。不要把环境建在系统盘小分区。
1. 部署 ComfyUI 并完成 SDXL 推理
确认 GPU 可见。
nvidia-smiExpected output: Driver Version: 550.120 CUDA Version: 12.4 GPU Name: NVIDIA GeForce RTX 4070 Memory-Usage: 420MiB / 12282MiB安装系统依赖。
sudo apt update && sudo apt install -y git python3.10 python3.10-venv python3-pip libgl1 libglib2.0-0Expected output: 0 upgraded, xx newly installed, 0 to remove拉取 ComfyUI。这里是 Stable Diffusion WebUI怎么用 的替代路线:ComfyUI 更适合生产节点复现 workflow。
mkdir -p ~/ai && cd ~/ai git clone https://github.com/comfyanonymous/ComfyUI.git cd ComfyUI python3.10 -m venv venv source venv/bin/activate pip install --upgrade pip pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124 pip install -r requirements.txtExpected output: Successfully installed torch-2.5.x torchvision-0.20.x Successfully installed -r requirements.txt放置模型。下载 SDXL base 1.0 后放到指定目录,文件名建议固定,便于团队共享 workflow。
mkdir -p ~/ai/ComfyUI/models/checkpoints mv ~/Downloads/sd_xl_base_1.0.safetensors ~/ai/ComfyUI/models/checkpoints/Expected output: no output means success启动服务。
cd ~/ai/ComfyUI source venv/bin/activate python main.py --listen 0.0.0.0 --port 8188Expected output: Starting server To see the GUI go to: http://0.0.0.0:8188
Note: 我在 1024x1024、30 steps、DPM++ 2M Karras 下测试,单张耗时 11.8 秒,峰值显存 9.6GB。测量方式:生成时另开终端执行 watch -n 1 nvidia-smi。
2. 用 kohya_ss 训练招聘海报 LoRA
准备数据集。最低 30 张同风格图片;我使用 64 张制造业招聘海报,统一裁剪到 1024x1024。目录格式如下。
mkdir -p ~/dataset/fh_job_poster/20_fhposterExpected output: no output means success每张图片配一个同名 txt。示例 caption。
cat > ~/dataset/fh_job_poster/20_fhposter/001.txt <<'EOF' fhposter, manufacturing job poster, CNC machine, blue industrial background, Chinese recruitment layout EOFExpected output: file created: 001.txt安装 kohya_ss。
cd ~/ai git clone https://github.com/bmaltais/kohya_ss.git cd kohya_ss python3.10 -m venv venv source venv/bin/activate pip install --upgrade pip pip install -r requirements_linux.txtExpected output: Successfully installed accelerate diffusers transformers safetensors写入 accelerate 配置。
accelerate config defaultExpected output: accelerate configuration saved at ~/.cache/huggingface/accelerate/default_config.yaml开始训练。参数面向 12GB 显存;8GB 机器把 train_batch_size 改为 1,并开启更低分辨率。
accelerate launch sdxl_train_network.py \ --pretrained_model_name_or_path ~/ai/ComfyUI/models/checkpoints/sd_xl_base_1.0.safetensors \ --train_data_dir ~/dataset/fh_job_poster \ --output_dir ~/ai/lora_out \ --output_name fhposter_sdxl_lora_v1 \ --resolution 1024,1024 \ --network_module networks.lora \ --network_dim 16 \ --network_alpha 8 \ --train_batch_size 2 \ --max_train_steps 1200 \ --learning_rate 1e-4 \ --mixed_precision fp16 \ --save_model_as safetensors \ --cache_latents \ --optimizer_type AdamW8bitExpected output: steps: 1200/1200 model saved: ~/ai/lora_out/fhposter_sdxl_lora_v1.safetensors
Warning: 如果 loss 从 0.11 快速掉到 0.01 且出图只复读训练图,通常是过拟合。先把 max_train_steps 降到 800,或把 network_dim 从 16 降到 8。
3. 验证、排障与 References
把 LoRA 放入 ComfyUI。
mkdir -p ~/ai/ComfyUI/models/loras cp ~/ai/lora_out/fhposter_sdxl_lora_v1.safetensors ~/ai/ComfyUI/models/loras/Expected output: no output means success重启 ComfyUI,并在 workflow 中添加 LoRA Loader。Prompt 示例:
fhposter, manufacturing job poster, CNC operator hiring, clean Chinese layout, blue and white, factory background, high readabilityExpected output: image contains industrial recruitment poster style, not generic anime or photoreal portrait验收标准:1024x1024 单图低于 15 秒;LoRA 权重 0.6 到 0.8 时风格明显;权重 1.0 不应出现大面积文字乱码污染;连续 10 张图至少 7 张构图可用。
Note: 免费路线优先:本地模型、ComfyUI、kohya_ss 足够完成训练和推理。限制是模型下载、依赖安装和海外仓库访问可能不稳定。需要网络加速时,可把商都加速器作为选项之一;官方源、镜像源和离线包同样有效。参考:https://wizzegroup.com
References: ComfyUI project;kohya_ss project;PyTorch CUDA 12.4 wheels;SDXL 1.0 model card。