|
| 1 | +import torch |
| 2 | + |
| 3 | +from typing import Literal |
| 4 | + |
| 5 | +from vismatch import THIRD_PARTY_DIR, BaseMatcher # noqa: F401 |
| 6 | +from vismatch.utils import add_to_path, resize_to_divisible |
| 7 | + |
| 8 | +add_to_path(THIRD_PARTY_DIR.joinpath("LoMa/src")) |
| 9 | + |
| 10 | +from loma.loma import ( |
| 11 | + LoMaB, |
| 12 | + LoMaB128, |
| 13 | + LoMaL, |
| 14 | + LoMaG, |
| 15 | + LoMaR, |
| 16 | + LoMa, |
| 17 | + filter_matches, |
| 18 | + to_pixel_coords, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class LoMaMatcher(BaseMatcher): |
| 23 | + divisible_size = 14 # for DINOv2 in the descriptor of LoMa-{B, L, G, R}. LoMa-B128 can handle arbitrary resolutions and is more lightweight. |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + device="cpu", |
| 28 | + max_num_keypoints=2048, |
| 29 | + arch: Literal["LoMa-B", "LoMa-L", "LoMa-G", "LoMa-B128", "LoMa-R"] = "LoMa-B", |
| 30 | + **kwargs, |
| 31 | + ): |
| 32 | + super().__init__(device, **kwargs) |
| 33 | + self.max_num_keypoints = max_num_keypoints |
| 34 | + |
| 35 | + if arch == "LoMa-B": |
| 36 | + cfg = LoMaB() |
| 37 | + elif arch == "LoMa-L": |
| 38 | + cfg = LoMaL() |
| 39 | + elif arch == "LoMa-G": |
| 40 | + cfg = LoMaG() |
| 41 | + elif arch == "LoMa-B128": |
| 42 | + cfg = LoMaB128() |
| 43 | + elif arch == "LoMa-R": |
| 44 | + cfg = LoMaR() |
| 45 | + else: |
| 46 | + raise ValueError( |
| 47 | + f"Unsupported architecture '{arch}' for LoMa. Supported: 'LoMa-B', 'LoMa-L', 'LoMa-G', 'LoMa-B128', 'LoMa-R'." |
| 48 | + ) |
| 49 | + |
| 50 | + # This automatically loads weights using torch.hub.load_state_dict_from_url |
| 51 | + self.matcher = LoMa(cfg) |
| 52 | + |
| 53 | + def preprocess(self, img): |
| 54 | + _, h, w = img.shape |
| 55 | + orig_shape = h, w |
| 56 | + img = resize_to_divisible(img, self.divisible_size) |
| 57 | + img = img.unsqueeze(0) |
| 58 | + return img, orig_shape |
| 59 | + |
| 60 | + def _forward(self, img0, img1): |
| 61 | + img0, img0_orig_shape = self.preprocess(img0) |
| 62 | + img1, img1_orig_shape = self.preprocess(img1) |
| 63 | + |
| 64 | + H0, W0 = img0.shape[-2:] |
| 65 | + H1, W1 = img1.shape[-2:] |
| 66 | + |
| 67 | + kpts0, desc0, _, _ = self.matcher.detect_and_describe(img0, self.max_num_keypoints) |
| 68 | + kpts1, desc1, _, _ = self.matcher.detect_and_describe(img1, self.max_num_keypoints) |
| 69 | + |
| 70 | + scores = self.matcher(kpts0, kpts1, desc0, desc1)["scores"] |
| 71 | + m0, _, _, _ = filter_matches(scores, self.matcher.cfg.filter_threshold) |
| 72 | + |
| 73 | + valid = m0[0] > -1 |
| 74 | + matched_kpts0 = to_pixel_coords(kpts0[0][torch.where(valid)[0]], H0, W0) |
| 75 | + matched_kpts1 = to_pixel_coords(kpts1[0][m0[0][valid]], H1, W1) |
| 76 | + |
| 77 | + all_kpts0 = to_pixel_coords(kpts0[0], H0, W0) |
| 78 | + all_kpts1 = to_pixel_coords(kpts1[0], H1, W1) |
| 79 | + |
| 80 | + matched_kpts0 = self.rescale_coords(matched_kpts0, *img0_orig_shape, H0, W0) |
| 81 | + matched_kpts1 = self.rescale_coords(matched_kpts1, *img1_orig_shape, H1, W1) |
| 82 | + all_kpts0 = self.rescale_coords(all_kpts0, *img0_orig_shape, H0, W0) |
| 83 | + all_kpts1 = self.rescale_coords(all_kpts1, *img1_orig_shape, H1, W1) |
| 84 | + |
| 85 | + # LoMa uses COLMAP convention for pixel coords (see https://github.com/gmberton/vismatch/pull/63) so we subtact 0.5 for repo compatability |
| 86 | + offset = 0.5 |
| 87 | + matched_kpts0 -= offset |
| 88 | + matched_kpts1 -= offset |
| 89 | + all_kpts0 -= offset |
| 90 | + all_kpts1 -= offset |
| 91 | + |
| 92 | + return matched_kpts0, matched_kpts1, all_kpts0, all_kpts1, desc0[0], desc1[0] |
0 commit comments