-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel.py
421 lines (357 loc) · 18.1 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from collections import OrderedDict
class LinearUnit(nn.Module):
def __init__(self, in_features, out_features, batchnorm=True, nonlinearity=nn.LeakyReLU(0.2)):
super(LinearUnit, self).__init__()
if batchnorm is True:
self.model = nn.Sequential(
nn.Linear(in_features, out_features),
nn.BatchNorm1d(out_features), nonlinearity)
else:
self.model = nn.Sequential(
nn.Linear(in_features, out_features), nonlinearity)
def forward(self, x):
return self.model(x)
class conv(nn.Module):
def __init__(self, nin, nout):
super(conv, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(nin, nout, 4, 2, 1),
nn.BatchNorm2d(nout),
nn.LeakyReLU(0.2, inplace=True),
)
def forward(self, input):
return self.main(input)
class encoder(nn.Module):
def __init__(self, dim, nc=1):
super(encoder, self).__init__()
self.dim = dim
nf = 64
# input is (nc) x 64 x 64
self.c1 = conv(nc, nf)
# state size. (nf) x 32 x 32
self.c2 = conv(nf, nf * 2)
# state size. (nf*2) x 16 x 16
self.c3 = conv(nf * 2, nf * 4)
# state size. (nf*4) x 8 x 8
self.c4 = conv(nf * 4, nf * 8)
# state size. (nf*8) x 4 x 4
self.c5 = nn.Sequential(
nn.Conv2d(nf * 8, dim, 4, 1, 0),
nn.BatchNorm2d(dim),
nn.Tanh()
)
def forward(self, input):
h1 = self.c1(input)
h2 = self.c2(h1)
h3 = self.c3(h2)
h4 = self.c4(h3)
h5 = self.c5(h4)
return h5.view(-1, self.dim), [h1, h2, h3, h4]
class upconv(nn.Module):
def __init__(self, nin, nout):
super(upconv, self).__init__()
self.main = nn.Sequential(
nn.ConvTranspose2d(nin, nout, 4, 2, 1),
nn.BatchNorm2d(nout),
nn.LeakyReLU(0.2, inplace=True),
)
def forward(self, input):
return self.main(input)
class decoder(nn.Module):
def __init__(self, dim, nc=1):
super(decoder, self).__init__()
self.dim = dim
nf = 64
self.upc1 = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d(dim, nf * 8, 4, 1, 0),
nn.BatchNorm2d(nf * 8),
nn.LeakyReLU(0.2, inplace=True)
)
# state size. (nf*8) x 4 x 4
self.upc2 = upconv(nf * 8, nf * 4)
# state size. (nf*4) x 8 x 8
self.upc3 = upconv(nf * 4, nf * 2)
# state size. (nf*2) x 16 x 16
self.upc4 = upconv(nf * 2, nf)
# state size. (nf) x 32 x 32
self.upc5 = nn.Sequential(
nn.ConvTranspose2d(nf, nc, 4, 2, 1),
nn.Sigmoid()
# state size. (nc) x 64 x 64
)
def forward(self, input):
d1 = self.upc1(input.view(-1, self.dim, 1, 1))
d2 = self.upc2(d1)
d3 = self.upc3(d2)
d4 = self.upc4(d3)
output = self.upc5(d4)
output = output.view(input.shape[0], input.shape[1], output.shape[1], output.shape[2], output.shape[3])
return output
class CDSVAE(nn.Module):
def __init__(self, opt):
super(CDSVAE, self).__init__()
self.f_dim = opt.f_dim # content
self.z_dim = opt.z_dim # motion
self.g_dim = opt.g_dim # frame feature
self.channels = opt.channels # frame feature
self.hidden_dim = opt.rnn_size
self.f_rnn_layers = opt.f_rnn_layers
self.frames = opt.frames
# Frame encoder and decoder
self.encoder = encoder(self.g_dim, self.channels)
self.decoder = decoder(self.z_dim + self.f_dim, self.channels)
# Prior of content is a uniform Gaussian and prior of the dynamics is an LSTM
self.z_prior_lstm_ly1 = nn.LSTMCell(self.z_dim, self.hidden_dim)
self.z_prior_lstm_ly2 = nn.LSTMCell(self.hidden_dim, self.hidden_dim)
self.z_prior_mean = nn.Linear(self.hidden_dim, self.z_dim)
self.z_prior_logvar = nn.Linear(self.hidden_dim, self.z_dim)
self.z_lstm = nn.LSTM(self.g_dim, self.hidden_dim, 1, bidirectional=True, batch_first=True)
self.f_mean = LinearUnit(self.hidden_dim * 2, self.f_dim, False)
self.f_logvar = LinearUnit(self.hidden_dim * 2, self.f_dim, False)
self.z_rnn = nn.RNN(self.hidden_dim * 2, self.hidden_dim, batch_first=True)
# Each timestep is for each z so no reshaping and feature mixing
self.z_mean = nn.Linear(self.hidden_dim, self.z_dim)
self.z_logvar = nn.Linear(self.hidden_dim, self.z_dim)
def encode_and_sample_post(self, x):
if isinstance(x, list):
conv_x = self.encoder_frame(x[0])
else:
conv_x = self.encoder_frame(x)
# pass the bidirectional lstm
lstm_out, _ = self.z_lstm(conv_x)
# get f:
backward = lstm_out[:, 0, self.hidden_dim:2 * self.hidden_dim]
frontal = lstm_out[:, self.frames - 1, 0:self.hidden_dim]
lstm_out_f = torch.cat((frontal, backward), dim=1)
f_mean = self.f_mean(lstm_out_f)
f_logvar = self.f_logvar(lstm_out_f)
f_post = self.reparameterize(f_mean, f_logvar, random_sampling=True)
# pass to one direction rnn
features, _ = self.z_rnn(lstm_out)
z_mean = self.z_mean(features)
z_logvar = self.z_logvar(features)
z_post = self.reparameterize(z_mean, z_logvar, random_sampling=True)
if isinstance(x, list):
f_mean_list = [f_mean]
for _x in x[1:]:
conv_x = self.encoder_frame(_x)
lstm_out, _ = self.z_lstm(conv_x)
# get f:
backward = lstm_out[:, 0, self.hidden_dim:2 * self.hidden_dim]
frontal = lstm_out[:, self.frames - 1, 0:self.hidden_dim]
lstm_out_f = torch.cat((frontal, backward), dim=1)
f_mean = self.f_mean(lstm_out_f)
f_mean_list.append(f_mean)
f_mean = f_mean_list
# f_mean is list if triple else not
return f_mean, f_logvar, f_post, z_mean, z_logvar, z_post
def forward(self, x):
f_mean, f_logvar, f_post, z_mean_post, z_logvar_post, z_post = self.encode_and_sample_post(x)
z_mean_prior, z_logvar_prior, z_prior = self.sample_z_prior_train(z_post, random_sampling=self.training)
z_flatten = z_post.view(-1, z_post.shape[2])
f_expand = f_post.unsqueeze(1).expand(-1, self.frames, self.f_dim)
zf = torch.cat((z_post, f_expand), dim=2)
recon_x = self.decoder(zf)
return f_mean, f_logvar, f_post, z_mean_post, z_logvar_post, z_post, z_mean_prior, z_logvar_prior, z_prior, \
recon_x
def forward_fixed_motion(self, x):
z_mean_prior, z_logvar_prior, _ = self.sample_z(x.size(0), random_sampling=self.training)
f_mean, f_logvar, f_post, z_mean_post, z_logvar_post, z_post = self.encode_and_sample_post(x)
z_repeat = z_post[0].repeat(z_post.shape[0], 1, 1)
f_expand = f_post.unsqueeze(1).expand(-1, self.frames, self.f_dim)
zf = torch.cat((z_repeat, f_expand), dim=2)
recon_x = self.decoder(zf)
return f_mean, f_logvar, f_post, z_mean_post, z_logvar_post, z_post, z_mean_prior, z_logvar_prior, recon_x
def forward_fixed_content(self, x):
z_mean_prior, z_logvar_prior, _ = self.sample_z(x.size(0), random_sampling=self.training)
f_mean, f_logvar, f_post, z_mean_post, z_logvar_post, z_post = self.encode_and_sample_post(x)
f_repeat = f_post[0].repeat(f_post.shape[0], 1)
f_expand = f_repeat.unsqueeze(1).expand(-1, self.frames, self.f_dim)
zf = torch.cat((z_post, f_expand), dim=2)
recon_x = self.decoder(zf)
return f_mean, f_logvar, f_post, z_mean_post, z_logvar_post, z_post, z_mean_prior, z_logvar_prior, recon_x
def forward_fixed_content_for_classification(self, x):
z_mean_prior, z_logvar_prior, _ = self.sample_z(x.size(0), random_sampling=True)
f_mean, f_logvar, f_post, z_mean_post, z_logvar_post, z_post = self.encode_and_sample_post(x)
f_expand = f_mean.unsqueeze(1).expand(-1, self.frames, self.f_dim)
zf = torch.cat((z_mean_prior, f_expand), dim=2)
recon_x_sample = self.decoder(zf)
zf = torch.cat((z_mean_post, f_expand), dim=2)
recon_x = self.decoder(zf)
return recon_x_sample, recon_x
def forward_fixed_motion_for_classification(self, x):
z_mean_prior, z_logvar_prior, _ = self.sample_z(x.size(0), random_sampling=True)
f_mean, f_logvar, f_post, z_mean_post, z_logvar_post, z_post = self.encode_and_sample_post(x)
f_prior = self.reparameterize(torch.zeros(f_mean.shape).cuda(), torch.zeros(f_logvar.shape).cuda(),
random_sampling=True)
f_expand = f_prior.unsqueeze(1).expand(-1, self.frames, self.f_dim)
zf = torch.cat((z_mean_post, f_expand), dim=2)
recon_x_sample = self.decoder(zf)
f_expand = f_mean.unsqueeze(1).expand(-1, self.frames, self.f_dim)
zf = torch.cat((z_mean_post, f_expand), dim=2)
recon_x = self.decoder(zf)
return recon_x_sample, recon_x
def encoder_frame(self, x):
# input x is list of length Frames [batchsize, channels, size, size]
# convert it to [batchsize, frames, channels, size, size]
# x = torch.stack(x, dim=1)
# [batch_size, frames, channels, size, size] to [batch_size * frames, channels, size, size]
x_shape = x.shape
x = x.view(-1, x_shape[-3], x_shape[-2], x_shape[-1])
x_embed = self.encoder(x)[0]
# to [batch_size , frames, embed_dim]
return x_embed.view(x_shape[0], x_shape[1], -1)
def reparameterize(self, mean, logvar, random_sampling=True):
# Reparametrization occurs only if random sampling is set to true, otherwise mean is returned
if random_sampling is True:
eps = torch.randn_like(logvar)
std = torch.exp(0.5 * logvar)
z = mean + eps * std
return z
else:
return mean
def sample_z_prior_test(self, n_sample, n_frame, random_sampling=True):
z_out = None # This will ultimately store all z_s in the format [batch_size, frames, z_dim]
z_means = None
z_logvars = None
batch_size = n_sample
z_t = torch.zeros(batch_size, self.z_dim).cuda()
h_t_ly1 = torch.zeros(batch_size, self.hidden_dim).cuda()
c_t_ly1 = torch.zeros(batch_size, self.hidden_dim).cuda()
h_t_ly2 = torch.zeros(batch_size, self.hidden_dim).cuda()
c_t_ly2 = torch.zeros(batch_size, self.hidden_dim).cuda()
for i in range(n_frame):
# two layer LSTM and two one-layer FC
h_t_ly1, c_t_ly1 = self.z_prior_lstm_ly1(z_t, (h_t_ly1, c_t_ly1))
h_t_ly2, c_t_ly2 = self.z_prior_lstm_ly2(h_t_ly1, (h_t_ly2, c_t_ly2))
z_mean_t = self.z_prior_mean(h_t_ly2)
z_logvar_t = self.z_prior_logvar(h_t_ly2)
z_prior = self.reparameterize(z_mean_t, z_logvar_t, random_sampling)
if z_out is None:
# If z_out is none it means z_t is z_1, hence store it in the format [batch_size, 1, z_dim]
z_out = z_prior.unsqueeze(1)
z_means = z_mean_t.unsqueeze(1)
z_logvars = z_logvar_t.unsqueeze(1)
else:
# If z_out is not none, z_t is not the initial z and hence append it to the previous z_ts collected in z_out
z_out = torch.cat((z_out, z_prior.unsqueeze(1)), dim=1)
z_means = torch.cat((z_means, z_mean_t.unsqueeze(1)), dim=1)
z_logvars = torch.cat((z_logvars, z_logvar_t.unsqueeze(1)), dim=1)
# z_t = z_post[:,i,:]
z_t = z_prior
return z_means, z_logvars, z_out
def sample_z_prior_train(self, z_post, random_sampling=True):
z_out = None # This will ultimately store all z_s in the format [batch_size, frames, z_dim]
z_means = None
z_logvars = None
batch_size = z_post.shape[0]
z_t = torch.zeros(batch_size, self.z_dim).cuda()
h_t_ly1 = torch.zeros(batch_size, self.hidden_dim).cuda()
c_t_ly1 = torch.zeros(batch_size, self.hidden_dim).cuda()
h_t_ly2 = torch.zeros(batch_size, self.hidden_dim).cuda()
c_t_ly2 = torch.zeros(batch_size, self.hidden_dim).cuda()
for i in range(self.frames):
# two layer LSTM and two one-layer FC
h_t_ly1, c_t_ly1 = self.z_prior_lstm_ly1(z_t, (h_t_ly1, c_t_ly1))
h_t_ly2, c_t_ly2 = self.z_prior_lstm_ly2(h_t_ly1, (h_t_ly2, c_t_ly2))
z_mean_t = self.z_prior_mean(h_t_ly2)
z_logvar_t = self.z_prior_logvar(h_t_ly2)
z_prior = self.reparameterize(z_mean_t, z_logvar_t, random_sampling)
if z_out is None:
# If z_out is none it means z_t is z_1, hence store it in the format [batch_size, 1, z_dim]
z_out = z_prior.unsqueeze(1)
z_means = z_mean_t.unsqueeze(1)
z_logvars = z_logvar_t.unsqueeze(1)
else:
# If z_out is not none, z_t is not the initial z and hence append it to the previous z_ts collected in z_out
z_out = torch.cat((z_out, z_prior.unsqueeze(1)), dim=1)
z_means = torch.cat((z_means, z_mean_t.unsqueeze(1)), dim=1)
z_logvars = torch.cat((z_logvars, z_logvar_t.unsqueeze(1)), dim=1)
z_t = z_post[:,i,:]
return z_means, z_logvars, z_out
# If random sampling is true, reparametrization occurs else z_t is just set to the mean
def sample_z(self, batch_size, random_sampling=True):
z_out = None # This will ultimately store all z_s in the format [batch_size, frames, z_dim]
z_means = None
z_logvars = None
# All states are initially set to 0, especially z_0 = 0
z_t = torch.zeros(batch_size, self.z_dim).cuda()
# z_mean_t = torch.zeros(batch_size, self.z_dim)
# z_logvar_t = torch.zeros(batch_size, self.z_dim)
h_t_ly1 = torch.zeros(batch_size, self.hidden_dim).cuda()
c_t_ly1 = torch.zeros(batch_size, self.hidden_dim).cuda()
h_t_ly2 = torch.zeros(batch_size, self.hidden_dim).cuda()
c_t_ly2 = torch.zeros(batch_size, self.hidden_dim).cuda()
for _ in range(self.frames):
# h_t, c_t = self.z_prior_lstm(z_t, (h_t, c_t))
# two layer LSTM and two one-layer FC
h_t_ly1, c_t_ly1 = self.z_prior_lstm_ly1(z_t, (h_t_ly1, c_t_ly1))
h_t_ly2, c_t_ly2 = self.z_prior_lstm_ly2(h_t_ly1, (h_t_ly2, c_t_ly2))
z_mean_t = self.z_prior_mean(h_t_ly2)
z_logvar_t = self.z_prior_logvar(h_t_ly2)
z_t = self.reparameterize(z_mean_t, z_logvar_t, random_sampling)
if z_out is None:
# If z_out is none it means z_t is z_1, hence store it in the format [batch_size, 1, z_dim]
z_out = z_t.unsqueeze(1)
z_means = z_mean_t.unsqueeze(1)
z_logvars = z_logvar_t.unsqueeze(1)
else:
# If z_out is not none, z_t is not the initial z and hence append it to the previous z_ts collected in z_out
z_out = torch.cat((z_out, z_t.unsqueeze(1)), dim=1)
z_means = torch.cat((z_means, z_mean_t.unsqueeze(1)), dim=1)
z_logvars = torch.cat((z_logvars, z_logvar_t.unsqueeze(1)), dim=1)
return z_means, z_logvars, z_out
class classifier_Sprite_all(nn.Module):
def __init__(self, opt):
super(classifier_Sprite_all, self).__init__()
self.g_dim = opt.g_dim # frame feature
self.channels = opt.channels # frame feature
self.hidden_dim = opt.rnn_size
self.frames = opt.frames
from model import encoder
self.encoder = encoder(self.g_dim, self.channels)
self.bilstm = nn.LSTM(self.g_dim, self.hidden_dim, 1, bidirectional=True, batch_first=True)
self.cls_skin = nn.Sequential(
nn.Linear(self.hidden_dim * 2, self.hidden_dim),
nn.ReLU(True),
nn.Linear(self.hidden_dim, 6))
self.cls_top = nn.Sequential(
nn.Linear(self.hidden_dim * 2, self.hidden_dim),
nn.ReLU(True),
nn.Linear(self.hidden_dim, 6))
self.cls_pant = nn.Sequential(
nn.Linear(self.hidden_dim * 2, self.hidden_dim),
nn.ReLU(True),
nn.Linear(self.hidden_dim, 6))
self.cls_hair = nn.Sequential(
nn.Linear(self.hidden_dim * 2, self.hidden_dim),
nn.ReLU(True),
nn.Linear(self.hidden_dim, 6))
self.cls_action = nn.Sequential(
nn.Linear(self.hidden_dim * 2, self.hidden_dim),
nn.ReLU(True),
nn.Linear(self.hidden_dim, 9))
def encoder_frame(self, x):
# input x is list of length Frames [batchsize, channels, size, size]
# convert it to [batchsize, frames, channels, size, size]
# x = torch.stack(x, dim=1)
# [batch_size, frames, channels, size, size] to [batch_size * frames, channels, size, size]
x_shape = x.shape
x = x.view(-1, x_shape[-3], x_shape[-2], x_shape[-1])
x_embed = self.encoder(x)[0]
# to [batch_size , frames, embed_dim]
return x_embed.view(x_shape[0], x_shape[1], -1)
def forward(self, x):
conv_x = self.encoder_frame(x)
# pass the bidirectional lstm
lstm_out, _ = self.bilstm(conv_x)
backward = lstm_out[:, 0, self.hidden_dim:2 * self.hidden_dim]
frontal = lstm_out[:, self.frames - 1, 0:self.hidden_dim]
lstm_out_f = torch.cat((frontal, backward), dim=1)
return self.cls_action(lstm_out_f), self.cls_skin(lstm_out_f), self.cls_pant(lstm_out_f), \
self.cls_top(lstm_out_f), self.cls_hair(lstm_out_f)