-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
executable file
·283 lines (247 loc) · 8.29 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# import torch module
import torch
import torch.nn as nn
import torch.nn.functional as F
class ConvNormRelu2d(nn.Module):
"""(conv => BN => ReLU)"""
def __init__(self, in_channels, out_channels, k, s, p=1):
super(ConvNormRelu2d, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=k, stride=s, padding=p),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.conv(x)
return x
class ConvNormRelu1d(nn.Module):
"""(conv => BN => ReLU)"""
def __init__(self, in_channels, out_channels, k, s, p=1):
super(ConvNormRelu1d, self).__init__()
self.conv = nn.Sequential(
nn.Conv1d(in_channels, out_channels, kernel_size=k, stride=s, padding=p),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.conv(x)
return x
class DoubleConv1d(nn.Module):
"""(conv => BN => ReLU) * 2"""
def __init__(self, in_channels, out_channels, k, s, p=1):
super(DoubleConv1d, self).__init__()
self.block = nn.Sequential(
ConvNormRelu1d(in_channels, out_channels, k, s, p),
ConvNormRelu1d(out_channels, out_channels, k, s, p),
)
def forward(self, x):
x = self.block(x)
return x
class DoubleConv2d(nn.Module):
"""(conv => BN => ReLU) * 2"""
def __init__(self, in_channels, out_channels, k, s, p=1):
super(DoubleConv2d, self).__init__()
self.block = nn.Sequential(
ConvNormRelu2d(in_channels, out_channels, k, s, p),
ConvNormRelu2d(out_channels, out_channels, k, s, p),
)
def forward(self, x):
x = self.block(x)
return x
class Down1d(nn.Module):
"""Max Pooling => Double Conv"""
def __init__(self, in_channels, out_channels):
super(Down1d, self).__init__()
self.block = nn.Sequential(
# nn.MaxPool1d(2, 2),
ConvNormRelu1d(in_channels, out_channels, k=4, s=2)
)
def forward(self, x):
x = self.block(x)
return x
class Up1d(nn.Module):
"""Up sampling??? => add => Double Conv"""
def __init__(self, in_channels, out_channels):
super(Up1d, self).__init__()
self.block = nn.Sequential(
DoubleConv1d(in_channels, out_channels, k=3, s=1)
)
def forward(self, x, y):
"""PoseGANの実装そのまま"""
x = torch.repeat_interleave(x, 2, dim=2)
x = x + y
x = self.block(x)
return x
class UNet1d(nn.Module):
"""
Text Encoder
"""
def __init__(self, in_channels):
super(UNet1d, self).__init__()
self.inconv = DoubleConv1d(in_channels, 256, k=3, s=1)
self.down1 = Down1d(256, 256)
self.down2 = Down1d(256, 256)
self.down3 = Down1d(256, 256)
self.down4 = Down1d(256, 256)
self.down5 = Down1d(256, 256)
self.up1 = Up1d(256, 256)
self.up2 = Up1d(256, 256)
self.up3 = Up1d(256, 256)
self.up4 = Up1d(256, 256)
self.up5 = Up1d(256, 256)
self.up6 = Up1d(256, 256)
def forward(self, x):
x0 = self.inconv(x)
x1 = self.down1(x0)
x2 = self.down2(x1)
x3 = self.down3(x2)
x4 = self.down4(x3)
x5 = self.down5(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
x = self.up5(x, x0)
return x
class Decoder(nn.Module):
"""
CNN Decoder
"""
def __init__(self, in_channels, out_channels):
super(Decoder, self).__init__()
self.layers = nn.Sequential(
DoubleConv1d(in_channels, out_channels, k=3, s=1),
DoubleConv1d(out_channels, out_channels, k=3, s=1),
DoubleConv1d(out_channels, out_channels, k=3, s=1),
DoubleConv1d(out_channels, out_channels, k=3, s=1),
nn.Conv1d(out_channels, 192, kernel_size=1, stride=1)
)
def forward(self, x):
x = self.layers(x)
return x
class PatchGan(nn.Module):
"""
Motion Discriminator
forwardへのinput shapeは(batch_size, 98, 64)を想定
"""
# def __init__(self, in_channel=98, ndf=64):
def __init__(self, in_channel=192, ndf=64):
"""
Parameter
----------
in_channel: int
入力チャネル数
ndf: int(default=64)
Size of feature maps in discriminator
"""
super(PatchGan, self).__init__()
self.layer1 = nn.Conv1d(in_channel, ndf, kernel_size=4, stride=2, padding=0)
self.layer2 = nn.LeakyReLU(0.2, inplace=True)
self.layer3 = ConvNormRelu1d(ndf, ndf * 2, k=4, s=2, p=1)
self.layer4 = ConvNormRelu1d(ndf * 2, ndf * 4, k=4, s=1, p=0)
self.layer5 = nn.Conv1d(ndf * 4, 1, kernel_size=4, stride=1, padding=0)
def forward(self, x):
x = self.layer1(F.pad(x, [1, 2], "constant", 0))
x = self.layer2(x)
x = self.layer3(F.pad(x, [1, 2], "constant", 0))
x = self.layer4(F.pad(x, [1, 2], "constant", 0))
x = self.layer5(F.pad(x, [1, 2], "constant", 0))
return x
class EncoderGRU(nn.Module):
"""
Text Encoder
"""
def __init__(self, batch_size, device, embedding_dim=64, hidden_dim=300, cnn_dim=64, channels=256):
super(EncoderGRU, self).__init__()
self.gru = nn.GRU(embedding_dim, hidden_dim, batch_first=True, bidirectional=True)
self.fc = nn.Linear(hidden_dim*2, channels*cnn_dim)
self.initial_hidden_state = torch.zeros(2, batch_size, hidden_dim, device=device)
self.batch_size = batch_size
self.cnn_dim = cnn_dim
self.channels = channels
def forward(self, x):
_, x = self.gru(x, self.initial_hidden_state)
x = F.relu(self.fc(torch.cat([x[0], x[1]], dim=1)))
return x.view([self.batch_size, self.channels, self.cnn_dim])
class GRU_Unet_Decoder(nn.Module):
"""
gru => unet => cnn_decoderモデル
※ Parameterをモデル間で統一
"""
def __init__(self, in_channels, out_channels, batch_size, device):
"""
Parameter
----------
in_channels : int
入力チャネル数
out_channels: int
出力チャネル数
batch_size: int
バッチサイズ
device:
cpu or gpu の設定
"""
super(GRU_Unet_Decoder, self).__init__()
self.gru = EncoderGRU(batch_size, device)
self.unet = UNet1d(in_channels)
self.decoder = Decoder(out_channels, out_channels)
self.batch_size = batch_size
self.device = device
def forward(self, x):
pad_frag = False # paddingは,入力発話文が可変長のときに実行
if not len(x) == self.batch_size:
pad_frag = True
pad_len = self.batch_size - len(x)
pad = torch.zeros(pad_len, 300, 64)
x = torch.cat((x, pad.to(self.device)), dim=0)
x = self.gru(x)
x = self.unet(x)
x = self.decoder(x)
if pad_frag:
x = x[:-pad_len]
return x
class Unet_Decoder(nn.Module):
"""
unet => cnn_decoderモデル
※ Parameterをモデル間で統一
"""
def __init__(self, in_channels, out_channels, batch_size, device):
"""
Parameter
----------
in_channels : int
入力チャネル数
out_channels: int
出力チャネル数
batch_size: int
バッチサイズ
device:
cpu or gpu の設定
"""
super(Unet_Decoder, self).__init__()
self.unet = UNet1d(in_channels)
self.decoder = Decoder(out_channels, out_channels)
def forward(self, x):
x = self.unet(x)
x = self.decoder(x)
return x
models = {
'gru_unet_decoder': GRU_Unet_Decoder,
'unet_decoder': Unet_Decoder,
'patchgan': PatchGan
}
def get_model(name):
"""
名前を指定してモデルを取得する
Parameters
----------
name: string
モデルの名前
Returns
-------
models[name]: class
指定したモデルのクラス
"""
return models[name]