-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.py
137 lines (71 loc) · 3.69 KB
/
evaluate.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
from pathlib import Path
import numpy as np
import torch
import torch.nn as nn
from model.face_model import FaceModel
from data.shapenet import ShapeNet
from scipy.optimize import linear_sum_assignment
from scipy.spatial import distance_matrix
from torchgeometry.losses.focal import FocalLoss
from pytorch3d.structures import Meshes
from pytorch3d.loss import chamfer_distance
from pytorch3d.ops import sample_points_from_meshes
import tqdm
import pickle
import gc
def evaluate(model, val_dataloader, device, config):
x_indices = []
y_indices = []
loss_faces_val = 0
normal_sim_val = 0
for i in range(config["num_vertices"]):
for j in range(config["num_vertices"]):
x_indices.append(i)
y_indices.append(j)
for batch_val in tqdm.tqdm(val_dataloader):
predicted_vertices, predicted_faces, edge_idx, hv, gt_faces = batch_val
predicted_faces.float().requires_grad_(True)
predicted_vertices.float().requires_grad_(True)
edge_idx = edge_idx.to(config["device"]).squeeze().long().transpose(0,1)
hv = hv.to(config["device"]).float()
gt_faces = gt_faces.to(config["device"]).long()
with torch.no_grad():
mask = model(hv, edge_idx).argmax(1)
output_faces = predicted_faces[mask == 1]
gt_mesh = Meshes([predicted_vertices.squeeze()], [predicted_faces.squeeze()])
output_mesh = Meshes([predicted_vertices.squeeze()], [output_faces.squeeze()])
gt_points, gt_normals = sample_points_from_meshes(gt_mesh, num_samples=1000,return_normals=True)
output_points, output_normals = sample_points_from_meshes(output_mesh, num_samples=1000,return_normals=True)
loss_faces, normal_sim = chamfer_distance(output_points, gt_points, x_normals=output_normals, y_normals=gt_normals)
loss_faces_val += loss_faces
normal_sim_val += normal_sim
loss_faces_val /= len(val_dataloader)
normal_sim_val /= len(val_dataloader)
print(f'chamfer_loss: {loss_faces_val:.6f} | normal_sim: {normal_sim_val:.6f}')
def main(config):
torch.autograd.set_detect_anomaly(True)
# Declare device
device = config["device"]
print("Device: {}".format(device))
if not config.get('val_pickle'):
val_dataset = ShapeNet(sdf_path=config["sdf_path"],meshes_path=config["meshes_path"], class_mapping=config["class_mapping"], split = "val" if not config["is_overfit"] else "overfit", threshold=config["num_vertices"], num_trajectories=config["num_trajectories"], face_mode=True)
val_dataset.filter_data(classes=config["classes"])
else:
with open(config["val_pickle"], 'rb') as handle:
val_dataset = pickle.load(handle)
val_dataset.calculate_class_statistics()
val_dataloader = torch.utils.data.DataLoader(
val_dataset, # Datasets return data one sample at a time; Dataloaders use them and aggregate samples into batches
batch_size=config["batch_size"], # The size of batches is defined here
shuffle=False, # During validation, shuffling is not necessary anymore
num_workers=0, # Data is usually loaded in parallel by num_workers
pin_memory=True, # This is an implementation detail to speed up data uploading to the GPU
# worker_init_fn=val_dataset.worker_init_fn TODO: Uncomment this line if you are using shapenet_zip on Google Colab
)
# Instantiate model
model = FaceModel(config["feature_size"])
model.load_state_dict(torch.load(config['evaluate_checkpoint'], map_location='cpu'))
# Move model to specified device
model.to(device)
# Start training
evaluate(model, val_dataloader, device, config)