megadepth_pose_estimation_benchmark.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import numpy as np
  2. import torch
  3. from romatch.utils import *
  4. from PIL import Image
  5. from tqdm import tqdm
  6. import torch.nn.functional as F
  7. import romatch
  8. import kornia.geometry.epipolar as kepi
  9. class MegaDepthPoseEstimationBenchmark:
  10. def __init__(self, data_root="data/megadepth", scene_names = None) -> None:
  11. if scene_names is None:
  12. self.scene_names = [
  13. "0015_0.1_0.3.npz",
  14. "0015_0.3_0.5.npz",
  15. "0022_0.1_0.3.npz",
  16. "0022_0.3_0.5.npz",
  17. "0022_0.5_0.7.npz",
  18. ]
  19. else:
  20. self.scene_names = scene_names
  21. self.scenes = [
  22. np.load(f"{data_root}/{scene}", allow_pickle=True)
  23. for scene in self.scene_names
  24. ]
  25. self.data_root = data_root
  26. def benchmark(self, model, model_name = None):
  27. with torch.no_grad():
  28. data_root = self.data_root
  29. tot_e_t, tot_e_R, tot_e_pose = [], [], []
  30. thresholds = [5, 10, 20]
  31. for scene_ind in range(len(self.scenes)):
  32. import os
  33. scene_name = os.path.splitext(self.scene_names[scene_ind])[0]
  34. scene = self.scenes[scene_ind]
  35. pairs = scene["pair_infos"]
  36. intrinsics = scene["intrinsics"]
  37. poses = scene["poses"]
  38. im_paths = scene["image_paths"]
  39. pair_inds = range(len(pairs))
  40. for pairind in tqdm(pair_inds):
  41. idx1, idx2 = pairs[pairind][0]
  42. K1 = intrinsics[idx1].copy()
  43. T1 = poses[idx1].copy()
  44. R1, t1 = T1[:3, :3], T1[:3, 3]
  45. K2 = intrinsics[idx2].copy()
  46. T2 = poses[idx2].copy()
  47. R2, t2 = T2[:3, :3], T2[:3, 3]
  48. R, t = compute_relative_pose(R1, t1, R2, t2)
  49. T1_to_2 = np.concatenate((R,t[:,None]), axis=-1)
  50. im_A_path = f"{data_root}/{im_paths[idx1]}"
  51. im_B_path = f"{data_root}/{im_paths[idx2]}"
  52. dense_matches, dense_certainty = model.match(
  53. im_A_path, im_B_path, K1.copy(), K2.copy(), T1_to_2.copy()
  54. )
  55. sparse_matches,_ = model.sample(
  56. dense_matches, dense_certainty, 5_000
  57. )
  58. im_A = Image.open(im_A_path)
  59. w1, h1 = im_A.size
  60. im_B = Image.open(im_B_path)
  61. w2, h2 = im_B.size
  62. if True: # Note: we keep this true as it was used in DKM/RoMa papers. There is very little difference compared to setting to False.
  63. scale1 = 1200 / max(w1, h1)
  64. scale2 = 1200 / max(w2, h2)
  65. w1, h1 = scale1 * w1, scale1 * h1
  66. w2, h2 = scale2 * w2, scale2 * h2
  67. K1, K2 = K1.copy(), K2.copy()
  68. K1[:2] = K1[:2] * scale1
  69. K2[:2] = K2[:2] * scale2
  70. kpts1, kpts2 = model.to_pixel_coordinates(sparse_matches, h1, w1, h2, w2)
  71. kpts1, kpts2 = kpts1.cpu().numpy(), kpts2.cpu().numpy()
  72. for _ in range(5):
  73. shuffling = np.random.permutation(np.arange(len(kpts1)))
  74. kpts1 = kpts1[shuffling]
  75. kpts2 = kpts2[shuffling]
  76. try:
  77. threshold = 0.5
  78. norm_threshold = threshold / (np.mean(np.abs(K1[:2, :2])) + np.mean(np.abs(K2[:2, :2])))
  79. R_est, t_est, mask = estimate_pose(
  80. kpts1,
  81. kpts2,
  82. K1,
  83. K2,
  84. norm_threshold,
  85. conf=0.99999,
  86. )
  87. T1_to_2_est = np.concatenate((R_est, t_est), axis=-1) #
  88. e_t, e_R = compute_pose_error(T1_to_2_est, R, t)
  89. e_pose = max(e_t, e_R)
  90. except Exception as e:
  91. print(repr(e))
  92. e_t, e_R = 90, 90
  93. e_pose = max(e_t, e_R)
  94. tot_e_t.append(e_t)
  95. tot_e_R.append(e_R)
  96. tot_e_pose.append(e_pose)
  97. tot_e_pose = np.array(tot_e_pose)
  98. auc = pose_auc(tot_e_pose, thresholds)
  99. acc_5 = (tot_e_pose < 5).mean()
  100. acc_10 = (tot_e_pose < 10).mean()
  101. acc_15 = (tot_e_pose < 15).mean()
  102. acc_20 = (tot_e_pose < 20).mean()
  103. map_5 = acc_5
  104. map_10 = np.mean([acc_5, acc_10])
  105. map_20 = np.mean([acc_5, acc_10, acc_15, acc_20])
  106. print(f"{model_name} auc: {auc}")
  107. return {
  108. "auc_5": auc[0],
  109. "auc_10": auc[1],
  110. "auc_20": auc[2],
  111. "map_5": map_5,
  112. "map_10": map_10,
  113. "map_20": map_20,
  114. }