2 Based on https://github.com/titu1994/keras-squeeze-excite-network/blob/master/se_resnet.py 4 Squeeze-and-Excitation ResNets 7 - [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385) 8 - []() # added when paper is published on Arxiv 10 from __future__
import print_function
11 from __future__
import absolute_import
12 from __future__
import division
14 from keras.models
import Model
15 from keras.layers
import Input
16 from keras.layers
import Dense
17 from keras.layers
import Reshape
18 from keras.layers
import Activation
19 from keras.layers
import BatchNormalization
20 from keras.layers
import MaxPooling2D
21 from keras.layers
import GlobalAveragePooling2D
22 from keras.layers
import GlobalMaxPooling2D
23 from keras.layers
import Conv2D
24 from keras.layers
import add
25 from keras.layers
import concatenate
26 from keras.layers
import multiply
27 from keras.regularizers
import l2
28 from keras.utils
import conv_utils
29 from keras.utils.data_utils
import get_file
30 from keras.engine.topology
import get_source_inputs
31 from keras.applications.imagenet_utils
import _obtain_input_shape
32 from keras.applications.resnet50
import preprocess_input
33 from keras.applications.imagenet_utils
import decode_predictions
34 from keras
import backend
as K
36 from se
import squeeze_excite_block
38 __all__ = [
'SEResNet',
'SEResNet50',
'SEResNet101',
'SEResNet154',
'preprocess_input',
'decode_predictions']
42 WEIGHTS_PATH_NO_TOP =
"" 46 initial_conv_filters=64,
48 filters=[64, 128, 256, 512],
57 """ Instantiate the Squeeze and Excite ResNet architecture. Note that , 58 when using TensorFlow for best performance you should set 59 `image_data_format="channels_last"` in your Keras config 60 at ~/.keras/keras.json. 61 The model are compatible with both 62 TensorFlow and Theano. The dimension ordering 63 convention used by the model is the one 64 specified in your Keras config file. 66 initial_conv_filters: number of features for the initial convolution 67 depth: number or layers in the each block, defined as a list. 68 ResNet-50 = [3, 4, 6, 3] 69 ResNet-101 = [3, 6, 23, 3] 70 ResNet-152 = [3, 8, 36, 3] 71 filter: number of filters per block, defined as a list. 72 filters = [64, 128, 256, 512 73 width: width multiplier for the network (for Wide ResNets) 74 bottleneck: adds a bottleneck conv to reduce computation 75 weight_decay: weight decay (l2 norm) 76 include_top: whether to include the fully-connected 77 layer at the top of the network. 78 weights: `None` (random initialization) or `imagenet` (trained 80 input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) 81 to use as image input for the model. 82 input_shape: optional shape tuple, only to be specified 83 if `include_top` is False (otherwise the input shape 84 has to be `(224, 224, 3)` (with `tf` dim ordering) 85 or `(3, 224, 224)` (with `th` dim ordering). 86 It should have exactly 3 inputs channels, 87 and width and height should be no smaller than 8. 88 E.g. `(200, 200, 3)` would be one valid value. 89 pooling: Optional pooling mode for feature extraction 90 when `include_top` is `False`. 91 - `None` means that the output of the model will be 92 the 4D tensor output of the 93 last convolutional layer. 94 - `avg` means that global average pooling 95 will be applied to the output of the 96 last convolutional layer, and thus 97 the output of the model will be a 2D tensor. 98 - `max` means that global max pooling will 100 classes: optional number of classes to classify images 101 into, only to be specified if `include_top` is True, and 102 if no `weights` argument is specified. 104 A Keras model instance. 107 if weights
not in {
'imagenet',
None}:
108 raise ValueError(
'The `weights` argument should be either ' 109 '`None` (random initialization) or `imagenet` ' 110 '(pre-training on ImageNet).')
112 if weights ==
'imagenet' and include_top
and classes != 1000:
113 raise ValueError(
'If using `weights` as imagenet with `include_top`' 114 ' as true, `classes` should be 1000')
116 assert len(depth) == len(filters),
"The length of filter increment list must match the length " \
123 data_format=K.image_data_format(),
124 require_flatten=
False)
126 if input_tensor
is None:
127 img_input = Input(shape=input_shape)
129 if not K.is_keras_tensor(input_tensor):
130 img_input = Input(tensor=input_tensor, shape=input_shape)
132 img_input = input_tensor
135 filters, depth, width, bottleneck, weight_decay, pooling)
139 if input_tensor
is not None:
140 inputs = get_source_inputs(input_tensor)
144 model = Model(inputs=inputs, outputs=x, name=
'resnext')
163 bottleneck=bottleneck,
164 weight_decay=weight_decay,
165 include_top=include_top,
167 input_tensor=input_tensor,
184 bottleneck=bottleneck,
185 weight_decay=weight_decay,
186 include_top=include_top,
188 input_tensor=input_tensor,
204 bottleneck=bottleneck,
205 weight_decay=weight_decay,
206 include_top=include_top,
208 input_tensor=input_tensor,
225 bottleneck=bottleneck,
226 weight_decay=weight_decay,
227 include_top=include_top,
229 input_tensor=input_tensor,
246 bottleneck=bottleneck,
247 weight_decay=weight_decay,
248 include_top=include_top,
250 input_tensor=input_tensor,
256 ''' Adds a pre-activation resnet block without bottleneck layers 260 filters: number of output filters 262 strides: strides of the convolution layer 264 Returns: a keras tensor 267 channel_axis = 1
if K.image_data_format() ==
"channels_first" else -1
269 x = BatchNormalization(axis=channel_axis)(input)
270 x = Activation(
'relu')(x)
272 if strides != (1, 1)
or init._keras_shape[channel_axis] != filters * k:
273 init = Conv2D(filters * k, (1, 1), padding=
'same', kernel_initializer=
'he_normal',
274 use_bias=
False, strides=strides)(x)
276 x = Conv2D(filters * k, (3, 3), padding=
'same', kernel_initializer=
'he_normal',
277 use_bias=
False, strides=strides)(x)
278 x = BatchNormalization(axis=channel_axis)(x)
279 x = Activation(
'relu')(x)
281 x = Conv2D(filters * k, (3, 3), padding=
'same', kernel_initializer=
'he_normal',
292 ''' Adds a pre-activation resnet block with bottleneck layers 296 filters: number of output filters 298 strides: strides of the convolution layer 300 Returns: a keras tensor 303 channel_axis = 1
if K.image_data_format() ==
"channels_first" else -1
304 bottleneck_expand = 4
306 x = BatchNormalization(axis=channel_axis)(input)
307 x = Activation(
'relu')(x)
309 if strides != (1, 1)
or init._keras_shape[channel_axis] != bottleneck_expand * filters * k:
310 init = Conv2D(bottleneck_expand * filters * k, (1, 1), padding=
'same', kernel_initializer=
'he_normal',
311 use_bias=
False, strides=strides)(x)
313 x = Conv2D(filters * k, (1, 1), padding=
'same', kernel_initializer=
'he_normal',
315 x = BatchNormalization(axis=channel_axis)(x)
316 x = Activation(
'relu')(x)
318 x = Conv2D(filters * k, (3, 3), padding=
'same', kernel_initializer=
'he_normal',
319 use_bias=
False, strides=strides)(x)
320 x = BatchNormalization(axis=channel_axis)(x)
321 x = Activation(
'relu')(x)
323 x = Conv2D(bottleneck_expand * filters * k, (1, 1), padding=
'same', kernel_initializer=
'he_normal',
333 def _create_se_resnet(classes, img_input, include_top, initial_conv_filters, filters,
334 depth, width, bottleneck, weight_decay, pooling):
335 '''Creates a SE ResNet model with specified parameters 337 initial_conv_filters: number of features for the initial convolution 338 include_top: Flag to include the last dense layer 339 filters: number of filters per block, defined as a list. 340 filters = [64, 128, 256, 512 341 depth: number or layers in the each block, defined as a list. 342 ResNet-50 = [3, 4, 6, 3] 343 ResNet-101 = [3, 6, 23, 3] 344 ResNet-152 = [3, 8, 36, 3] 345 width: width multiplier for network (for Wide ResNet) 346 bottleneck: adds a bottleneck conv to reduce computation 347 weight_decay: weight_decay (l2 norm) 348 pooling: Optional pooling mode for feature extraction 349 when `include_top` is `False`. 350 - `None` means that the output of the model will be 351 the 4D tensor output of the 352 last convolutional layer. 353 - `avg` means that global average pooling 354 will be applied to the output of the 355 last convolutional layer, and thus 356 the output of the model will be a 2D tensor. 357 - `max` means that global max pooling will 359 Returns: a Keras Model 361 channel_axis = 1
if K.image_data_format() ==
'channels_first' else -1
365 x = Conv2D(initial_conv_filters, (7, 7), padding=
'same', use_bias=
False, strides=(2, 2),
366 kernel_initializer=
'he_normal', kernel_regularizer=l2(weight_decay))(img_input)
368 x = MaxPooling2D((3, 3), strides=(2, 2), padding=
'same')(x)
371 for i
in range(N[0]):
378 for k
in range(1, len(N)):
384 for i
in range(N[k] - 1):
390 x = BatchNormalization(axis=channel_axis)(x)
391 x = Activation(
'relu')(x)
394 x = GlobalAveragePooling2D()(x)
395 x = Dense(classes, use_bias=
False, kernel_regularizer=l2(weight_decay),
396 activation=
'sigmoid', name=
'neutrino')(x)
399 x = GlobalAveragePooling2D()(x)
400 elif pooling ==
'max':
401 x = GlobalMaxPooling2D()(x)
def SEResNet(input_shape=None, initial_conv_filters=64, depth=[3, filters=[64, width=1, bottleneck=False, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
def SEResNet18(input_shape=None, width=1, bottleneck=False, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
Coord add(Coord c1, Coord c2)
def squeeze_excite_block(input, ratio=16)
def _create_se_resnet(classes, img_input, include_top, initial_conv_filters, filters, depth, width, bottleneck, weight_decay, pooling)
def _resnet_block(input, filters, k=1, strides=(1, 1))
def SEResNet101(input_shape=None, width=1, bottleneck=True, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
def SEResNet34(input_shape=None, width=1, bottleneck=False, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
def _resnet_bottleneck_block(input, filters, k=1, strides=(1, 1))
def _obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights=None)
def SEResNet50(input_shape=None, width=1, bottleneck=True, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
def SEResNet154(input_shape=None, width=1, bottleneck=True, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)