Ascend-SACT/FLUX-Kontext-dev
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

模型训练指导

一、模型介绍

FLUX.1-Kontext-dev 是基于FLUX,当前先进的上下文图像生成与编辑技术的生成模型,它可以结合文本与图像,实现精确、连贯的生成效果

二、环境准备

配套版本
torch2.7.1
torch_npu2.7.1
CANN8.5.1

2.1 Mindspeed-MM下载

按如下命令下载对应分支代码到本地:

git clone -b 26.1.0 https://gitcode.com/Ascend/MindSpeed-MM.git

2.2 模型代码下载

按如下下载对应模型代码:

git clone https://github.com/huggingface/diffusers.git
cd diffusers
git checkout c222570
cp -r ../MindSpeed-MM/examples/diffusers/flux-kontext/* ./examples/dreambooth
cp ../MindSpeed-MM/sources/images/flux_kontext.png ./examples/dreambooth

2.3 安装依赖库

pip install -e . 
pip install -r examples/dreambooth/mm_requirements_kontext.txt # 安装对应依赖
pip install torch_npu==2.7.1

三、模型微调

3.1准备微调数据集

import os
 # 在导入 datasets 库之前设置 
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com' 
# 然后是你的正常代码 
from datasets import load_dataset dataset = load_dataset("kontext-community/relighting")

kontext-community/relighting数据集格式如下:

relighting
├── .gitattributes
├── README.md
└── data
      └── train-00000-of-00001.parquet

3.2 下载模型

modelscope download --model black-forest-labs/FLUX.1-Kontext-dev --local_dir ./

3.3微调脚本配置

找到如下脚本finetune_kontext_dreambooth_lora_deepspeed_bf16.sh修改模型名称和数据集路径如下:

model_name="black-forest-labs/FLUX.1-Kontext-dev"
dataset_name="kontext-community/relighting"

3.4修改代码文件

打开train_dreambooth_lora_flux_kontext.py文件:

cd examples/dreambooth/ # 从diffusers目录进入dreambooth目录
vim train_dreambooth_lora_flux_kontext.py # 进入Python文件

在import栏if is_wandb_available():上方添加如下代码

from transformer_patches import apply_patches
apply_patches()

if is_wandb_available(): # 原代码
  import wandb

在train_dataloader前修改batch_sample,将数据集drop_last修改为False

batch_sampler = BucketBatchSampler(train_dataset, batch_size=args.train_batch_size, drop_last=False)

如果为Ubuntu系统需要在如下代码附近添加 accelerator.print("")

if global_step >= args.max_train_steps: # 原代码
  break
accelerator.print("") # 添加

3.5 启动LoRA微调

启动微调脚本:

bash finetune_kontext_dreambooth_lora_deepspeed_bf16.sh

910B 8卡运行效果如下:

Steps:  78%|███████▊  | 39/50 [06:09<01:42,  9.30s/it, loss=1.57, lr=1e-5]
Steps:  80%|████████  | 40/50 [06:18<01:32,  9.28s/it, loss=0.59, lr=1e-5]
Steps:  82%|████████▏ | 41/50 [06:27<01:23,  9.29s/it, loss=3.79, lr=1e-5]
Steps:  84%|████████▍ | 42/50 [06:37<01:14,  9.28s/it, loss=2.77, lr=1e-5]
Steps:  86%|████████▌ | 43/50 [06:46<01:05,  9.29s/it, loss=3.7, lr=1e-5] 
Steps:  88%|████████▊ | 44/50 [06:55<00:55,  9.27s/it, loss=3.41, lr=1e-5]
Steps:  90%|█████████ | 45/50 [07:04<00:46,  9.29s/it, loss=3.67, lr=1e-5]
Steps:  92%|█████████▏| 46/50 [07:14<00:37,  9.27s/it, loss=0.608, lr=1e-5]
Steps:  94%|█████████▍| 47/50 [07:23<00:27,  9.29s/it, loss=1.17, lr=1e-5] 
Steps:  96%|█████████▌| 48/50 [07:32<00:18,  9.27s/it, loss=3.01, lr=1e-5]
Steps:  98%|█████████▊| 49/50 [07:42<00:09,  9.29s/it, loss=0.861, lr=1e-5]
Steps: 100%|██████████| 50/50 [07:51<00:00,  9.27s/it, loss=2.37, lr=1e-5] 

四、微调效果验证

提供了图生图推理脚本infer_kontext_img2img_bf16.py

import torch
from diffusers import FluxKontextInpaintPipeline
from diffusers.utils import load_image
from transformer_patches import apply_patches

apply_patches()

pipe = FluxKontextInpaintPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-Kontext-dev/FLUX.1-dev", torch_dtype=torch.bfloat16
)

pipe.to("npu")
pipe.load_lora_weights(black-forest-labs/lora)

prompt = "Replace this ball"
img_url = "https://images.pexels.com/photos/39362/the-ball-stadion-football-the-pitch-39362.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
mask_url = "https://github.com/ZenAI-Vietnam/Flux-Kontext-pipelines/blob/main/assets/ball_mask.png?raw=true"
image_reference_url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTah3x6OL_ECMBaZ5ZlJJhNsyC-OSMLWAI-xw&s"

source = load_image(img_url)
mask = load_image(mask_url)
image_reference = load_image(image_reference_url)

mask = pipe.mask_processor.blur(mask, blur_factor=12)
image = pipe(
    prompt=prompt, image=source, mask_image=mask, image_reference=image_reference, strength=1.0
).images[0]
image.save("kontext_inpainting_ref.png")

指定模型权重路径

MODEL_PATH = "black-forest-labs/FLUX.1-Kontext-dev"  # Flux 模型路径
LORA_WEIGHTS = "./logs/pytorch_lora_weights.safetensors"  # LoRA权重路径

运行代码

python infer_kontext_img2img_bf16.py