-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtcn_model.py
245 lines (181 loc) · 7.7 KB
/
tcn_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
from __future__ import division
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict
import pdb
# This module should be tested carefully
class LSTM_Layer(nn.Module):
def __init__(self, input_size, hidden_size, num_layers,
bi_dir=True, use_gru=True):
super(LSTM_Layer, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.bi_dir = bi_dir
self.use_gru = use_gru
if self.use_gru:
self.lstm = nn.GRU(input_size, hidden_size, num_layers,
batch_first=True, bidirectional=bi_dir)
else:
self.lstm = nn.LSTM(input_size, hidden_size, num_layers,
batch_first=True, bidirectional=bi_dir)
def forward(self, x): # x: (batch,feature,seq)
x = x.permute(0, 2, 1)
batch_size = x.size(0)
x, _ = self.lstm(x, self.__get_init_state(batch_size)) # x: (batch,seq,hidden)
x = x.permute(0, 2, 1)
return x
def __get_init_state(self, batch_size):
if self.bi_dir:
nl_x_nd = 2 * self.num_layers
else:
nl_x_nd = 1 * self.num_layers
h0 = torch.zeros(nl_x_nd, batch_size, self.hidden_size)
h0 = h0.cuda()
if self.use_gru:
return h0
else:
c0 = torch.zeros(nl_x_nd, batch_size, self.hidden_size)
c0 = c0.cuda()
return (h0, c0)
class ChannelNorm(nn.Module):
def __init__(self):
super(ChannelNorm, self).__init__()
def forward(self, x): #(batch, feature, seq)
divider = torch.max(torch.max(torch.abs(x), dim=0)[0], dim=1)[0] + 1e-5
divider = divider.unsqueeze(0).unsqueeze(2)
divider = divider.repeat(x.size(0), 1, x.size(2))
x = x / divider
return x
class Encoder(nn.Module):
def __init__(self, input_size,
layer_type, layer_sizes,
kernel_size=None, norm_type=None,
downsample=True):
super(Encoder, self).__init__()
if layer_type not in ['TempConv', 'Bi-LSTM']:
raise Exception('Invalid Layer Type')
if layer_type == 'TempConv' and kernel_size is None:
raise Exception('Kernel Size For TempConv Not Specified')
self.output_size = layer_sizes[-1]
module_list = []
for layer in range(len(layer_sizes)):
if layer == 0:
in_chl = input_size
else:
in_chl = layer_sizes[layer-1]
out_chl = layer_sizes[layer]
if layer_type == 'TempConv':
conv_pad = kernel_size // 2
module_list.append(('conv_{}'.format(layer),
nn.Conv1d(in_chl, out_chl, kernel_size, padding=conv_pad)))
elif layer_type == 'Bi-LSTM':
module_list.append(('lstm_{}'.format(layer),
LSTM_Layer(in_chl, out_chl // 2, 1, bi_dir=True)))
if norm_type == 'Channel':
module_list.append(('cn_{}'.format(layer),
ChannelNorm()))
elif norm_type == 'Batch':
module_list.append(('bn_{}'.format(layer),
nn.BatchNorm1d(out_chl)))
elif norm_type == 'Instance':
module_list.append(('in_{}'.format(layer),
nn.InstanceNorm1d(out_chl)))
else:
print('No Norm Used!')
if layer_type == 'TempConv':
module_list.append(('relu_{}'.format(layer),
nn.ReLU()))
else:
pass
if downsample:
module_list.append(('pool_{}'.format(layer),
nn.MaxPool1d(kernel_size=2, stride=2)))
self.module = nn.Sequential(OrderedDict(module_list))
def forward(self, x): # x: (batch,feature, seq)
return self.module(x)
class Decoder(nn.Module):
def __init__(self, input_size,
layer_type, layer_sizes,
kernel_size=None, transposed_conv=None,
norm_type=None):
super(Decoder, self).__init__()
if layer_type not in ['TempConv', 'Bi-LSTM']:
raise Exception('Invalid Layer Type')
if layer_type == 'TempConv' and kernel_size is None:
raise Exception('Kernel Size For TempConv Not Specified')
if layer_type == 'TempConv' and transposed_conv is None:
raise Exception('If Use Transposed Conv Not Specified')
self.output_size = layer_sizes[-1]
module_list = []
for layer in range(len(layer_sizes)):
if layer == 0:
in_chl = input_size
else:
in_chl = layer_sizes[layer-1]
out_chl = layer_sizes[layer]
module_list.append(('up_{}'.format(layer),
nn.Upsample(scale_factor=2)))
if layer_type == 'TempConv':
conv_pad = kernel_size // 2
if transposed_conv:
module_list.append(('conv_{}'.format(layer),
nn.ConvTranspose1d(in_chl, out_chl, kernel_size,
padding=conv_pad)))
else:
module_list.append(('conv_{}'.format(layer),
nn.Conv1d(in_chl, out_chl, kernel_size,
padding=conv_pad)))
elif layer_type == 'Bi-LSTM':
module_list.append(('lstm_{}'.format(layer),
LSTM_Layer(in_chl, out_chl // 2, 1, bi_dir=True)))
if norm_type == 'Channel':
module_list.append(('cn_{}'.format(layer),
ChannelNorm()))
elif norm_type == 'Batch':
module_list.append(('bn_{}'.format(layer),
nn.BatchNorm1d(out_chl)))
elif norm_type == 'Instance':
module_list.append(('in_{}'.format(layer),
nn.InstanceNorm1d(out_chl)))
else:
print('No Norm Used!')
if layer_type == 'TempConv':
module_list.append(('relu_{}'.format(layer),
nn.ReLU()))
else:
pass
self.module = nn.Sequential(OrderedDict(module_list))
def forward(self, x): # x: (batch,feature, seq)
return self.module(x)
class EncoderDecoderNet(nn.Module):
def __init__(self, class_num, fc_size,
encoder_params,
decoder_params,
mid_lstm_params=None):
super(EncoderDecoderNet, self).__init__()
self.encoder = Encoder(**encoder_params)
self.middle_lstm = None
if mid_lstm_params is not None:
self.middle_lstm = LSTM_Layer(mid_lstm_params['input_size'],
mid_lstm_params['hidden_size'],
mid_lstm_params['layer_num'],
bi_dir=True) # batch_first
self.decoder = Decoder(**decoder_params)
self.fc1 = nn.Linear(self.decoder.output_size, fc_size)
self.fc2 = nn.Linear(fc_size, class_num)
def forward(self, x):
x = F.relu(self.extract_feature(x))
x = self.fc2(x)
return x
def extract_feature(self, x):
x = x.permute(0, 2, 1)
x = self.encoder(x)
if self.middle_lstm is not None:
x = self.middle_lstm(x)
x = self.decoder(x)
x = x.permute(0, 2, 1)
x = self.fc1(x)
return x