-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmodels.py
214 lines (175 loc) · 8.43 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
import logging
import keras.backend as K
import math
from keras import layers
from keras import regularizers
from keras.layers import Input, GRU
from keras.layers.convolutional import Conv2D
from keras.layers.core import Lambda, Dense, RepeatVector
from keras.layers.core import Reshape
from keras.layers.normalization import BatchNormalization
from keras.models import Model
import tensorflow as tf
from constants import *
def clipped_relu(inputs):
return Lambda(lambda y: K.minimum(K.maximum(y, 0), 20))(inputs)
def identity_block(input_tensor, kernel_size, filters, stage, block):
conv_name_base = 'res{}_{}_branch'.format(stage, block)
x = Conv2D(filters,
kernel_size=kernel_size,
strides=1,
activation=None,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.00001),
name=conv_name_base + '_2a')(input_tensor)
x = BatchNormalization(name=conv_name_base + '_2a_bn')(x)
x = clipped_relu(x)
x = Conv2D(filters,
kernel_size=kernel_size,
strides=1,
activation=None,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.00001),
name=conv_name_base + '_2b')(x)
x = BatchNormalization(name=conv_name_base + '_2b_bn')(x)
x = layers.add([x, input_tensor])
x = clipped_relu(x)
return x
def identity_block2(input_tensor, kernel_size, filters, stage, block): # next step try full-pre activation
conv_name_base = 'res{}_{}_branch'.format(stage, block)
x = Conv2D(filters,
kernel_size=1,
strides=1,
activation=None,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.00001),
name=conv_name_base + '_conv1_1')(input_tensor)
x = BatchNormalization(name=conv_name_base + '_conv1.1_bn')(x)
x = clipped_relu(x)
x = Conv2D(filters,
kernel_size=kernel_size,
strides=1,
activation=None,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.00001),
name=conv_name_base + '_conv3')(x)
x = BatchNormalization(name=conv_name_base + '_conv3_bn')(x)
x = clipped_relu(x)
x = Conv2D(filters,
kernel_size=1,
strides=1,
activation=None,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.00001),
name=conv_name_base + '_conv1_2')(x)
x = BatchNormalization(name=conv_name_base + '_conv1.2_bn')(x)
x = layers.add([x, input_tensor])
x = clipped_relu(x)
return x
def convolutional_model(input_shape=(NUM_FRAMES,64, 1), #input_shape(32,32,3)
batch_size=BATCH_SIZE * TRIPLET_PER_BATCH , num_frames=NUM_FRAMES):
# http://cs231n.github.io/convolutional-networks/
# conv weights
# #params = ks * ks * nb_filters * num_channels_input
# Conv128-s
# 5*5*128*128/2+128
# ks*ks*nb_filters*channels/strides+bias(=nb_filters)
# take 100 ms -> 4 frames.
# if signal is 3 seconds, then take 100ms per 100ms and average out this network.
# 8*8 = 64 features.
# used to share all the layers across the inputs
# num_frames = K.shape() - do it dynamically after.
def conv_and_res_block(inp, filters, stage):
conv_name = 'conv{}-s'.format(filters)
o = Conv2D(filters,
kernel_size=5,
strides=2,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.00001), name=conv_name)(inp)
o = BatchNormalization(name=conv_name + '_bn')(o)
o = clipped_relu(o)
for i in range(3):
o = identity_block(o, kernel_size=3, filters=filters, stage=stage, block=i)
return o
def cnn_component(inp):
x_ = conv_and_res_block(inp, 64, stage=1)
x_ = conv_and_res_block(x_, 128, stage=2)
x_ = conv_and_res_block(x_, 256, stage=3)
x_ = conv_and_res_block(x_, 512, stage=4)
return x_
inputs = Input(shape=input_shape) # TODO the network should be definable without explicit batch shape
#x = Lambda(lambda y: K.reshape(y, (batch_size*num_frames,input_shape[1], input_shape[2], input_shape[3])), name='pre_reshape')(inputs)
x = cnn_component(inputs) # .shape = (BATCH_SIZE , num_frames/16, 64/16, 512)
#x = Reshape((-1,2048))(x)
x = Lambda(lambda y: K.reshape(y, (-1, math.ceil(num_frames/16), 2048)), name='reshape')(x)
x = Lambda(lambda y: K.mean(y, axis=1), name='average')(x) #shape = (BATCH_SIZE, 512)
x = Dense(512, name='affine')(x) # .shape = (BATCH_SIZE , 512)
x = Lambda(lambda y: K.l2_normalize(y, axis=1), name='ln')(x)
model = Model(inputs, x, name='convolutional')
#print(model.summary())
return model
def convolutional_model_simple(input_shape=(NUM_FRAMES,64, 1), #input_shape(32,32,3)
batch_size=BATCH_SIZE * TRIPLET_PER_BATCH , num_frames=NUM_FRAMES):
# http://cs231n.github.io/convolutional-networks/
# conv weights
# #params = ks * ks * nb_filters * num_channels_input
# Conv128-s
# 5*5*128*128/2+128
# ks*ks*nb_filters*channels/strides+bias(=nb_filters)
# take 100 ms -> 4 frames.
# if signal is 3 seconds, then take 100ms per 100ms and average out this network.
# 8*8 = 64 features.
# used to share all the layers across the inputs
# num_frames = K.shape() - do it dynamically after.
def conv_and_res_block(inp, filters, stage):
conv_name = 'conv{}-s'.format(filters)
o = Conv2D(filters,
kernel_size=5,
strides=2,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.00001), name=conv_name)(inp)
o = BatchNormalization(name=conv_name + '_bn')(o)
o = clipped_relu(o)
for i in range(3):
o = identity_block2(o, kernel_size=3, filters=filters, stage=stage, block=i)
return o
def cnn_component(inp):
x_ = conv_and_res_block(inp, 64, stage=1)
x_ = conv_and_res_block(x_, 128, stage=2)
x_ = conv_and_res_block(x_, 256, stage=3)
#x_ = conv_and_res_block(x_, 512, stage=4)
return x_
inputs = Input(shape=input_shape) # TODO the network should be definable without explicit batch shape
x = cnn_component(inputs) # .shape = (BATCH_SIZE , num_frames/8, 64/8, 512)
x = Lambda(lambda y: K.reshape(y, (-1, math.ceil(num_frames / 8), 2048)), name='reshape')(x)
x = Lambda(lambda y: K.mean(y, axis=1), name='average')(x) #shape = (BATCH_SIZE, 512)
x = Dense(512, name='affine')(x) # .shape = (BATCH_SIZE , 512)
x = Lambda(lambda y: K.l2_normalize(y, axis=1), name='ln')(x)
model = Model(inputs, x, name='convolutional')
return model
def recurrent_model(input_shape=(NUM_FRAMES, 64, 1),
batch_size=BATCH_SIZE * TRIPLET_PER_BATCH ,num_frames=NUM_FRAMES):
inputs = Input(shape=input_shape)
#x = Permute((2,1))(inputs)
x = Conv2D(64,kernel_size=5,strides=2,padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=regularizers.l2(l=0.0001))(inputs)
x = BatchNormalization()(x) #shape = (BATCH_SIZE , num_frames/2, 64/2, 64)
x = clipped_relu(x)
x = Lambda(lambda y: K.reshape(y, (-1, math.ceil(num_frames / 2), 2048)), name='reshape')(x) #shape = (BATCH_SIZE , num_frames/2, 2048)
x = GRU(1024,return_sequences=True)(x) #shape = (BATCH_SIZE , num_frames/2, 1024)
x = GRU(1024,return_sequences=True)(x)
x = GRU(1024,return_sequences=True)(x) #shape = (BATCH_SIZE , num_frames/2, 1024)
x = Lambda(lambda y: K.mean(y, axis=1), name='average')(x) #shape = (BATCH_SIZE, 1024)
x = Dense(512)(x) #shape = (BATCH_SIZE, 512)
x = Lambda(lambda y: K.l2_normalize(y, axis=1), name='ln')(x)
model = Model(inputs,x,name='recurrent')
#print(model.summary())
return model
if __name__ == '__main__':
convolutional_model()