forked from lochenchou/MOSNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
179 lines (120 loc) · 6.82 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
import tensorflow
from keras.layers import ELU, BatchNormalization
from tensorflow import keras
from tensorflow.keras import Model, layers
from tensorflow.keras.layers import Dense, Dropout, Conv2D
from tensorflow.keras.layers import LSTM, TimeDistributed, Bidirectional, MultiHeadAttention
from tensorflow.keras.constraints import max_norm
def block(x, filters):
res = x
pre_res = (Conv2D(filters, (3, 3), strides=(1, 3), activation='relu', padding='same'))(res)
conv1 = (Conv2D(filters, (3, 3), strides=(1, 1), activation='relu', padding='same'))(x)
conv1 = BatchNormalization()(conv1)
conv1 = (Conv2D(filters, (3, 3), strides=(1, 1), activation='relu', padding='same'))(conv1)
conv1 = (Conv2D(filters, (3, 3), strides=(1, 3), activation='relu', padding='same'))(conv1)
out = keras.layers.add([pre_res, conv1])
return out
class ResNet_BLSTM(object):
def __init__(self):
print('ResNet_BLSTM init')
def build(self):
_input = keras.Input(shape=(None, 257))
re_input = layers.Reshape((-1, 257, 1), input_shape=(-1, 257))(_input)
# # resnet
res1 = block(re_input, 16)
res2 = block(res1, 32)
res3 = block(res2, 64)
res4 = block(res3, 128)
re_shape = layers.Reshape((-1, 4 * 128), input_shape=(-1, 4, 128))(res4)
# BLSTM
blstm1 = Bidirectional(
LSTM(128, return_sequences=True, dropout=0.3,
recurrent_constraint=max_norm(0.00001)),
merge_mode='concat')(re_shape)
atten1 = MultiHeadAttention(num_heads=1, key_dim=256)(blstm1, blstm1, blstm1)
# DNN
flatten = TimeDistributed(layers.Flatten())(atten1)
dense1 = TimeDistributed(Dense(128, activation='relu'))(flatten)
dense1 = Dropout(0.3)(dense1)
frame_score = TimeDistributed(Dense(1), name='frame')(dense1)
average_score = layers.GlobalAveragePooling1D(name='avg')(frame_score)
model = Model(outputs=[average_score, frame_score], inputs=_input)
return model
class CNN_BLSTM(object):
def __init__(self):
print('CNN_BLSTM init')
def build(self):
_input = keras.Input(shape=(None, 257))
re_input = layers.Reshape((-1, 257, 1), input_shape=(-1, 257))(_input)
# CNN
conv1 = (Conv2D(16, (3,3), strides=(1, 1), activation='relu', padding='same'))(re_input)
conv1 = (Conv2D(16, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv1)
conv1 = (Conv2D(16, (3,3), strides=(1, 3), activation='relu', padding='same'))(conv1)
conv2 = (Conv2D(32, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv1)
conv2 = (Conv2D(32, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv2)
conv2 = (Conv2D(32, (3,3), strides=(1, 3), activation='relu', padding='same'))(conv2)
conv3 = (Conv2D(64, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv2)
conv3 = (Conv2D(64, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv3)
conv3 = (Conv2D(64, (3,3), strides=(1, 3), activation='relu', padding='same'))(conv3)
conv4 = (Conv2D(128, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv3)
conv4 = (Conv2D(128, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv4)
conv4 = (Conv2D(128, (3,3), strides=(1, 3), activation='relu', padding='same'))(conv4)
re_shape = layers.Reshape((-1, 4*128), input_shape=(-1, 4, 128))(conv4)
# BLSTM
blstm1 = Bidirectional(
LSTM(128, return_sequences=True, dropout=0.3,
recurrent_dropout=0.3, recurrent_constraint=max_norm(0.00001)),
merge_mode='concat')(re_shape)
# DNN
flatten = TimeDistributed(layers.Flatten())(blstm1)
dense1=TimeDistributed(Dense(128, activation='relu'))(flatten)
dense1=Dropout(0.3)(dense1)
frame_score=TimeDistributed(Dense(1), name='frame')(dense1)
average_score=layers.GlobalAveragePooling1D(name='avg')(frame_score)
model = Model(outputs=[average_score, frame_score], inputs=_input)
return model
class CNN(object):
def __init__(self):
print('CNN init')
def build(self):
_input = keras.Input(shape=(None, 257))
re_input = layers.Reshape((-1, 257, 1), input_shape=(-1, 257))(_input)
# CNN
conv1 = (Conv2D(16, (3,3), strides=(1, 1), activation='relu', padding='same'))(re_input)
conv1 = (Conv2D(16, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv1)
conv1 = (Conv2D(16, (3,3), strides=(1, 3), activation='relu', padding='same'))(conv1)
conv2 = (Conv2D(32, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv1)
conv2 = (Conv2D(32, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv2)
conv2 = (Conv2D(32, (3,3), strides=(1, 3), activation='relu', padding='same'))(conv2)
conv3 = (Conv2D(64, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv2)
conv3 = (Conv2D(64, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv3)
conv3 = (Conv2D(64, (3,3), strides=(1, 3), activation='relu', padding='same'))(conv3)
conv4 = (Conv2D(128, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv3)
conv4 = (Conv2D(128, (3,3), strides=(1, 1), activation='relu', padding='same'))(conv4)
conv4 = (Conv2D(128, (3,3), strides=(1, 3), activation='relu', padding='same'))(conv4)
# DNN
flatten = TimeDistributed(layers.Flatten())(conv4)
dense1=TimeDistributed(Dense(64, activation='relu'))(flatten)
dense1=Dropout(0.3)(dense1)
frame_score=TimeDistributed(Dense(1), name='frame')(dense1)
average_score=layers.GlobalAveragePooling1D(name='avg')(frame_score)
model = Model(outputs=[average_score, frame_score], inputs=_input)
return model
class BLSTM(object):
def __init__(self):
print('BLSTM init')
def build(self):
_input = keras.Input(shape=(None, 257))
# BLSTM
blstm1 = Bidirectional(
LSTM(128, return_sequences=True, dropout=0.3,
recurrent_dropout=0.3, recurrent_constraint=max_norm(0.00001)),
merge_mode='concat')(_input)
# DNN
flatten = TimeDistributed(layers.Flatten())(blstm1)
dense1=TimeDistributed(Dense(64, activation='relu'))(flatten)
dense1=Dropout(0.3)(dense1)
frame_score=TimeDistributed(Dense(1), name='frame')(dense1)
average_score=layers.GlobalAveragePooling1D(name='avg')(frame_score)
model = Model(outputs=[average_score, frame_score], inputs=_input)
return model