/
tf_models.py
335 lines (258 loc) · 11.8 KB
/
tf_models.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import numpy as np
from keras.models import Sequential, Model
from keras.layers import Input, Dense, TimeDistributed, merge, Lambda
from keras.layers.core import *
from keras.layers.convolutional import *
from keras.layers.recurrent import *
import tensorflow as tf
from keras import backend as K
from keras.activations import relu
from functools import partial
clipped_relu = partial(relu, max_value=5)
def max_filter(x):
# Max over the best filter score (like ICRA paper)
max_values = K.max(x, 2, keepdims=True)
max_flag = tf.greater_equal(x, max_values)
out = x * tf.cast(max_flag, tf.float32)
return out
def channel_normalization(x):
# Normalize by the highest activation
max_values = K.max(K.abs(x), 2, keepdims=True)+1e-5
out = x / max_values
return out
def WaveNet_activation(x):
tanh_out = Activation('tanh')(x)
sigm_out = Activation('sigmoid')(x)
return Merge(mode='mul')([tanh_out, sigm_out])
# -------------------------------------------------------------
def temporal_convs_linear(n_nodes, conv_len, n_classes, n_feat, max_len,
causal=False, loss='categorical_crossentropy',
optimizer='adam', return_param_str=False):
""" Used in paper:
Segmental Spatiotemporal CNNs for Fine-grained Action Segmentation
Lea et al. ECCV 2016
Note: Spatial dropout was not used in the original paper.
It tends to improve performance a little.
"""
inputs = Input(shape=(max_len,n_feat))
if causal: model = ZeroPadding1D((conv_len//2,0))(model)
model = Convolution1D(n_nodes, conv_len, input_dim=n_feat, input_length=max_len, border_mode='same', activation='relu')(inputs)
if causal: model = Cropping1D((0,conv_len//2))(model)
model = SpatialDropout1D(0.3)(model)
model = TimeDistributed(Dense(n_classes, activation="softmax" ))(model)
model = Model(input=inputs, output=model)
model.compile(loss=loss, optimizer=optimizer, sample_weight_mode="temporal")
if return_param_str:
param_str = "tConv_C{}".format(conv_len)
if causal:
param_str += "_causal"
return model, param_str
else:
return model
def ED_TCN(n_nodes, conv_len, n_classes, n_feat, max_len,
loss='categorical_crossentropy', causal=False,
optimizer="rmsprop", activation='norm_relu',
return_param_str=False):
n_layers = len(n_nodes)
inputs = Input(shape=(max_len,n_feat))
model = inputs
# ---- Encoder ----
for i in range(n_layers):
# Pad beginning of sequence to prevent usage of future data
if causal: model = ZeroPadding1D((conv_len//2,0))(model)
model = Convolution1D(n_nodes[i], conv_len, border_mode='same')(model)
if causal: model = Cropping1D((0,conv_len//2))(model)
model = SpatialDropout1D(0.3)(model)
if activation=='norm_relu':
model = Activation('relu')(model)
model = Lambda(channel_normalization, name="encoder_norm_{}".format(i))(model)
elif activation=='wavenet':
model = WaveNet_activation(model)
else:
model = Activation(activation)(model)
model = MaxPooling1D(2)(model)
# ---- Decoder ----
for i in range(n_layers):
model = UpSampling1D(2)(model)
if causal: model = ZeroPadding1D((conv_len//2,0))(model)
model = Convolution1D(n_nodes[-i-1], conv_len, border_mode='same')(model)
if causal: model = Cropping1D((0,conv_len//2))(model)
model = SpatialDropout1D(0.3)(model)
if activation=='norm_relu':
model = Activation('relu')(model)
model = Lambda(channel_normalization, name="decoder_norm_{}".format(i))(model)
elif activation=='wavenet':
model = WaveNet_activation(model)
else:
model = Activation(activation)(model)
# Output FC layer
model = TimeDistributed(Dense(n_classes, activation="softmax" ))(model)
model = Model(input=inputs, output=model)
model.compile(loss=loss, optimizer=optimizer, sample_weight_mode="temporal", metrics=['accuracy'])
if return_param_str:
param_str = "ED-TCN_C{}_L{}".format(conv_len, n_layers)
if causal:
param_str += "_causal"
return model, param_str
else:
return model
def ED_TCN_atrous(n_nodes, conv_len, n_classes, n_feat, max_len,
loss='categorical_crossentropy', causal=False,
optimizer="rmsprop", activation='norm_relu',
return_param_str=False):
n_layers = len(n_nodes)
inputs = Input(shape=(None,n_feat))
model = inputs
# ---- Encoder ----
for i in range(n_layers):
# Pad beginning of sequence to prevent usage of future data
if causal: model = ZeroPadding1D((conv_len//2,0))(model)
model = AtrousConvolution1D(n_nodes[i], conv_len, atrous_rate=i+1, border_mode='same')(model)
if causal: model = Cropping1D((0,conv_len//2))(model)
model = SpatialDropout1D(0.3)(model)
if activation=='norm_relu':
model = Activation('relu')(model)
model = Lambda(channel_normalization, name="encoder_norm_{}".format(i))(model)
elif activation=='wavenet':
model = WaveNet_activation(model)
else:
model = Activation(activation)(model)
# ---- Decoder ----
for i in range(n_layers):
if causal: model = ZeroPadding1D((conv_len//2,0))(model)
model = AtrousConvolution1D(n_nodes[-i-1], conv_len, atrous_rate=n_layers-i, border_mode='same')(model)
if causal: model = Cropping1D((0,conv_len//2))(model)
model = SpatialDropout1D(0.3)(model)
if activation=='norm_relu':
model = Activation('relu')(model)
model = Lambda(channel_normalization, name="decoder_norm_{}".format(i))(model)
elif activation=='wavenet':
model = WaveNet_activation(model)
else:
model = Activation(activation)(model)
# Output FC layer
model = TimeDistributed(Dense(n_classes, activation="softmax" ))(model)
model = Model(input=inputs, output=model)
model.compile(loss=loss, optimizer=optimizer, sample_weight_mode="temporal", metrics=['accuracy'])
if return_param_str:
param_str = "ED-TCNa_C{}_L{}".format(conv_len, n_layers)
if causal:
param_str += "_causal"
return model, param_str
else:
return model
def TimeDelayNeuralNetwork(n_nodes, conv_len, n_classes, n_feat, max_len,
loss='categorical_crossentropy', causal=False,
optimizer="rmsprop", activation='sigmoid',
return_param_str=False):
# Time-delay neural network
n_layers = len(n_nodes)
inputs = Input(shape=(max_len,n_feat))
model = inputs
inputs_mask = Input(shape=(max_len,1))
model_masks = [inputs_mask]
# ---- Encoder ----
for i in range(n_layers):
# Pad beginning of sequence to prevent usage of future data
if causal: model = ZeroPadding1D((conv_len//2,0))(model)
model = AtrousConvolution1D(n_nodes[i], conv_len, atrous_rate=i+1, border_mode='same')(model)
# model = SpatialDropout1D(0.3)(model)
if causal: model = Cropping1D((0,conv_len//2))(model)
if activation=='norm_relu':
model = Activation('relu')(model)
model = Lambda(channel_normalization, name="encoder_norm_{}".format(i))(model)
elif activation=='wavenet':
model = WaveNet_activation(model)
else:
model = Activation(activation)(model)
# Output FC layer
model = TimeDistributed(Dense(n_classes, activation="softmax"))(model)
model = Model(input=inputs, output=model)
model.compile(loss=loss, optimizer=optimizer, sample_weight_mode="temporal", metrics=['accuracy'])
if return_param_str:
param_str = "TDN_C{}".format(conv_len)
if causal:
param_str += "_causal"
return model, param_str
else:
return model
def Dilated_TCN(num_feat, num_classes, nb_filters, dilation_depth, nb_stacks, max_len,
activation="wavenet", tail_conv=1, use_skip_connections=True, causal=False,
optimizer='adam', return_param_str=False):
"""
dilation_depth : number of layers per stack
nb_stacks : number of stacks.
"""
def residual_block(x, s, i, activation):
original_x = x
if causal:
x = ZeroPadding1D(((2**i)//2,0))(x)
conv = AtrousConvolution1D(nb_filters, 2, atrous_rate=2**i, border_mode='same',
name='dilated_conv_%d_tanh_s%d' % (2**i, s))(x)
conv = Cropping1D((0,(2**i)//2))(conv)
else:
conv = AtrousConvolution1D(nb_filters, 3, atrous_rate=2**i, border_mode='same',
name='dilated_conv_%d_tanh_s%d' % (2**i, s))(x)
conv = SpatialDropout1D(0.3)(conv)
# x = WaveNet_activation(conv)
if activation=='norm_relu':
x = Activation('relu')(conv)
x = Lambda(channel_normalization)(x)
elif activation=='wavenet':
x = WaveNet_activation(conv)
else:
x = Activation(activation)(conv)
#res_x = Convolution1D(nb_filters, 1, border_mode='same')(x)
#skip_x = Convolution1D(nb_filters, 1, border_mode='same')(x)
x = Convolution1D(nb_filters, 1, border_mode='same')(x)
res_x = Merge(mode='sum')([original_x, x])
#return res_x, skip_x
return res_x, x
input_layer = Input(shape=(max_len, num_feat))
skip_connections = []
x = input_layer
if causal:
x = ZeroPadding1D((1,0))(x)
x = Convolution1D(nb_filters, 2, border_mode='same', name='initial_conv')(x)
x = Cropping1D((0,1))(x)
else:
x = Convolution1D(nb_filters, 3, border_mode='same', name='initial_conv')(x)
for s in range(nb_stacks):
for i in range(0, dilation_depth+1):
x, skip_out = residual_block(x, s, i, activation)
skip_connections.append(skip_out)
if use_skip_connections:
x = Merge(mode='sum')(skip_connections)
x = Activation('relu')(x)
x = Convolution1D(nb_filters, tail_conv, border_mode='same')(x)
x = Activation('relu')(x)
x = Convolution1D(num_classes, tail_conv, border_mode='same')(x)
x = Activation('softmax', name='output_softmax')(x)
model = Model(input_layer, x)
model.compile(optimizer, loss='categorical_crossentropy', sample_weight_mode='temporal')
if return_param_str:
param_str = "D-TCN_C{}_B{}_L{}".format(2, nb_stacks, dilation_depth)
if causal:
param_str += "_causal"
return model, param_str
else:
return model
def BidirLSTM(n_nodes, n_classes, n_feat, max_len=None,
causal=True, loss='categorical_crossentropy', optimizer="adam",
return_param_str=False):
inputs = Input(shape=(None,n_feat))
model = LSTM(n_nodes, return_sequences=True)(inputs)
# Birdirectional LSTM
if not causal:
model_backwards = LSTM(n_nodes, return_sequences=True, go_backwards=True)(inputs)
model = Merge(mode="concat")([model, model_backwards])
model = TimeDistributed(Dense(n_classes, activation="softmax"))(model)
model = Model(input=inputs, output=model)
model.compile(optimizer=optimizer, loss=loss, sample_weight_mode="temporal", metrics=['accuracy'])
if return_param_str:
param_str = "LSTM_N{}".format(n_nodes)
if causal:
param_str += "_causal"
return model, param_str
else:
return model