e
gcw_GSiqzzLf/flux-2-klein-4B-outpaint-lora-npu
模型介绍文件和版本Pull Requests讨论分析

FLUX.2-klein-4B Outpaint LoRA - NPU

概述

本仓库提供 FLUX.2-klein-4B Outpaint LoRA 模型在华为昇腾 Ascend910 NPU 上的适配与推理方案。该 LoRA 用于扩展图像边界,填充图像周围的绿色边框区域。

  • 原始模型: fal/flux-2-klein-4B-outpaint-lora
  • 基础模型: black-forest-labs/FLUX.2-klein-4B
  • 任务类型: 图像编辑 / 图像外延(Outpainting)
  • 模型框架: PyTorch + Diffusers
  • 输入格式: RGB 图像(带绿色边框)
  • 输出格式: RGB 图像(扩展后的图像)
  • LoRA 权重: ~76MB(safetensors 格式)
  • 基础模型: ~23GB
  • 许可证: Apache 2.0

环境要求

组件版本
Python3.11+
PyTorch>= 2.0.0
torch_npu>= 2.9.0
Diffusers>= 0.38.0
Ascend NPUAscend910

NPU 适配说明

使用 Flux2KleinPipeline + load_lora_weights() 加载。输入图像需要包含绿色边框区域,模型会自动填充这些区域以扩展图像内容。

快速开始

python3 inference.py --device npu:0 --steps 20 --seed 42 --height 512 --width 512
python3 compare_cpu_npu.py --steps 4 --seed 42 --height 128 --width 128

推理结果

NPU 推理性能(512x512,20步)

指标值
推理耗时3.89 秒
设备Ascend910

CPU vs NPU 性能对比

指标CPUNPU加速比
推理耗时490.23 秒0.76 秒644x

CPU/NPU 精度测试

测试条件:128x128 输入,4步推理,seed=42,相同初始潜变量

指标值
MAE(0-255)0.8202
最大绝对误差59.0000
相对误差0.3216%
RMSE2.2610
PSNR41.04 dB
SSIM0.999292
像素一致率(<1%)97.79%

结论:NPU 与 CPU 推理结果误差为 0.3216%,小于 1%。

核心推理代码

import torch, torch_npu
from diffusers import Flux2KleinPipeline
from PIL import Image

pipe = Flux2KleinPipeline.from_pretrained(
    "black-forest-labs/FLUX.2-klein-4B", torch_dtype=torch.bfloat16
)
pipe.load_lora_weights("flux-outpaint-lora.safetensors")
pipe = pipe.to("npu:0")

input_img = Image.open("input.jpg").convert("RGB").resize((512, 512))
generator = torch.Generator(device="cpu").manual_seed(42)

with torch.no_grad():
    output = pipe(
        image=input_img,
        prompt="Fill the green spaces according to the image",
        height=512, width=512,
        num_inference_steps=20,
        guidance_scale=3.5,
        generator=generator,
    )
output.images[0].save("output.png")

推理成功证据

本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:

# NPU 推理
python3 inference.py --device npu

# CPU 推理
python3 inference.py --device cpu

推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。

模型标签

#+NPU #+CV #+图像编辑 #+图像外延 #+扩散模型 #+FLUX #+LoRA #+昇腾

下载使用量0