-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
308 lines (252 loc) · 8.5 KB
/
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
import torch
import torch.nn as nn
# feature extractor
from diffusion_net.layers import DiffusionNet
class toLimitshape(nn.Module):
"""Project vertex wise features towards the spectral space.
Then project towards the limit shape basis."""
def __init__(self):
super().__init__()
def forward(self, x, phi_pinv_shape, Y_pinv_shape):
# 1. to spectral space
# 2. to limit shape basis
x_emb = torch.matmul(Y_pinv_shape, torch.matmul(phi_pinv_shape, x))
return x_emb
class toTemplateshape(nn.Module):
"""Project shape features represented in the limit shape basis towards the spectral representation of the templates mesh.
Then project towards the template mesh."""
def __init__(self):
super().__init__()
def forward(self, x_emb, Y_template, Phi_template):
x = torch.matmul(Phi_template, torch.matmul(Y_template, x_emb))
return x
class concatenateTemplateFeatures(nn.Module):
"""Project shape features represented in the limit shape basis towards the spectral representation of the templates mesh.
Then project towards the template mesh."""
def __init__(self, Feature_template):
super().__init__()
self.concatenate_features = False
if Feature_template is not None:
self.concatenate_features = True
def forward(self, x, Features_template):
if self.concatenate_features:
x = torch.cat((x, Features_template), dim=-1)
return x
# limit shape diffusion net autoencoder. only trainable part are the diffusion net blocks
class LS_DF_net(nn.Module):
"""
Global model :
- diffusion net as feature extractor
- diffusion net in decoder
- project to limit shape in encoder
- project to template shape in decoder
- V2V loss
"""
def __init__(self, cfg):
super().__init__()
# feature extractor #
with_grad = True
self.encoder_diffnet = DiffusionNet(
C_in=cfg["dataset"]["ndim"],
C_out=cfg["diffnet"]["nfeature"],
C_width=cfg["diffnet"]["k_eig_enc"],
N_block=cfg["diffnet"]["N_block_enc"],
mlp_hidden_dims=[128, 128] * cfg["diffnet"]["expand_internal"],
dropout=cfg["diffnet"]["dropout"],
with_gradient_features=with_grad,
with_gradient_rotations=with_grad,
)
# projection to and from limit shape
self.toLimitshape = toLimitshape()
self.toTemplateshape = toTemplateshape()
self.concatenateTemplateFeatures = concatenateTemplateFeatures(
cfg["model"]["template_features"]
)
self.decoder_diffnet = DiffusionNet(
C_in=cfg["diffnet"]["nfeature"] + cfg["model"]["template_features_dim"],
C_out=cfg["dataset"]["ndim"],
C_width=cfg["diffnet"]["k_eig_dec"],
N_block=cfg["diffnet"]["N_block_dec"],
mlp_hidden_dims=[128, 128] * cfg["diffnet"]["expand_internal"],
dropout=cfg["diffnet"]["dropout"],
with_gradient_features=with_grad,
with_gradient_rotations=with_grad,
)
def encoder(
self,
verts,
phi_pinv_shape,
Y_pinv_shape,
mass,
LL,
evals,
evecs,
gradX,
gradY,
faces,
):
# get vertex-wise diffusionnet features
features = self.encoder_diffnet(
verts,
mass,
L=LL,
evals=evals,
evecs=evecs,
gradX=gradX,
gradY=gradY,
faces=faces,
)
emb = self.toLimitshape(features, phi_pinv_shape, Y_pinv_shape)
return emb
def decoder(
self,
emb,
Y_template,
Phi_template,
Features_template,
T_mass,
LL,
evals,
evecs,
gradX,
gradY,
faces,
):
T_features = self.toTemplateshape(emb, Y_template, Phi_template)
T_features = self.concatenateTemplateFeatures(T_features, Features_template)
# predict 3D coordinates
verts_reconstruct = self.decoder_diffnet(
T_features,
T_mass,
L=LL,
evals=evals,
evecs=evecs,
gradX=gradX,
gradY=gradY,
faces=faces,
)
return verts_reconstruct
def forward(self, batch):
verts, phi_pinv_shape, Y_pinv_shape = (
batch["shape"]["xyz"],
batch["shape"]["phi_pinv"],
batch["shape"]["Y_pinv"],
)
faces, mass, LL, evals, evecs, gradX, gradY = (
batch["shape"]["faces"],
batch["shape"]["mass"],
batch["shape"]["L"],
batch["shape"]["evals"],
batch["shape"]["evecs"],
batch["shape"]["gradX"],
batch["shape"]["gradY"],
)
Y_template, Phi_template = (batch["template"]["Y"], batch["template"]["phi"])
Features_template = batch["template"]["meshfeatures"]
T_faces, T_mass, T_LL, T_evals, T_evecs, T_gradX, T_gradY = (
batch["template"]["faces"],
batch["template"]["mass"],
batch["template"]["L"],
batch["template"]["evals"],
batch["template"]["evecs"],
batch["template"]["gradX"],
batch["template"]["gradY"],
)
# ## ENCODER
emb = self.encoder(
verts,
phi_pinv_shape,
Y_pinv_shape,
mass,
LL=LL,
evals=evals,
evecs=evecs,
gradX=gradX,
gradY=gradY,
faces=faces,
)
# ## DECODER
verts_reconstruct = self.decoder(
emb,
Y_template,
Phi_template,
Features_template,
T_mass,
LL=T_LL,
evals=T_evals,
evecs=T_evecs,
gradX=T_gradX,
gradY=T_gradY,
faces=T_faces,
)
return verts_reconstruct
def only_encoder(self, batch):
verts, phi_pinv_shape, Y_pinv_shape = (
batch["shape"]["xyz"],
batch["shape"]["phi_pinv"],
batch["shape"]["Y_pinv"],
)
faces, mass, LL, evals, evecs, gradX, gradY = (
batch["shape"]["faces"],
batch["shape"]["mass"],
batch["shape"]["L"],
batch["shape"]["evals"],
batch["shape"]["evecs"],
batch["shape"]["gradX"],
batch["shape"]["gradY"],
)
# ## ENCODER
emb = self.encoder(
verts,
phi_pinv_shape,
Y_pinv_shape,
mass,
LL=LL,
evals=evals,
evecs=evecs,
gradX=gradX,
gradY=gradY,
faces=faces,
)
return emb
def only_decoder(self, batch, emb):
Y_template, Phi_template = (batch["template"]["Y"], batch["template"]["phi"])
Features_template = batch["template"]["meshfeatures"]
T_faces, T_mass, T_LL, T_evals, T_evecs, T_gradX, T_gradY = (
batch["template"]["faces"],
batch["template"]["mass"],
batch["template"]["L"],
batch["template"]["evals"],
batch["template"]["evecs"],
batch["template"]["gradX"],
batch["template"]["gradY"],
)
# ## DECODER
verts_reconstruct = self.decoder(
emb,
Y_template,
Phi_template,
Features_template,
T_mass,
LL=T_LL,
evals=T_evals,
evecs=T_evecs,
gradX=T_gradX,
gradY=T_gradY,
faces=T_faces,
)
return verts_reconstruct
class p2p_to_FM(nn.Module):
"""Get the FM from the provided p2p maps from shape to template shape."""
def __init__(self):
super().__init__()
def forward(self, p2p_21, evects1, evects2):
"""
p2p_21 : (n2,) vertex to vertex map from target to source.
For each vertex on the target shape, gives the index of the corresponding vertex on mesh 1.
Can also be presented as a (n2,n1) sparse matrix.
eigvects1 : (n1,k1) eigenvectors on source mesh. Possibly subsampled on the first dimension.
eigvects2 : (n2,k2) eigenvectors on target mesh. Possibly subsampled on the first dimension.
"""
evects1_pb = evects1[p2p_21, :]
return evects2.T @ evects1_pb