-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDCASE2019_network.py
More file actions
136 lines (111 loc) · 5.41 KB
/
DCASE2019_network.py
File metadata and controls
136 lines (111 loc) · 5.41 KB
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
import keras
from keras.layers import Conv2D, BatchNormalization, Activation, GlobalAveragePooling2D
from keras.layers import AveragePooling2D, Input, concatenate, Lambda
from keras.regularizers import l2
from keras.models import Model
#network definition
def resnet_layer(inputs,num_filters=16,kernel_size=3,strides=1,learn_bn = True,wd=1e-4,use_relu=True):
x = inputs
x = BatchNormalization(center=learn_bn, scale=learn_bn)(x)
if use_relu:
x = Activation('relu')(x)
x = Conv2D(num_filters,kernel_size=kernel_size,strides=strides,padding='same',kernel_initializer='he_normal',
kernel_regularizer=l2(wd),use_bias=False)(x)
return x
def pad_depth(inputs, desired_channels):
from keras import backend as K
y = K.zeros_like(inputs, name='pad_depth1')
return y
def My_freq_split1(x):
from keras import backend as K
return x[:,0:64,:,:]
def My_freq_split2(x):
from keras import backend as K
return x[:,64:128,:,:]
def model_resnet(num_classes,input_shape =[128,None,6], num_filters =24,wd=1e-3):
My_wd = wd #this is 5e-3 in matlab, so quite large
num_res_blocks=2
inputs = Input(shape=input_shape)
#split up frequency into two branches
Split1= Lambda(My_freq_split1)(inputs)
Split2= Lambda(My_freq_split2)(inputs)
ResidualPath1 = resnet_layer(inputs=Split1,
num_filters=num_filters,
strides=[1,2],
learn_bn = True,
wd=My_wd,
use_relu = False)
ResidualPath2 = resnet_layer(inputs=Split2,
num_filters=num_filters,
strides=[1,2],
learn_bn = True,
wd=My_wd,
use_relu = False)
# Instantiate the stack of residual units
for stack in range(4):
for res_block in range(num_res_blocks):
strides = 1
if stack > 0 and res_block == 0: # first layer but not first stack
strides = [1,2] # downsample
ConvPath1 = resnet_layer(inputs=ResidualPath1,
num_filters=num_filters,
strides=strides,
learn_bn = False,
wd=My_wd,
use_relu = True)
ConvPath2 = resnet_layer(inputs=ResidualPath2,
num_filters=num_filters,
strides=strides,
learn_bn = False,
wd=My_wd,
use_relu = True)
ConvPath1 = resnet_layer(inputs=ConvPath1,
num_filters=num_filters,
strides=1,
learn_bn = False,
wd=My_wd,
use_relu = True)
ConvPath2 = resnet_layer(inputs=ConvPath2,
num_filters=num_filters,
strides=1,
learn_bn = False,
wd=My_wd,
use_relu = True)
if stack > 0 and res_block == 0:
# first layer but not first stack: this is where we have gone up in channels and down in feature map size
#so need to account for this in the residual path
#average pool and downsample the residual path
ResidualPath1 = AveragePooling2D(pool_size=(3, 3), strides=[1,2], padding='same')(ResidualPath1)
ResidualPath2 = AveragePooling2D(pool_size=(3, 3), strides=[1,2], padding='same')(ResidualPath2)
#zero pad to increase channels
desired_channels = ConvPath1.shape.as_list()[-1]
Padding1=Lambda(pad_depth,arguments={'desired_channels':desired_channels})(ResidualPath1)
ResidualPath1 = keras.layers.Concatenate(axis=-1)([ResidualPath1,Padding1])
Padding2=Lambda(pad_depth,arguments={'desired_channels':desired_channels})(ResidualPath2)
ResidualPath2 = keras.layers.Concatenate(axis=-1)([ResidualPath2,Padding2])
ResidualPath1 = keras.layers.add([ConvPath1,ResidualPath1])
ResidualPath2 = keras.layers.add([ConvPath2,ResidualPath2])
#when we are here, we double the number of filters
num_filters *= 2
ResidualPath = concatenate([ResidualPath1,ResidualPath2],axis=1)
OutputPath = resnet_layer(inputs=ResidualPath,
num_filters=2*num_filters,
kernel_size=1,
strides=1,
learn_bn = False,
wd=My_wd,
use_relu = True)
#output layers after last sum
OutputPath = resnet_layer(inputs=OutputPath,
num_filters=num_classes,
strides = 1,
kernel_size=1,
learn_bn = False,
wd=My_wd,
use_relu=False)
OutputPath = BatchNormalization(center=False, scale=False)(OutputPath)
OutputPath = GlobalAveragePooling2D()(OutputPath)
OutputPath = Activation('softmax')(OutputPath)
# Instantiate model.
model = Model(inputs=inputs, outputs=OutputPath)
return model