-
Notifications
You must be signed in to change notification settings - Fork 12
/
model_resnet.py
86 lines (65 loc) · 2.79 KB
/
model_resnet.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
import keras.backend as K
from keras import layers
from keras import regularizers
from keras.layers import Activation, BatchNormalization, Conv1D, Dense, GlobalAveragePooling1D, Input, MaxPooling1D, \
Lambda
from keras.models import Model
from constants import AUDIO_LENGTH
# For m34 Residual, use RepeatVector. Or tensorflow backend.repeat
def identity_block(input_tensor, kernel_size, filters, stage, block):
conv_name_base = 'res' + str(stage) + str(block) + '_branch'
bn_name_base = 'bn' + str(stage) + str(block) + '_branch'
x = Conv1D(filters,
kernel_size=kernel_size,
strides=1,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.0001),
name=conv_name_base + '2a')(input_tensor)
x = BatchNormalization(name=bn_name_base + '2a')(x)
x = Activation('relu')(x)
x = Conv1D(filters,
kernel_size=kernel_size,
strides=1,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.0001),
name=conv_name_base + '2b')(x)
x = BatchNormalization(name=bn_name_base + '2b')(x)
# up-sample from the activation maps.
# otherwise it's a mismatch. Recommendation of the authors.
# here we x2 the number of filters.
# See that as duplicating everything and concatenate them.
if input_tensor.shape[2] != x.shape[2]:
x = layers.add([x, Lambda(lambda y: K.repeat_elements(y, rep=2, axis=2))(input_tensor)])
else:
x = layers.add([x, input_tensor])
x = BatchNormalization()(x)
x = Activation('relu')(x)
return x
def resnet_34(num_classes=10):
inputs = Input(shape=[AUDIO_LENGTH, 1])
x = Conv1D(48,
kernel_size=80,
strides=4,
padding='same',
kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(l=0.0001))(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling1D(pool_size=4, strides=None)(x)
for i in range(3):
x = identity_block(x, kernel_size=3, filters=48, stage=1, block=i)
x = MaxPooling1D(pool_size=4, strides=None)(x)
for i in range(4):
x = identity_block(x, kernel_size=3, filters=96, stage=2, block=i)
x = MaxPooling1D(pool_size=4, strides=None)(x)
for i in range(6):
x = identity_block(x, kernel_size=3, filters=192, stage=3, block=i)
x = MaxPooling1D(pool_size=4, strides=None)(x)
for i in range(3):
x = identity_block(x, kernel_size=3, filters=384, stage=4, block=i)
x = GlobalAveragePooling1D()(x)
x = Dense(num_classes, activation='softmax')(x)
m = Model(inputs, x, name='resnet34')
return m