-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
252 lines (217 loc) · 9.9 KB
/
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
from keras.models import Sequential, Model
from keras.layers import Conv2D, MaxPooling2D, ConvLSTM2D, Conv3D, LSTM, TimeDistributed
from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization
from keras.layers import Input, Average, Masking, Reshape, Lambda
from keras.layers import Bidirectional, Concatenate
from keras import backend as K
from keras.models import load_model
import sys
sys.path.append("../prednet")
import prednet_model
def convnet(input_shape, n_classes, hidden_dims, drop_rate=0.5):
if K.image_data_format() == 'channels_first':
input_shape = (input_shape[2], input_shape[0], input_shape[1])
model = Sequential()
model.add(Conv2D(100, (3, 3), padding='same',
input_shape=input_shape,
activation='relu'))
#model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(drop_rate))
model.add(Flatten())
for dim in hidden_dims:
model.add(Dense(dim, activation='relu'))
#model.add(BatchNormalization())
model.add(Dropout(drop_rate))
model.add(Dense(n_classes))
model.add(Activation('softmax'))
return model
def convlstm(input_shape, n_classes, hidden_dims, drop_rate=0.5, mask_value=None):
if K.image_data_format() == 'channels_first':
input_shape = (input_shape[0], input_shape[3], input_shape[1], input_shape[2])
inputs = Input(shape=input_shape)
if mask_value is not None:
x = TimeDistributed(Flatten())(inputs)
x = TimeDistributed(Masking(mask_value=mask_value))(x)
x = TimeDistributed(Reshape(input_shape[1:]))(x)
else:
x = inputs
x = ConvLSTM2D(filters=200, kernel_size=(3, 3), dropout=drop_rate,
padding='same', return_sequences=True)(x)
x = Conv3D(filters=1, kernel_size=(3, 3, 3), activation='sigmoid',
padding='same', data_format='channels_last')(x)
x = Flatten()(x)
for dim in hidden_dims:
x = Dense(dim, activation='relu')(x)
x = Dropout(drop_rate)(x)
predictions = Dense(n_classes, activation='softmax')(x)
return Model(inputs=inputs, outputs=[predictions])
def lstm(input_shape, n_classes, hidden_dims, bidirectional=None,
drop_rate=0.5, mask_value=None, **config):
if K.image_data_format() == 'channels_first':
input_shape = (input_shape[0], input_shape[3], input_shape[1], input_shape[2])
inputs = Input(shape=input_shape)
x = TimeDistributed(Flatten())(inputs)
if mask_value is not None:
x = Masking(mask_value=mask_value)(x)
for i, dim in enumerate(hidden_dims):
if bidirectional:
x = Bidirectional(LSTM(dim, return_sequences=(i<len(hidden_dims) - 1), dropout=drop_rate), merge_mode='concat')(x)
else:
x = LSTM(dim, return_sequences=(i<len(hidden_dims) - 1), dropout=drop_rate)(x)
predictions = Dense(n_classes, activation='softmax')(x)
return Model(inputs=inputs, outputs=predictions)
def crop(dimension, start=None, end=None, stride=1, name=None):
# Crops (or slices) a Tensor on a given dimension from start to end
# example : to crop tensor x[:, :, 5:10]
# call slice(2, 5, 10) as you want to crop on the second dimension
# See https://github.com/keras-team/keras/issues/890
def func(x):
if dimension == 0:
return x[start:end:stride]
if dimension == 1:
return x[:, start:end:stride]
if dimension == 2:
return x[:, :, start:end:stride]
if dimension == 3:
return x[:, :, :, start:end:stride]
if dimension == 4:
return x[:, :, :, :, start:end:stride]
return Lambda(func, name=name)
def conv_layer(tensor, filters, dropout, data_format, name):
x = tensor
for i, f in enumerate(filters):
name_ = name + '_' + str(i)
x = TimeDistributed(Conv2D(f, (3, 3), padding='same',
data_format=data_format,
activation='relu'),
name='conv_' + name_)(x)
#x = TimeDistributed(BatchNormalization())(x)
x = TimeDistributed(MaxPooling2D(pool_size=(2, 2),
data_format=data_format,
padding='same'),
name='maxpool_' + name_)(x)
x = TimeDistributed(Dropout(dropout), name='dropout_' + name_)(x)
return x
def lstm_layer(tensor, mask_value, hidden_dims,
dropout, name, bidirectional=False,
return_sequences=False):
x = TimeDistributed(Flatten(), name='flatten_' + name)(tensor)
if mask_value is not None:
x = Masking(mask_value=mask_value)(x)
for dim in hidden_dims:
if bidirectional:
x = Bidirectional(LSTM(dim, return_sequences=return_sequences, dropout=dropout),
merge_mode='concat', name='BiLSTM_' + name)(x)
else:
x = LSTM(dim, return_sequences=return_sequences, dropout=dropout)(x)
return x
def multistream2(input_shape, n_classes, hidden_dims,
drop_rate=0.5, mask_value=None, **config):
if config is None:
config = {}
config['input_width'] = input_shape[1]
config['input_height'] = input_shape[2]
config['input_channels'] = input_shape[3]
model = prednet_model.create_model(train=False,
output_mode='representation',
**config)
prednet_layer = model.layers[1]
prednet_layer.trainable = False
layer_config = prednet_layer.get_config()
data_format = layer_config['data_format'] if 'data_format' in layer_config else layer_config['dim_ordering']
image = model.inputs[0]
conv_filters = [50, 50, 10]
image = conv_layer(image, conv_filters, drop_rate, data_format, 'image')
image = lstm_layer(image, mask_value, hidden_dims, drop_rate, 'image')
index = 0
reps = []
flat_shapes = [61440, 245760, 122880, 61440]
filters = [3, 48, 96, 192]
prednet_out = model.outputs[0]
for l in range(prednet_layer.nb_layers):
if l not in [1, 2]:
r = crop(2, index, index + flat_shapes[l],
name='r_crop_' + str(l))(prednet_out)
# Unflatten representation
width = input_shape[1] / (2 ** l)
height = input_shape[2] / (2 ** l)
if data_format == 'channels_first':
shape = (-1, filters[l], height, width)
else:
shape = (-1, height, width, filters[l])
r = Reshape(shape)(r)
reps.append(r)
index += flat_shapes[l]
rep_layers = []
conv_filters = [[50, 50, 10], [50]]
for i, r in enumerate(reps):
rep_l = conv_layer(r, conv_filters[i], drop_rate, data_format, 'r' + str(i))
rep_l = lstm_layer(rep_l, mask_value, hidden_dims, drop_rate, 'r' + str(i))
rep_layers.append(rep_l)
x = Concatenate(axis=1)([image] + [l for l in rep_layers])
predictions = Dense(n_classes, activation='softmax')(x)
model = Model(inputs=model.inputs, outputs=predictions)
return model
def multistream(input_shape, n_classes, hidden_dims,
drop_rate=0.5, mask_value=None, **config):
if config is None:
config = {}
config['input_width'] = input_shape[1]
config['input_height'] = input_shape[2]
config['input_channels'] = input_shape[3]
model = prednet_model.create_model(train=False,
output_mode='representation',
**config)
prednet_layer = model.layers[1]
for l in model.layers:
l.trainable = False
#image_input = model.inputs[0]
#image = lstm_layer(image_input, mask_value, hidden_dims, drop_rate, 'image')
index = 0
reps = []
flat_shapes = [61440, 245760, 122880, 61440]
for l in range(prednet_layer.nb_layers):
if l not in [1]:
reps.append(crop(2, index, index + flat_shapes[l])(model.outputs[0]))
index += flat_shapes[l]
lstms = []
for i, r in enumerate(reps):
lstms.append(lstm_layer(r, mask_value, hidden_dims, drop_rate,
'r' + str(i), return_sequences=True))
#x = Concatenate(axis=1)([image] + [l for l in lstms])
x = Concatenate(axis=2)([l for l in lstms])
x = lstm_layer(x, mask_value, hidden_dims, drop_rate,
'joint', bidirectional=True)
predictions = Dense(n_classes, activation='softmax')(x)
model = Model(inputs=model.inputs, outputs=predictions)
return model
def prednet(input_shape, n_classes, hidden_dims,
drop_rate=0.5, mask_value=None, **config):
if config is None:
config = {}
config['input_height'] = input_shape[1]
config['input_width'] = input_shape[2]
config['input_channels'] = input_shape[3]
model = prednet_model.create_model(train=False,
output_mode='representation',
**config)
x = crop(1, -1)(model.outputs[0])
x = Flatten()(x)
for dim in hidden_dims:
x = Dense(dim, activation='relu')(x)
x = Dropout(drop_rate)(x)
predictions = Dense(n_classes, activation='softmax')(x)
model = Model(inputs=model.inputs, outputs=predictions)
return model
def ensemble(models, input_shape):
if models and len(models) < 2:
raise ValueError('To get an ensemble you need at least two models')
for i, model in enumerate(models):
for l in model.layers:
l.name = l.name + '_ens_{}'.format(i)
inputs = [inp for model in models for inp in model.inputs]
outputs = [inp for model in models for inp in model.outputs]
avg = Average()(outputs)
ensemble = Model(inputs=inputs, outputs=avg)
return ensemble