-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_model.py
495 lines (407 loc) · 23 KB
/
eval_model.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import argparse
import time
import torch
from model import SingleViewto3D
from r2n2_custom import R2N2
from pytorch3d.datasets.r2n2.utils import collate_batched_R2N2
import dataset_location
import pytorch3d
from pytorch3d.ops import sample_points_from_meshes
from pytorch3d.ops import knn_points
import mcubes
import utils_vox
import matplotlib.pyplot as plt
from utils import get_device, get_mesh_renderer, get_points_renderer, unproject_depth_image
import numpy as np
import imageio
from PIL import Image, ImageDraw
def get_args_parser():
parser = argparse.ArgumentParser('Singleto3D', add_help=False)
parser.add_argument("--image_size", type=int, default=512)
parser.add_argument('--arch', default='resnet18', type=str)
parser.add_argument('--max_iter', default=10000, type=str)
parser.add_argument('--vis_freq', default=100, type=str)
parser.add_argument('--batch_size', default=8, type=str)
parser.add_argument('--num_workers', default=2, type=str)
parser.add_argument('--type', default='vox', choices=['vox', 'point', 'mesh'], type=str)
parser.add_argument('--n_points', default=10000, type=int)
parser.add_argument('--w_chamfer', default=1.0, type=float)
parser.add_argument('--w_smooth', default=0.5, type=float)
parser.add_argument('--load_checkpoint', action='store_true')
parser.add_argument('--device', default='cuda', type=str)
parser.add_argument('--load_feat', action='store_true')
return parser
def preprocess(feed_dict, args):
for k in ['images']:
feed_dict[k] = feed_dict[k].to(args.device)
images = feed_dict['images'].squeeze(1)
mesh = feed_dict['mesh']
if args.load_feat:
images = torch.stack(feed_dict['feats']).to(args.device)
return images, mesh
def save_plot(thresholds, avg_f1_score, args):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(thresholds, avg_f1_score, marker='o')
ax.set_xlabel('Threshold')
ax.set_ylabel('F1-score')
ax.set_title(f'Evaluation {args.type}')
plt.savefig(f'output/eval_{args.type}', bbox_inches='tight')
def compute_sampling_metrics(pred_points, gt_points, thresholds, eps=1e-8):
metrics = {}
lengths_pred = torch.full(
(pred_points.shape[0],), pred_points.shape[1], dtype=torch.int64, device=pred_points.device
)
lengths_gt = torch.full(
(gt_points.shape[0],), gt_points.shape[1], dtype=torch.int64, device=gt_points.device
)
# For each predicted point, find its neareast-neighbor GT point
knn_pred = knn_points(pred_points, gt_points, lengths1=lengths_pred, lengths2=lengths_gt, K=1)
# Compute L1 and L2 distances between each pred point and its nearest GT
pred_to_gt_dists2 = knn_pred.dists[..., 0] # (N, S)
pred_to_gt_dists = pred_to_gt_dists2.sqrt() # (N, S)
# For each GT point, find its nearest-neighbor predicted point
knn_gt = knn_points(gt_points, pred_points, lengths1=lengths_gt, lengths2=lengths_pred, K=1)
# Compute L1 and L2 dists between each GT point and its nearest pred point
gt_to_pred_dists2 = knn_gt.dists[..., 0] # (N, S)
gt_to_pred_dists = gt_to_pred_dists2.sqrt() # (N, S)
# Compute precision, recall, and F1 based on L2 distances
for t in thresholds:
precision = 100.0 * (pred_to_gt_dists < t).float().mean(dim=1)
recall = 100.0 * (gt_to_pred_dists < t).float().mean(dim=1)
f1 = (2.0 * precision * recall) / (precision + recall + eps)
metrics["Precision@%f" % t] = precision
metrics["Recall@%f" % t] = recall
metrics["F1@%f" % t] = f1
# Move all metrics to CPU
metrics = {k: v.cpu() for k, v in metrics.items()}
return metrics
def evaluate(predictions, mesh_gt, thresholds, args):
if args.type == "vox":
voxels_src = predictions
H,W,D = voxels_src.shape[2:]
print(voxels_src.shape)
vertices_src, faces_src = mcubes.marching_cubes(voxels_src.detach().cpu().squeeze().numpy(), isovalue=0.0)
vertices_src = torch.tensor(vertices_src).float()
faces_src = torch.tensor(faces_src.astype(int))
mesh_src = pytorch3d.structures.Meshes([vertices_src], [faces_src])
pred_points = sample_points_from_meshes(mesh_src, args.n_points)
pred_points = utils_vox.Mem2Ref(pred_points, H, W, D)
elif args.type == "point":
pred_points = predictions.cpu()
elif args.type == "mesh":
pred_points = sample_points_from_meshes(predictions, args.n_points).cpu()
gt_points = sample_points_from_meshes(mesh_gt, args.n_points)
metrics = compute_sampling_metrics(pred_points, gt_points, thresholds)
return metrics
def evaluate_model(args, voxel_size = 32, device = 'cuda', duration = 200,):
r2n2_dataset = R2N2("test", dataset_location.SHAPENET_PATH, dataset_location.R2N2_PATH, dataset_location.SPLITS_PATH, return_voxels=True, return_feats=args.load_feat)
loader = torch.utils.data.DataLoader(
r2n2_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers,
collate_fn=collate_batched_R2N2,
pin_memory=True,
drop_last=True)
eval_loader = iter(loader)
model = SingleViewto3D(args)
model.to(args.device)
model.eval()
start_iter = 0
start_time = time.time()
thresholds = [0.01, 0.02, 0.03, 0.04, 0.05]
avg_f1_score_05 = []
avg_f1_score = []
avg_p_score = []
avg_r_score = []
if args.load_checkpoint:
checkpoint = torch.load(f'checkpoint_{args.type}.pth')
model.load_state_dict(checkpoint['model_state_dict'])
print(f"Succesfully loaded iter {start_iter}")
print("Starting evaluating !")
max_iter = len(eval_loader)
for step in range(start_iter, max_iter):
iter_start_time = time.time()
read_start_time = time.time()
feed_dict = next(eval_loader)
images_gt, mesh_gt = preprocess(feed_dict, args)
read_time = time.time() - read_start_time
predictions = model(images_gt, args)
if args.type == "vox":
predictions = predictions.permute(0,1,4,3,2)
metrics = evaluate(predictions, mesh_gt, thresholds, args)
color = [0, 0.7, 0.7]
# TODO:
# if (step % args.vis_freq) == 0:
# visualization block
img_step = step % 100
num = step // 100
if img_step == 0:
# ---------- VOXEL ------------
if args.type == "vox":
rgb_image = images_gt.squeeze()
rgb_image = (rgb_image*255).byte().cpu().numpy()
plt.imsave(f"output/RGB_voxel_{num}.png", rgb_image)
#-------------------- ground truth mesh -------------------------------
renderer = get_mesh_renderer(image_size=args.image_size)
mesh = mesh_gt
textures = torch.ones_like(mesh.verts_list()[0].unsqueeze(0), device = 'cuda')
textures = textures * torch.tensor([0.7, 0.7, 1], device = 'cuda')
mesh.textures=pytorch3d.renderer.TexturesVertex(textures)
mesh = mesh.to(device)
# Initialize an empty list to store rendered images
renders = []
for theta in range(0, 360, 10):
R = torch.tensor([
[np.cos(np.radians(theta)), 0.0, -np.sin(np.radians(theta))],
[0.0, 1.0, 0.0],
[np.sin(np.radians(theta)), 0.0, np.cos(np.radians(theta))]
])
T = torch.tensor([[0, 0, 2]]) # Move the camera to the side
cameras = pytorch3d.renderer.FoVPerspectiveCameras(R=R.unsqueeze(0), T=T, fov=60, device=device)
lights = pytorch3d.renderer.PointLights(location=[[0, 0, -3]], device='cuda')
rend = renderer(mesh, cameras=cameras, lights=lights)
rend = rend[0, ..., :3].detach().cpu().numpy() # (N, H, W, 3)
renders.append(rend)
images = []
for i, r in enumerate(renders):
image = Image.fromarray((r * 255).astype(np.uint8))
images.append(np.array(image))
imageio.mimsave(f"output/SingletoVox_gtmesh_{num}.gif", images, duration=duration, loop = 0)
# ------------------- voxel ground truth ---------------------------
vox_gt = feed_dict['voxels'].to(args.device)
# get the renderer
renderer = get_mesh_renderer(image_size = 512)
mesh_vgt = pytorch3d.ops.cubify(vox_gt[0], thresh=0.5)
textures = torch.ones_like(mesh_vgt.verts_list()[0].unsqueeze(0))
textures = textures * torch.tensor([0.7, 0.7, 1], device='cuda')
mesh_vgt.textures=pytorch3d.renderer.TexturesVertex(textures)
mesh_vgt = mesh_vgt.to(device)
# Initialize an empty list to store rendered images
renders = []
for theta in range(0, 360, 10):
R = torch.tensor([
[np.cos(np.radians(theta)), 0.0, -np.sin(np.radians(theta))],
[0.0, 1.0, 0.0],
[np.sin(np.radians(theta)), 0.0, np.cos(np.radians(theta))]
])
T = torch.tensor([[0, 0, 3]]) # Move the camera to the side
# prepare the camera:
cameras = pytorch3d.renderer.FoVPerspectiveCameras(R = R.unsqueeze(0), T = T, fov=60, device='cuda')
lights = pytorch3d.renderer.PointLights(location = [[0, 0, -4]], device = 'cuda')
rend = renderer(mesh_vgt, cameras=cameras, lights=lights)
rend = rend[0, ..., :3].cpu().detach().numpy().clip(0,1) # (B, H, W, 4) -> (H, W, 3)
renders.append(rend)
images = []
for i, r in enumerate(renders):
image = Image.fromarray((r * 255).clip(0,255).astype(np.uint8))
images.append(np.array(image))
imageio.mimsave(f"output/SingletoVox_gtvox_{num}.gif", images, duration=duration, loop = 0)
# ------------------- voxel prediction ---------------------------
# get the renderer
renderer = get_mesh_renderer(image_size = 512)
mesh_vpred = pytorch3d.ops.cubify(predictions[0], thresh=0.0)
textures = torch.ones_like(mesh_vpred.verts_list()[0].unsqueeze(0))
textures = textures * torch.tensor([0.7, 0.7, 1], device='cuda')
mesh_vpred.textures=pytorch3d.renderer.TexturesVertex(textures)
mesh_vpred = mesh_vpred.to(device)
# Initialize an empty list to store rendered images
renders = []
for theta in range(0, 360, 10):
R = torch.tensor([
[np.cos(np.radians(theta)), 0.0, -np.sin(np.radians(theta))],
[0.0, 1.0, 0.0],
[np.sin(np.radians(theta)), 0.0, np.cos(np.radians(theta))]
])
T = torch.tensor([[0, 0, 3]]) # Move the camera to the side
# prepare the camera:
cameras = pytorch3d.renderer.FoVPerspectiveCameras(R = R.unsqueeze(0), T = T, fov=60, device='cuda')
lights = pytorch3d.renderer.PointLights(location = [[0, 0, -4]], device = 'cuda')
rend = renderer(mesh_vpred, cameras=cameras, lights=lights)
rend = rend[0, ..., :3].cpu().detach().numpy().clip(0,1) # (B, H, W, 4) -> (H, W, 3)
renders.append(rend)
images = []
for i, r in enumerate(renders):
image = Image.fromarray((r * 255).clip(0,255).astype(np.uint8))
images.append(np.array(image))
imageio.mimsave(f"output/SingletoVox_pred_{num}.gif", images, duration=duration, loop = 0)
# ---------- POINT CLOUD ------------
if args.type == "point":
rgb_image = images_gt.squeeze()
rgb_image = (rgb_image*255).byte().cpu().numpy()
plt.imsave(f"output/RGB_pointcloud_{num}.png", rgb_image)
#-------------------- ground truth mesh -------------------------------
renderer = get_mesh_renderer(image_size=args.image_size)
vertices = mesh_gt.verts_list()
faces = mesh_gt.faces_list()
vertices = vertices[0] # (N_v, 3) -> (N_v, 3)
faces = faces[0] # (N_f, 3) -> (N_f, 3)
vertices = vertices.unsqueeze(0) # (N_v, 3) -> (1, N_v, 3)
faces = faces.unsqueeze(0) # (N_f, 3) -> (1, N_f, 3)
textures = torch.ones_like(vertices) # (1, N_v, 3)
# textures = textures * torch.tensor(color).to(device) # (1, N_v, 3)
textures = textures * torch.tensor(color)
mesh = pytorch3d.structures.Meshes(
verts=vertices,
faces=faces,
textures=pytorch3d.renderer.TexturesVertex(textures),
)
mesh = mesh.to(device)
# Initialize an empty list to store rendered images
renders = []
for theta in range(0, 360, 10):
R = torch.tensor([
[np.cos(np.radians(theta)), 0.0, -np.sin(np.radians(theta))],
[0.0, 1.0, 0.0],
[np.sin(np.radians(theta)), 0.0, np.cos(np.radians(theta))]
])
T = torch.tensor([[0, 0, 2]]) # Move the camera to the side
cameras = pytorch3d.renderer.FoVPerspectiveCameras(R=R.unsqueeze(0), T=T, fov=60, device=device)
lights = pytorch3d.renderer.PointLights(location=[[0, 0, -3]], device=device)
rend = renderer(mesh, cameras=cameras, lights=lights)
rend = rend[0, ..., :3].detach().cpu().numpy() # (N, H, W, 3)
renders.append(rend)
images = []
for i, r in enumerate(renders):
image = Image.fromarray((r * 255).astype(np.uint8))
images.append(np.array(image))
imageio.mimsave(f"output/SingletoPC_gtmesh_{num}.gif", images, duration=duration, loop = 0)
# ------------------- point cloud prediction ---------------------------
pointcloud_tgt = predictions[0].to(device)
points = pointcloud_tgt
color = (points - points.min()) / (points.max() - points.min())
sphere_point_cloud = pytorch3d.structures.Pointclouds(points=[points],
features=[color],)
renders = []
for theta in range(0, 360, 10):
R = torch.tensor([
[np.cos(np.radians(theta)), 0.0, -np.sin(np.radians(theta))],
[0.0, 1.0, 0.0],
[np.sin(np.radians(theta)), 0.0, np.cos(np.radians(theta))]
])
cameras = pytorch3d.renderer.FoVPerspectiveCameras(R=R.unsqueeze(0), T=[[0, 0, 2]], device=device)
renderer = get_points_renderer(image_size=args.image_size, device=device)
rend = renderer(sphere_point_cloud, cameras=cameras)
rend = rend[0, ..., :3].cpu().detach().numpy()
renders.append(rend)
images = []
for i, r in enumerate(renders):
image = Image.fromarray((r * 255).astype(np.uint8))
draw = ImageDraw.Draw(image)
images.append(np.array(image))
imageio.mimsave(f"output/SingletoPC_pred_{num}.gif", images, duration=4, loop = 0) # change here
# ------------------- point cloud ground truth ---------------------------
gt_points = sample_points_from_meshes(mesh_gt, args.n_points)
pointcloud_tgt = gt_points[0].to(device)
points = pointcloud_tgt
color = (points - points.min()) / (points.max() - points.min())
sphere_point_cloud = pytorch3d.structures.Pointclouds(points=[points],
features=[color],)
renders = []
for theta in range(0, 360, 10):
R = torch.tensor([
[np.cos(np.radians(theta)), 0.0, -np.sin(np.radians(theta))],
[0.0, 1.0, 0.0],
[np.sin(np.radians(theta)), 0.0, np.cos(np.radians(theta))]
])
cameras = pytorch3d.renderer.FoVPerspectiveCameras(R=R.unsqueeze(0), T=[[0, 0, 2]], device=device)
renderer = get_points_renderer(image_size=args.image_size, device=device)
rend = renderer(sphere_point_cloud, cameras=cameras)
rend = rend[0, ..., :3].cpu().detach().numpy()
renders.append(rend)
images = []
for i, r in enumerate(renders):
image = Image.fromarray((r * 255).astype(np.uint8))
draw = ImageDraw.Draw(image)
images.append(np.array(image))
imageio.mimsave(f"output/SingletoPC_gt_{num}.gif", images, duration=4, loop = 0) # change here
# ---------- MESH ------------
if args.type == "mesh":
rgb_image = images_gt.squeeze()
rgb_image = (rgb_image*255).byte().cpu().numpy()
plt.imsave(f"output/RGB_mesh_{num}.png", rgb_image)
# ------------------- mesh prediction ---------------------------
renderer = get_mesh_renderer(image_size=args.image_size)
vertices = predictions.verts_list()
faces = predictions.faces_list()
vertices = vertices[0].to(device) # (N_v, 3) -> (N_v, 3)
faces = faces[0].to(device) # (N_f, 3) -> (N_f, 3)
vertices = vertices.unsqueeze(0) # (N_v, 3) -> (1, N_v, 3)
faces = faces.unsqueeze(0) # (N_f, 3) -> (1, N_f, 3)
textures = torch.ones_like(vertices) # (1, N_v, 3)
textures = textures * torch.tensor(color).to(device) # (1, N_v, 3)
mesh = pytorch3d.structures.Meshes(
verts=vertices,
faces=faces,
textures=pytorch3d.renderer.TexturesVertex(textures),
)
mesh = mesh.to(device)
# Initialize an empty list to store rendered images
renders = []
for theta in range(0, 360, 10):
R = torch.tensor([
[np.cos(np.radians(theta)), 0.0, -np.sin(np.radians(theta))],
[0.0, 1.0, 0.0],
[np.sin(np.radians(theta)), 0.0, np.cos(np.radians(theta))]
])
T = torch.tensor([[0, 0, 2]]) # Move the camera to the side
cameras = pytorch3d.renderer.FoVPerspectiveCameras(R=R.unsqueeze(0), T=T, fov=60, device=device)
lights = pytorch3d.renderer.PointLights(location=[[0, 0, -3]], device=device)
rend = renderer(mesh, cameras=cameras, lights=lights)
rend = rend[0, ..., :3].detach().cpu().numpy() # (N, H, W, 3)
renders.append(rend)
images = []
for i, r in enumerate(renders):
image = Image.fromarray((r * 255).astype(np.uint8))
images.append(np.array(image))
imageio.mimsave(f"output/SingletoMesh_pred_{num}.gif", images, duration=duration, loop = 0)
# ------------------- ground truth mesh ---------------------------
renderer = get_mesh_renderer(image_size=args.image_size)
vertices = mesh_gt.verts_list()
faces = mesh_gt.faces_list()
vertices = vertices[0].to(device) # (N_v, 3) -> (N_v, 3)
faces = faces[0].to(device) # (N_f, 3) -> (N_f, 3)
vertices = vertices.unsqueeze(0) # (N_v, 3) -> (1, N_v, 3)
faces = faces.unsqueeze(0) # (N_f, 3) -> (1, N_f, 3)
textures = torch.ones_like(vertices) # (1, N_v, 3)
textures = textures * torch.tensor(color).to(device) # (1, N_v, 3)
mesh = pytorch3d.structures.Meshes(
verts=vertices,
faces=faces,
textures=pytorch3d.renderer.TexturesVertex(textures),
)
mesh = mesh.to(device)
# Initialize an empty list to store rendered images
renders = []
for theta in range(0, 360, 10):
R = torch.tensor([
[np.cos(np.radians(theta)), 0.0, -np.sin(np.radians(theta))],
[0.0, 1.0, 0.0],
[np.sin(np.radians(theta)), 0.0, np.cos(np.radians(theta))]
])
T = torch.tensor([[0, 0, 2]]) # Move the camera to the side
cameras = pytorch3d.renderer.FoVPerspectiveCameras(R=R.unsqueeze(0), T=T, fov=60, device=device)
lights = pytorch3d.renderer.PointLights(location=[[0, 0, -3]], device=device)
rend = renderer(mesh, cameras=cameras, lights=lights)
rend = rend[0, ..., :3].detach().cpu().numpy() # (N, H, W, 3)
renders.append(rend)
images = []
for i, r in enumerate(renders):
image = Image.fromarray((r * 255).astype(np.uint8))
images.append(np.array(image))
imageio.mimsave(f"output/SingletoMesh_gtmesh_{num}.gif", images, duration=duration, loop = 0)
total_time = time.time() - start_time
iter_time = time.time() - iter_start_time
f1_05 = metrics['F1@0.050000']
avg_f1_score_05.append(f1_05)
avg_p_score.append(torch.tensor([metrics["Precision@%f" % t] for t in thresholds]))
avg_r_score.append(torch.tensor([metrics["Recall@%f" % t] for t in thresholds]))
avg_f1_score.append(torch.tensor([metrics["F1@%f" % t] for t in thresholds]))
print("[%4d/%4d]; ttime: %.0f (%.2f, %.2f); F1@0.05: %.3f; Avg F1@0.05: %.3f" % (step, max_iter, total_time, read_time, iter_time, f1_05, torch.tensor(avg_f1_score_05).mean()))
avg_f1_score = torch.stack(avg_f1_score).mean(0)
save_plot(thresholds, avg_f1_score, args)
print('Done!')
if __name__ == '__main__':
parser = argparse.ArgumentParser('Singleto3D', parents=[get_args_parser()])
args = parser.parse_args()
evaluate_model(args, voxel_size = 32, device = 'cuda', duration = 200,)