demo1984s 的个人博客 demo1984s 的个人博客

记录精彩的程序人生

目录
Flux Gym:低显存训练FLUX LoRA的福音
/      

Flux Gym:低显存训练FLUX LoRA的福音

  本文内容结合 Flux Gym 项目 https://github.com/cocktailpeanut/fluxgymREADME.md 英文文档的翻译,以及实践总结完成。

  附上实践训练过程中生成的猫猫图,文末提供了模型文件下载:

image.png

一、简介

  Flux Gym:简单易用的 FLUX LoRA 训练 UI 工具,低 VRAM (显存)即可运行,支持 12GB/16GB/20GB 几种规格。

AI-Toolkit 项目本身需要具备 24GB VRM 才可运行。
Kohya Scripts 项目本身只适用于在终端运行。

  FluxGym 通过默认隐藏的 Advanced 选项卡,支持 Kohya sd-scripts 的所有功能。

screenshot.png

图片出处 https://github.com/cocktailpeanut/fluxgym/blob/main/screenshot.png

二、模型

  1. Flux1-dev
  2. flux1-dev2pro(如此处所述:https://medium.com/@zhiwangshi28/why-flux-lora-so-hard-to-train-and-how-to-overcome-it-a0c70bc59eaf))
  3. Flux1-schnell (无法获得高质量的结果,所以不推荐,但请随意尝试)
  4. 待续

  在您选好了模型并开始训练时,系统会自动下载选中的相关模型。另外,您还可以通过编辑 models.yaml 文件轻松地将您感兴趣的模型添加到支持列表中。

 1# Add your own model here
 2# <name that will show up on the dropdown>:
 3#   repo: <the huggingface repo ID to pull from>
 4#   base: <the model used to run inference with (The Huggingface "Inference API" widget will use this to generate demo images)>
 5#   license: <follow the other examples. Any model inherited from DEV should use the dev license, schenll is apache-2.0>
 6#   license_name: <follow the other examples. only needed for dev inherited models>
 7#   license_link: <follow the other examples. only needed for dev inherited models>
 8#   file: <the file name within the huggingface repo>
 9flux-dev:
10    repo: cocktailpeanut/xulf-dev
11    base: black-forest-labs/FLUX.1-dev
12    license: other
13    license_name: flux-1-dev-non-commercial-license
14    license_link: https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md
15    file: flux1-dev.sft
16flux-schnell:
17    repo: black-forest-labs/FLUX.1-schnell
18    base: black-forest-labs/FLUX.1-schnell
19    license: apache-2.0
20    file: flux1-schnell.safetensors
21bdsqlsz/flux1-dev2pro-single:
22    repo: bdsqlsz/flux1-dev2pro-single
23    base: black-forest-labs/FLUX.1-dev
24    license: other
25    license_name: flux-1-dev-non-commercial-license
26    license_link: https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md
27    file: flux1-dev2pro.safetensors
28~                                                     
29~     
30~                           
31"autodl-tmp/fluxgym/fluxgym/models.yaml" 27L, 1325B

  如果您电脑上已经有现成模型的话,也可以将模型放到 models 目录下对应的子目录里(以 FLUX.1-dev 为例):

1/fluxgym
2  /models
3      /clip
4          clip_l.safetensors
5          t5xxl_fp16.safetensors
6      /unet
7          flux1-dev.sft
8      /vae
9          ae.sft
  • flux1-dev.sft 是旧的后缀名命名方式,可以将 flux1-dev.safetensors 改名为 flux1-dev.sftLinux/Mac 用户也可在 unet 目录下运行 ln -s /path/to/flux1-dev.safetensors flux1-dev.sft 命令做个软链接。
  • ae.sft 同理。
  • clipunetvae 模型使用的更多代码细节可打开 app.py 文件查看。
  • 模型文件百度网盘下载链接 https://pan.baidu.com/s/1nMwi4JPlaXiElsEo6lFvsg?pwd=7g96

三、安装

(一)一键安装

  您可以使用 Pinokio 一键式启动器在本地自动安装和启动所有内容:https://pinokio.computer/item?uri=https://github.com/cocktailpeanut/fluxgym

(二)手动安装

  首先将 Fluxgymkohya-ss/sd-scripts 两个项目克隆到本地:

1git clone https://github.com/cocktailpeanut/fluxgym
2cd fluxgym
3git clone -b sd3 https://github.com/kohya-ss/sd-scripts

  克隆完成后您的文件夹结构会是这样的:

1/fluxgym
2  app.py
3  requirements.txt
4  /sd-scripts

  在根目录 fluxgym 下创建并激活一个 Python 虚拟环境 venv

Windows 用户在命令行窗口分别运行以下两行命令:

1python -m venv env
2env\Scripts\activate

Linux 用户在终端窗口分别运行以下两行命令:

1python -m venv env
2source env/bin/activate

  您会发现 fluxgym 目录下新创建了一个子目录 env

1/fluxgym
2  app.py
3  requirements.txt
4  /sd-scripts
5  /env

  接下来要进入到目录 sd-scripts 里将项目依赖项安装到当前环境中:

1cd sd-scripts
2pip install -r requirements.txt

  接着返回到根目录并安装应用程序依赖项:

1cd ..
2pip install -r requirements.txt

  最后,将 pytorch 的 Nightly 版本安装上:

1pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

四、使用

(一)启动应用

手动安装的情况

  返回到根目录 fluxgym,运行以下命令启动应用:

1python app.py

要确保启动应用前已激活了虚拟环境:
Windows:env/Scripts/activate
Linux:source env/bin/activate

Docker 的方式运行

  将 Fluxgymkohya-ss/sd-scripts 两个项目克隆到本地:

1git clone https://github.com/cocktailpeanut/fluxgym
2cd fluxgym
3git clone -b sd3 https://github.com/kohya-ss/sd-scripts

  直接运行以下命令:

1docker compose up -d --build

  打开 Web 浏览器即可访问:http://localhost:7860

image.png

(二)模型训练

  用法非常简单直接,按界面上的 3 步走基本就能满足。

Step 1. LoRA Info

  • The name of your LoRALoRA 名称,必填。
  • Trigger word/sentence:触发词,选填。
  • Base model (edit the models.yaml file to add more to this list):选择使用的模型,必选。
  • VRAM:选择训练机器的显存规格,必选。
  • Repeat trains per image:每张图片的训练次数,默认值为 10。
  • Max Train Epochs:图片训练的最大轮数,默认值为 16。
  • Expected training steps:取值为 图片数量 * Repeat trains per image * Max Train Epochs,上传完图片后会自动计算填充,必填。
  • Sample Image Prompts (Separate with new lines):训练过程中自动生成样例图的提示词,如果生成多张就要写多条提示词,每条提示词要新起一行。在每条提示词后加上 --d 来设置固定的种子参数值,还可以直观的观察 LoRA 训练过程中的演变。选填。
  • Sample Image Every N Steps:假如 Expected training steps640Sample Image Every N Steps 设置为 80,则在训练过程中分别会在第 80160240320400480560640 步的时候为每条提示词生成对应的样例图像。结合 Sample Image Prompts (Separate with new lines) 使用。
  • Resize dataset images:自动将上传的图片裁剪成这里设置的大小,默认值为 512。

提示词支持使用以下参数:

  • --n:指定反面提示词。
  • --w:指定生成的图像的宽度。
  • --h:指定生成的图像的高度。
  • --d:指定生成的图像的种子。
  • --l:指定生成图像的 CFG 值。
  • --s:指定生成中的步数。

提示词权重 ( )[ ] 也可正常使用。(点击 https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#attentionemphasis 了解更多关于注意力/强调的信息)

Step 2. Dataset

  1. 上传图片,假设在 Step 1. LoRA InfoThe name of your LoRA 填的是 MyLoRA,那么上传的图片会保存在目录 fluxgym/datasets/MyLoRA 下。
  2. 点击 Add AI captions with Florence-2 自动根据图片生成提示词填充到图片右侧的文本框,不需要的特征可以删掉。第一次运行的时候会需要联网自动下载 Florence-2,本文涉及需要自动下载的地方都需要先 打开魔法

Step 3. Train

  1. 点击 Start training 开始训练,在目录 fluxgym/datasets/MyLoRA 下会根据 Step 2. Dataset 里的图片生成的同名提示词文件,后缀是 .txt
  2. 训练过程中在目录 fluxgym/outputs/MyLoRA 下会生成训练相关的文件及模型。如果在 Step 1. LoRA Info 配置了在训练过程中生成样例图片的提示词,那么指定步数生成的样例图片会保存在目录 fluxgym/outputs/MyLoRA/sample 里。

Advanced options

  高级参数配置暂未实际体验。

五、下载

模型文件

百度网盘下载链接:
https://pan.baidu.com/s/1nMwi4JPlaXiElsEo6lFvsg?pwd=7g96


标题:Flux Gym:低显存训练FLUX LoRA的福音
作者:demo1984s
地址:http://www.demo1984s.com/articles/2025/01/11/1736528633416.html