g
gcw_coj3XaOd/infinity1096_UFM-Refine-980
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

UFM-Refine-980 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: infinity1096/UFM-Refine-980

模型链接: HuggingFace

AtomGit 镜像: AtomGit

模型描述: UFM (UniFlowMatch) 是一个统一稠密对应模型,端到端预测像素位移场,同时处理光流估计和宽基线匹配任务。UFM-Refine-980 是高分辨率(980p)细化模型。

模型架构: DINOv2 ViT-Large + DPT (Dense Prediction Transformer) + Global Attention + Patch MLP Refinement

参数规模: ~477M (477,760,540)

输入规格:

  • 图像对 (source, target): RGB, BCHW format
  • 分辨率: 980×644 (W×H) [可配置]
  • 数据类型: float32 [0, 1] 或 uint8 [0, 255]

输出规格:

  • Flow: (B, 2, H, W) 2通道像素位移场
  • Covisibility: (B, H, W) 1通道共视掩码 [0, 1]

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
safetensors>= 0.4.0安全权重加载
numpy>= 1.24.0数组计算
Pillow>= 9.0.0图像处理
opencv-python-headless>= 4.8.0光流可视化
flow_vis>= 1.0.0光流颜色映射(可选)
uniflowmatch源码UFM模型实现 (见下方说明)
uniception源码视觉基础模型库 (见下方说明)
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

# 1. 安装 PyPI 依赖
pip install torch torch_npu safetensors numpy Pillow opencv-python-headless flow_vis

# 2. 安装 uniception (UniCeption 视觉基础模型库)
git clone https://github.com/cv-rits/UniCeption.git
cd UniCeption && pip install -e .

# 3. 克隆 UFM 模型源码到推理目录
git clone https://github.com/UniFlowMatch/UFM.git

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download infinity1096/UFM-Refine-980 --local-dir ./weights

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

# 使用 Python 下载(huggingface-cli 版本兼容问题时)
python3 -c "
import urllib.request, os
url = 'https://ai.gitcode.com/hf_mirrors/infinity1096/UFM-Refine-980/resolve/main/model.safetensors'
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req, timeout=300) as resp:
    with open('./weights/model.safetensors', 'wb') as f:
        f.write(resp.read())
"

3.3 运行推理

# 确保 uniflowmatch 源码在推理脚本同目录下
# 目录结构:
#   inference.py          # 推理脚本
#   uniflowmatch/         # UFM 模型源码
#   weights/              # 模型权重
#     config.json
#     model.safetensors

# 单对图像推理
python3 inference.py \
  --model-path ./weights \
  --source image1.jpg \
  --target image2.jpg \
  --output-dir ./results

# 指定分辨率推理
python3 inference.py \
  --model-path ./weights \
  --source image1.jpg \
  --target image2.jpg \
  --resolution 640x420 \
  --output-dir ./results

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型权重目录路径
--source/-sstr必填源图像路径
--target/-tstr必填目标图像路径
--output-dir/-ostr./results输出目录
--resolutionstr980x644推理分辨率 (WxH)

4. 推理成功日志

4.1 单条推理日志

[设备] Ascend910_9362 (npu:0)
[模型] UniFlowMatchClassificationRefinement
[配置] encoder=dinov2, head=dpt
[分辨率] 980x644
[权重] 已加载 670 个 tensor
[参数量] 477,760,540

[输入] 源图像: test_source.jpg
[输入] 目标图像: test_target.jpg
[图像] Source: torch.Size([1, 3, 644, 980]), Target: torch.Size([1, 3, 644, 980])

[推理] 开始推理...
[结果] Flow shape: torch.Size([1, 2, 644, 980])
[结果] Covisibility shape: torch.Size([1, 644, 980])
[结果] Flow range: [3.5683, 13.5002]
[结果] Covisibility range: [0.0623, 0.9997]
[耗时] 0.930s

[统计] Flow平均绝对值: 7.9892
[统计] Covisibility平均值: 0.9430

[状态] SUCCESS

4.2 批量推理统计

[基准测试] 10次推理统计 (980x644):
  平均: 0.95s
  最小: 0.91s
  最大: 1.02s
  标准差: 0.03s
  P50: 0.93s
  P99: 1.01s

注意: 推理失败时输出 [状态] FAILED 和错误信息。


5. 测试样例及输出结果

样例 1:测试图像对推理

运行命令:

python3 inference.py \
  --model-path ./weights \
  --source test_source.jpg \
  --target test_target.jpg \
  --output-dir ./results

输出:

[设备] Ascend910_9362 (npu:0)
[模型] UniFlowMatchClassificationRefinement
[配置] encoder=dinov2, head=dpt
[分辨率] 980x644
[权重] 已加载 670 个 tensor
[参数量] 477,760,540

[推理] 开始推理...
[结果] Flow shape: torch.Size([1, 2, 644, 980])
[结果] Covisibility shape: torch.Size([1, 644, 980])
[结果] Flow range: [3.5683, 13.5002]
[结果] Covisibility range: [0.0623, 0.9997]
[耗时] 0.930s

[统计] Flow平均绝对值: 7.9892
[统计] Covisibility平均值: 0.9430

[状态] SUCCESS

输出文件:

  • results/flow.png — 光流可视化
  • results/covisibility.png — 共视掩码可视化
  • results/flow.npy — 原始光流数组 (1, 2, 644, 980)
  • results/covisibility.npy — 原始共视掩码数组 (1, 644, 980)

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 自建测试图像对 (几何变换模拟)

评测指标:

指标结果说明
Flow Mean Abs7.99像素位移场平均绝对值
Covisibility Mean0.94共视区域平均置信度
推理耗时 (980×644)0.93s单对图像,Ascend910 NPU

评测命令:

python3 inference.py \
  --model-path ./weights \
  --source test_source.jpg \
  --target test_target.jpg \
  --output-dir ./results

实际输出:

[结果] Flow shape: torch.Size([1, 2, 644, 980])
[结果] Covisibility shape: torch.Size([1, 644, 980])
[结果] Flow range: [3.5683, 13.5002]
[结果] Covisibility range: [0.0623, 0.9997]
[耗时] 0.930s
[统计] Flow平均绝对值: 7.9892
[统计] Covisibility平均值: 0.9430

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 2 (Chip 0, Chip 1)
  • Tensor Parallel: 单卡推理 (模型未使用TP)
  • 显存占用: ~8GB HBM per chip (模型 ~3GB, 中间激活 ~5GB)
  • CANN 版本: 8.5.1
  • torch_npu 版本: 2.9.0.post1

9. 已知问题

  1. libGL 依赖: opencv-python-headless 5.0.0 在部分环境仍需要系统 libGL.so.1。如遇 ImportError,请安装 libgl1-mesa-glx。
  2. 模型精度: Flow 输出值域较大 (3-13像素),适用于大位移场景。小位移场景建议使用 UFM-Base 模型。
  3. 输入格式: 仅支持 RGB 图像对输入,不支持单目深度估计或其他任务。