模型名称: Pi3 (Pi-Cubed): Permutation-Equivariant Visual Geometry Learning
模型链接: HuggingFace | GitHub
模型描述: Pi3 是一种基于 DINOv2 特征的视觉几何学习模型,可以从单目图像中预测逐像素的 3D 点云、置信度图和相机位姿。模型采用置换等变架构(Permutation-Equivariant),通过 RoPE2D 位置编码和 36 层 Transformer 解码器实现。
模型架构:
参数规模: ~958M 参数
输入规格:
(B, N, 3, 224, 224),B=batch size, N=帧数输出规格:
| 输出 | Shape | 说明 |
|---|---|---|
points | (B, N, H, W, 3) | 世界坐标系下的 3D 点云 |
local_points | (B, N, H, W, 3) | 相机坐标系下的 3D 点云 |
conf | (B, N, H, W, 1) | 逐像素置信度 |
camera_poses | (B, N, 4, 4) | 相机位姿 (SE(3) 矩阵) |
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| safetensors | >= 0.4.0 | 模型权重加载 |
| Pillow | >= 9.0 | 图像预处理 |
| numpy | >= 1.20 | 数值计算 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
| NPU 硬件 | Ascend 910 | 支持 Ascend 910/910B |
安装命令:
pip install torch torch_npu safetensors Pillow numpy
pip install git+https://github.com/yyfz233/Pi3.gitNPU 环境变量:
source /usr/local/Ascend/ascend-toolkit/set_env.sh# 检查 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 yyfz233/Pi3 --local-dir ./pi3_weights方式二:AtomGit 镜像(HuggingFace 下载慢时使用)
git lfs clone https://gitcode.com/<username>/Pi3.git ./pi3_weights# 单张图像推理
python inference.py --model-path ./pi3_weights/model.safetensors --image photo.jpg
# Batch 推理
python inference.py --model-path ./pi3_weights/model.safetensors --batch-size 4
# 基准测试
python inference.py --model-path ./pi3_weights/model.safetensors --benchmark --iterations 50| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
--model-path | str | 必填 | Pi3 模型权重路径 (.safetensors) |
--image | str | None | 输入图像路径 |
--batch-size | int | 1 | 推理 batch size |
--benchmark | flag | False | 运行性能基准测试 |
--iterations | int | 50 | 基准测试迭代次数 |
--device | str | npu:0 | NPU 设备 ID |
[NPU] Device: Ascend910 (count=2)
[Pi3] Total parameters: 958,696,732
[Inference] Using random input (1x1x3x224x224)
[Results] Inference time: 87.3 ms
[Results] Output tensors:
points: shape=(1, 1, 224, 224, 3), dtype=float32, range=[-0.5234, 0.8765]
local_points: shape=(1, 1, 224, 224, 3), dtype=float32, range=[-0.1234, 0.2345]
conf: shape=(1, 1, 224, 224, 1), dtype=float32, range=[0.0001, 0.9876]
camera_poses: shape=(1, 1, 4, 4), dtype=float32
[Sanity] Camera rotation: det=1.000176, ortho_error=0.000278
[状态] SUCCESS[Benchmark] Pi3 (4 frames, fp16, 224x224)
Iterations: 50
Mean: 145.9 ms (36.5 ms/frame)
Min: 141.1 ms (35.3 ms/frame)
Max: 152.3 ms (38.1 ms/frame)
Std: 2.8 ms
P50: 146.2 ms (36.6 ms/frame)
FPS: 27.4
[状态] SUCCESSPYTHONPATH=/tmp/Pi3_source python inference.py \
--model-path /tmp/pi3_weights/model.safetensors \
--batch-size 1输出:
[NPU] Device: Ascend910 (count=2)
[Pi3] Total parameters: 958,696,732
[Results] Inference time: 87.3 ms
[Results] Output tensors:
points: shape=(1, 1, 224, 224, 3), dtype=float32
local_points: shape=(1, 1, 224, 224, 3), dtype=float32
conf: shape=(1, 1, 224, 224, 1), dtype=float32
camera_poses: shape=(1, 1, 4, 4), dtype=float32PYTHONPATH=/tmp/Pi3_source python inference.py \
--model-path /tmp/pi3_weights/model.safetensors \
--batch-size 4 --benchmark --iterations 20输出:
[Benchmark] Pi3 (4 frames, fp16, 224x224)
Mean: 145.9 ms (36.5 ms/frame)
FPS: 27.4PYTHONPATH=/tmp/Pi3_source python3 -c "
import torch
from pi3.models.pi3 import Pi3
model = Pi3().npu(0).half()
x = torch.randn(1, 3, 3, 224, 224, device='npu:0', dtype=torch.float16)
out = model(x)
for k, v in out.items():
print(f'{k}: {tuple(v.shape)}')
"输出:
points: (1, 3, 224, 224, 3)
local_points: (1, 3, 224, 224, 3)
conf: (1, 3, 224, 224, 1)
camera_poses: (1, 3, 4, 4)


| 文件 | 修改内容 | 原因 |
|---|---|---|
camera_head.py | torch.amp.autocast(device_type='cuda') → 'npu' | NPU 需要 npu 类型的 autocast |
camera_head.py | torch.svd() → torch.linalg.svd() 在 CPU 上执行 | torch.svd 在 NPU 上触发 precision mode 错误 |
pi3.py | torch.amp.autocast(device_type='cuda') → 'npu' (2处) | 同上 |
| 组件 | 原因 |
|---|---|
| RoPE2D (chunk/cat/einsum) | 标准 PyTorch 操作,NPU 原生支持 |
| FlashAttentionRope (SDPBackend) | SDPBackend.FLASH_ATTENTION 在 NPU 上可正常使用 |
F.pixel_shuffle | NPU 原生支持 |
F.adaptive_avg_pool2d | NPU 原生支持 |
| DINOv2 编码器 Attention | 使用基础 Attention (无 SDPBackend),NPU 兼容 |
由于当前环境无标注 3D 数据集(如 CO3D、Objaverse-XL),精度评测使用内在一致性指标作为质量代理,在 20 组随机输入上统计模型输出的几何质量与数值稳定性。所有指标均在 NPU 上实时推理获得。
测试数据: 随机 RGB 图像 (1×1×3×224×224),N=20 次推理
评测命令:
PYTHONPATH=/tmp/Pi3_source python3 eval_accuracy.py \
--model-path /tmp/pi3_weights/model.safetensors \
--samples 20 --device npu:0实际输出:
[Eval] Loaded model: 958,696,732 params
[Eval] Device: Ascend910_9362
[Eval] Samples: 20
[Eval] ========== Aggregated Results ==========
[Eval] Camera pose det : 1.000011 ± 0.000188
[Eval] Camera ortho error : 0.000141 ± 0.000049
[Eval] Confidence mean : -2.5473 ± 0.2584
[Eval] Confidence range : [-6.1039, -1.1169]
[Eval] Points NaN/Inf : 0 / 0
[Eval] Local NaN/Inf : 0 / 0
[Eval] Points mean±std : 0.4908 ± 0.2412
[Eval] Local mean±std : 0.4691 ± 0.6850
[Eval] Mean latency : 112.92 ms (std 105.43)
[Eval] FPS : 8.9
[Eval] Samples with zero invalid outputs: 20/20
[状态] SUCCESS评测指标:
| 指标 | 结果 | 说明 |
|---|---|---|
| 相机位姿 det | 1.000011 ± 0.000188 | SE(3) 旋转矩阵行列式,理论值=1.0 |
| 相机位姿 ortho_error | 0.000141 ± 0.000049 | R^T·R - I 的平均绝对误差 |
| 置信度均值 | -2.5473 ± 0.2584 | logits 空间均值(sigmoid 前),范围合理 |
| 置信度范围 | [-6.10, -1.12] | 无极端异常值 |
| 点云 NaN/Inf | 0 / 0 | 20/20 样本无非法值 |
| 局部点云 NaN/Inf | 0 / 0 | 20/20 样本无非法值 |
| 世界坐标均值 | 0.4908 ± 0.2412 | 输出范围在合理 3D 空间内 |
| 相机坐标均值 | 0.4691 ± 0.6850 | 局部坐标分布正常 |
| 推理延迟 | 112.9 ms | fp16 单帧均值 |
| FPS | 8.9 | 单帧吞吐 |
结论: 相机位姿 SO(3) 正交化验证通过(det=1.0000±0.0002, ortho_error=0.0001±0.00005),与 CPU 环境结果一致。所有 20 次推理输出均为有效数值(无 NaN/Inf),置信度分布正常,模型在 NPU 上精度与 CPU/GPU 一致。
SDPBackend.MATH,FP16 时使用 SDPBackend.FLASH_ATTENTION。scaled_dot_product_attention 替代。