-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathttsnet.py
166 lines (123 loc) · 5.46 KB
/
ttsnet.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
import tensorflow as tf
import keras
from keras.models import Model, Layer
import numpy as np
def get_angles(pos, k, d: int):
i = k // 2
angles = pos / (10000 ** (2 * i / d))
return angles
def positional_encoding(positions: int, d: int):
angle_rads = get_angles(np.arange(positions)[:, np.newaxis],
np.arange(d)[np.newaxis, :],
d)
angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
angle_rads[:, 1::2] = np.cos(angle_rads[:, 1::2])
pos_encoding = angle_rads[np.newaxis, :, :].reshape(1, positions, d)
return tf.cast(pos_encoding, dtype=tf.float32)
class attention(Model):
def __init__(self, filters=96, kernel_size=[1, 3, 5, 7], reduction=24):
super(attention, self).__init__()
self.dconv_1 = keras.layers.Conv1D(filters=filters, kernel_size=kernel_size[0], strides=1, padding='same', dilation_rate=1)
self.dconv_2 = keras.layers.Conv1D(filters=filters, kernel_size=kernel_size[1], strides=1, padding='same', dilation_rate=3)
self.dconv_3 = keras.layers.Conv1D(filters=filters, kernel_size=kernel_size[2], strides=1, padding='same', dilation_rate=9)
self.fc_1 = keras.layers.Dense(filters)
self.fc_2 = keras.layers.Dense(filters)
self.fc_3 = keras.layers.Dense(filters)
self.fc_4 = keras.layers.Dense(filters)
self.avgpool = keras.layers.AveragePooling1D(1)
self.fc = keras.layers.Dense(reduction)
self.softmax = keras.layers.Softmax()
def call(self, input):
conv_x = []
x_1 = self.dconv_1(input)
x_2 = self.dconv_2(input)
x_3 = self.dconv_3(input)
conv_x.append(x_1)
conv_x.append(x_2)
conv_x.append(x_3)
U = sum(conv_x)
S = self.avgpool(U)
Z = self.fc(S)
weight_1 = self.fc_1(Z)
weight_1 = self.softmax(weight_1)
weight_2 = self.fc_2(Z)
weight_2 = self.softmax(weight_2)
weight_3 = self.fc_3(Z)
weight_3 = self.softmax(weight_3)
V_1 = weight_1 * x_1
V_2 = weight_2 * x_2
V_3 = weight_3 * x_3
V = tf.add(V_1, V_2)
V = tf.add(V, V_3)
return V
class transformer(Model):
def __init__(self):
super(transformer, self).__init__()
self.mha = keras.layers.MultiHeadAttention(num_heads=2,
key_dim=32,
dropout=0.2,)
self.Dense_1 = keras.layers.Dense(1)
self.act = keras.layers.ReLU()
self.Norm_1 = keras.layers.BatchNormalization(momentum=0.95)
self.Norm_2 = keras.layers.BatchNormalization(momentum=0.95)
def __call__(self, input):
x = self.mha(query=input, value=input) + input
x = self.Norm_1(x)
x = self.Dense_1(x) + x
x = self.Norm_2(x)
return x
class ttsnet(Model):
def __init__(self, time_step, hidden_layer):
super(ttsnet, self).__init__()
self.MLP_1 = keras.layers.Dense(time_step)
self.softmax = keras.layers.Softmax()
self.MLP_2 = keras.layers.Dense(80)
self.dropout = keras.layers.Dropout(rate=0.1)
self.MLP_3 = keras.layers.Dense(1)
self.act = keras.layers.ReLU()
self.Attention = attention()
self.Attention_1 = attention(filters=64, reduction=16)
self.Attention_2 = attention(filters=96, reduction=24)
self.Attention_3 = attention(filters=128, reduction=32)
self.transformer_V_1 = transformer()
self.transformer_V_2 = transformer()
self.transformer_I_1 = transformer()
self.transformer_I_2 = transformer()
self.transformer_T_1 = transformer()
self.transformer_T_2 = transformer()
self.transformer_VIT = transformer()
self.CNN_V = keras.layers.Conv1D(filters=hidden_layer, kernel_size=1, strides=1, padding='same')
self.CNN_I = keras.layers.Conv1D(filters=hidden_layer, kernel_size=1, strides=1, padding='same')
self.CNN_T = keras.layers.Conv1D(filters=hidden_layer, kernel_size=1, strides=1, padding='same')
self.LSTM_V = keras.layers.LSTM(hidden_layer, return_sequences=True)
self.LSTM_I = keras.layers.LSTM(hidden_layer, return_sequences=True)
self.LSTM_T = keras.layers.LSTM(hidden_layer, return_sequences=True)
self.pos_encoding = positional_encoding(time_step, hidden_layer)
def __call__(self, input, **kwargs):
x_V = input[:,:, 0][:,:,None]
x_V = self.CNN_V(x_V)
x_V = self.act(x_V)
x_V = self.LSTM_V(x_V)
x_V = keras.layers.Permute((2, 1))(x_V)
x_V = self.transformer_V_2(x_V)
x_I = input[:, :, 1][:, :, None]
x_I = self.CNN_I(x_I)
x_I = self.act(x_I)
x_I = self.LSTM_I(x_I)
x_I = keras.layers.Permute((2,1))(x_I)
x_I = self.transformer_I_2(x_I)
x_T = input[:, :, 2][:, :, None]
x_T = self.CNN_T(x_T)
x_T = self.act(x_T)
x_T = self.LSTM_T(x_T)
x_T = keras.layers.Permute((2,1))(x_T)
x_T = self.transformer_T_2(x_T)
x = x_V + x_I + x_T
x = self.SKAttention(x)
x = self.MLP_1(x)
x = self.softmax(x)
x = keras.layers.Flatten()(x)
x = self.act(x)
x = self.dropout(x)
out = self.MLP_3(x)
return out