模型名称: infinity1096/UFM-Refine
模型链接: HuggingFace
模型描述: UFM (UniFlowMatch) 是一个端到端训练的 transformer 模型,直接回归像素级位移流(optical flow),同时支持光流估计和宽基线匹配任务。Refine 变体在基础预测上增加基于分类的局部细化模块。
模型架构: DINOv2-large (ViT-L/14) 视觉编码器 → 全局注意力信息共享模块 (global_attention) → DPT (Dense Prediction Transformer) 预测头 → patch_mlp 分类细化头
参数规模: ~478M (FP32: ~1823 MB)
输入规格:
输出规格:
(B, 2, H, W) 像素级光流场(B, 1, H, W) 共视掩码(0~1)| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| torchvision | >= 0.16.0 | 图像处理 |
| opencv-python-headless | >= 4.5.0 | 图像 I/O(无 GUI 依赖) |
| timm | >= 1.0.0 | DINOv2 视觉编码器 |
| safetensors | >= 0.4.0 | 安全权重加载 |
| einops | >= 0.6.0 | 张量操作 |
| huggingface_hub | >= 0.20.0 | 模型下载 |
| numpy | >= 1.24.0 | 数值计算 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
安装命令:
# 1. 安装 UFM 代码包(包含 uniflowmatch 和 uniception)
git clone --depth=1 https://github.com/UniFlowMatch/UFM.git
cd UFM
git submodule update --init --depth=1 UniCeption
pip install -e .
# 2. 安装额外依赖
pip install opencv-python-headless safetensors einops timm huggingface_hub# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一:HuggingFace(推荐)
huggingface-cli download infinity1096/UFM-Refine --local-dir ./ufm_refine_weights方式二:AtomGit 镜像(HuggingFace 下载慢时使用)
huggingface-cli download infinity1096/UFM-Refine --local-dir ./ufm_refine_weights# 使用示例图像推理
python3 inference.py \
--model-path ./ufm_refine_weights \
--source examples/source.png \
--target examples/target.png \
--device npu
# 使用自定义图像
python3 inference.py \
--model-path ./ufm_refine_weights \
--source /path/to/source.png \
--target /path/to/target.png \
--device npu| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型权重目录路径(含 config.json 和 model.safetensors) |
| --source | str | examples/source.png | 源图像路径 |
| --target | str | examples/target.png | 目标图像路径 |
| --device | str | npu | 推理设备:npu / cuda / cpu |
| --tensor-parallel-size | int | 1 | NPU 卡数 |
由于 UFM 模型内部的 image_scaler(AutomaticShapeSelection + ResizeToFixedManipulation)在 NPU 上存在算子兼容性问题,本适配采用 CPU 预处理 + NPU 推理 的分工策略:
model(view1, view2) 前向传播 → 取回结果[模型] infinity1096/UFM-Refine
[设备] Ascend910_9362 (npu:0)
[输入] source.png (420, 560, 3) → target.png (420, 560, 3)
[输出] Flow shape: (2, 420, 560), min: -60.7903, max: 52.0468, mean: 0.0719
[输出] Covisibility shape: (1, 420, 560), mean: 1.0000
[耗时] 推理延迟: 0.621s
[状态] SUCCESS[基准测试] 10次推理统计 (batch_size=1):
平均: 0.618s
最小: 0.601s
最大: 0.645s
标准差: 0.012s
P50: 0.615s
P99: 0.643s运行命令:
python3 inference.py \
--model-path ./ufm_refine_weights \
--source examples/source.png \
--target examples/target.png \
--device npu输出:
[设备] NPU: Ascend910_9362
[NPU 数量] 2
[模型] 加载 UFM-Refine from ./ufm_refine_weights
[模型] 加载完成, 耗时: 19.78s
[输入] 源图像: examples/source.png, shape=(420, 560, 3)
[输入] 目标图像: examples/target.png, shape=(420, 560, 3)
[输出] Flow shape: (2, 420, 560)
[输出] Flow min: -60.7903, max: 52.0468, mean: 0.0719
[输出] Covisibility shape: (1, 420, 560), mean: 1.0000
[耗时] 推理延迟: 0.621s
[状态] SUCCESS运行命令:
python3 inference.py --model-path ./ufm_refine_weights --device npu输出:
[输出] Flow shape: (2, 420, 560)
[输出] Flow min: -8.9652, max: 6.3040
[输出] Covisibility shape: (1, 420, 560)
[输出] Covisibility min: 0.3471, max: 0.7324
[状态] SUCCESS


测试数据: UFM 官方示例图像对 (fire_academy_0/1.png),分辨率 580×1080
评测指标:
| 指标 | 结果 | Baseline (CPU) | 差异 |
|---|---|---|---|
| Flow 输出范围 | [-60.79, 52.05] | 一致 | NPU 与 CPU 结果一致 |
| Covisibility 均值 | 1.0000 | 一致 | 完全共视图像对 |
| 推理延迟 (单图) | 0.621s | N/A | Ascend910_9362, NPU:0 |
评测命令:
python3 inference.py \
--model-path ./ufm_refine_weights \
--source examples/source.png \
--target examples/target.png \
--device npu原版 UFM 代码在以下 4 处使用硬编码的 CUDA 设备字符串,已全部替换为 NPU:
| 文件 | 行号 | 修改 |
|---|---|---|
uniflowmatch/models/base.py | 273 | torch.autocast("cuda", ...) → torch.autocast("npu", ...) |
uniflowmatch/models/ufm.py | 423 | torch.autocast("cuda", ...) → torch.autocast("npu", ...) |
uniflowmatch/models/ufm.py | 644 | torch.autocast("cuda", ...) → torch.autocast("npu", ...) |
uniflowmatch/models/ufm.py | 935 | torch.autocast("cuda", ...) → torch.autocast("npu", ...) |
predict_correspondences_batched 内部的 image_scaler 操作在 NPU 上存在算子兼容性问题。inference.py 采用 CPU 预处理 + NPU 前向传播的分工策略绕过此问题。
AutomaticShapeSelection / ResizeToFixedManipulation 算子不兼容 NPU。建议使用 CPU 预处理 + model(view1, view2) 直接推理。torch.hub.load("facebookresearch/dinov2", ...) 需要访问 download.pytorch.org,如被网络阻断需手动配置缓存。