-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackbone.py
224 lines (198 loc) · 10.2 KB
/
backbone.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
from __future__ import print_function
from __future__ import absolute_import
from keras import layers
from keras.regularizers import l2
from keras.layers import Activation, Conv1D, Conv2D, Input, Lambda
from keras.layers import BatchNormalization, Flatten, Dense, Reshape
from keras.layers import MaxPooling2D, AveragePooling2D, GlobalAveragePooling2D
weight_decay = 1e-4
def identity_block_2D(input_tensor, kernel_size, filters, stage, block, trainable=True):
"""The identity block is the block that has no conv layer at shortcut.
# Arguments
input_tensor: input tensor
kernel_size: default 3, the kernel size of middle conv layer at main path
filters: list of integers, the filterss 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
# Returns
Output tensor for the block.
"""
filters1, filters2, filters3 = filters
bn_axis = 3
conv_name_1 = 'conv' + str(stage) + '_' + str(block) + '_1x1_reduce'
bn_name_1 = 'conv' + str(stage) + '_' + str(block) + '_1x1_reduce/bn'
x = Conv2D(filters1, (1, 1),
kernel_initializer='orthogonal',
use_bias=False,
trainable=trainable,
kernel_regularizer=l2(weight_decay),
name=conv_name_1)(input_tensor)
x = BatchNormalization(axis=bn_axis, trainable=trainable, name=bn_name_1)(x)
x = Activation('relu')(x)
conv_name_2 = 'conv' + str(stage) + '_' + str(block) + '_3x3'
bn_name_2 = 'conv' + str(stage) + '_' + str(block) + '_3x3/bn'
x = Conv2D(filters2, kernel_size,
padding='same',
kernel_initializer='orthogonal',
use_bias=False,
trainable=trainable,
kernel_regularizer=l2(weight_decay),
name=conv_name_2)(x)
x = BatchNormalization(axis=bn_axis, trainable=trainable, name=bn_name_2)(x)
x = Activation('relu')(x)
conv_name_3 = 'conv' + str(stage) + '_' + str(block) + '_1x1_increase'
bn_name_3 = 'conv' + str(stage) + '_' + str(block) + '_1x1_increase/bn'
x = Conv2D(filters3, (1, 1),
kernel_initializer='orthogonal',
use_bias=False,
trainable=trainable,
kernel_regularizer=l2(weight_decay),
name=conv_name_3)(x)
x = BatchNormalization(axis=bn_axis, trainable=trainable, name=bn_name_3)(x)
x = layers.add([x, input_tensor])
x = Activation('relu')(x)
return x
def conv_block_2D(input_tensor, kernel_size, filters, stage, block, strides=(2, 2), trainable=True):
"""A block that has a conv layer at shortcut.
# Arguments
input_tensor: input tensor
kernel_size: default 3, the kernel size of middle conv layer at main path
filters: list of integers, the filterss 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
# Returns
Output tensor for the block.
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
"""
filters1, filters2, filters3 = filters
bn_axis = 3
conv_name_1 = 'conv' + str(stage) + '_' + str(block) + '_1x1_reduce'
bn_name_1 = 'conv' + str(stage) + '_' + str(block) + '_1x1_reduce/bn'
x = Conv2D(filters1, (1, 1),
strides=strides,
kernel_initializer='orthogonal',
use_bias=False,
trainable=trainable,
kernel_regularizer=l2(weight_decay),
name=conv_name_1)(input_tensor)
x = BatchNormalization(axis=bn_axis, trainable=trainable, name=bn_name_1)(x)
x = Activation('relu')(x)
conv_name_2 = 'conv' + str(stage) + '_' + str(block) + '_3x3'
bn_name_2 = 'conv' + str(stage) + '_' + str(block) + '_3x3/bn'
x = Conv2D(filters2, kernel_size, padding='same',
kernel_initializer='orthogonal',
use_bias=False,
trainable=trainable,
kernel_regularizer=l2(weight_decay),
name=conv_name_2)(x)
x = BatchNormalization(axis=bn_axis, trainable=trainable, name=bn_name_2)(x)
x = Activation('relu')(x)
conv_name_3 = 'conv' + str(stage) + '_' + str(block) + '_1x1_increase'
bn_name_3 = 'conv' + str(stage) + '_' + str(block) + '_1x1_increase/bn'
x = Conv2D(filters3, (1, 1),
kernel_initializer='orthogonal',
use_bias=False,
trainable=trainable,
kernel_regularizer=l2(weight_decay),
name=conv_name_3)(x)
x = BatchNormalization(axis=bn_axis, trainable=trainable, name=bn_name_3)(x)
conv_name_4 = 'conv' + str(stage) + '_' + str(block) + '_1x1_proj'
bn_name_4 = 'conv' + str(stage) + '_' + str(block) + '_1x1_proj/bn'
shortcut = Conv2D(filters3, (1, 1), strides=strides,
kernel_initializer='orthogonal',
use_bias=False,
trainable=trainable,
kernel_regularizer=l2(weight_decay),
name=conv_name_4)(input_tensor)
shortcut = BatchNormalization(axis=bn_axis, trainable=trainable, name=bn_name_4)(shortcut)
x = layers.add([x, shortcut])
x = Activation('relu')(x)
return x
def resnet_2D_v1(input_dim, mode='train'):
bn_axis = 3
if mode == 'train':
inputs = Input(shape=input_dim, name='input')
else:
inputs = Input(shape=(input_dim[0], None, input_dim[-1]), name='input')
# ===============================================
# Convolution Block 1
# ===============================================
x1 = Conv2D(64, (7, 7),
kernel_initializer='orthogonal',
use_bias=False, trainable=True,
kernel_regularizer=l2(weight_decay),
padding='same',
name='conv1_1/3x3_s1')(inputs)
x1 = BatchNormalization(axis=bn_axis, name='conv1_1/3x3_s1/bn', trainable=True)(x1)
x1 = Activation('relu')(x1)
x1 = MaxPooling2D((2, 2), strides=(2, 2))(x1)
# ===============================================
# Convolution Section 2
# ===============================================
x2 = conv_block_2D(x1, 3, [48, 48, 96], stage=2, block='a', strides=(1, 1), trainable=True)
x2 = identity_block_2D(x2, 3, [48, 48, 96], stage=2, block='b', trainable=True)
# ===============================================
# Convolution Section 3
# ===============================================
x3 = conv_block_2D(x2, 3, [96, 96, 128], stage=3, block='a', trainable=True)
x3 = identity_block_2D(x3, 3, [96, 96, 128], stage=3, block='b', trainable=True)
x3 = identity_block_2D(x3, 3, [96, 96, 128], stage=3, block='c', trainable=True)
# ===============================================
# Convolution Section 4
# ===============================================
x4 = conv_block_2D(x3, 3, [128, 128, 256], stage=4, block='a', trainable=True)
x4 = identity_block_2D(x4, 3, [128, 128, 256], stage=4, block='b', trainable=True)
x4 = identity_block_2D(x4, 3, [128, 128, 256], stage=4, block='c', trainable=True)
# ===============================================
# Convolution Section 5
# ===============================================
x5 = conv_block_2D(x4, 3, [256, 256, 512], stage=5, block='a', trainable=True)
x5 = identity_block_2D(x5, 3, [256, 256, 512], stage=5, block='b', trainable=True)
x5 = identity_block_2D(x5, 3, [256, 256, 512], stage=5, block='c', trainable=True)
y = MaxPooling2D((3, 1), strides=(2, 1), name='mpool2')(x5)
return inputs, y
def resnet_2D_v2(input_dim, mode='train'):
bn_axis = 3
if mode == 'train':
inputs = Input(shape=input_dim, name='input')
else:
inputs = Input(shape=(input_dim[0], None, input_dim[-1]), name='input')
# ===============================================
# Convolution Block 1
# ===============================================
x1 = Conv2D(64, (7, 7), strides=(2, 2),
kernel_initializer='orthogonal',
use_bias=False, trainable=True,
kernel_regularizer=l2(weight_decay),
padding='same',
name='conv1_1/3x3_s1')(inputs)
x1 = BatchNormalization(axis=bn_axis, name='conv1_1/3x3_s1/bn', trainable=True)(x1)
x1 = Activation('relu')(x1)
x1 = MaxPooling2D((2, 2), strides=(2, 2))(x1)
# ===============================================
# Convolution Section 2
# ===============================================
x2 = conv_block_2D(x1, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), trainable=True)
x2 = identity_block_2D(x2, 3, [64, 64, 256], stage=2, block='b', trainable=True)
x2 = identity_block_2D(x2, 3, [64, 64, 256], stage=2, block='c', trainable=True)
# ===============================================
# Convolution Section 3
# ===============================================
x3 = conv_block_2D(x2, 3, [128, 128, 512], stage=3, block='a', trainable=True)
x3 = identity_block_2D(x3, 3, [128, 128, 512], stage=3, block='b', trainable=True)
x3 = identity_block_2D(x3, 3, [128, 128, 512], stage=3, block='c', trainable=True)
# ===============================================
# Convolution Section 4
# ===============================================
x4 = conv_block_2D(x3, 3, [256, 256, 1024], stage=4, block='a', strides=(1, 1), trainable=True)
x4 = identity_block_2D(x4, 3, [256, 256, 1024], stage=4, block='b', trainable=True)
x4 = identity_block_2D(x4, 3, [256, 256, 1024], stage=4, block='c', trainable=True)
# ===============================================
# Convolution Section 5
# ===============================================
x5 = conv_block_2D(x4, 3, [512, 512, 2048], stage=5, block='a', trainable=True)
x5 = identity_block_2D(x5, 3, [512, 512, 2048], stage=5, block='b', trainable=True)
x5 = identity_block_2D(x5, 3, [512, 512, 2048], stage=5, block='c', trainable=True)
y = MaxPooling2D((3, 1), strides=(2, 1), name='mpool2')(x5)
return inputs, y