-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnetDef.py
333 lines (259 loc) · 11.7 KB
/
resnetDef.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
'''
THIS MODEL AND CODE WAS ADAPTED FROM
https://github.com/qubvel/classification_models/blob/a0f006e05485a34ccf871c421279864b0ccd220b/classification_models/models/resnet.py#L173
THIS WAS DONE TO BE ABLE TO USE PRE-TRAINED WEIGHTS AS I DO NOT HAVE ACCESS OR THE TIME TO TRAIN A CLASSIFIER ON IMAGENET
'''
import os
import collections
import keras
from keras import Model
import keras.layers as layers
import keras.backend as backend
import keras.utils as keras_utils
ModelParams = collections.namedtuple(
'ModelParams',
['model_name', 'repetitions', 'residual_block', 'attention']
)
# -------------------------------------------------------------------------
# Helpers functions
# -------------------------------------------------------------------------
def handle_block_names(stage, block):
name_base = 'stage{}_unit{}_'.format(stage + 1, block + 1)
conv_name = name_base + 'conv'
bn_name = name_base + 'bn'
relu_name = name_base + 'relu'
sc_name = name_base + 'sc'
return conv_name, bn_name, relu_name, sc_name
def get_conv_params(**params):
default_conv_params = {
'kernel_initializer': 'he_uniform',
'use_bias': False,
'padding': 'valid',
}
default_conv_params.update(params)
return default_conv_params
def get_bn_params(**params):
axis = 3 if backend.image_data_format() == 'channels_last' else 1
default_bn_params = {
'axis': axis,
'momentum': 0.99,
'epsilon': 2e-5,
'center': True,
'scale': True,
}
default_bn_params.update(params)
return default_bn_params
# -------------------------------------------------------------------------
# Residual blocks
# -------------------------------------------------------------------------
def residual_conv_block(filters, stage, block, strides=(1, 1), attention=None, cut='pre'):
"""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 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
cut: one of 'pre', 'post'. used to decide where skip connection is taken
# Returns
Output tensor for the block.
"""
def layer(input_tensor):
# get params and names of layers
conv_params = get_conv_params()
bn_params = get_bn_params()
conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(input_tensor)
x = layers.Activation('relu', name=relu_name + '1')(x)
# defining shortcut connection
if cut == 'pre':
shortcut = input_tensor
elif cut == 'post':
shortcut = layers.Conv2D(filters, (1, 1), name=sc_name, strides=strides, **conv_params)(x)
else:
raise ValueError('Cut type not in ["pre", "post"]')
# continue with convolution layers
x = layers.ZeroPadding2D(padding=(1, 1))(x)
x = layers.Conv2D(filters, (3, 3), strides=strides, name=conv_name + '1', **conv_params)(x)
x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
x = layers.Activation('relu', name=relu_name + '2')(x)
x = layers.ZeroPadding2D(padding=(1, 1))(x)
x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
# use attention block if defined
if attention is not None:
x = attention(x)
# add residual connection
x = layers.Add()([x, shortcut])
return x
return layer
def residual_bottleneck_block(filters, stage, block, strides=None, attention=None, cut='pre'):
"""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 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
cut: one of 'pre', 'post'. used to decide where skip connection is taken
# Returns
Output tensor for the block.
"""
def layer(input_tensor):
# get params and names of layers
conv_params = get_conv_params()
bn_params = get_bn_params()
conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(input_tensor)
x = layers.Activation('relu', name=relu_name + '1')(x)
# defining shortcut connection
if cut == 'pre':
shortcut = input_tensor
elif cut == 'post':
shortcut = layers.Conv2D(filters * 4, (1, 1), name=sc_name, strides=strides, **conv_params)(x)
else:
raise ValueError('Cut type not in ["pre", "post"]')
# continue with convolution layers
x = layers.Conv2D(filters, (1, 1), name=conv_name + '1', **conv_params)(x)
x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
x = layers.Activation('relu', name=relu_name + '2')(x)
x = layers.ZeroPadding2D(padding=(1, 1))(x)
x = layers.Conv2D(filters, (3, 3), strides=strides, name=conv_name + '2', **conv_params)(x)
x = layers.BatchNormalization(name=bn_name + '3', **bn_params)(x)
x = layers.Activation('relu', name=relu_name + '3')(x)
x = layers.Conv2D(filters * 4, (1, 1), name=conv_name + '3', **conv_params)(x)
# use attention block if defined
if attention is not None:
x = attention(x)
# add residual connection
x = layers.Add()([x, shortcut])
return x
return layer
# -------------------------------------------------------------------------
# Residual Model Builder
# -------------------------------------------------------------------------
def ResNet(model_params, input_shape=None, input_tensor=None, include_top=True,
classes=1000, weights='imagenet', create_encoder=False):
"""Instantiates the ResNet, SEResNet architecture.
Optionally loads weights pre-trained on ImageNet.
Note that the data format convention used by the model is
the one specified in your Keras config at `~/.keras/keras.json`.
Args:
include_top: whether to include the fully-connected
layer at the top of the network.
weights: one of `None` (random initialization),
'imagenet' (pre-training on ImageNet),
or the path to the weights file to be loaded.
input_tensor: optional Keras tensor
(i.e. output of `layers.Input()`)
to use as image input for the model.
input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` data format)
or `(3, 224, 224)` (with `channels_first` data format).
It should have exactly 3 inputs channels.
classes: optional number of classes to classify images
into, only to be specified if `include_top` is True, and
if no `weights` argument is specified.
Returns:
A Keras model instance.
Raises:
ValueError: in case of invalid argument for `weights`,
or invalid input shape.
"""
if input_tensor is None:
img_input = layers.Input(shape=input_shape, name='data')
else:
if not backend.is_keras_tensor(input_tensor):
img_input = layers.Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
# choose residual block type
ResidualBlock = model_params.residual_block
Attention = None
# get out intermidiate scales
scales = []
# get parameters for model layers
no_scale_bn_params = get_bn_params(scale=False)
bn_params = get_bn_params()
conv_params = get_conv_params()
init_filters = 64
# resnet bottom
x = layers.BatchNormalization(name='bn_data', **no_scale_bn_params)(img_input)
x = layers.ZeroPadding2D(padding=(3, 3))(x)
x = layers.Conv2D(init_filters, (7, 7), strides=(2, 2), name='conv0', **conv_params)(x)
x = layers.BatchNormalization(name='bn0', **bn_params)(x)
x = layers.Activation('relu', name='relu0')(x)
x = layers.ZeroPadding2D(padding=(1, 1))(x)
x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='valid', name='pooling0')(x)
# resnet body
for stage, rep in enumerate(model_params.repetitions):
for block in range(rep):
filters = init_filters * (2 ** stage)
# first block of first stage without strides because we have maxpooling before
if block == 0 and stage == 0:
x = ResidualBlock(filters, stage, block, strides=(1, 1),
cut='post', attention=Attention)(x)
elif block == 0:
x = ResidualBlock(filters, stage, block, strides=(2, 2),
cut='post', attention=Attention)(x)
else:
x = ResidualBlock(filters, stage, block, strides=(1, 1),
cut='pre', attention=Attention)(x)
if block == rep - 1:
scales.append(x)
x = layers.BatchNormalization(name='bn1', **bn_params)(x)
x = layers.Activation('relu', name='relu1')(x)
# resnet top
if include_top:
x = layers.GlobalAveragePooling2D(name='pool1')(x)
x = layers.Dense(classes, name='fc1')(x)
x = layers.Activation('softmax', name='softmax')(x)
# Ensure that the model takes into account any potential predecessors of `input_tensor`.
if input_tensor is not None:
inputs = keras_utils.get_source_inputs(input_tensor)
else:
inputs = img_input
# Create model.
if not create_encoder:
model = Model(inputs, x)
return model
else:
return inputs, x, scales
# -------------------------------------------------------------------------
# Residual Models
# -------------------------------------------------------------------------
MODELS_PARAMS = {
'resnet18': ModelParams('resnet18', (2, 2, 2, 2), residual_conv_block, None),
'resnet50': ModelParams('resnet50', (3, 4, 6, 3), residual_bottleneck_block, None),
}
def ResNet18(input_shape=None, input_tensor=None, weights=None, classes=1000, include_top=True, create_encoder=False):
return ResNet(
MODELS_PARAMS['resnet18'],
input_shape=input_shape,
input_tensor=input_tensor,
include_top=include_top,
classes=classes,
weights=weights,
create_encoder=create_encoder
)
def ResNet50(input_shape=None, input_tensor=None, weights=None, classes=1000, include_top=True, create_encoder=False):
return ResNet(
MODELS_PARAMS['resnet50'],
input_shape=input_shape,
input_tensor=input_tensor,
include_top=include_top,
classes=classes,
weights=weights,
create_encoder=create_encoder
)
def preprocess_input(x, **kwargs):
return x
setattr(ResNet18, '__doc__', ResNet.__doc__)
setattr(ResNet50, '__doc__', ResNet.__doc__)
if __name__ == "__main__":
test = ResNet50(input_shape=(640,192,3),include_top=False, create_encoder=False)
test.load_weights(by_name=True, filepath='resnet50_imagenet_1000.h5')
print(test.summary())
inputLayer, outputLayer, scaleLayers = ResNet50(input_shape=(640,192,3),include_top=False, create_encoder=True)