-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstyle_encoder.py
More file actions
213 lines (179 loc) · 7.41 KB
/
Copy pathstyle_encoder.py
File metadata and controls
213 lines (179 loc) · 7.41 KB
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
import torch.nn as nn
import sys
import torch
from utils.model_common import PositionalEncoding
from utils.flame import FLAME, FLAMEConfig
def get_style_encoder(args, style_encoder_model_style="diffposetalk"):
print(args.dataset_type)
if style_encoder_model_style == "vae2":
print("training model: StyleEncoder_VAE2")
return StyleEncoder_VAE2(args)
class Permute(nn.Module):
def __init__(self, dims):
super(Permute, self).__init__()
self.dims = dims # Tuple of dimensions to permute
def forward(self, x):
return x.permute(*self.dims)
class StyleEncoder_VAE(nn.Module):
def __init__(self, args) -> None:
super().__init__()
self.input_dim = 67
if args.dataset_type[:9] == 'HDTF_TFHP' or args.dataset_type == "flame_mead_ravdess":
self.input_dim = 54
self.motion_coef_dim = self.input_dim
self.conv_feature_dim = 512
self.output_size = args.d_style * 2 * 2
self.pre_conv_permute = Permute((0, 2, 1))
self.post_conv_permute = Permute((0, 2, 1))
# these are the input layers
self.input_layers = [
# conv1d
self.pre_conv_permute,
nn.Conv1d(in_channels=self.motion_coef_dim, out_channels=self.conv_feature_dim, kernel_size=3, padding=1),
self.post_conv_permute,
nn.Dropout(0.2),
nn.ELU(),
# apply layer norm
nn.LayerNorm(self.conv_feature_dim),
# second conv1d
self.pre_conv_permute,
nn.Conv1d(in_channels=self.conv_feature_dim, out_channels=self.conv_feature_dim, kernel_size=3, padding=1),
self.post_conv_permute,
nn.Dropout(0.2),
nn.ELU(),
# apply layer norm
nn.LayerNorm(self.conv_feature_dim),
]
self.input_layers = nn.Sequential(*self.input_layers)
# apply positional encoding
self.PE = PositionalEncoding(self.conv_feature_dim)
# one transformer decoder
self.encoder = nn.TransformerEncoderLayer(
d_model=self.conv_feature_dim, nhead=8, dim_feedforward=self.conv_feature_dim, activation='gelu', batch_first=True
)
# end up with two more 1D conv layers
self.output_layers = [
self.pre_conv_permute,
nn.Conv1d(in_channels=self.conv_feature_dim, out_channels=self.output_size, kernel_size=3, padding=1),
self.post_conv_permute,
nn.Dropout(0.1),
nn.ReLU(),
# apply layer norm
nn.LayerNorm(self.output_size),
# second conv1d
self.pre_conv_permute,
nn.Conv1d(in_channels=self.output_size, out_channels=self.output_size, kernel_size=3, padding=1),
self.post_conv_permute,
nn.ReLU(),
]
self.output_layers = nn.Sequential(*self.output_layers)
def forward(self, motion_coef, do_sample=False):
"""
:param motion_coef: (batch_size, seq_len, motion_coef_dim)
:param audio: (batch_size, seq_len)
:return: (batch_size, feature_dim)
"""
batch_size, seq_len, _ = motion_coef.shape
# Motion
motion_feat = self.input_layers(motion_coef)
motion_feat = self.PE(motion_feat)
feat = self.encoder(motion_feat)
out = self.output_layers(feat)
# average pooling
out = out.mean(dim=1) # dim 1 is the seq_len
mu = out[:, :self.output_size//2]
logvar = out[:, self.output_size//2:]
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
# determine if we should sample or not
if do_sample:
return mu + eps * std
else:
out = mu + eps * std
return out, mu, logvar
def sample(self, motion_coef):
out, mu, logvar = self.forward(motion_coef)
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
class StyleEncoder_VAE2(nn.Module):
def __init__(self, args) -> None:
super().__init__()
self.input_dim = 67
if args.dataset_type[:9] == 'HDTF_TFHP' or args.dataset_type == "flame_mead_ravdess":
self.input_dim = 54
self.motion_coef_dim = self.input_dim
self.conv_feature_dim = 512
self.output_size = args.d_style * 2
self.pre_conv_permute = Permute((0, 2, 1))
self.post_conv_permute = Permute((0, 2, 1))
# these are the input layers
self.input_layers = [
# conv1d
self.pre_conv_permute,
nn.Conv1d(in_channels=self.motion_coef_dim, out_channels=self.conv_feature_dim, kernel_size=3, padding=1),
self.post_conv_permute,
nn.Dropout(0.2),
nn.ELU(),
# apply layer norm
nn.LayerNorm(self.conv_feature_dim),
# second conv1d
self.pre_conv_permute,
nn.Conv1d(in_channels=self.conv_feature_dim, out_channels=self.conv_feature_dim, kernel_size=3, padding=1),
self.post_conv_permute,
nn.Dropout(0.2),
nn.ELU(),
# apply layer norm
nn.LayerNorm(self.conv_feature_dim),
]
self.input_layers = nn.Sequential(*self.input_layers)
# apply positional encoding
self.PE = PositionalEncoding(self.conv_feature_dim)
# one transformer decoder
self.encoder = nn.TransformerEncoderLayer(
d_model=self.conv_feature_dim, nhead=8, dim_feedforward=self.conv_feature_dim, activation='gelu', batch_first=True
)
# end up with two more 1D conv layers
self.output_layers = [
self.pre_conv_permute,
nn.Conv1d(in_channels=self.conv_feature_dim, out_channels=self.output_size, kernel_size=3, padding=1),
self.post_conv_permute,
nn.Dropout(0.1),
nn.ELU(),
# apply layer norm
nn.LayerNorm(self.output_size),
# second conv1d
self.pre_conv_permute,
nn.Conv1d(in_channels=self.output_size, out_channels=self.output_size, kernel_size=3, padding=1),
self.post_conv_permute,
]
self.output_layers = nn.Sequential(*self.output_layers)
def forward(self, motion_coef, do_sample=False):
"""
:param motion_coef: (batch_size, seq_len, motion_coef_dim)
:param audio: (batch_size, seq_len)
:return: (batch_size, feature_dim)
"""
batch_size, seq_len, _ = motion_coef.shape
# Motion
motion_feat = self.input_layers(motion_coef)
motion_feat = self.PE(motion_feat)
feat = self.encoder(motion_feat)
out = self.output_layers(feat)
# average pooling
out = out.mean(dim=1) # dim 1 is the seq_len
mu = out[:, :self.output_size//2]
logvar = out[:, self.output_size//2:] # this cannot have a relu since we need negative values!!!!!
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
# determine if we should sample or not
if do_sample:
return mu + eps * std
else:
out = mu + eps * std
return out, mu, logvar
def sample(self, motion_coef):
out, mu, logvar = self.forward(motion_coef)
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std