-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patheval.py
321 lines (251 loc) · 12.1 KB
/
eval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import time
import argparse
import torch
import numpy as np
from tqdm import tqdm
import open3d as o3d
from nnutils.chamfer_distance import ChamferDistance
def visualize_occlusion_mask(occlusion_mask, world2grid):
dim_x = occlusion_mask.shape[0]
dim_y = occlusion_mask.shape[1]
dim_z = occlusion_mask.shape[2]
# Generate voxel indices.
x = torch.arange(dim_x, dtype=occlusion_mask.dtype, device=occlusion_mask.device)
y = torch.arange(dim_y, dtype=occlusion_mask.dtype, device=occlusion_mask.device)
z = torch.arange(dim_z, dtype=occlusion_mask.dtype, device=occlusion_mask.device)
grid_x, grid_y, grid_z = torch.meshgrid(x, y, z)
grid_xyz = torch.cat([
grid_x.view(dim_x, dim_y, dim_z, 1),
grid_y.view(dim_x, dim_y, dim_z, 1),
grid_z.view(dim_x, dim_y, dim_z, 1)
], dim=3)
# Filter visible points.
grid_xyz = grid_xyz[occlusion_mask > 0.5]
num_occluded_voxels = grid_xyz.shape[0]
# Transform voxels to world space.
grid2world = torch.inverse(world2grid)
R_grid2world = grid2world[:3, :3].view(1, 3, 3).expand(num_occluded_voxels, -1, -1)
t_grid2world = grid2world[:3, 3].view(1, 3, 1).expand(num_occluded_voxels, -1, -1)
grid_xyz_world = (torch.matmul(R_grid2world, grid_xyz.view(-1, 3, 1)) + t_grid2world).view(-1, 3)
return grid_xyz_world
def filter_occluded_points(points_pred, world2grid, occlusion_mask):
dim_x = occlusion_mask.shape[0]
dim_y = occlusion_mask.shape[1]
dim_z = occlusion_mask.shape[2]
num_points_pred = points_pred.shape[0]
# Transform points to bbox space.
R_world2grid = world2grid[:3, :3].view(1, 3, 3).expand(num_points_pred, -1, -1)
t_world2grid = world2grid[:3, 3].view(1, 3, 1).expand(num_points_pred, -1, -1)
points_pred_coords = (torch.matmul(R_world2grid, points_pred.view(num_points_pred, 3, 1)) + t_world2grid).view(num_points_pred, 3)
# Normalize to [-1, 1]^3 space.
# The world2grid transforms world positions to voxel centers, so we need to
# use "align_corners=True".
points_pred_coords[:, 0] /= (dim_x - 1)
points_pred_coords[:, 1] /= (dim_y - 1)
points_pred_coords[:, 2] /= (dim_z - 1)
points_pred_coords = points_pred_coords * 2 - 1
# Trilinearly interpolate occlusion mask.
# Occlusion mask is given as (x, y, z) storage, but the grid_sample method
# expects (c, z, y, x) storage.
visibility_mask = 1 - occlusion_mask.view(dim_x, dim_y, dim_z)
visibility_mask = visibility_mask.permute(2, 1, 0).contiguous()
visibility_mask = visibility_mask.view(1, 1, dim_z, dim_y, dim_x)
points_pred_coords = points_pred_coords.view(1, 1, 1, num_points_pred, 3)
points_pred_visibility = torch.nn.functional.grid_sample(
visibility_mask, points_pred_coords.cpu(), mode='bilinear', padding_mode='zeros', align_corners=True
).cuda()
points_pred_visibility = points_pred_visibility.view(num_points_pred)
eps = 1e-5
points_pred_visibility = points_pred_visibility >= 1 - eps
# Filter occluded predicted points.
if points_pred_visibility.sum() == 0:
# If no points are visible, we keep the original points, otherwise
# we would penalize the sample as if nothing is predicted.
print("All points occluded, keeping all predicted points!")
points_pred_visible = points_pred.clone()
else:
points_pred_visible = points_pred[points_pred_visibility]
return points_pred_visible
def main():
#####################################################################################
# Settings.
#####################################################################################
dist_threshold = 0.05
max_dist = 1.0
num_points_samples = 200000
#####################################################################################
# Parse command line arguments.
#####################################################################################
parser = argparse.ArgumentParser()
parser.add_argument('--groundtruth_dir', action='store', dest='groundtruth_dir', default='./data/groundtruth', help='Provide root directory of ground truth data')
parser.add_argument('--prediction_dir', action='store', dest='prediction_dir', default='./data/reconstructions', help='Provide root directory of prediction data')
args = parser.parse_args()
groundtruth_dir = args.groundtruth_dir
prediction_dir = args.prediction_dir
assert os.path.exists(groundtruth_dir)
#####################################################################################
# Evaluate every scene.
#####################################################################################
# Metrics
acc_sum = 0.0
compl_sum = 0.0
chamfer_sum = 0.0
prc_sum = 0.0
rec_sum = 0.0
f1_score_sum = 0.0
total_num_scenes = 0
scene_stats = []
chamfer_dist = ChamferDistance()
scene_ids = sorted(os.listdir(groundtruth_dir))
for scene_id in tqdm(scene_ids):
# Load predicted mesh.
missing_scene = False
mesh_pred_path = os.path.join(prediction_dir, "{}.ply".format(scene_id))
if not os.path.exists(mesh_pred_path):
# We have no extracted geometry, so we use default metrics for missing scene.
missing_scene = True
else:
mesh_pred = o3d.io.read_triangle_mesh(mesh_pred_path)
if np.asarray(mesh_pred.vertices).shape[0] <= 0 or np.asarray(mesh_pred.triangles).shape[0] <= 0:
# No vertices or faces present.
missing_scene = True
# If no result is present for the scene, we use the maximum errors.
if missing_scene:
# We use default metrics for missing scene.
print("Missing scene reconstruction: {0}".format(mesh_pred_path))
acc_sum += max_dist
compl_sum += max_dist
chamfer_sum += max_dist
prc_sum += 1.0
rec_sum += 0.0
f1_score_sum += 0.0
total_num_scenes += 1
continue
# Load groundtruth mesh.
mesh_gt_path = os.path.join(groundtruth_dir, scene_id, "mesh_gt.ply".format(scene_id))
mesh_gt = o3d.io.read_triangle_mesh(mesh_gt_path)
points_gt = np.asarray(mesh_gt.vertices)
# To have a fair comparison even in the case of different mesh resolutions,
# we always sample consistent amount of points on predicted mesh.
pcd_pred = mesh_pred.sample_points_uniformly(number_of_points=num_points_samples)
points_pred = np.asarray(pcd_pred.points)
# Load occlusion mask grid, with world2grid transform.
occlusion_mask_path = os.path.join(groundtruth_dir, scene_id, "occlusion_mask.npy")
occlusion_mask = np.load(occlusion_mask_path)
world2grid_path = os.path.join(groundtruth_dir, scene_id, "world2grid.txt")
world2grid = np.loadtxt(world2grid_path)
# Put data to device memory.
points_pred = torch.from_numpy(points_pred).float().cuda()
points_gt = torch.from_numpy(points_gt).float().cuda()
world2grid = torch.from_numpy(world2grid).float().cuda()
# We keep occlusion mask on host memory, since it can be very large for big scenes.
occlusion_mask = torch.from_numpy(occlusion_mask).float()
# Compute gt -> predicted distance.
dist2_gt2pred, _ = chamfer_dist(points_gt.unsqueeze(0), points_pred.unsqueeze(0))
dist_gt2pred = torch.sqrt(dist2_gt2pred)
dist_gt2pred[~torch.isfinite(dist_gt2pred)] = 0.0 # sqrt() operation is undefined for distance == 0
dist_gt2pred = torch.minimum(dist_gt2pred, max_dist * torch.ones_like(dist_gt2pred))
# Compute predicted -> gt distance.
# All occluded predicted points should be masked out for , to not
# penalize completion beyond groundtruth.
points_pred_visible = filter_occluded_points(points_pred, world2grid, occlusion_mask)
if points_pred_visible.shape[0] > 0:
dist2_pred2gt, _ = chamfer_dist(points_pred_visible.unsqueeze(0), points_gt.unsqueeze(0))
dist_pred2gt = torch.sqrt(dist2_pred2gt)
dist_pred2gt[~torch.isfinite(dist_pred2gt)] = 0.0 # sqrt() operation is undefined for distance == 0
dist_pred2gt = torch.minimum(dist_pred2gt, max_dist * torch.ones_like(dist_pred2gt))
# Geometry accuracy/completion/Chamfer.
if points_pred_visible.shape[0] > 0:
acc = torch.mean(dist_pred2gt).item()
else:
acc = max_dist
compl = torch.mean(dist_gt2pred).item()
chamfer = 0.5 * (acc + compl)
# Precision/recall/F1 score.
if points_pred_visible.shape[0] > 0:
prc = (dist_pred2gt <= dist_threshold).float().mean().item()
else:
prc = 0.0
rec = (dist_gt2pred <= dist_threshold).float().mean().item()
if prc + rec > 0:
f1_score = 2 * prc * rec / (prc + rec)
else:
f1_score = 0.0
# print("acc =", acc)
# print("compl =", compl)
# print("chamfer =", chamfer)
# print("prc =", prc)
# print("rec =", rec)
# print("f1_score =", f1_score)
# Update total metrics.
acc_sum += acc
compl_sum += compl
chamfer_sum += chamfer
prc_sum += prc
rec_sum += rec
f1_score_sum += f1_score
total_num_scenes += 1
# Update scene stats.
scene_stats.append({
"scene_id": scene_id,
"acc": acc,
"compl": compl,
"chamfer": chamfer,
"prc": prc,
"rec": rec,
"f1_score": f1_score
})
# Just for debugging: Visualize occluded points.
# occluded_pcd = o3d.geometry.PointCloud()
# occluded_pcd.points = o3d.utility.Vector3dVector(visualize_occlusion_mask(occlusion_mask, world2grid).cpu().numpy())
# occluded_pcd.paint_uniform_color([0.7, 0.0, 0.0])
# o3d.visualization.draw_geometries([mesh_gt, occluded_pcd], mesh_show_back_face=True)
#####################################################################################
# Report evaluation results.
#####################################################################################
# Report independent scene stats.
# Sort by speficied metric.
sorted_idxs = [i[0] for i in sorted(enumerate(scene_stats), key=lambda x:-x[1]["f1_score"])]
print()
print("#" * 50)
print("SCENE STATS")
print("#" * 50)
print()
num_best_scenes = 20
for i, idx in enumerate(sorted_idxs):
if i >= num_best_scenes:
break
print("Scene {0}: acc = {1}, compl = {2}, chamfer = {3}, prc = {4}, rec = {5}, f1_score = {6}".format(
scene_stats[idx]["scene_id"],
scene_stats[idx]["acc"], scene_stats[idx]["compl"], scene_stats[idx]["chamfer"],
scene_stats[idx]["prc"], scene_stats[idx]["rec"], scene_stats[idx]["f1_score"]
))
# Metrics summary.
mean_acc = acc_sum / total_num_scenes
mean_compl = compl_sum / total_num_scenes
mean_chamfer = chamfer_sum / total_num_scenes
mean_prc = prc_sum / total_num_scenes
mean_rec = rec_sum / total_num_scenes
mean_f1_score = f1_score_sum / total_num_scenes
metrics = {
"acc": mean_acc,
"compl": mean_compl,
"chamfer": mean_chamfer,
"prc": mean_prc,
"rec": mean_rec,
"f1_score": mean_f1_score
}
print()
print("#" * 50)
print("EVALUATION SUMMARY")
print("#" * 50)
print("{:<30} {}".format("GEOMETRY ACCURACY:", metrics["acc"]))
print("{:<30} {}".format("GEOMETRY COMPLETION:", metrics["compl"]))
print("{:<30} {}".format("CHAMFER:", metrics["chamfer"]))
print("{:<30} {}".format("PRECISION:", metrics["prc"]))
print("{:<30} {}".format("RECALL:", metrics["rec"]))
print("{:<30} {}".format("F1_SCORE:", metrics["f1_score"]))
if __name__=="__main__":
main()