-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
219 lines (179 loc) · 7.24 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
import torch
import numpy as np
import os
from torch import nn
def num_flat_features(x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
class NvidiaNet(nn.Module):
def __init__(self, num_blendshapes=52):
super(NvidiaNet, self).__init__()
# formant analysis network
self.num_blendshapes = num_blendshapes
self.formant = nn.Sequential(
nn.Conv2d(1, 72, kernel_size=(1,3), stride=(1,2), padding=(0,1)),
nn.ReLU(),
nn.Conv2d(72, 108, kernel_size=(1,3), stride=(1,2), padding=(0,1)),
nn.ReLU(),
nn.Conv2d(108, 162, kernel_size=(1,3), stride=(1,2), padding=(0,1)),
nn.ReLU(),
nn.Conv2d(162, 243, kernel_size=(1,3), stride=(1,2), padding=(0,1)),
nn.ReLU(),
nn.Conv2d(243, 256, kernel_size=(1,4), stride=(1,2), padding=(0,1)),
nn.ReLU()
)
# articulation network
self.articulation = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0)),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0)),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0)),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0)),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=(1,1), stride=(2,1)),
nn.ReLU()
)
# output network
self.output = nn.Sequential(
nn.Linear(256, 150),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(150, self.num_blendshapes)
#nn.Linear(256, self.num_blendshapes)
)
def forward(self, x):
x = torch.unsqueeze(x, dim=1) # (-1, channel, height, width)
# or x = x.view(-1, 1, 64, 32)
#print('x.shape',x.shape)
#exit()
# convolution
#print('----------------------type(x)',x.dtype)
x = self.formant(x)
#print('--------------after_formant.shape',x.shape)
#exit()
x = self.articulation(x)
#print('----------------articulation.shape',x.shape)
#exit()
# fully connected
x = x.view(-1, num_flat_features(x))
x = self.output(x)
#print('---------------------final_x.shape',x.shape)
return x
class FullyLSTM(nn.Module):
def __init__(self, num_features=39, num_blendshapes=52):
super(FullyLSTM, self).__init__()
self.rnn = nn.LSTM(input_size=num_features, hidden_size=256, num_layers=2,
batch_first=True, dropout=0.5, bidirectional=True)
self.out = nn.Linear(256*2, num_blendshapes)
def forward(self, input):
# self.rnn.flatten_parameters()
output, _ = self.rnn(input)
#print('output.shape',output.shape)
output = self.out(output[:, -1, :])
return output
class LSTM(nn.Module):
def __init__(self, num_features=39, num_blendshapes=52):
super(LSTM, self).__init__()
self.rnn = nn.LSTM(input_size=num_features, hidden_size=256, num_layers=2,
batch_first=True, dropout=0.5, bidirectional=False)
self.out = nn.Linear(256, num_blendshapes)
def forward(self, input):
# self.rnn.flatten_parameters()
output, _ = self.rnn(input)
#print('output.shape',output.shape)
output = self.out(output[:, -1, :])
return output
class LSTMNvidiaNet(nn.Module):
def __init__(self, num_blendshapes=52, num_emotions=16):
super(LSTMNvidiaNet, self).__init__()
self.num_blendshapes = num_blendshapes
self.num_emotions = num_emotions
# emotion network with LSTM
self.emotion = nn.LSTM(input_size=39, hidden_size=128, num_layers=1,
batch_first=True, dropout=0.5, bidirectional=True)
self.dense = nn.Sequential(
nn.Linear(128*2, 150),
nn.ReLU(),
nn.Linear(150, self.num_emotions)
)
# formant analysis network
self.formant = nn.Sequential(
nn.Conv2d(1, 72, kernel_size=(1,3), stride=(1,2), padding=(0,1)),
nn.ReLU(),
nn.Conv2d(72, 108, kernel_size=(1,3), stride=(1,2), padding=(0,1)),
nn.ReLU(),
nn.Conv2d(108, 162, kernel_size=(1,3), stride=(1,2), padding=(0,1)),
nn.ReLU(),
nn.Conv2d(162, 243, kernel_size=(1,3), stride=(1,2), padding=(0,1)),
nn.ReLU(),
nn.Conv2d(243, 256, kernel_size=(1,4), stride=(1,2), padding=(0,1)),
nn.ReLU()
)
# articulation network
self.conv1 = nn.Conv2d(256, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0))
self.conv2 = nn.Conv2d(256+self.num_emotions, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0))
self.conv5 = nn.Conv2d(256+self.num_emotions, 256, kernel_size=(2,1), stride=(2,1))
self.relu = nn.ReLU()
# output network
self.output = nn.Sequential(
nn.Linear(256+self.num_emotions, 150),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(150, self.num_blendshapes)
)
def forward(self, x):
# extract emotion state
#print('----------------------x[:,::2].shape',x[:,::2].shape)
e_state, _ = self.emotion(x[:, ::2]) # input features are 2* overlapping
e_state = self.dense(e_state[:, -1, :]) # last
e_state = e_state.view(-1, self.num_emotions, 1, 1)
x = torch.unsqueeze(x, dim=1)
# convolution
x = self.formant(x)
#print('----------------x.shape',x.shape)
#print('e_state.shape',e_state.shape)
# conv+concat
x = self.relu(self.conv1(x))
#print('----------------x.shape', x.shape)
x = torch.cat((x, e_state.repeat(1, 1, 13, 1)), 1)
x = self.relu(self.conv2(x))
x = torch.cat((x, e_state.repeat(1, 1, 7, 1)), 1)
x = self.relu(self.conv2(x))
x = torch.cat((x, e_state.repeat(1, 1, 4, 1)), 1)
x = self.relu(self.conv2(x))
x = torch.cat((x, e_state.repeat(1, 1, 2, 1)), 1)
#print('----------------conv5(x).shape',x.shape)
x = self.relu(self.conv5(x))
x = torch.cat((x, e_state), 1)
# fully connected
x = x.view(-1, num_flat_features(x))
x = self.output(x)
return x
def count_param(model):
param_count = 0
for param in model.parameters():
param_count += param.view(-1).size()[0]
return param_count
if __name__ == '__main__':
a=torch.randn(8,25,39)
print(a.dtype)
model=NvidiaNet()
nvidianet_param=count_param(model)
print('nvidianet_param',nvidianet_param)
model_LSTM=FullyLSTM()
BiLSTM_param=count_param(model_LSTM)
print('BiLSTM_param',BiLSTM_param)
model_NvidiaLSTM=LSTMNvidiaNet()
NvidiaLstm=count_param(model_NvidiaLSTM)
print('NvidiaLSTM', NvidiaLstm)
model_lstm=LSTM()
lstm_param=count_param(model_lstm)
print('lstm_param',lstm_param)
# print(output_lstm.shape)
#output_NvidiaLSTM=model_NvidiaLSTM(a)
#print('------------------------output_NvidiaLSTM.shape',output_NvidiaLSTM.shape)