Y
YYPGD/UFM-Refine
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

UFM-Refine 昇腾NPU部署文档

1. 模型简介

模型名称: UFM-Refine(UniFlowMatch Refinement) 模型链接: infinity1096/UFM-Refine 模型描述: UFM(UniFlowMatch) 是 CMU 提出的端到端训练的 Transformer 模型,直接回归像素位移场,同时适用于光流估计与宽基线稠密匹配任务。本仓库为 Refine 变体:在 Base 的基础上增加局部分类精修头(classification refinement),对初始光流预测进行逐像素分类精修,并输出共视度掩码,典型应用包括光流估计、图像配准、SfM/SLAM 前端对应点求解等。 模型架构: DINOv2-Large ViT 编码器 + 全局注意力信息共享模块 + DPT 回归头 + patch MLP 分类精修头 + DPT 共视度头 参数规模: 约 0.9B(model.safetensors 约 1.9 GB,fp32) 论文: UFM: A Simple Path towards Unified Dense Correspondence with Flow (arXiv:2506.09278)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch / torch_npu2.1.0+torch_npu 从 CANN 安装
uniflowmatch / uniceptionUFM 官方 GitHub 源码已随依赖目录提供
timm / einops>= 0.9 / >= 0.7编码器与特征处理依赖
opencv-python-headless>= 4.x图像读取
flow_vis / matplotlib / pillow最新光流可视化

安装命令:

pip install --target /mnt/workspace/UFM-Refine/libs -i https://pypi.tuna.tsinghua.edu.cn/simple \
    numpy opencv-python-headless pillow matplotlib flow_vis einops timm huggingface_hub safetensors
# uniflowmatch / uniception 来自官方源码(已放置于依赖目录),torch/torch_npu 使用共享环境

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info
# 权重位于 /home/developer/models/UFM-Refine(config.json + model.safetensors)

3.2 运行推理

python3 inference.py --model_path /home/developer/models/UFM-Refine

脚本自动选择设备 npu > cuda > cpu,内置示例图像对,无需其他必填参数。

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/UFM-Refine本地权重目录
--devicestr自动(npu>cuda>cpu)推理设备
--outputstrassets/model_result.png结果可视化保存路径

4. 测试样例及输出结果

样例 1:内置示例图像对光流估计

输入:

assets/sample_source.png 与 assets/sample_target.png(若缺失则自动合成平移图案)

输出:

[INFO] device = npu
[INFO] 输出光流形状: (2, H, W), 共视度掩码形状: (H, W)
[INFO] 光流幅值 mean=... max=..., 共视度比例=...
[RESULT] status=ok device=npu 耗时/elapsed=x.xx s

样例 2:合成平移图案(无样例图时自动使用)

输入:

224x224 随机纹理图向 (dx=12, dy=7) 平移得到目标图

输出:

[INFO] 光流幅值 mean≈13.9 max≈... , 共视度比例≈...
[INFO] 结果图已保存: assets/model_result.png

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果

6. 注意事项

  • 推理脚本通过 torch_npu 调用 NPU,输入张量为 float32(NPU 不支持 float64 算子);
  • 模型为自定义架构(PyTorchModelHubMixin),通过 UniFlowMatchClassificationRefinement.from_pretrained 加载本地权重目录,全程离线,不联网下载;
  • 依赖隔离:第三方包装在 /mnt/workspace/UFM-Refine/libs,运行时由脚本自动叠加 PYTHONPATH,不污染共享环境;
  • Refine 变体推理分辨率默认 560x420(宽x高),显存占用约 8-12 GiB;
  • 如有精度差异请与 GPU/CPU fp32 结果对比并在 README 中注明。