模型概述
模型特點
模型能力
使用案例
🚀 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}
}
模型卡片作者









