forked from SamsungLabs/genren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape_adversaries.py
395 lines (347 loc) · 16.2 KB
/
shape_adversaries.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
import torch, torch.nn as nn, torch.nn.functional as F
#import torch3d.models as models
from torch.autograd import Variable
from networks import GraphResNet
from networks.networks import *
from networks.helpers import GlobalMean
class ShapeAdversarySimplePC(nn.Module):
def __init__(self, nfeats=3):
super(ShapeAdversarySimplePC, self).__init__()
self.f = PointNetT(nfeats, 1)
self.mse = torch.nn.MSELoss()
def forward(self, for_gen, pc_fake, pc_real=None):
if for_gen:
return self.compute_loss_for_generator(pc_fake)
else:
device = pc_fake.device
valid = Variable(torch.Tensor(pc_fake.shape[0], 1).fill_(1.0), requires_grad=False).to(device)
fake = Variable(torch.Tensor(pc_real.shape[0], 1).fill_(0.0), requires_grad=False).to(device)
real_loss = self.mse( self.f(pc_real), valid)
fake_loss = self.mse( self.f(pc_fake.detach()), fake)
d_loss = 0.5 * (real_loss + fake_loss)
return d_loss
def compute_loss_for_generator(self, pc):
return (self.f(pc) - 1.0).pow(2).mean() * 0.5
class FcTemplatePositionShapeAdversary(nn.Module):
"""
Operates on deformed template vertex locations only.
Due to the correspondence between template nodes,
we can use simple FC networks.
This does assume that the mapping from true_mesh to
deformed template is of decent quality.
"""
def __init__(self, template_size):
super(FcTemplatePositionShapeAdversary, self).__init__()
self.nf = template_size * 3
self.mse = torch.nn.MSELoss()
self.f = nn.Sequential(
Unfolder(),
LBA_stack( sizes = (self.nf, 512, 256, 1),
norm_type = 'spectral', # 'batch',
act_type = 'elu',
end_with_lin = True )
)
def forward(self, for_gen, fakes, reals=None):
if for_gen:
return self.compute_loss_for_generator(fakes)
else:
real_scores = self.f(reals)
fake_scores = self.f(fakes)
B = real_scores.shape[0]
device = fake_scores.device
valid = Variable(torch.Tensor(B, 1).fill_(1.0), requires_grad=False).to(device)
fake = Variable(torch.Tensor(B, 1).fill_(0.0), requires_grad=False).to(device)
real_loss = self.mse( real_scores, valid)
fake_loss = self.mse( fake_scores, fake)
d_loss = 0.5 * (real_loss + fake_loss)
return d_loss
def compute_loss_for_generator(self, fakes):
return (self.f(fakes) - 1.0).pow(2).mean() * 0.5
class ComTwoStageShapeAdversary(nn.Module):
def __init__(self, nTV, dim_lat_pert):
super(ComTwoStageShapeAdversary, self).__init__()
self.nf = nTV * 3
self.dim_v = dim_lat_pert
self.mse = torch.nn.MSELoss()
self.intdim = 128
# Critic on latent perturbations
self.f_v = LBA_stack( sizes = (self.dim_v, 256, self.intdim),
norm_type = 'spectral', #
act_type = 'elu',
end_with_lin = False )
# Critic on template vertices
self.f_tverts = nn.Sequential(
Unfolder(),
LBA_stack( sizes = (self.nf, 512, self.intdim),
norm_type = 'spectral', #
act_type = 'elu',
end_with_lin = False ) )
# Combined critic
self.com_crit = nn.Sequential(
LBA_stack( sizes = (self.intdim * 2, 128, 1),
norm_type = 'spectral', #
act_type = 'elu',
end_with_lin = True ) )
def f(self, x):
return self.com_crit(
torch.cat(
( self.f_v(x[0]), self.f_tverts(x[1]) ),
dim = 1 )
)
def forward(self, for_gen, fakes, reals=None):
""" Pass each input as a tuple (v, delta, M) """
if for_gen:
return self.compute_loss_for_generator(fakes)
else:
real_scores = self.f(reals)
fake_scores = self.f(fakes)
Br = real_scores.shape[0]
Bf = fake_scores.shape[0]
device = fake_scores.device
valid = Variable(torch.Tensor(Br, 1).fill_(1.0), requires_grad=False).to(device)
fake = Variable(torch.Tensor(Bf, 1).fill_(0.0), requires_grad=False).to(device)
real_loss = self.mse( real_scores, valid)
fake_loss = self.mse( fake_scores, fake)
d_loss = 0.5 * (real_loss + fake_loss)
return d_loss
def compute_loss_for_generator(self, fakes):
return (self.f(fakes) - 1.0).pow(2).mean() * 0.5
class SingleStageShapeAdversary(nn.Module):
"""
Only apply the shape adversary to v, the latent shape.
"""
def __init__(self, dim_lat_pert, layer_sizes, wgan_gp_pen_weight, drift_mag_weight):
super(SingleStageShapeAdversary, self).__init__()
from vector_adversaries import VectorAdversaryLinWGANGP
self.critic = VectorAdversaryLinWGANGP(dim_lat_pert, layer_sizes,
wgan_gp_pen_weight=wgan_gp_pen_weight,
drift_mag_weight=drift_mag_weight)
def forward(self, for_gen, fakes, reals=None):
fakes = fakes[0]
if not reals is None: reals = reals[0]
return self.critic(for_gen=for_gen, v_fake=fakes, v_real=reals)
class ComSingleStageShapeAdversary(nn.Module):
"""
Only apply the shape adversary to v, the latent shape.
"""
def __init__(self, dim_lat_pert, nV, layer_sizes, wgan_gp_pen_weight, drift_mag_weight):
super(ComSingleStageShapeAdversary, self).__init__()
from vector_adversaries import VectorAdversaryLinWGANGP
self.nV = nV
self.reduced_M_dim = 128
self.intermed_dim = 512
self.M_preeuc_preproc = nn.Sequential(
Unfolder(),
LBA_stack_to_reshape(
[ 3 * self.nV, self.intermed_dim, self.reduced_M_dim ],
[ self.reduced_M_dim ],
norm_type = 'bn',
end_with_lin = False) # Next network starts with linear
)
self.critic = VectorAdversaryLinWGANGP(dim_lat_pert + self.reduced_M_dim, layer_sizes,
wgan_gp_pen_weight = wgan_gp_pen_weight,
drift_mag_weight = drift_mag_weight)
def forward(self, for_gen, fakes, reals=None):
fakes = torch.cat( (fakes[0], self.M_preeuc_preproc(fakes[1])), dim = -1)
if not reals is None:
reals = torch.cat( (reals[0], self.M_preeuc_preproc(reals[1])), dim = -1)
return self.critic(for_gen=for_gen, v_fake=fakes, v_real=reals)
class TwoStageShapeAdversary(nn.Module):
def __init__(self, nTV, dim_lat_pert):
super(TwoStageShapeAdversary, self).__init__()
self.nf = nTV * 3
self.dim_v = dim_lat_pert
self.mse = torch.nn.MSELoss()
self.intdim = 1
# Critic on latent perturbations
self.f_v = LBA_stack( sizes = (self.dim_v, 256, 128, self.intdim),
norm_type = 'spectral', #
act_type = 'elu',
end_with_lin = True )
# Critic on template vertices
self.f_tverts = nn.Sequential(
Unfolder(),
LBA_stack( sizes = (self.nf, 512, 256, self.intdim),
norm_type = 'spectral', #
act_type = 'elu',
end_with_lin = True ) )
# Combiner
self.f = DoubleAvg(self.f_v, self.f_tverts)
def forward(self, for_gen, fakes, reals=None):
""" Pass each input as a tuple (v, delta, M) """
if for_gen:
return self.compute_loss_for_generator(fakes)
else:
real_scores = self.f(reals)
fake_scores = self.f(fakes)
Br = real_scores.shape[0]
Bf = fake_scores.shape[0]
device = fake_scores.device
valid = Variable(torch.Tensor(Br, 1).fill_(1.0), requires_grad=False).to(device)
fake = Variable(torch.Tensor(Bf, 1).fill_(0.0), requires_grad=False).to(device)
real_loss = self.mse( real_scores, valid)
fake_loss = self.mse( fake_scores, fake)
d_loss = 0.5 * (real_loss + fake_loss)
return d_loss
def compute_loss_for_generator(self, fakes):
return (self.f(fakes) - 1.0).pow(2).mean() * 0.5
class MultiStageShapeAdversary(nn.Module):
def __init__(self, nTV, dim_lat_pert, nts):
super(MultiStageShapeAdversary, self).__init__()
self.nf = nTV * 3
self.dim_v = dim_lat_pert
self.mse = torch.nn.MSELoss()
self.intdim = 1
self.d_nts = nts
# Critic on latent perturbations
self.f_v = LBA_stack( sizes = (self.dim_v, 128, self.intdim),
norm_type = 'spectral', #
act_type = 'elu',
end_with_lin = True )
# Critic on delta perturbations
self.f_delta = nn.Sequential(
Unfolder(),
LBA_stack( sizes = (self.nf*self.d_nts, 512, 256, self.intdim),
norm_type = 'spectral', #
act_type = 'elu',
end_with_lin = True ) )
# Critic on template vertices
self.f_tverts = nn.Sequential(
Unfolder(),
LBA_stack( sizes = (self.nf, 512, 256, self.intdim),
norm_type = 'spectral', #
act_type = 'elu',
end_with_lin = True ) )
#
if self.intdim == 1:
self.f = TripletAvg(self.f_v, self.f_delta, self.f_tverts)
else:
self.f = TripletCom(self.intdim, self.f_v, self.f_delta, self.f_tverts)
def forward(self, for_gen, fakes, reals=None):
""" Pass each input as a tuple (v, delta, M) """
if for_gen:
return self.compute_loss_for_generator(fakes)
else:
real_scores = self.f(reals)
fake_scores = self.f(fakes)
B = real_scores.shape[0]
device = fake_scores.device
valid = Variable(torch.Tensor(B, 1).fill_(1.0), requires_grad=False).to(device)
fake = Variable(torch.Tensor(B, 1).fill_(0.0), requires_grad=False).to(device)
real_loss = self.mse( real_scores, valid)
fake_loss = self.mse( fake_scores, fake)
d_loss = 0.5 * (real_loss + fake_loss)
return d_loss
def compute_loss_for_generator(self, fakes):
return (self.f(fakes) - 1.0).pow(2).mean() * 0.5
class DoubleAvg(nn.Module):
def __init__(self, f1, f2):
super(DoubleAvg, self).__init__()
self.f1 = f1
self.f2 = f2
def forward(self, x):
return (self.f1(x[0]) + self.f2(x[1])) / 2.0
class TripletAvg(nn.Module):
def __init__(self, f1, f2, f3):
super(TripletAvg, self).__init__()
self.f1 = f1
self.f2 = f2
self.f3 = f3
def forward(self, x):
return (self.f1(x[0]) + self.f2(x[1]) + self.f3(x[2])) / 3.0
class TripletCom(nn.Module):
def __init__(self, D, f1, f2, f3):
super(TripletCom, self).__init__()
self.D = D
self.f1 = f1
self.f2 = f2
self.f3 = f3
self.f = nn.Sequential(
nn.ELU(),
nn.utils.spectral_norm( nn.Linear(self.D*3, 1) )
)
def forward(self, x):
return self.f( torch.cat( (self.f1(x[0]), self.f2(x[1]), self.f3(x[2])), dim = -1 ) )
class DglGcnSimpleMeshAdversary(nn.Module):
"""
A simple GCNN critic operating on mesh inputs using the Deep graph library
"""
def __init__(self, nfeats=3):
super(DglGcnSimpleMeshAdversary, self).__init__()
# f is a GCN that maps from a batched DGL graph set to a scalar (per graph)
from networks.DglGCN import GCNClassifier
self.f = GCNClassifier(in_dim = 3, hidden_dim = 64, outdim = 1)
self.mse = torch.nn.MSELoss()
def forward(self, for_gen, fakes, reals=None):
"""
Inputs must be batched DGL graphs with nodal features in the entry named "features"
"""
if for_gen:
return self.compute_loss_for_generator(fakes)
else:
real_scores = self.f(reals)
fake_scores = self.f(fakes)
B = real_scores.shape[0]
device = fake_scores.device
valid = Variable(torch.Tensor(B, 1).fill_(1.0), requires_grad=False).to(device)
fake = Variable(torch.Tensor(B, 1).fill_(0.0), requires_grad=False).to(device)
real_loss = self.mse( real_scores, valid)
fake_loss = self.mse( fake_scores, fake)
d_loss = 0.5 * (real_loss + fake_loss)
return d_loss
def compute_loss_for_generator(self, pc):
return (self.f(pc) - 1.0).pow(2).mean() / 2
class SilhouetteAdversary(nn.Module):
def __init__(self):
super(SilhouetteAdversary, self).__init__()
self.f = SilhouetteCriticNetwork()
def forward(self, for_gen, fakes, reals=None, faces_fake=None, faces_real=None):
# Here, fakes and reals are vertex positions
if for_gen:
return self.compute_loss_for_generator(fakes)
else:
real_scores = self.f(reals)
fake_scores = self.f(fakes)
B = real_scores.shape[0]
device = fake_scores.device
valid = Variable(torch.Tensor(B, 1).fill_(1.0), requires_grad=False).to(device)
fake = Variable(torch.Tensor(B, 1).fill_(0.0), requires_grad=False).to(device)
real_loss = self.mse( real_scores, valid)
fake_loss = self.mse( fake_scores, fake)
d_loss = 0.5 * (real_loss + fake_loss)
return d_loss
def compute_loss_for_generator(self, I):
return (self.f(I) - 1.0).pow(2).mean() * 0.5
##################################################################################################
from networks.pc_archs import PointNet as LocPointNet, VaePointNet as VaePN
class PointNetT(nn.Module):
def __init__(self, nfeats, dim_out, for_vae=False):
super(PointNetT, self).__init__()
#self.f = models.PointNet(nfeats, dim_out, dropout=dropout)
if for_vae:
self.f = VaePN(indim=nfeats, outdim=dim_out)
else:
self.f = LocPointNet(in_channels=nfeats, num_classes=dim_out)
def forward(self, pc):
return self.f( pc.transpose(1,2) )
##################################################################################################
# Consider the trivial approach of a simple image critic on the projections
# See "Synthesizing 3D shapes from Silhouette Image Collections", by Li et al [1]
# Also "SiCloPe: Silhouette-Based Clothed People", by Natsume et al [2]
class SilhouetteCriticNetwork(nn.Module): # Based on [1]
def __init__(self):
super(SilhouetteCriticNetwork, self).__init__()
self.f = nn.Sequential(
nn.utils.spectral_norm( nn.Conv2d(1, 32, 3, stride=2, padding=1) ),
nn.LeakyReLU(0.2),
nn.utils.spectral_norm( nn.Conv2d(32, 64, 3, stride=2, padding=1) ),
nn.LeakyReLU(0.2),
nn.utils.spectral_norm( nn.Conv2d(64, 128, 3, stride=2, padding=1) ),
nn.LeakyReLU(0.2),
nn.utils.spectral_norm( nn.Conv2d(128, 256, 3, stride=2, padding=1) ),
nn.LeakyReLU(0.2),
nn.Conv2d(256, 1, 1),
GlobalMean() )
def forward(self, x):
return self.f(x)
#