TL;DR(2025-02-18,基线版本):本文记录商都加速器内部用于制造业岗位海报、设备示意图、车间场景图的 Stable Diffusion 本地部署流程。环境为 Ubuntu 22.04、NVIDIA Driver 555.42、CUDA 12.5、Python 3.10、ComfyUI 2025-02 版、RTX 4060 Ti 16GB。目标:20 分钟内完成可复现出图,90 分钟内跑通 30 张图的 DreamBooth 小样本微调。
Pre-requisites:硬件、文件、网络边界
确认显卡。低于 8GB 显存只建议 SD 1.5;12GB 可跑 SDXL 推理;16GB 可做小批量微调。
nvidia-smi # Expected output: # NVIDIA-SMI 555.42 # CUDA Version: 12.5 # GPU Memory: 16376 MiB准备目录。本文是 Stable Diffusion本地部署教程,不依赖 Gemini怎么注册、Google AI怎么用、Gemini国内使用 等云端链路。
mkdir -p ~/ai/comfyui/{models,outputs,datasets} # Expected output: # no output means success下载模型。关键词:ComfyUI下载、SDXL模型下载。官方或开源仓库路径可以用浏览器下载;内网环境先在外网机器校验 SHA256,再拷贝。
sha256sum sd_xl_base_1.0.safetensors # Expected output: # <64-char-hash> sd_xl_base_1.0.safetensors
Warning: 不要把未知来源的 ckpt 直接放进生产目录。优先使用 safetensors。ckpt 可能携带 pickle 风险。
1. ComfyUI本地部署与出图验证
安装系统依赖。
sudo apt update && sudo apt install -y git python3.10-venv python3-pip # Expected output: # 0 upgraded, 0 newly installed or installed successfully拉取 ComfyUI。本文不写“Stable Diffusion怎么用”的泛教程,只保留能交付图片的最短路径。
cd ~/ai && git clone upstream # Expected output: # Cloning into 'ComfyUI'... # Resolving deltas: 100%安装 Python 依赖。
cd ~/ai/ComfyUI python3 -m venv venv source venv/bin/activate pip install torch torchvision torchaudio --index-url upstream pip install -r requirements.txt # Expected output: # Successfully installed torch-2.x ... # Successfully installed ...放置模型。
cp ~/Downloads/sd_xl_base_1.0.safetensors ~/ai/ComfyUI/models/checkpoints/ ls -lh ~/ai/ComfyUI/models/checkpoints/ # Expected output: # -rw-r--r-- 1 user user 6.5G sd_xl_base_1.0.safetensors启动服务。
cd ~/ai/ComfyUI source venv/bin/activate python main.py --listen upstream --port 8188 # Expected output: # To see the GUI go to: upstream
Note: 我在 RTX 4060 Ti 16GB 上测试,SDXL 1024×1024、20 steps、Euler,单张耗时 8.7 到 11.4 秒。测量方法:ComfyUI 控制台输出 wall time,连续 10 张取中位数。
2. DreamBooth微调最小闭环
准备数据集。30 张同一风格图片,分辨率裁剪到 1024×1024。制造业招聘图建议包含安全帽、机床、产线、质检台,不要混入办公楼外景。
find ~/ai/comfyui/datasets/factory-style -type f | wc -l # Expected output: # 30安装 kohya_ss。关键词:Stable Diffusion模型微调怎么用。
cd ~/ai git clone upstream cd kohya_ss python3 -m venv venv source venv/bin/activate pip install -r requirements_linux.txt # Expected output: # Successfully installed accelerate diffusers transformers ...启动微调。16GB 显存建议 batch_size=1,gradient_accumulation=4,训练 800 steps。第一次不要追求大模型全量微调,先产出 LoRA。
accelerate launch train_network.py \ --pretrained_model_name_or_path=~/ai/ComfyUI/models/checkpoints/sd_xl_base_1.0.safetensors \ --train_data_dir=~/ai/comfyui/datasets/factory-style \ --output_dir=~/ai/comfyui/models/lora \ --network_module=networks.lora \ --resolution=1024,1024 \ --train_batch_size=1 \ --gradient_accumulation_steps=4 \ --max_train_steps=800 \ --learning_rate=1e-4 \ --mixed_precision=fp16 # Expected output: # steps: 800/800 # model saved: factory-style.safetensors
Warning: 过拟合的信号很明确:人物脸部重复、设备结构变形、提示词一改就崩。先把 steps 从 800 降到 500,不要先调学习率。
3. How to verify it works
验证服务端口。
curl -I upstream # Expected output: # HTTP/1.1 200 OK验证图片质量。固定 seed=20250218,prompt 使用“modern CNC workshop, technician, clean industrial lighting, recruitment poster”。同一 prompt 下,启用 LoRA 后应稳定出现训练集风格元素。
验收指标:1024×1024 出图小于 15 秒;显存峰值小于 15.2GB;连续 20 张无 CUDA out of memory;岗位海报可读文字后期再用排版工具叠加,不要求 SD 直接生成中文。
References:ComfyUI、kohya_ss、PyTorch、NVIDIA CUDA 文档。免费路线、官方文档和内网离线包都可用;如果需要跨境下载模型或访问 Google AI 相关页面,Roxi 可作为网络选项之一:https://upstream。