xuelunshen commited on
Commit
35867bd
1 Parent(s): 29ecebd

update: gim

Browse files
common/utils.py CHANGED
@@ -14,7 +14,7 @@ from .viz import draw_matches, fig2im, plot_images, plot_color_line_matches
14
  device = "cuda" if torch.cuda.is_available() else "cpu"
15
 
16
  DEFAULT_SETTING_THRESHOLD = 0.1
17
- DEFAULT_SETTING_MAX_FEATURES = 2000
18
  DEFAULT_DEFAULT_KEYPOINT_THRESHOLD = 0.01
19
  DEFAULT_ENABLE_RANSAC = True
20
  DEFAULT_RANSAC_METHOD = "USAC_MAGSAC"
@@ -44,12 +44,7 @@ def gen_examples():
44
  "gim",
45
  "gim",
46
  "gim",
47
- "disk+lightglue",
48
- "loftr",
49
- "disk",
50
- "d2net",
51
- "superpoint+superglue",
52
- "disk+dualsoftmax",
53
  ]
54
 
55
  def gen_images_pairs(path: str, count: int = 5):
@@ -67,8 +62,9 @@ def gen_examples():
67
  pairs = gen_images_pairs(path, len(example_matchers))
68
  gim_pairs = [('datasets/gim/0a.png', 'datasets/gim/0b.png'),
69
  ('datasets/gim/1a.png', 'datasets/gim/1b.png'),
70
- ('datasets/gim/2a.png', 'datasets/gim/2b.png')]
71
- pairs = gim_pairs + pairs
 
72
  match_setting_threshold = DEFAULT_SETTING_THRESHOLD
73
  match_setting_max_features = DEFAULT_SETTING_MAX_FEATURES
74
  detect_keypoints_threshold = DEFAULT_DEFAULT_KEYPOINT_THRESHOLD
@@ -77,7 +73,7 @@ def gen_examples():
77
  ransac_confidence = DEFAULT_RANSAC_CONFIDENCE
78
  ransac_max_iter = DEFAULT_RANSAC_MAX_ITER
79
  input_lists = []
80
- for pair, mt in zip(pairs, example_matchers):
81
  input_lists.append(
82
  [
83
  pair[0],
 
14
  device = "cuda" if torch.cuda.is_available() else "cpu"
15
 
16
  DEFAULT_SETTING_THRESHOLD = 0.1
17
+ DEFAULT_SETTING_MAX_FEATURES = 4096
18
  DEFAULT_DEFAULT_KEYPOINT_THRESHOLD = 0.01
19
  DEFAULT_ENABLE_RANSAC = True
20
  DEFAULT_RANSAC_METHOD = "USAC_MAGSAC"
 
44
  "gim",
45
  "gim",
46
  "gim",
47
+ "gim",
 
 
 
 
 
48
  ]
49
 
50
  def gen_images_pairs(path: str, count: int = 5):
 
62
  pairs = gen_images_pairs(path, len(example_matchers))
63
  gim_pairs = [('datasets/gim/0a.png', 'datasets/gim/0b.png'),
64
  ('datasets/gim/1a.png', 'datasets/gim/1b.png'),
65
+ ('datasets/gim/2a.png', 'datasets/gim/2b.png'),
66
+ ('datasets/gim/3a.png', 'datasets/gim/3b.png')]
67
+ pairs = gim_pairs
68
  match_setting_threshold = DEFAULT_SETTING_THRESHOLD
69
  match_setting_max_features = DEFAULT_SETTING_MAX_FEATURES
70
  detect_keypoints_threshold = DEFAULT_DEFAULT_KEYPOINT_THRESHOLD
 
73
  ransac_confidence = DEFAULT_RANSAC_CONFIDENCE
74
  ransac_max_iter = DEFAULT_RANSAC_MAX_ITER
75
  input_lists = []
76
+ for pair, mt in zip(gim_pairs, example_matchers):
77
  input_lists.append(
78
  [
79
  pair[0],
datasets/gim/__init__.py DELETED
File without changes
hloc/match_dense.py CHANGED
@@ -14,16 +14,11 @@ confs = {
14
  "model": {
15
  "name": "gim",
16
  "weights": "gim_dkm_100h.ckpt",
17
- "max_keypoints": 2000,
18
  "match_threshold": 0.2,
19
  },
20
  "preprocessing": {
21
  "grayscale": False,
22
- "force_resize": True,
23
- "resize_max": 1024,
24
- "width": 80,
25
- "height": 60,
26
- "dfactor": 8,
27
  },
28
  },
29
  "loftr": {
@@ -279,12 +274,10 @@ def match(model, path_0, path_1, conf):
279
  def match_images(model, image_0, image_1, conf, device="cpu"):
280
  default_conf = {
281
  "grayscale": True,
282
- "resize_max": 1024,
283
  "dfactor": 8,
284
  "cache_images": False,
285
  "force_resize": False,
286
- "width": 320,
287
- "height": 240,
288
  }
289
 
290
  def preprocess(image: np.ndarray):
 
14
  "model": {
15
  "name": "gim",
16
  "weights": "gim_dkm_100h.ckpt",
17
+ "max_keypoints": 4096,
18
  "match_threshold": 0.2,
19
  },
20
  "preprocessing": {
21
  "grayscale": False,
 
 
 
 
 
22
  },
23
  },
24
  "loftr": {
 
274
  def match_images(model, image_0, image_1, conf, device="cpu"):
275
  default_conf = {
276
  "grayscale": True,
277
+ "resize_max": 8192,
278
  "dfactor": 8,
279
  "cache_images": False,
280
  "force_resize": False,
 
 
281
  }
282
 
283
  def preprocess(image: np.ndarray):
hloc/matchers/gim.py CHANGED
@@ -72,21 +72,38 @@ class GIM(BaseModel):
72
  # )
73
 
74
  image0, image1 = data['image0'], data['image1']
75
- orig_width = image0.shape[3]
76
- orig_height = image0.shape[2]
 
 
 
77
  aspect_ratio = 896 / 672
78
- new_width = max(orig_width, int(orig_height * aspect_ratio))
79
- new_height = max(orig_height, int(orig_width / aspect_ratio))
80
- pad_height = new_height - orig_height
81
- pad_width = new_width - orig_width
82
- pad_top = pad_height // 2
83
- pad_bottom = pad_height - pad_top
84
- pad_left = pad_width // 2
85
- pad_right = pad_width - pad_left
86
- image0 = torch.nn.functional.pad(image0, (pad_left, pad_right, pad_top, pad_bottom))
87
- image1 = torch.nn.functional.pad(image1, (pad_left, pad_right, pad_top, pad_bottom))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  dense_matches, dense_certainty = self.net.match(image0, image1)
89
- sparse_matches, mconf = self.net.sample(dense_matches, dense_certainty, 2048)
90
  height0, width0 = image0.shape[-2:]
91
  height1, width1 = image1.shape[-2:]
92
  kpts0 = sparse_matches[:, :2]
@@ -95,17 +112,17 @@ class GIM(BaseModel):
95
  kpts1 = torch.stack((width1 * (kpts1[:, 0] + 1) / 2, height1 * (kpts1[:, 1] + 1) / 2), dim=-1, )
96
  b_ids, i_ids = torch.where(mconf[None])
97
  # before padding
98
- kpts0 -= kpts0.new_tensor((pad_left, pad_top))[None]
99
- kpts1 -= kpts1.new_tensor((pad_left, pad_top))[None]
100
  mask = (kpts0[:, 0] > 0) & \
101
  (kpts0[:, 1] > 0) & \
102
  (kpts1[:, 0] > 0) & \
103
  (kpts1[:, 1] > 0)
104
  mask = mask & \
105
- (kpts0[:, 0] <= (orig_width - 1)) & \
106
- (kpts1[:, 0] <= (orig_width - 1)) & \
107
- (kpts0[:, 1] <= (orig_height - 1)) & \
108
- (kpts1[:, 1] <= (orig_height - 1))
109
  pred = {
110
  'keypoints0': kpts0[i_ids],
111
  'keypoints1': kpts1[i_ids],
 
72
  # )
73
 
74
  image0, image1 = data['image0'], data['image1']
75
+ print('1.', 'image0', image0.shape, 'image1', image1.shape)
76
+ orig_width0 = image0.shape[3]
77
+ orig_height0 = image0.shape[2]
78
+ orig_width1 = image1.shape[3]
79
+ orig_height1 = image1.shape[2]
80
  aspect_ratio = 896 / 672
81
+
82
+ new_width0 = max(orig_width0, int(orig_height0 * aspect_ratio))
83
+ new_height0 = max(orig_height0, int(orig_width0 / aspect_ratio))
84
+ new_width1 = max(orig_width1, int(orig_height1 * aspect_ratio))
85
+ new_height1 = max(orig_height1, int(orig_width1 / aspect_ratio))
86
+
87
+ new_width = max(new_width0, new_width1)
88
+ new_height = max(new_height0, new_height1)
89
+
90
+ pad_height0 = new_height - orig_height0
91
+ pad_width0 = new_width - orig_width0
92
+ pad_height1 = new_height - orig_height1
93
+ pad_width1 = new_width - orig_width1
94
+ pad_top0 = pad_height0 // 2
95
+ pad_bottom0 = pad_height0 - pad_top0
96
+ pad_left0 = pad_width0 // 2
97
+ pad_right0 = pad_width0 - pad_left0
98
+ pad_top1 = pad_height1 // 2
99
+ pad_bottom1 = pad_height1 - pad_top1
100
+ pad_left1 = pad_width1 // 2
101
+ pad_right1 = pad_width1 - pad_left1
102
+ image0 = torch.nn.functional.pad(image0, (pad_left0, pad_right0, pad_top0, pad_bottom0))
103
+ image1 = torch.nn.functional.pad(image1, (pad_left1, pad_right1, pad_top1, pad_bottom1))
104
+ print('2.', 'image0', image0.shape, 'image1', image1.shape)
105
  dense_matches, dense_certainty = self.net.match(image0, image1)
106
+ sparse_matches, mconf = self.net.sample(dense_matches, dense_certainty, self.conf["max_keypoints"])
107
  height0, width0 = image0.shape[-2:]
108
  height1, width1 = image1.shape[-2:]
109
  kpts0 = sparse_matches[:, :2]
 
112
  kpts1 = torch.stack((width1 * (kpts1[:, 0] + 1) / 2, height1 * (kpts1[:, 1] + 1) / 2), dim=-1, )
113
  b_ids, i_ids = torch.where(mconf[None])
114
  # before padding
115
+ kpts0 -= kpts0.new_tensor((pad_left0, pad_top0))[None]
116
+ kpts1 -= kpts1.new_tensor((pad_left1, pad_top1))[None]
117
  mask = (kpts0[:, 0] > 0) & \
118
  (kpts0[:, 1] > 0) & \
119
  (kpts1[:, 0] > 0) & \
120
  (kpts1[:, 1] > 0)
121
  mask = mask & \
122
+ (kpts0[:, 0] <= (orig_width0 - 1)) & \
123
+ (kpts1[:, 0] <= (orig_width1 - 1)) & \
124
+ (kpts0[:, 1] <= (orig_height0 - 1)) & \
125
+ (kpts1[:, 1] <= (orig_height1 - 1))
126
  pred = {
127
  'keypoints0': kpts0[i_ids],
128
  'keypoints1': kpts1[i_ids],