forked from zhongshaoyy/Audio2Face
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels_testae.py
258 lines (208 loc) · 8.38 KB
/
models_testae.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
import torch.nn as nn
import torch
from torch.autograd import Variable
## LSTM-AE model
# concat
class LSTMAEsigm(nn.Module):
def __init__(self, num_features=39, num_audiof=128, num_bsf=2, num_blendshapes=51, is_concat=True):
super(LSTMAEsigm, self).__init__()
# assign
self.is_concat = is_concat
## encoder
# audio part with LSTM
self.rnn = nn.LSTM(input_size=num_features, hidden_size=256, num_layers=2,
batch_first=True, dropout=0.5, bidirectional=True)
self.audio_fc = nn.Linear(256*2, num_audiof)
# blendshape part with fc
self.bs_fc = nn.Sequential(
nn.Linear(num_blendshapes, 24),
nn.ReLU(True),
nn.Linear(24, num_bsf),
nn.Sigmoid() # constrain to 0~1, control variable
)
## decoder?
if self.is_concat:
self.decoder_fc = nn.Sequential(
nn.Linear(num_audiof+num_bsf, 64),
nn.ReLU(True),
nn.Linear(64, num_blendshapes),
nn.Sigmoid()
)
else:
self.bilinear = nn.Bilinear(num_bsf, num_audiof, num_audiof)
self.decoder_fc = nn.Sequential(
nn.Linear(num_audiof, 64),
nn.ReLU(True),
nn.Linear(64, num_blendshapes),
nn.Sigmoid()
)
def fuse(self, audio_z, bs_z):
# concat or bilinear
if self.is_concat:
return torch.cat((audio_z, bs_z), dim=1)
else:
return self.bilinear(bs_z, audio_z)
def decode(self, z):
return self.decoder_fc(z)
def decode_audio(self, audio, bs_z):
audio_rnn, _ = self.rnn(audio)
audio_z = self.audio_fc(audio_rnn[:, -1, :])
bs_z = bs_z.repeat(audio_z.size()[0], 1) # to batch size
z = self.fuse(audio_z, bs_z)
return self.decode(z)
def forward(self, audio, blendshape):
# encode
audio_rnn, _ = self.rnn(audio)
audio_z = self.audio_fc(audio_rnn[:, -1, :])
bs_z = self.bs_fc(blendshape)
# decode
z = self.fuse(audio_z, bs_z)
output = self.decode(z)
return audio_z, bs_z, output
class LSTMAEdist(nn.Module):
def __init__(self, num_features=39, num_audiof=128, num_bsf=2, num_blendshapes=51, is_concat=True):
super(LSTMAEdist, self).__init__()
# assign
self.is_concat = is_concat
## encoder
# audio part with LSTM
self.rnn = nn.LSTM(input_size=num_features, hidden_size=256, num_layers=2,
batch_first=True, dropout=0.5, bidirectional=True)
self.audio_fc = nn.Linear(256*2, num_audiof)
# blendshape part with fc
self.bs_fc1 = nn.Sequential(
nn.Linear(num_blendshapes, 24),
nn.ReLU(True),
)
self.bs_fc21 = nn.Linear(24, num_bsf)
self.bs_fc22 = nn.Linear(24, num_bsf)
## decoder?
if self.is_concat:
self.decoder_fc = nn.Sequential(
nn.Linear(num_audiof+num_bsf, 64),
nn.ReLU(True),
nn.Linear(64, num_blendshapes),
nn.Sigmoid()
)
else:
self.bilinear = nn.Bilinear(num_bsf, num_audiof, num_audiof)
self.decoder_fc = nn.Sequential(
nn.Linear(num_audiof, 64),
nn.ReLU(True),
nn.Linear(64, num_blendshapes),
nn.Sigmoid()
)
def encode(self, audio, blendshape):
audio_rnn, _ = self.rnn(audio)
audio_z = self.audio_fc(audio_rnn[:, -1, :])
bs_h1 = self.bs_fc1(blendshape)
return audio_z, self.bs_fc21(bs_h1), self.bs_fc22(bs_h1)
def fuse(self, audio_z, bs_z):
# concat or bilinear
if self.is_concat:
return torch.cat((audio_z, bs_z), dim=1)
else:
return self.bilinear(bs_z, audio_z)
def reparameterize(self, mu, logvar):
if self.training:
std = torch.exp(0.5*logvar)
# eps = torch.randn_like(std)
# eps = torch.randn(std.size(), dtype=std.dtype, layout=std.layout, device=std.device)
eps = Variable(torch.randn(std.size())).cuda()
return eps.mul(std).add_(mu)
else:
return mu
def decode(self, z):
return self.decoder_fc(z)
def decode_audio(self, audio, bs_z):
audio_rnn, _ = self.rnn(audio)
audio_z = self.audio_fc(audio_rnn[:, -1, :])
bs_z = bs_z.repeat(audio_z.size()[0], 1)
z = self.fuse(audio_z, bs_z)
return self.decode(z)
def forward(self, audio, blendshape):
# encode
audio_z, bs_mu, bs_logvar = self.encode(audio, blendshape)
bs_z = self.reparameterize(bs_mu, bs_logvar)
# decode
z = self.fuse(audio_z, bs_z)
output = self.decode(z)
return audio_z, bs_z, output, bs_mu, bs_logvar
class LSTMAE2dist(nn.Module):
def __init__(self, num_features=39, num_audiof=128, num_bsf=2, num_blendshapes=51, is_concat=True):
super(LSTMAE2dist, self).__init__()
# assign
self.is_concat = is_concat
self.num_audiof = num_audiof
self.num_bsf = num_bsf
## encoder
# audio part with LSTM
self.rnn = nn.LSTM(input_size=num_features, hidden_size=256, num_layers=2,
batch_first=True, dropout=0.5, bidirectional=True)
self.audio_fc11 = nn.Linear(256*2, num_audiof)
self.audio_fc12 = nn.Linear(256*2, num_audiof)
# blendshape part with fc
self.bs_fc1 = nn.Sequential(
nn.Linear(num_blendshapes, 24),
nn.ReLU(True),
)
self.bs_fc21 = nn.Linear(24, num_bsf)
self.bs_fc22 = nn.Linear(24, num_bsf)
## decoder?
if self.is_concat:
self.decoder_fc = nn.Sequential(
nn.Linear(num_audiof+num_bsf, 64),
nn.ReLU(True),
nn.Linear(64, num_blendshapes),
nn.Sigmoid()
)
else:
# self.bilinear = nn.Bilinear(num_bsf, num_audiof, num_audiof)
# self.decoder_fc = nn.Sequential(
# nn.Linear(num_audiof, 64),
# nn.ReLU(True),
# nn.Linear(64, num_blendshapes),
# nn.Sigmoid()
# )
print('NO bilinear combination in 2dist model')
def encode(self, audio, blendshape):
audio_rnn, _ = self.rnn(audio)
audio_h = audio_rnn[:, -1, :]
bs_h1 = self.bs_fc1(blendshape)
return self.audio_fc11(audio_h), self.audio_fc12(audio_h), self.bs_fc21(bs_h1), self.bs_fc22(bs_h1)
# def fuse(self, audio_z, bs_z):
# # concat or bilinear
# if self.is_concat:
# return torch.cat((audio_z, bs_z), dim=1)
# else:
# return self.bilinear(bs_z, audio_z)
def reparameterize(self, mu, logvar):
if self.training:
std = torch.exp(0.5*logvar)
# eps = torch.randn_like(std)
# eps = torch.randn(std.size(), dtype=std.dtype, layout=std.layout, device=std.device)
eps = Variable(torch.randn(std.size())).cuda()
return eps.mul(std).add_(mu)
else:
return mu
def decode(self, z):
return self.decoder_fc(z)
def decode_audio(self, audio, bs_z):
audio_rnn, _ = self.rnn(audio)
audio_h = audio_rnn[:, -1, :]
audio_mu = self.audio_fc11(audio_h)
audio_logvar = self.audio_fc12(audio_h)
audio_z = self.reparameterize(audio_mu, audio_logvar)
bs_z = bs_z.repeat(audio_z.size()[0], 1)
z = torch.cat((audio_z, bs_z), dim=1)
return self.decode(z)
def forward(self, audio, blendshape):
# encode
audio_mu, audio_logvar, bs_mu, bs_logvar = self.encode(audio, blendshape)
mu = torch.cat((audio_mu, bs_mu), dim=1)
logvar = torch.cat((audio_logvar, bs_logvar), dim=1)
z = self.reparameterize(mu, logvar)
# decode
# z = self.fuse(audio_z, bs_z)
output = self.decode(z)
return z[:, :self.num_audiof], z[:, self.num_audiof:], output, mu, logvar