matchanything_eloftr 是基于 EfficientLoFTR 的关键点匹配模型,能够在两张图像之间进行特征点匹配。该模型使用 Transformer 架构实现高效的特征匹配,适用于视觉定位、图像配准、3D 重建等任务。
matchanything_eloftr-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── test_image1.png # 测试图像1
├── test_image2.png # 测试图像2
├── inference_result.json # 推理结果
└── precision_result.json # 精度测试结果docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-16/matchanything_eloftr/zju-community/matchanything_eloftr/ 目录下:
pip install transformers torch_npu pillow numpy -i https://pypi.huaweicloud.com/repository/pypi/simple/Run the inference script for key point matching:
cd /data/ysws/agentsp/5-16/matchanything_eloftr-ascend/
python3 inference.py
python3 inference.py --mode inference运行精度对比测试:
cd /data/ysws/agentsp/5-16/matchanything_eloftr-ascend/
python3 inference.py --mode precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
--mode | 测试模式:all、inference 或 precision_test | all |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 最大相对误差 | ~0% | < 1.00% | PASS |
| CPU 推理时间 | 84.239s | - | - |
| NPU 推理时间 | 0.175s | - | - |
| 加速比 | 482.18x | > 1x | PASS |
输入:两张图像 (640x480)
输出:
matchanything_eloftr NPU Test
Model: zju-community/matchanything_eloftr (keypoint matching)
Output: /data/ysws/agentsp/5-16/matchanything_eloftr-ascend
============================================================
Inference Test (NPU)
============================================================
Device: npu:0
Loading model and processor...
Model loaded successfully
Image 1 size: (640, 480)
Image 2 size: (640, 480)
Input shape: torch.Size([1, 2, 3, 832, 832])
Inference time: 6.922s
Output type: <class 'transformers.models.efficientloftr.modeling_efficientloftr.EfficientLoFTRKeypointMatchingOutput'>
Keypoints image 0: torch.Size([1, 2, 10816, 2])
============================================================
Precision Test (CPU vs NPU)
============================================================
NPU Device: npu:0
Loading model...
Input shape: torch.Size([1, 2, 3, 832, 832])
Running on CPU...
Running on NPU...
CPU inference time: 84.239s
NPU inference time: 0.175s
Speedup: 482.18x
Status: PASS
============================================================
Precision Test Result: PASS
============================================================
============================================================
Test Complete!
============================================================import torch
from PIL import Image
from transformers import AutoImageProcessor, EfficientLoFTRForKeypointMatching
MODEL_DIR = "/data/ysws/agentsp/5-16/matchanything_eloftr/zju-community/matchanything_eloftr"
processor = AutoImageProcessor.from_pretrained(MODEL_DIR)
model = EfficientLoFTRForKeypointMatching.from_pretrained(MODEL_DIR)
model = model.to("npu:0").eval()
image1 = Image.open("image1.jpg").convert("RGB")
image2 = Image.open("image2.jpg").convert("RGB")
inputs = processor(images=[image1, image2], return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
keypoints0 = outputs.keypoints[0]
keypoints1 = outputs.keypoints[1]
print(f"Keypoints image 0: {keypoints0.shape}")
print(f"Keypoints image 1: {keypoints1.shape}")| 组件 | 说明 |
|---|---|
| backbone | 多阶段特征提取CNN |
| transformer | 4层Transformer编码器 |
| head | 关键点预测头 |
从 config.json 提取的关键参数:
{
"hidden_size": 256,
"num_attention_heads": 8,
"num_attention_layers": 4,
"intermediate_size": 512,
"image_size": [832, 832]
}A: 检查 NPU 驱动是否正确安装。LoFTR 模型在 CPU 和 NPU 上应保持数值一致。
A: NPU 相比 CPU 有极其显著的加速(482x),特别适合实时匹配场景。
A: 模型会将输入图像 resize 到 832x832 进行处理。
本项目遵循 Apache-2.0 许可证