-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathresnet_helpers.py
169 lines (146 loc) · 7.89 KB
/
resnet_helpers.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
from keras.layers import *
from keras.layers.merge import Add
from keras.regularizers import l2
# The original help functions from keras does not have weight regularizers, so I modified them.
# Also, I changed these two functions into functional style
def identity_block(kernel_size, filters, stage, block, weight_decay=0., batch_momentum=0.99):
'''The identity_block is the block that has no conv layer at shortcut
# Arguments
kernel_size: defualt 3, the kernel size of middle conv layer at main path
filters: list of integers, the nb_filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
'''
def f(input_tensor):
nb_filter1, nb_filter2, nb_filter3 = filters
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
bn_axis = 1
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
x = Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a', kernel_regularizer=l2(weight_decay))(input_tensor)
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a', momentum=batch_momentum)(x)
x = Activation('relu')(x)
x = Conv2D(nb_filter2, (kernel_size, kernel_size),
padding='same', name=conv_name_base + '2b', kernel_regularizer=l2(weight_decay))(x)
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b', momentum=batch_momentum)(x)
x = Activation('relu')(x)
x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', kernel_regularizer=l2(weight_decay))(x)
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c', momentum=batch_momentum)(x)
x = Add()([x, input_tensor])
x = Activation('relu')(x)
return x
return f
def conv_block(kernel_size, filters, stage, block, weight_decay=0., strides=(2, 2), batch_momentum=0.99):
'''conv_block is the block that has a conv layer at shortcut
# Arguments
kernel_size: defualt 3, the kernel size of middle conv layer at main path
filters: list of integers, the nb_filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
Note that from stage 3, the first conv layer at main path is with strides=(2,2)
And the shortcut should have strides=(2,2) as well
'''
def f(input_tensor):
nb_filter1, nb_filter2, nb_filter3 = filters
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
bn_axis = 1
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
x = Conv2D(nb_filter1, (1, 1), strides=strides,
name=conv_name_base + '2a', kernel_regularizer=l2(weight_decay))(input_tensor)
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a', momentum=batch_momentum)(x)
x = Activation('relu')(x)
x = Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same',
name=conv_name_base + '2b', kernel_regularizer=l2(weight_decay))(x)
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b', momentum=batch_momentum)(x)
x = Activation('relu')(x)
x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', kernel_regularizer=l2(weight_decay))(x)
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c', momentum=batch_momentum)(x)
shortcut = Conv2D(nb_filter3, (1, 1), strides=strides,
name=conv_name_base + '1', kernel_regularizer=l2(weight_decay))(input_tensor)
shortcut = BatchNormalization(axis=bn_axis, name=bn_name_base + '1', momentum=batch_momentum)(shortcut)
x = Add()([x, shortcut])
x = Activation('relu')(x)
return x
return f
# Atrous-Convolution version of residual blocks
def atrous_identity_block(kernel_size, filters, stage, block, weight_decay=0., atrous_rate=(2, 2), batch_momentum=0.99):
'''The identity_block is the block that has no conv layer at shortcut
# Arguments
kernel_size: defualt 3, the kernel size of middle conv layer at main path
filters: list of integers, the nb_filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
'''
nb_filter1, nb_filter2, nb_filter3 = filters
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
bn_axis = 1
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
b1 = Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a', kernel_regularizer=l2(weight_decay))
b2 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a', momentum=batch_momentum)
b3 = Conv2D(nb_filter2, (kernel_size, kernel_size), dilation_rate=atrous_rate,
padding='same', name=conv_name_base + '2b', kernel_regularizer=l2(weight_decay))
b4 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b', momentum=batch_momentum)
b5 = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', kernel_regularizer=l2(weight_decay))
b6 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c', momentum=batch_momentum)
def f(input_tensor):
x = b1(input_tensor)
x = b2(x)
x = Activation('relu')(x)
x = b3(x)
x = b4(x)
x = Activation('relu')(x)
x = b5(x)
x = b6(x)
x = Add()([x, input_tensor])
x = Activation('relu')(x)
return x
return f
def atrous_conv_block(kernel_size, filters, stage, block, weight_decay=0., strides=(1, 1), atrous_rate=(2, 2), batch_momentum=0.99):
'''conv_block is the block that has a conv layer at shortcut
# Arguments
kernel_size: defualt 3, the kernel size of middle conv layer at main path
filters: list of integers, the nb_filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
'''
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
bn_axis = 1
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
nb_filter1, nb_filter2, nb_filter3 = filters
b1 = Conv2D(nb_filter1, (1, 1), strides=strides,
name=conv_name_base + '2a', kernel_regularizer=l2(weight_decay))
b2 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a', momentum=batch_momentum)
b3 = Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same', dilation_rate=atrous_rate,
name=conv_name_base + '2b', kernel_regularizer=l2(weight_decay))
b4 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b', momentum=batch_momentum)
b5 = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', kernel_regularizer=l2(weight_decay))
b6 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c', momentum=batch_momentum)
b7 = Conv2D(nb_filter3, (1, 1), strides=strides,
name=conv_name_base + '1', kernel_regularizer=l2(weight_decay))
b8 = BatchNormalization(axis=bn_axis, name=bn_name_base + '1', momentum=batch_momentum)
def f(input_tensor):
x = b1(input_tensor)
x = b2(x)
x = Activation('relu')(x)
x = b3(x)
x = b4(x)
x = Activation('relu')(x)
x = b5(x)
x = b6(x)
shortcut = b7(input_tensor)
shortcut = b8(shortcut)
x = Add()([x, shortcut])
x = Activation('relu')(x)
return x
return f