dmytromishkin commited on
Commit
2633f6b
1 Parent(s): edc7860

Added a fix to the metric: corrected indexes mismatch, and added zeromean normalization

Browse files
Files changed (2) hide show
  1. hoho/wed.py +23 -6
  2. setup.py +1 -1
hoho/wed.py CHANGED
@@ -2,9 +2,20 @@ 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
 
@@ -15,7 +26,6 @@ def compute_WED(pd_vertices, pd_edges, gt_vertices, gt_edges, cv=1.0, ce=1.0, no
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:
@@ -32,13 +42,18 @@ def compute_WED(pd_vertices, pd_edges, gt_vertices, gt_edges, cv=1.0, ce=1.0, no
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 +61,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 zeromean_normalize(vertices):
7
+ vertices = np.array(vertices)
8
+ vertices = vertices - vertices.mean(axis=0)
9
+ vertices = vertices / (1e-6 + np.linalg.norm(vertices, axis=1)[:, None])
10
+ return vertices
11
+
12
+
13
  def compute_WED(pd_vertices, pd_edges, gt_vertices, gt_edges, cv=1.0, ce=1.0, normalized=True, squared=False):
14
  pd_vertices = np.array(pd_vertices)
15
  gt_vertices = np.array(gt_vertices)
16
+ pd_vertices = zeromean_normalize(pd_vertices)
17
+ gt_vertices = zeromean_normalize(gt_vertices)
18
+
19
  pd_edges = np.array(pd_edges)
20
  gt_edges = np.array(gt_edges)
21
 
 
26
  distances = cdist(pd_vertices, gt_vertices, metric='euclidean')
27
 
28
  row_ind, col_ind = linear_sum_assignment(distances)
 
29
  # Step 2: Vertex Translation
30
 
31
  if squared:
 
42
  insertion_costs = cv * len(unmatched_gt_indices) # Assuming a fixed cost for vertex insertion
43
 
44
  # Step 4: Edge Deletion and Insertion
45
+ 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]
46
+ pd_edges_set = set(map(tuple, [set(edge) for edge in updated_pd_edges]))
47
+ gt_edges_set = set(map(tuple, [set(edge) for edge in gt_edges]))
48
+
49
 
50
  # Delete edges not in ground truth
51
  edges_to_delete = pd_edges_set - gt_edges_set
52
+
53
+ #deletion_edge_costs = ce * sum(np.linalg.norm(pd_vertices[edge[0]] - pd_vertices[edge[1]]) for edge in edges_to_delete)
54
+ vert_tf = [np.where(col_ind == v)[0][0] if v in col_ind else 0 for v in range(len(gt_vertices))]
55
+ 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)
56
+
57
 
58
  # Insert missing edges from ground truth
59
  edges_to_insert = gt_edges_set - pd_edges_set
 
61
 
62
  # Step 5: Calculation of WED
63
  WED = translation_costs + deletion_costs + insertion_costs + deletion_edge_costs + insertion_edge_costs
64
+ print ("translation_costs, deletion_costs, insertion_costs, deletion_edge_costs, insertion_edge_costs")
65
+ print (translation_costs, deletion_costs, insertion_costs, deletion_edge_costs, insertion_edge_costs)
66
 
67
  if normalized:
68
  total_length_of_gt_edges = np.linalg.norm((gt_vertices[gt_edges[:, 0]] - gt_vertices[gt_edges[:, 1]]), axis=1).sum()
69
  WED = WED / total_length_of_gt_edges
70
+ print ("Total length", total_length_of_gt_edges)
71
  return WED
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.3',
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',