Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

docs(feature): make the DescriptorMatcherWithSteerer example a small CPU one (kornia#4041) (#4085)

Dmytro Mishkind2166cfabd94
diff --git a/kornia/feature/matching.py b/kornia/feature/matching.py
index f8a329eba..4be067926 100644
--- a/kornia/feature/matching.py
+++ b/kornia/feature/matching.py
@@ -398,27 +398,37 @@ class DescriptorMatcherWithSteerer(nn.Module):
th: threshold on distance ratio, or other quality measure.
Example:
- >>> import kornia as K
+ The example below is deliberately small so that it runs anywhere. A real workload uses
+ full-resolution images and many more keypoints, and is worth moving onto an accelerator --
+ uncomment the ``device`` lines to do that.
+
>>> import kornia.feature as KF
- >>> device = K.core.utils.get_cuda_or_mps_device_if_available()
- >>> img1 = torch.randn([1, 3, 768, 768], device=device)
- >>> img2 = torch.randn([1, 3, 768, 768], device=device)
- >>> dedode = KF.DeDoDe.from_pretrained(detector_weights="L-C4-v2", descriptor_weights="B-SO2").to(device)
+ >>> # import kornia as K
+ >>> # device = K.core.utils.get_cuda_or_mps_device_if_available()
+ >>> img1 = torch.randn([1, 3, 128, 128])
+ >>> img2 = torch.randn([1, 3, 128, 128])
+ >>> # img1, img2 = img1.to(device), img2.to(device)
+ >>> dedode = KF.DeDoDe.from_pretrained(detector_weights="L-C4-v2", descriptor_weights="B-SO2")
+ >>> # dedode = dedode.to(device)
>>> steerer_order = 8 # discretisation order of rotation angles
>>> steerer = KF.steerers.DiscreteSteerer.create_dedode_default(
... generator_type="SO2", steerer_order=steerer_order
... )
- >>> steerer = steerer.to(device)
+ >>> # steerer = steerer.to(device)
>>> matcher = KF.matching.DescriptorMatcherWithSteerer(
... steerer=steerer, steerer_order=steerer_order, steer_mode="global", match_mode="smnn", th=0.98
... )
>>> with torch.inference_mode():
- ... kps1, scores1, descs1 = dedode(img1, n=20_000)
- ... kps2, scores2, descs2 = dedode(img2, n=20_000)
+ ... kps1, scores1, descs1 = dedode(img1, n=1_000)
+ ... kps2, scores2, descs2 = dedode(img2, n=1_000)
... kps1, kps2, descs1, descs2 = kps1[0], kps2[0], descs1[0], descs2[0]
... dists, idxs, num_rot = matcher(
- ... descs1, descs2, normalize=True, subset_size=1000,
+ ... descs1, descs2, normalize=True, subset_size=500,
... )
+ >>> idxs.shape == (dists.shape[0], 2), 0 <= num_rot < steerer_order
+ (True, True)
+ >>> bool((idxs[:, 0] < len(kps1)).all()), bool((idxs[:, 1] < len(kps2)).all())
+ (True, True)
>>> # print(f"{idxs.shape[0]} tentative matches with steered DeDoDe")
>>> # print(f"at rotation of {num_rot * 360 / steerer_order} degrees")