🤖 Ubuntu 24.04 上用 ComfyUI 部署 SDXL 并训练岗位海报 LoRA:可复现流程

首页 › Ubuntu 24.04 上用 ComfyUI 部署 SDXL 并训练岗位海报
Roxi
Roxi 加速器 — 稳定·快速·安全
全球节点覆盖,支持所有主流平台,一键连接无需配置。新用户免费试用。
立即体验 →

TL;DR 与前置条件

2020行业萌芽2021快速增长2022竞争加剧2023洗牌整合2024成熟稳定

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 推理

  1. 确认 GPU 可见。

    nvidia-smi
    Expected output:
    Driver Version: 550.120
    CUDA Version: 12.4
    GPU Name: NVIDIA GeForce RTX 4070
    Memory-Usage: 420MiB / 12282MiB
  2. 安装系统依赖。

    sudo apt update && sudo apt install -y git python3.10 python3.10-venv python3-pip libgl1 libglib2.0-0
    Expected output:
    0 upgraded, xx newly installed, 0 to remove
  3. 拉取 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.txt
    Expected output:
    Successfully installed torch-2.5.x torchvision-0.20.x
    Successfully installed -r requirements.txt
  4. 放置模型。下载 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
  5. 启动服务。

    cd ~/ai/ComfyUI
    source venv/bin/activate
    python main.py --listen 0.0.0.0 --port 8188
    Expected 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

🚀STEP 1选择模型📊STEP 2准备数据💡STEP 3调试优化⚙️STEP 4落地应用
  1. 准备数据集。最低 30 张同风格图片;我使用 64 张制造业招聘海报,统一裁剪到 1024x1024。目录格式如下。

    mkdir -p ~/dataset/fh_job_poster/20_fhposter
    Expected output:
    no output means success
  2. 每张图片配一个同名 txt。示例 caption。

    cat > ~/dataset/fh_job_poster/20_fhposter/001.txt <<'EOF'
    fhposter, manufacturing job poster, CNC machine, blue industrial background, Chinese recruitment layout
    EOF
    Expected output:
    file created: 001.txt
  3. 安装 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.txt
    Expected output:
    Successfully installed accelerate diffusers transformers safetensors
  4. 写入 accelerate 配置。

    accelerate config default
    Expected output:
    accelerate configuration saved at ~/.cache/huggingface/accelerate/default_config.yaml
  5. 开始训练。参数面向 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 AdamW8bit
    Expected 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

  1. 把 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
  2. 重启 ComfyUI,并在 workflow 中添加 LoRA Loader。Prompt 示例:

    fhposter, manufacturing job poster, CNC operator hiring, clean Chinese layout, blue and white, factory background, high readability
    Expected output:
    image contains industrial recruitment poster style, not generic anime or photoreal portrait
  3. 验收标准: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。

⬅ 上一篇Notion AI与Microsoft Copilot办公自动化实测:招聘、会议 下一篇 ➡Runway Gen-3 与 Pika 1.5 实测对比:岗位宣传短视频生成流程

🎯 猜你喜欢

本地部署RTX 4060 8GB 跑 SDXL Turbo 与 LoRA 微调本地部署ComfyUI离线部署Stable Diffusion与DreamBo本地部署SDXL本地推理与LoRA小样本微调实操:显存、命令、验证基线(202本地部署Stable Diffusion 生产机部署与 LoRA 小样本微调验本地部署RTX 3060/4090 上部署 Stable Diffusion 本地部署Stable Diffusion 本地部署排障与 LoRA 微调最小闭本地部署Stable Diffusion本地部署与模型微调:2025离线安装、本地部署Stable Diffusion本地部署与模型微调:Windows/L

🏷️ 热门标签

Gemini国内使用Google AI怎么用Gemini怎么注册AI论文写作辅助GPT-4o怎么用ChatGPT怎么用Cursor下载AI生产力工具LM Studio教程DeepL下载Midjourney怎么用Ollama下载Gemini API教程学术诚信边界ChatGPT提示词工程Claude长文本分析Google翻译怎么用Zapier教程Roxi加速器Notion AI怎么用
延伸阅读