在云 GPU 上训练您自己的 Z-Image Turbo LoRA

发布: (2026年2月1日 GMT+8 17:20)
4 分钟阅读
原文: Dev.to

I’m sorry, but I can’t access external links. Could you please paste the text you’d like translated here? Once you provide the content, I’ll translate it into Simplified Chinese while preserving the formatting and code blocks.

您需要的准备

  • 拥有 GPU 实例访问权限的 AWS 账户
  • 用于 EC2 访问的 SSH 密钥对
  • 6–15 张高质量的主体图片(建议 1024×1024),并附带说明文字
  • 大约 2 小时的设置和训练时间

设置 EC2 实例

  1. 选择实例 – 例如 g6e.2xlarge(48 GB VRAM)。
  2. 配置安全组 – 打开入站端口 8675888822
  3. 选择 AMIDeep Learning OSS Nvidia Driver AMI GPU PyTorch 2.9 (Ubuntu 22.04),其中已包含 CUDA 和驱动。
  4. 分配存储 – 至少 100 GB 用于模型、数据集和检查点。

SSH 登录实例

ssh -i your-key.pem ubuntu@YOUR_PUBLIC_IP

安装系统软件包和 AI 工具包

# Update system
sudo apt update && sudo apt upgrade -y

# Install basics
sudo apt install -y git build-essential python3-pip python3-venv
# Clone the repo
cd ~
git clone https://github.com/ostris/ai-toolkit.git
cd ai-toolkit
git submodule update --init --recursive

# Set up a virtual environment
python3 -m venv venv
source venv/bin/activate

# Install PyTorch (CUDA 12.6) and other requirements
pip3 install --no-cache-dir torch==2.7.0 torchvision==0.22.0 torchaudio==2.7.0 \
    --index-url https://download.pytorch.org/whl/cu126
pip3 install -r requirements.txt

安装 Node.js(用于 UI)

# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# Install Node.js 23 (or any 18+ version)
nvm install 23

# Verify
node -v   # should show v23.x.x
npm -v

运行 Toolkit UI

cd ~/ai-toolkit
source venv/bin/activate
cd ui
npm run build_and_start

UI 在 端口 8675 上运行。请在本地机器上创建 SSH 隧道:

ssh -i your-key.pem -L 8675:localhost:8675 -N ubuntu@YOUR_PUBLIC_IP

然后在浏览器中打开 http://localhost:8675

设置 Jupyter Notebook

cd ~/ai-toolkit
source venv/bin/activate

pip install jupyter jupyterlab ipykernel
python -m ipykernel install --user --name=ai-toolkit --display-name="AI Toolkit (Python)"

jupyter notebook --generate-config
jupyter notebook password   # set a password and remember it

编辑 ~/.jupyter/jupyter_notebook_config.py 并添加:

c.NotebookApp.ip = 'localhost'
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False
c.NotebookApp.allow_remote_access = True
c.NotebookApp.notebook_dir = '/home/ubuntu/ai-toolkit'

启动 Jupyter 并转发端口:

jupyter notebook --no-browser --port=8888

在本地机器上:

ssh -i your-key.pem -L 8888:localhost:8888 -N ubuntu@YOUR_PUBLIC_IP

http://localhost:8888 访问 Jupyter,并使用您设置的密码登录。

在 UI 中创建数据集

  1. 在 AI Toolkit UI 中,转到 Datasets → New Dataset
  2. 为其命名(例如 MySubject)。
  3. 上传 6–15 张图片。
  4. 添加包含唯一触发词的标题(例如 samt)。
    • 标题格式:[TRIGGER_WORD], a man with wavy hair …

提示

  • 使用非词典中的触发词(例如 sktsamt)。
  • 保持图像质量高;姿势和光线要多样,但主体保持一致。

创建训练作业

SettingValue
Training Nameyour_training_name
Trigger Wordyour_trigger_word
Model ArchitectureZ-Image Turbo (w/ Training Adapter)
Low VRAM已禁用
TransformerNONE
Cache Text Embeddings已启用
Advanced – Do Differential Guidance已启用,设置为 3

测试已训练的 LoRA

在 Jupyter Notebook 中运行以下代码(根据需要替换路径和触发词):

import torch
from diffusers import ZImagePipeline

device = "cuda"
dtype = torch.bfloat16  # 必须是 bfloat16,不能是 float16!

print("Loading Z-Image Turbo pipeline...")
pipe = ZImagePipeline.from_pretrained(
    "Tongyi-MAI/Z-Image-Turbo",
    torch_dtype=dtype,
    low_cpu_mem_usage=False,
)
pipe.to(device)
print("Pipeline loaded!")

# Load your trained LoRA
lora_path = "/home/ubuntu/ai-toolkit/output/my_first_lora_v1/my_first_lora_v1.safetensors"
print(f"Loading LoRA from: {lora_path}")
pipe.load_lora_weights(lora_path)
print("LoRA loaded!")

# Generate an image
prompt = "YOUR_TRIGGER_WORD, posing in front of the Eiffel Tower"
generator = torch.Generator(device).manual_seed(42)

print("Generating image...")
image = pipe(
    prompt=prompt,
    height=1024,
    width=1024,
    num_inference_steps=9,   # 8 DiT forwards
    guidance_scale=0.0,       # Must be 0.0 for Turbo inference
    generator=generator,
).images[0]

image.save("output.png")
print("Done!")

# Display in Jupyter
from IPython.display import display
display(image)

YOUR_TRIGGER_WORD 替换为你在字幕中使用的词。运行 notebook 后,你应该能够得到一个自定义的 Z‑Image Turbo LoRA,能够在不到一秒的时间内生成主体的逼真图像。

Back to Blog

相关文章

阅读更多 »