库名称: transformers
推理: 否
许可证: 其他
SuperGlue
SuperGlue模型由Paul-Edouard Sarlin、Daniel DeTone、Tomasz Malisiewicz和Andrew Rabinovich在论文《SuperGlue: 基于图神经网络的特征匹配学习》中提出。
该模型用于匹配图像中检测到的两组兴趣点。与SuperPoint模型配合使用,可用于匹配两幅图像并估计它们之间的姿态。该模型适用于图像匹配、单应性估计等任务。
论文摘要如下:
本文介绍了SuperGlue,一种通过联合寻找对应点并剔除不可匹配点来匹配两组局部特征的神经网络。通过解决一个可微分的最优传输问题来估计匹配关系,其成本由图形神经网络预测。我们提出了一种基于注意力的灵活上下文聚合机制,使SuperGlue能够同时推理底层3D场景和特征匹配关系。与传统手工设计的启发式方法相比,我们的技术通过端到端的图像对训练学习几何变换和3D世界规律性的先验知识。SuperGlue在具有挑战性的真实室内外环境姿态估计任务中优于其他学习方法,并达到最先进的性能。该方法在现代GPU上可实时运行,并能轻松集成到现代SfM或SLAM系统中。代码和训练权重可通过此链接公开获取。

该模型由stevenbucaille贡献。原始代码可在此处找到。
演示笔记本
展示SuperGlue推理与可视化的演示笔记本可在此处查看。
模型详情
模型描述
SuperGlue是一种通过联合寻找对应点并剔除不可匹配点来匹配两组局部特征的神经网络。它引入了基于注意力的灵活上下文聚合机制,使其能够推理底层3D场景和特征匹配关系。架构主要由两部分组成:注意力图神经网络和最优匹配层。

注意力图神经网络使用关键点编码器映射关键点位置和视觉描述符,通过自注意力和交叉注意力层生成强大表示。最优匹配层创建分数矩阵,通过“垃圾桶”机制增强,并使用Sinkhorn算法找到最优部分匹配。
- 开发者: MagicLeap
- 模型类型: 图像匹配
- 许可证: 学术或非营利组织非商业研究用途
模型来源
- 代码库: https://github.com/magicleap/SuperGluePretrainedNetwork
- 论文: https://arxiv.org/pdf/1911.11763
- 演示: https://psarlin.com/superglue/
用途
直接使用
SuperGlue专为计算机视觉中的特征匹配和姿态估计任务设计,适用于多视角几何问题,并能处理真实室内外环境的挑战。但对于需要不同类型视觉理解的任务(如目标检测或图像分类),其表现可能不佳。
快速上手
以下是使用该模型的简单示例。由于这是图像匹配模型,需要成对图像输入:
from transformers import AutoImageProcessor, AutoModel
import torch
from PIL import Image
import requests
url = "https://raw.githubusercontent.com/magicleap/SuperGluePretrainedNetwork/refs/heads/master/assets/phototourism_sample_images/united_states_capitol_98169888_3347710852.jpg"
im1 = Image.open(requests.get(url, stream=True).raw)
url = "https://raw.githubusercontent.com/magicleap/SuperGluePretrainedNetwork/refs/heads/master/assets/phototourism_sample_images/united_states_capitol_26757027_6717084061.jpg"
im2 = Image.open(requests.get(url, stream=True).raw)
images = [im1, im2]
processor = AutoImageProcessor.from_pretrained("stevenbucaille/superglue_outdoor")
model = AutoModel.from_pretrained("stevenbucaille/superglue_outdoor")
inputs = processor(images, return_tensors="pt")
outputs = model(**inputs)
输出包含关键点检测器检测到的关键点列表及匹配得分。由于SuperGlue输出动态匹配数量,需使用掩码属性提取对应信息:
image0_mask, image1_mask = outputs_mask[0]
image0_indices = torch.nonzero(image0_mask).squeeze()
image1_indices = torch.nonzero(image1_mask).squeeze()
image0_matches = outputs.matches[0, 0][image0_indices]
image1_matches = outputs.matches[0, 1][image1_indices]
image0_matching_scores = outputs.matching_scores[0, 0][image0_indices]
image1_matching_scores = outputs.matching_scores[0, 1][image1_indices]
使用SuperGlueImageProcessor
的post_process_keypoint_matching
方法可获取更易读的关键点和匹配结果:
image_sizes = [(image.height, image.width) for image in images]
outputs = processor.post_process_keypoint_matching(outputs, image_sizes, threshold=0.2)
for i, output in enumerate(outputs):
print("图像对", i)
for keypoint0, keypoint1, matching_score in zip(output["keypoints0"], output["keypoints1"], output["matching_scores"]):
print(f"第一幅图像中坐标{keypoint0.numpy()}的关键点与第二幅图像中坐标{keypoint1.numpy()}的关键点匹配,得分{matching_score}.")
可通过以下代码可视化两幅图像的匹配结果:
import matplotlib.pyplot as plt
import numpy as np
merged_image = np.zeros((max(image1.height, image2.height), image1.width + image2.width, 3))
merged_image[:image1.height, :image1.width] = np.array(image1) / 255.0
merged_image[:image2.height, image1.width:] = np.array(image2) / 255.0
plt.imshow(merged_image)
plt.axis("off")
keypoints0 = outputs[0]["keypoints0"]
keypoints1 = outputs[0]["keypoints1"]
matching_scores = outputs[0]["matching_scores"]
for kp0, kp1, score in zip(keypoints0, keypoints1, matching_scores):
plt.plot([kp0[0], kp1[0] + image1.width], [kp0[1], kp1[1]],
color=plt.get_cmap("RdYlGn")(score.item()), alpha=0.9, linewidth=0.5)
plt.scatter(kp0[0], kp0[1], c="black", s=2)
plt.scatter(kp1[0] + image1.width, kp1[1], c="black", s=2)
plt.savefig("matched_image.png", dpi=300, bbox_inches='tight')
plt.close()

训练详情
训练数据
SuperGlue在大型姿态估计标注数据集上训练,学习姿态估计先验和3D场景推理能力。训练数据包含带有真实匹配关系和未匹配关键点的图像对,这些数据源自真实姿态和深度图。
训练过程
采用监督学习,通过最大化分配矩阵的负对数似然来同时优化精度和召回率。
训练超参数
速度与规模
- 推理速度: 室内图像对单次前向传播约69毫秒(15 FPS)
- 参数量: 1200万
- 适用性: 适合实时应用,可集成至SLAM或SfM系统
引用
BibTeX:
@inproceedings{sarlin2020superglue,
title={Superglue: Learning feature matching with graph neural networks},
author={Sarlin, Paul-Edouard and DeTone, Daniel and Malisiewicz, Tomasz and Rabinovich, Andrew},
booktitle={CVPR},
pages={4938--4947},
year={2020}
}
模型卡作者
Steven Bucaille