Bug_Factory_w/profusion_pr
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PROFusion 昇腾 NPU 迁移与推理部署指南

PROFusion (ICRA 2026) 是一个用于实时相机跟踪与稠密场景重建的 RGB-D SLAM 系统。 本仓库基于官方 siyandong/PROFusion 代码, 将其 位姿回归模块(基于 PyTorch)迁移适配到华为昇腾 NPU, 并提供 HTTP 服务化推理 与完整测试用例。


1. 目录结构

profusion_pr-npu/
├── README.md                  # 本文档 (中文部署指引)
├── README_origin.md           # 官方原始 README
├── requirements.txt           # 昇腾 NPU 运行环境依赖
├── inference.py               # ★ 服务化推理脚本 (Flask HTTP 服务)
├── test_inference.py          # ★ 测试用例 (环境检查 + 模块级 + HTTP 端到端)
├── pose_regression/           # 位姿回归模块 (已适配 NPU)
│   ├── model.py               #   PROFusionPoseRegression 模型定义
│   ├── run.py                 #   模型加载 / 位姿回归流程
│   ├── inference.py           #   推理辅助函数 rpr_inference
│   ├── modules/               #   编码器 / 多视图解码器 / 位姿头
│   └── utils/                 #   设备 / 几何 / 图像工具
├── data_loader/               # 数据集加载器 (FemtoBolt / FastCaMo / ETH3D)
├── scripts/
│   ├── gen_screenshots.py     #   交付截图生成脚本
│   └── adaptation_log.txt     #   适配过程记录
└── assets/                    # 交付截图
    ├── npu_device_call.png    #   NPU 设备调用截图
    ├── model_result.png       #   测试结果截图
    └── agent_workflow.png     #   适配过程截图

2. 环境要求

项目要求本机实测
硬件Ascend910 系列 (Atlas 800), ≥1 卡12x Ascend910 (61 GB HBM)
OSopenEuler / Ubuntu / KylinOS (aarch64)openEuler 22.03 SP4 aarch64
CANN≥ 8.08.5.1
Python3.8 – 3.113.11.14
PyTorch2.9.02.9.0
torch_npu与 PyTorch 配套2.9.0.post1+gitee7ba04

2.1 环境初始化

# 1) 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh

# 2) 指定可见 NPU 卡
export ASCEND_RT_VISIBLE_DEVICES=0

# 3) 安装依赖 (建议使用华为镜像源)
pip install -r requirements.txt \
    -i https://repo.huaweicloud.com/repository/pypi/simple/

# 4) 校验 NPU 可用
python3 -c "import torch; import torch_npu; \
a = torch.randn(3, 4).npu(); print(a + a)"   # 输出 device='npu:0' 即正常

权重文件位置:/mnt/old_data/whl/models/siyan824/profusion_pr/ (model.safetensors 与 profusion_pr.pth 均为同一套权重,二选一即可)

3. 昇腾 NPU 迁移适配说明

使用 ascend-model-agent-plugin 的 ai4s-basic(PyTorch 迁移)skill 完成,关键改动如下:

文件原始 CUDA 代码昇腾适配
pose_regression/utils/device.pyMyNvtxRange 调用 torch.cuda.synchronize() / torch.cuda.nvtx.*改为 NPU 同步 + NVTX no-op 兼容;并修复 todevice 对 dict 不原地移动张量 的缺陷
pose_regression/model.pytorch.amp.autocast('cuda', enabled=False)按实际设备动态选择 device_type(npu / cuda)
pose_regression/modules/base_model.pytorch.backends.cuda.matmul.allow_tf32 = Truetry/except 兼容 + NPU matmul 优化;新增 from_local_pretrained() 本地权重加载
pose_regression/run.pyPR.to('cuda')、to_device(view, device='cuda')设备参数化,get_device() 自动选择 npu:0 / cuda / cpu
curope (CUDA 扩展)kernels.cu RoPE2D CUDA 算子不编译,自动回退纯 PyTorch 版 RoPE2D(pos_embed.py 内建 fallback)

详细适配过程见 scripts/adaptation_log.txt 与 assets/agent_workflow.png。

4. 服务化推理

4.1 启动服务

source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0

python3 inference.py \
    --ckpt /mnt/old_data/whl/models/siyan824/profusion_pr/model.safetensors \
    --port 8080 --device npu:0

参数说明:

  • --ckpt:本地权重路径,支持 .safetensors / .pth;缺省指向默认权重目录
  • --device:推理设备,缺省自动选择 npu:0(无 NPU 时回退 cuda/cpu)
  • --port:HTTP 服务端口,默认 8080

4.2 接口定义

GET /health — 健康检查

curl http://127.0.0.1:8080/health
# {"status":"ok","device":"npu:0","load_time":3.05}

POST /infer — 相对位姿估计

输入:两帧 RGB-D 图像(reference 帧 + query 帧),图像与深度图为 base64 编码二进制:

{
  "intrinsics": [fx, fy, cx, cy],
  "frames": [
    {"rgb_b64": "<uint8 RGB 图像 bytes>", "depth_b64": "<float32 深度图 bytes>", "h": 224, "w": 224},
    {"rgb_b64": "<...>", "depth_b64": "<...>", "h": 224, "w": 224}
  ]
}

输出:query 帧在 reference 帧坐标系下的相对位姿(4x4 齐次矩阵):

{
  "status": "ok",
  "pose": [[4x4 位姿矩阵]],
  "rotation": [[3x3 旋转矩阵]],
  "translation": [x, y, z],
  "elapsed": 0.06,
  "total_elapsed": 0.10
}

约束:图像宽高需为 16 的整数倍(与 patch_size 对齐),深度单位为米(m), 平移输出单位为米,乘以 1000 即毫米。

5. 测试验证

5.1 一键运行全部测试

source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0

python3 test_inference.py \
    --ckpt /mnt/old_data/whl/models/siyan824/profusion_pr/model.safetensors \
    --device npu:0

测试覆盖:

  1. 环境检查 — torch / torch_npu 版本、NPU 数量与显存
  2. 模块级推理 — 合成 RGB-D 帧对直接调用 infer_relative_pose()
  3. HTTP 服务化推理 — 启动 Flask 服务,通过 POST /infer 端到端验证

5.2 本机实测结果(Ascend910, npu:0)

[env] NPU[0]  : Ascend910_9382, 显存 61 GB
[inference] 模型加载完成: 540.6M 参数, 设备 npu:0, 耗时 3.05s

[module] 相对位姿:
[[ 0.99993 -0.00935  0.00773 -0.18337]
 [ 0.0095   0.99975 -0.02014  0.10296]
 [-0.00754  0.02021  0.99977 -0.26872]
 [ 0.       0.       0.       1.     ]]
[module] 旋转行列式 : 1.000001 (PASS, 应≈1)
[module] 平移(毫米) : [-183.4  103.0 -268.7] (PASS, 非零)

[http] HTTP 状态  : 200
[http] 推理耗时    : 0.0589 s   (热推理, 已含算子编译缓存)
  • 旋转矩阵正交性 ✓(det(R) = 1.000001)
  • 平移量非零 ✓(模型正确响应输入运动)
  • NPU 显存占用 ~2.02 GB(模型为 540.6M 参数 fp32)

6. 使用建议与已知限制

  1. 性能:首次推理含算子编译(约 2.6s),后续热推理 ~0.06s。 如需进一步加速,可使用 torch_npu.npu_fusion_attention 替换自注意力, 或对编码器部分使用 bf16 混合精度(位姿头需保持 fp32)。 注意:位姿头的 SVD 正交化(torch.svd / torch.det)在 NPU 上会 CPU fallback(仅影响毫秒级耗时,不影响精度),属已知现象。
  2. 数据格式:测试用例使用合成 RGB-D 帧;接入真实相机时, 深度图需先转为米(m),内参需与实际传感器标定一致。
  3. 完整重建管线:本仓库只交付位姿回归模块的服务化推理。 完整稠密重建(randomized_optimization 的 C++ 优化融合管线)需另行 编译 optimization_fusion(见官方仓库 build.sh),不在本迁移范围内。
  4. 分布式:当前为单卡推理。多卡场景需将 backend 切换为 hccl 并 使用 torchrun --nproc_per_node=N inference.py ...。

7. 引用

@article{dong2025profusion,
  title={PROFusion: Robust and Accurate Dense Reconstruction via Camera Pose Regression and Optimization},
  author={Dong, Siyan and Wang, Zijun and Cai, Lulu and Ma, Yi and Yang, Yanchao},
  journal={arXiv preprint arXiv:2509.24236},
  year={2025}
}

许可证:CC BY-NC-SA 4.0(非商业使用)