-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit_data.py
158 lines (113 loc) · 5.3 KB
/
fit_data.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
import argparse
import os
import time
import pytorch3d
import losses
from pytorch3d.utils import ico_sphere
from r2n2_custom import R2N2
from pytorch3d.ops import sample_points_from_meshes
from pytorch3d.structures import Meshes
import dataset_location
import torch
def get_args_parser():
parser = argparse.ArgumentParser('Model Fit', add_help=False)
parser.add_argument('--lr', default=4e-4, type=float)
parser.add_argument('--max_iter', default=10000, type=int)
parser.add_argument('--log_freq', default=1000, type=int)
parser.add_argument('--type', default='vox', choices=['vox', 'point', 'mesh'], type=str)
parser.add_argument('--n_points', default=5000, type=int)
parser.add_argument('--w_chamfer', default=1.0, type=float)
parser.add_argument('--w_smooth', default=0.1, type=float)
parser.add_argument('--device', default='cuda', type=str)
return parser
def fit_mesh(mesh_src, mesh_tgt, args, device):
start_iter = 0
start_time = time.time()
deform_vertices_src = torch.zeros(mesh_src.verts_packed().shape, requires_grad=True, device=device)
optimizer = torch.optim.Adam([deform_vertices_src], lr = args.lr)
print("Starting training !")
for step in range(start_iter, args.max_iter):
iter_start_time = time.time()
new_mesh_src = mesh_src.offset_verts(deform_vertices_src)
sample_trg = sample_points_from_meshes(mesh_tgt, args.n_points)
sample_src = sample_points_from_meshes(new_mesh_src, args.n_points)
loss_reg = losses.chamfer_loss(sample_src, sample_trg, args.n_points)
loss_smooth = losses.smoothness_loss(new_mesh_src)
loss = args.w_chamfer * loss_reg + args.w_smooth * loss_smooth
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_time = time.time() - start_time
iter_time = time.time() - iter_start_time
loss_vis = loss.cpu().item()
print("[%4d/%4d]; time: %.0f (%.2f); loss: %.3f" % (step, args.max_iter, total_time, iter_time, loss_vis))
mesh_src.offset_verts_(deform_vertices_src)
print('Done!')
return new_mesh_src, mesh_tgt
def fit_pointcloud(pointclouds_src, pointclouds_tgt, args):
start_iter = 0
start_time = time.time()
optimizer = torch.optim.Adam([pointclouds_src], lr = args.lr)
for step in range(start_iter, args.max_iter):
iter_start_time = time.time()
loss = losses.chamfer_loss(pointclouds_src, pointclouds_tgt, n_points=10000)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_time = time.time() - start_time
iter_time = time.time() - iter_start_time
loss_vis = loss.cpu().item()
print("[%4d/%4d]; ttime: %.0f (%.2f); loss: %.3f" % (step, args.max_iter, total_time, iter_time, loss_vis))
print('Done!')
return pointclouds_src, pointclouds_tgt
def fit_voxel(voxels_src, voxels_tgt, args):
start_iter = 0
start_time = time.time()
optimizer = torch.optim.Adam([voxels_src], lr = args.lr)
for step in range(start_iter, args.max_iter):
iter_start_time = time.time()
loss = losses.voxel_loss(voxels_src,voxels_tgt)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_time = time.time() - start_time
iter_time = time.time() - iter_start_time
loss_vis = loss.cpu().item()
print("[%4d/%4d]; ttime: %.0f (%.2f); loss: %.3f" % (step, args.max_iter, total_time, iter_time, loss_vis))
print('Done!')
return voxels_src, voxels_tgt
def train_model(args):
r2n2_dataset = R2N2("train", dataset_location.SHAPENET_PATH, dataset_location.R2N2_PATH, dataset_location.SPLITS_PATH, return_voxels=True)
feed = r2n2_dataset[0]
feed_cuda = {}
for k in feed:
if torch.is_tensor(feed[k]):
feed_cuda[k] = feed[k].to(args.device).float()
if args.type == "vox":
# initialization
voxels_src = torch.rand(feed_cuda['voxels'].shape,requires_grad=True, device=args.device)
voxel_coords = feed_cuda['voxel_coords'].unsqueeze(0)
voxels_tgt = feed_cuda['voxels']
# fitting
voxels_src, voxels_tgt = fit_voxel(voxels_src, voxels_tgt, args)
# return voxels_src, voxels_tgt
elif args.type == "point":
# initialization
pointclouds_src = torch.randn([1,args.n_points,3],requires_grad=True, device=args.device)
mesh_tgt = Meshes(verts=[feed_cuda['verts']], faces=[feed_cuda['faces']])
pointclouds_tgt = sample_points_from_meshes(mesh_tgt, args.n_points)
# fitting
pointclouds_src, pointclouds_tgt = fit_pointcloud(pointclouds_src, pointclouds_tgt, args)
# return pointclouds_src, pointclouds_tgt
elif args.type == "mesh":
# initialization
# try different ways of initializing the source mesh
mesh_src = pytorch3d.utils.ico_sphere(4, args.device)
mesh_tgt = Meshes(verts=[feed_cuda['verts']], faces=[feed_cuda['faces']])
# fitting
mesh_src, mesh_tgt = fit_mesh(mesh_src, mesh_tgt, args, device=args.device)
# return mesh_src, mesh_tgt
if __name__ == '__main__':
parser = argparse.ArgumentParser('Model Fit', parents=[get_args_parser()])
args = parser.parse_args()
train_model(args)