模型简介
模型特点
模型能力
使用案例
🚀 SuperGlue
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的特性,要输出动态数量的匹配结果,需要使用掩码属性来获取相应信息:
from transformers import AutoImageProcessor, AutoModel
import torch
from PIL import Image
import requests
url_image_1 = "https://raw.githubusercontent.com/magicleap/SuperGluePretrainedNetwork/refs/heads/master/assets/phototourism_sample_images/united_states_capitol_98169888_3347710852.jpg"
image_1 = Image.open(requests.get(url_image_1, stream=True).raw)
url_image_2 = "https://raw.githubusercontent.com/magicleap/SuperGluePretrainedNetwork/refs/heads/master/assets/phototourism_sample_images/united_states_capitol_26757027_6717084061.jpg"
image_2 = Image.open(requests.get(url_image_2, stream=True).raw)
images = [image_1, image_2]
processor = AutoImageProcessor.from_pretrained("stevenbucaille/superglue_indoor")
model = AutoModel.from_pretrained("stevenbucaille/superglue_indoor")
inputs = processor(images, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
# Get the respective image masks
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("For the image pair", i)
for keypoint0, keypoint1, matching_score in zip(output["keypoints0"], output["keypoints1"],
output["matching_scores"]):
print(
f"Keypoint at coordinate {keypoint0.numpy()} in the first image matches with keypoint at coordinate {keypoint1.numpy()} in the second image with a score of {matching_score}."
)
从输出结果中,可以使用以下代码可视化两幅图像之间的匹配结果:
import matplotlib.pyplot as plt
import numpy as np
# Create side by side image
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")
# Retrieve the keypoints and matches
output = outputs[0]
keypoints0 = output["keypoints0"]
keypoints1 = output["keypoints1"]
matching_scores = output["matching_scores"]
keypoints0_x, keypoints0_y = keypoints0[:, 0].numpy(), keypoints0[:, 1].numpy()
keypoints1_x, keypoints1_y = keypoints1[:, 0].numpy(), keypoints1[:, 1].numpy()
# Plot the matches
for keypoint0_x, keypoint0_y, keypoint1_x, keypoint1_y, matching_score in zip(
keypoints0_x, keypoints0_y, keypoints1_x, keypoints1_y, matching_scores
):
plt.plot(
[keypoint0_x, keypoint1_x + image1.width],
[keypoint0_y, keypoint1_y],
color=plt.get_cmap("RdYlGn")(matching_score.item()),
alpha=0.9,
linewidth=0.5,
)
plt.scatter(keypoint0_x, keypoint0_y, c="black", s=2)
plt.scatter(keypoint1_x + image1.width, keypoint1_y, c="black", s=2)
# Save the plot
plt.savefig("matched_image.png", dpi=300, bbox_inches='tight')
plt.close()
✨ 主要特性
- 特征匹配能力:能够有效地匹配两组局部特征,通过联合寻找对应关系和排除不可匹配的点。
- 上下文聚合机制:引入了基于注意力的灵活上下文聚合机制,使其能够对底层3D场景和特征分配进行推理。
- 高效性:设计高效,在现代GPU上可以实时运行,适用于实时应用。
- 适应性强:可以处理具有挑战性的真实室内外环境,适用于多种多视图几何问题。
📚 详细文档
模型描述
SuperGlue是一个神经网络,通过联合寻找对应关系和排除不可匹配的点来匹配两组局部特征。它引入了基于注意力的灵活上下文聚合机制,使其能够对底层3D场景和特征分配进行推理。该架构主要由两个组件组成:注意力图神经网络和最优匹配层。
注意力图神经网络使用关键点编码器来映射关键点位置和视觉描述符。它采用自注意力和交叉注意力层来创建强大的表示。最优匹配层创建一个得分矩阵,用垃圾桶进行扩充,并使用Sinkhorn算法找到最优的部分分配。
属性 | 详情 |
---|---|
开发团队 | MagicLeap |
模型类型 | 图像匹配 |
许可证 | 仅限学术或非营利组织非商业研究使用 |
模型资源
- 代码仓库:https://github.com/magicleap/SuperGluePretrainedNetwork
- 论文:https://arxiv.org/pdf/1911.11763
- 演示:https://psarlin.com/superglue/
应用场景
SuperGlue专为计算机视觉中的特征匹配和姿态估计任务而设计。它可以应用于各种多视图几何问题,并且能够处理具有挑战性的真实室内外环境。然而,它在需要不同类型视觉理解的任务(如目标检测或图像分类)上可能表现不佳。
演示笔记本
一个展示SuperGlue推理和可视化的演示笔记本可以在这里找到。
🔧 技术细节
训练数据
SuperGlue在用于姿态估计的大型标注数据集上进行训练,使其能够学习姿态估计的先验知识并对3D场景进行推理。训练数据由具有真实对应关系的图像对以及从真实姿态和深度图派生的未匹配关键点组成。
训练过程
SuperGlue使用真实匹配和未匹配关键点进行有监督训练。损失函数最大化分配矩阵的负对数似然,旨在同时最大化精度和召回率。
训练超参数
- 训练模式:fp32
速度、大小和时间
SuperGlue设计高效,在现代GPU上可以实时运行。对于室内图像对,一次前向传播大约需要69毫秒(15 FPS)。该模型有1200万个参数,与其他一些深度学习模型相比相对紧凑。SuperGlue的推理速度适用于实时应用,并且可以轻松集成到现代同时定位与地图构建(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={Proceedings of the IEEE/CVF conference on computer vision and pattern recognition},
pages={4938--4947},
year={2020}
}
模型卡片作者









