Files changed (4) hide show
  1. hoho/vis.py +3 -2
  2. hoho/wed.py +78 -22
  3. requirements.txt +7 -5
  4. setup.py +1 -1
hoho/vis.py CHANGED
@@ -133,7 +133,8 @@ def create_image_grid(images, target_length=312, num_per_row=2):
133
  return grid_img
134
 
135
 
136
- import matplotlib
 
137
  def visualize_depth(depth, min_depth=None, max_depth=None, cmap='rainbow'):
138
  depth = np.array(depth)
139
 
@@ -148,7 +149,7 @@ def visualize_depth(depth, min_depth=None, max_depth=None, cmap='rainbow'):
148
  depth = np.clip(depth, 0, 1)
149
 
150
  # Use the matplotlib colormap to convert the depth to an RGB image
151
- cmap = matplotlib.cm.get_cmap(cmap)
152
  depth_image = (cmap(depth) * 255).astype(np.uint8)
153
 
154
  # Convert the depth image to a PIL image
 
133
  return grid_img
134
 
135
 
136
+ import matplotlib.pyplot as plt
137
+
138
  def visualize_depth(depth, min_depth=None, max_depth=None, cmap='rainbow'):
139
  depth = np.array(depth)
140
 
 
149
  depth = np.clip(depth, 0, 1)
150
 
151
  # Use the matplotlib colormap to convert the depth to an RGB image
152
+ cmap = plt.get_cmap(cmap)
153
  depth_image = (cmap(depth) * 255).astype(np.uint8)
154
 
155
  # Convert the depth image to a PIL image
hoho/wed.py CHANGED
@@ -2,43 +2,97 @@ from scipy.spatial.distance import cdist
2
  from scipy.optimize import linear_sum_assignment
3
  import numpy as np
4
 
5
- def compute_WED(pd_vertices, pd_edges, gt_vertices, gt_edges, cv=1.0, ce=1.0, normalized=True, squared=False):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  pd_vertices = np.array(pd_vertices)
7
  gt_vertices = np.array(gt_vertices)
8
  pd_edges = np.array(pd_edges)
9
- gt_edges = np.array(gt_edges)
 
 
 
 
 
 
 
 
10
 
11
- # Step 1: Bipartite Matching
12
- if squared:
13
- distances = cdist(pd_vertices, gt_vertices, metric='sqeuclidean')
14
- else:
15
- distances = cdist(pd_vertices, gt_vertices, metric='euclidean')
16
 
 
 
17
  row_ind, col_ind = linear_sum_assignment(distances)
 
18
 
19
  # Step 2: Vertex Translation
 
20
 
21
- if squared:
22
- translation_costs = cv * np.sqrt(np.sum(distances[row_ind, col_ind]))
23
- else:
24
- translation_costs = cv * np.sum(distances[row_ind, col_ind])
25
-
26
- # Additional: Vertex Deletion
27
  unmatched_pd_indices = set(range(len(pd_vertices))) - set(row_ind)
28
- deletion_costs = cv * len(unmatched_pd_indices) # Assuming a fixed cost for vertex deletion
29
 
30
- # Step 3: Vertex Insertion
31
  unmatched_gt_indices = set(range(len(gt_vertices))) - set(col_ind)
32
- insertion_costs = cv * len(unmatched_gt_indices) # Assuming a fixed cost for vertex insertion
33
 
34
- # Step 4: Edge Deletion and Insertion
35
- updated_pd_edges = [(row_ind[np.where(col_ind == edge[0])[0][0]], row_ind[np.where(col_ind == edge[1])[0][0]]) for edge in pd_edges if edge[0] in col_ind and edge[1] in col_ind]
36
- pd_edges_set = set(map(tuple, updated_pd_edges))
37
- gt_edges_set = set(map(tuple, gt_edges))
 
38
 
39
  # Delete edges not in ground truth
40
  edges_to_delete = pd_edges_set - gt_edges_set
41
- deletion_edge_costs = ce * sum(np.linalg.norm(pd_vertices[edge[0]] - pd_vertices[edge[1]]) for edge in edges_to_delete)
 
 
 
42
 
43
  # Insert missing edges from ground truth
44
  edges_to_insert = gt_edges_set - pd_edges_set
@@ -46,9 +100,11 @@ def compute_WED(pd_vertices, pd_edges, gt_vertices, gt_edges, cv=1.0, ce=1.0, no
46
 
47
  # Step 5: Calculation of WED
48
  WED = translation_costs + deletion_costs + insertion_costs + deletion_edge_costs + insertion_edge_costs
 
49
 
50
  if normalized:
51
  total_length_of_gt_edges = np.linalg.norm((gt_vertices[gt_edges[:, 0]] - gt_vertices[gt_edges[:, 1]]), axis=1).sum()
52
  WED = WED / total_length_of_gt_edges
53
-
 
54
  return WED
 
2
  from scipy.optimize import linear_sum_assignment
3
  import numpy as np
4
 
5
+
6
+ def preregister_mean_std(verts_to_transform, target_verts, single_scale=True):
7
+ mu_target = target_verts.mean(axis=0)
8
+ mu_in = verts_to_transform.mean(axis=0)
9
+ std_target = np.std(target_verts, axis=0)
10
+ std_in = np.std(verts_to_transform, axis=0)
11
+
12
+ if np.any(std_in == 0):
13
+ std_in[std_in == 0] = 1
14
+ if np.any(std_target == 0):
15
+ std_target[std_target == 0] = 1
16
+ if np.any(np.isnan(std_in)):
17
+ std_in[np.isnan(std_in)] = 1
18
+ if np.any(np.isnan(std_target)):
19
+ std_target[np.isnan(std_target)] = 1
20
+
21
+ if single_scale:
22
+ std_target = np.linalg.norm(std_target)
23
+ std_in = np.linalg.norm(std_in)
24
+
25
+ transformed_verts = (verts_to_transform - mu_in) / std_in
26
+ transformed_verts = transformed_verts * std_target + mu_target
27
+
28
+ return transformed_verts
29
+
30
+
31
+ def update_cv(cv, gt_vertices):
32
+ if cv < 0:
33
+ diameter = cdist(gt_vertices, gt_vertices).max()
34
+ # Cost of adding or deleting a vertex is set to -cv times the diameter of the ground truth wireframe
35
+ cv = -cv * diameter
36
+ elif cv == 0:
37
+ # Cost of adding or deleting a vertex is set to the average distance of the ground truth vertices from their mean
38
+ cv = np.linalg.norm(np.mean(gt_vertices, axis=0) - gt_vertices, axis=1).mean()
39
+ return cv
40
+
41
+ def compute_WED(pd_vertices, pd_edges, gt_vertices, gt_edges, cv_ins=-1/2, cv_del=-1/4, ce=1.0, normalized=True, preregister=True, single_scale=True):
42
+ '''The function computes the Wireframe Edge Distance (WED) between two graphs.
43
+ pd_vertices: list of predicted vertices
44
+ pd_edges: list of predicted edges
45
+ gt_vertices: list of ground truth vertices
46
+ gt_edges: list of ground truth edges
47
+ cv_ins: vertex insertion cost: if positive, the cost in centimeters of inserting vertex, if negative, multiplies diameter to compute cost (default is -1/2)
48
+ cv_del: vertex deletion cost: if positive, the cost in centimeters of deleting a vertex, if negative, multiplies diameter to compute cost (default is -1/2)
49
+ ce: edge cost (multiplier of the edge length for edge deletion and insertion, default is 1.0)
50
+ normalized: if True, the WED is normalized by the total length of the ground truth edges
51
+ preregister: if True, the predicted vertices have their mean and scale matched to the ground truth vertices
52
+ '''
53
+
54
  pd_vertices = np.array(pd_vertices)
55
  gt_vertices = np.array(gt_vertices)
56
  pd_edges = np.array(pd_edges)
57
+ gt_edges = np.array(gt_edges)
58
+
59
+
60
+ cv_del = update_cv(cv_del, gt_vertices)
61
+ cv_ins = update_cv(cv_ins, gt_vertices)
62
+
63
+ # Step 0: Prenormalize / preregister
64
+ if preregister:
65
+ pd_vertices = preregister_mean_std(pd_vertices, gt_vertices, single_scale=single_scale)
66
 
 
 
 
 
 
67
 
68
+ # Step 1: Bipartite Matching
69
+ distances = cdist(pd_vertices, gt_vertices, metric='euclidean')
70
  row_ind, col_ind = linear_sum_assignment(distances)
71
+
72
 
73
  # Step 2: Vertex Translation
74
+ translation_costs = np.sum(distances[row_ind, col_ind])
75
 
76
+ # Step 3: Vertex Deletion
 
 
 
 
 
77
  unmatched_pd_indices = set(range(len(pd_vertices))) - set(row_ind)
78
+ deletion_costs = cv_del * len(unmatched_pd_indices)
79
 
80
+ # Step 4: Vertex Insertion
81
  unmatched_gt_indices = set(range(len(gt_vertices))) - set(col_ind)
82
+ insertion_costs = cv_ins * len(unmatched_gt_indices)
83
 
84
+ # Step 5: Edge Deletion and Insertion
85
+ updated_pd_edges = [(col_ind[np.where(row_ind == edge[0])[0][0]], col_ind[np.where(row_ind == edge[1])[0][0]]) for edge in pd_edges if edge[0] in row_ind and edge[1] in row_ind]
86
+ pd_edges_set = set(map(tuple, [set(edge) for edge in updated_pd_edges]))
87
+ gt_edges_set = set(map(tuple, [set(edge) for edge in gt_edges]))
88
+
89
 
90
  # Delete edges not in ground truth
91
  edges_to_delete = pd_edges_set - gt_edges_set
92
+
93
+ vert_tf = [np.where(col_ind == v)[0][0] if v in col_ind else 0 for v in range(len(gt_vertices))]
94
+ deletion_edge_costs = ce * sum(np.linalg.norm(pd_vertices[vert_tf[edge[0]]] - pd_vertices[vert_tf[edge[1]]]) for edge in edges_to_delete)
95
+
96
 
97
  # Insert missing edges from ground truth
98
  edges_to_insert = gt_edges_set - pd_edges_set
 
100
 
101
  # Step 5: Calculation of WED
102
  WED = translation_costs + deletion_costs + insertion_costs + deletion_edge_costs + insertion_edge_costs
103
+
104
 
105
  if normalized:
106
  total_length_of_gt_edges = np.linalg.norm((gt_vertices[gt_edges[:, 0]] - gt_vertices[gt_edges[:, 1]]), axis=1).sum()
107
  WED = WED / total_length_of_gt_edges
108
+
109
+ # print ("Total length", total_length_of_gt_edges)
110
  return WED
requirements.txt CHANGED
@@ -1,8 +1,10 @@
 
 
 
1
  numpy
2
  pillow
3
- webdataset
4
- trimesh
5
- scipy
6
- datasets
7
  pycolmap
8
- plotly
 
 
 
1
+ datasets
2
+ ipywidgets
3
+ matplotlib
4
  numpy
5
  pillow
6
+ plotly
 
 
 
7
  pycolmap
8
+ scipy
9
+ trimesh
10
+ webdataset
setup.py CHANGED
@@ -6,7 +6,7 @@ with open('requirements.txt') as f:
6
  required = f.read().splitlines()
7
 
8
  setup(name='hoho',
9
- version='0.0.2',
10
  description='Tools and utilites for the HoHo Dataset and S23DR Competition',
11
  url='usm3d.github.io',
12
  author='Jack Langerman, Dmytro Mishkin, S23DR Orgainizing Team',
 
6
  required = f.read().splitlines()
7
 
8
  setup(name='hoho',
9
+ version='0.0.4',
10
  description='Tools and utilites for the HoHo Dataset and S23DR Competition',
11
  url='usm3d.github.io',
12
  author='Jack Langerman, Dmytro Mishkin, S23DR Orgainizing Team',