Z-Image ComfyUI LoRA 灯光修复与自动 Prompt 优化完全指南

May 12, 2026

Z-Image ComfyUI LoRA 灯光修复与自动 Prompt 优化完全指南

告别扁平化灯光和 LoRA 加载错误:掌握 Z-Image 在 ComfyUI 中的高级 LoRA 工作流,配合自动 Prompt 优化,让训练成果完美呈现。


你遇到过这些问题吗?

  • 在 ComfyUI 中加载 Z-Image LoRA 时出现 "lora key not found" 错误
  • 生成的图片灯光太平,缺乏立体感和层次
  • LoRA 训练效果在 AI Toolkit 中很好,但在 ComfyUI 中明显下降
  • 不确定 Z-Image 应该用哪种 LoRA 格式(LoRA vs LoRA-LyCORIS vs LoKR)

如果你遇到以上任何一个问题,这篇文章就是为你写的。

问题根源:Z-Image 的架构特殊性

Z-Image 采用 单流扩散 Transformer(Single-Stream Diffusion Transformer) 架构,与传统的 Stable Diffusion 架构有本质区别:

特性 Stable Diffusion XL Z-Image
架构 U-Net + 双编码器 单流 Diffusion Transformer
参数量 ~6.6B ~6B
LoRA 格式 标准 SD LoRA 需要适配
默认 LoRA 节点 直接工作 可能损坏数据

为什么默认 LoRA 节点会出问题?

ComfyUI 的默认 LoRA 加载节点是为 SDXL/SD1.5 设计的,它假设特定的权重分布和层结构。当直接用于 Z-Image 时:

  1. 键名不匹配:Z-Image 的 Transformer 层命名与 SDXL 不同
  2. 注意力头形状不同:单流架构的注意力机制结构差异
  3. 数据破坏:错误的加载方式可能覆盖或损坏基础模型权重

这就是为什么你看到 "lora key not found" 警告,以及灯光效果变平的根本原因。

解决方案一:使用正确的 LoRA 节点

推荐方案:AI Toolkit 原生 LoRA 节点

# 在 ComfyUI 中安装 Ostris AI Toolkit
git clone https://github.com/ostris/ai-toolkit.git custom_nodes/ai-toolkit
pip install -r custom_nodes/ai-toolkit/requirements.txt

AI Toolkit 提供的 LoRA 节点支持 Z-Image 的 Transformer 架构:

# ComfyUI 工作流配置
{
    "loRA_node": "AIT_LoRALoader",  # AI Toolkit 专用节点
    "model": "Tongyi-MAI/Z-Image-Turbo",
    "lora": "/path/to/your/lora.safetensors",
    "strength_model": 0.8,
    "strength_clip": 0.8
}

不推荐:标准 ComfyUI LoRA 节点

❌ LoadLoRA (标准节点) — 可能导致:
   - 部分权重无法加载
   - 灯光效果变平
   - 角色特征丢失

解决方案二:训练时使用正确的格式

训练格式选择

格式 Z-Image 兼容性 推荐度 说明
标准 LoRA ✅ 完全兼容 ⭐⭐⭐⭐⭐ 使用 AI Toolkit 训练
LoRA-LyCORIS ⚠️ 部分兼容 ⭐⭐⭐ 需额外配置
LoKR ❌ 不兼容 避免使用
Full Fine-tune ✅ 完全兼容 ⭐⭐⭐⭐ 文件体积大

AI Toolkit 训练配置(推荐)

# config.yaml — AI Toolkit 训练配置
base_model: Tongyi-MAI/Z-Image-Base
precision: bf16
mixed_precision: bf16
optimizer:
  name: prodigy
  lr: 1.0
  weight_decay: 0.01

network:
  type: loha  # 标准 LoRA 格式,Z-Image 最佳
  dim: 32
  alpha: 16
  modules:
    - .*to_q
    - .*to_k
    - .*to_v
    - .*to_out.0
    - .*ff.net.0
    - .*ff.net.2

dataset:
  directory: ./dataset
  caption_extension: .txt
  shuffle: true
  cache_latents: true

training:
  steps: 2000
  batch_size: 1
  save_every: 500
  save_precision: fp16

关键训练参数

# 灯光效果优化相关参数
training:
  # 保留光影信息
  keep_input_rgb: true
  # 梯度裁剪,防止训练不稳定
  gradient_accumulation_steps: 4
  # 学习率调度
  lr_scheduler: cosine_with_restarts
  lr_warmup_steps: 100

解决方案三:修复扁平化灯光

问题现象

使用错误的 LoRA 加载方式后,生成图片会出现:

  • 阴影缺失或不自然
  • 面部光照过于均匀
  • 物体缺乏立体感
  • 整体色调偏灰

修复方法

方法 A:Prompt 光照增强

在 prompt 中加入光照描述关键词:

# 基础 prompt
portrait of a warrior

# 增强光照 prompt
cinematic portrait of a warrior, dramatic side lighting, 
rim light, volumetric fog, golden hour, 
studio lighting setup, soft shadows, depth of field

方法 B:ComfyUI 光照增强工作流

[Checkpoint] → [AI Toolkit LoRA] → [KSampler] → [VAE Decode]
                                    ↓
                          [CLIP Set Last Layer] (设置为 20)
                                    ↓
                          [CLIP Vision Encode] (参考图光照)

CLIP Set Last Layer 节点设置为 20 可以显著增强光照效果,这是 Z-Image 特有的优化技巧。

方法 C:使用 ControlNet 光照控制

# ControlNet 光照参考工作流
from comfyui_nodes import ControlNetApply

controlnet = ControlNetApply(
    controlnet_model="light-controlnet-v1",
    strength=0.6,
    start_percent=0.0,
    end_percent=1.0,
    condition_image=light_reference_image
)

光照关键词速查表

光照类型 英文关键词 适用场景
侧光 side lighting, dramatic shadows 肖像、戏剧性效果
轮廓光 rim light, backlight 突出主体轮廓
柔光 soft light, diffused lighting, beauty dish 人像、产品
体积光 volumetric lighting, god rays, light rays 氛围感、电影感
黄金时段 golden hour, warm sunset light 自然风光、温暖色调
工作室灯光 studio lighting, three-point lighting 专业肖像、产品

解决方案四:自动 Prompt 优化

什么是自动 Prompt 优化?

手动编写高质量 prompt 既耗时又需要经验。自动 Prompt 优化技术通过分析你的 LoRA 训练数据,自动生成最优 prompt 模板。

方案 A:ComfyUI 自动 Prompt 节点

# 安装 Auto Prompt 节点
git clone https://github.com/jonstreeter/comfyui-Lora-Tag-Power-Loader.git

工作原理

  1. 分析 LoRA 文件中的训练标签
  2. 生成基础 prompt 模板
  3. 根据 Z-Image 特性优化关键词
  4. 自动添加光照、风格、质量增强词

方案 B:LLM 辅助 Prompt 生成

import requests

def optimize_prompt(base_prompt, style="photorealistic"):
    """使用 LLM 优化 Z-Image prompt"""
    system_prompt = """You are a Z-Image prompt optimization expert. 
    Given a base prompt, enhance it with:
    1. Lighting descriptors (golden hour, side lighting, etc.)
    2. Quality boosters (8k, ultra-detailed, photorealistic)
    3. Camera settings (35mm, f/1.8, shallow depth of field)
    4. Style modifiers matching the requested style
    
    Keep the enhanced prompt under 75 words."""
    
    response = requests.post(
        "http://localhost:8000/v1/chat/completions",
        json={
            "model": "qwen-7b",
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": f"Style: {style}\nBase prompt: {base_prompt}"}
            ]
        }
    )
    return response.json()["choices"][0]["message"]["content"]

# 使用示例
base = "a woman reading a book in a library"
optimized = optimize_prompt(base, style="cinematic")
# 输出: "A beautiful woman reading a vintage book in a grand library, 
#        warm golden hour light streaming through tall arched windows, 
#        dust particles floating in volumetric light beams, 
#        cinematic 35mm shot, f/1.8, ultra-detailed, photorealistic"

方案 C:Z-Image 专用 Prompt 模板

# prompts.yaml — Z-Image Prompt 模板库
portrait:
  template: |
    {quality}, portrait of {subject}, {lighting}, 
    {camera}, {background}, {mood}
  quality: "8k resolution, ultra-detailed, photorealistic, masterpiece"
  lighting: "cinematic lighting, dramatic side light, rim light"
  camera: "shot on Canon EOS R5, 85mm f/1.4, shallow depth of field"
  background: "bokeh background, elegant studio backdrop"
  mood: "moody atmosphere, golden hour warmth"

product:
  template: |
    {quality}, product photography of {subject}, {lighting},
    {camera}, {background}
  quality: "commercial photography, 8k, ultra-sharp focus"
  lighting: "studio lighting, softbox, clean white background"
  camera: "Canon EOS R3, 100mm macro, f/8"
  background: "pure white seamless background"

landscape:
  template: |
    {quality}, {scene}, {lighting}, {camera}, {mood}
  quality: "national geographic quality, ultra HD, ultra-detailed"
  scene: "breathtaking landscape with dramatic sky"
  lighting: "golden hour, volumetric fog, dramatic clouds"
  camera: "wide angle shot, tilt-shift effect"
  mood: "epic, majestic, awe-inspiring"

完整工作流:从训练到生成

第一步:训练 LoRA

# 使用 AI Toolkit 训练 Z-Image 兼容 LoRA
python train.py --config config.yaml --base_model Tongyi-MAI/Z-Image-Base

第二步:在 ComfyUI 中加载

1. 使用 AI Toolkit LoRA 节点(非标准 LoRA 节点)
2. 设置 strength_model = 0.6-0.8(过高会破坏基础模型)
3. 设置 CLIP Set Last Layer = 20(增强光照)
4. 添加光照关键词到 prompt

第三步:验证和调优

1. 生成测试图片(同一 prompt,不同 LoRA strength)
2. 检查灯光效果是否自然
3. 调整 strength_model 和 strength_clip
4. 如有必要,微调 prompt 中的光照关键词

常见问题

Q: 为什么我的 LoRA 在 AI Toolkit 中效果好但在 ComfyUI 中差?

A: 最可能的原因是使用了标准的 ComfyUI LoRA 节点而不是 AI Toolkit 专用节点。切换到 AIT_LoRALoader 并设置 CLIP Set Last Layer = 20 通常能解决问题。

Q: LoKR 格式不能用吗?

A: Z-Image 目前不支持 LoKR 格式(ComfyUI Issue #10973)。训练 LoRA 时请使用标准 LoRA 或 LoHA 格式。

Q: 推荐的 LoRA 训练步数是多少?

A:

  • 角色/风格 LoRA:1000-3000 步
  • 物体/场景 LoRA:500-1500 步
  • 使用 Prodigy 优化器时,学习率设为 1.0

Q: 如何解决 "lora key not found" 错误?

A:

  1. 确保 LoRA 是用 Z-Image Base 模型训练的
  2. 使用 AI Toolkit 的 LoRA 节点
  3. 检查 LoRA 是否为标准格式(非 LoKR)
  4. 更新 ComfyUI 到最新版本

总结

Z-Image 在 ComfyUI 中的 LoRA 工作流有特殊的注意事项,但只要遵循以下原则,就能获得最佳效果:

  1. 使用 AI Toolkit LoRA 节点(非标准节点)
  2. 训练时使用标准 LoRA/LoHA 格式(非 LoKR)
  3. 设置 CLIP Last Layer = 20 增强光照
  4. 在 prompt 中添加光照关键词
  5. 使用自动 Prompt 优化工具减少手动编写

这些技巧让你的 Z-Image LoRA 训练成果在 ComfyUI 中完美呈现,灯光自然、细节丰富、质量一致。

Z-Image Team