174 """Instantiates the SE-Inception-ResNet v2 architecture. 175 Optionally loads weights pre-trained on ImageNet. 176 Note that when using TensorFlow, for best performance you should 177 set `"image_data_format": "channels_last"` in your Keras config 178 at `~/.keras/keras.json`. 179 The model and the weights are compatible with both TensorFlow and Theano 180 backends (but not CNTK). The data format convention used by the model is 181 the one specified in your Keras config file. 182 Note that the default input image size for this model is 299x299, instead 183 of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing 184 function is different (i.e., do not use `imagenet_utils.preprocess_input()` 185 with this model. Use `preprocess_input()` defined in this module instead). 187 include_top: whether to include the fully-connected 188 layer at the top of the network. 189 weights: one of `None` (random initialization) 190 or `'imagenet'` (pre-training on ImageNet). 191 input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) 192 to use as image input for the model. 193 input_shape: optional shape tuple, only to be specified 194 if `include_top` is `False` (otherwise the input shape 195 has to be `(299, 299, 3)` (with `'channels_last'` data format) 196 or `(3, 299, 299)` (with `'channels_first'` data format). 197 It should have exactly 3 inputs channels, 198 and width and height should be no smaller than 139. 199 E.g. `(150, 150, 3)` would be one valid value. 200 pooling: Optional pooling mode for feature extraction 201 when `include_top` is `False`. 202 - `None` means that the output of the model will be 203 the 4D tensor output of the last convolutional layer. 204 - `'avg'` means that global average pooling 205 will be applied to the output of the 206 last convolutional layer, and thus 207 the output of the model will be a 2D tensor. 208 - `'max'` means that global max pooling will be applied. 209 classes: optional number of classes to classify images 210 into, only to be specified if `include_top` is `True`, and 211 if no `weights` argument is specified. 213 A Keras `Model` instance. 215 ValueError: in case of invalid argument for `weights`, 216 or invalid input shape. 217 RuntimeError: If attempting to run this model with an unsupported backend. 219 if K.backend()
in {
'cntk'}:
220 raise RuntimeError(K.backend() +
' backend is currently unsupported for this model.')
222 if weights
not in {
'imagenet',
None}:
223 raise ValueError(
'The `weights` argument should be either ' 224 '`None` (random initialization) or `imagenet` ' 225 '(pre-training on ImageNet).')
227 if weights ==
'imagenet' and include_top
and classes != 1000:
228 raise ValueError(
'If using `weights` as imagenet with `include_top`' 229 ' as true, `classes` should be 1000')
236 data_format=K.image_data_format(),
237 require_flatten=
False,
240 if input_tensor
is None:
241 img_input = Input(shape=input_shape)
243 if not K.is_keras_tensor(input_tensor):
244 img_input = Input(tensor=input_tensor, shape=input_shape)
246 img_input = input_tensor
249 x =
conv2d_bn(img_input, 32, 3, strides=2, padding=
'valid')
252 x = MaxPooling2D(3, strides=2)(x)
254 x =
conv2d_bn(x, 192, 3, padding=
'valid')
255 x = MaxPooling2D(3, strides=2)(x)
264 branch_pool = AveragePooling2D(3, strides=1, padding=
'same')(x)
265 branch_pool =
conv2d_bn(branch_pool, 64, 1)
266 branches = [branch_0, branch_1, branch_2, branch_pool]
267 channel_axis = 1
if K.image_data_format() ==
'channels_first' else 3
268 x = Concatenate(axis=channel_axis, name=
'mixed_5b')(branches)
274 for block_idx
in range(1, 11):
277 block_type=
'block35',
281 branch_0 =
conv2d_bn(x, 384, 3, strides=2, padding=
'valid')
284 branch_1 =
conv2d_bn(branch_1, 384, 3, strides=2, padding=
'valid')
285 branch_pool = MaxPooling2D(3, strides=2, padding=
'valid')(x)
286 branches = [branch_0, branch_1, branch_pool]
287 x = Concatenate(axis=channel_axis, name=
'mixed_6a')(branches)
293 for block_idx
in range(1, 21):
296 block_type=
'block17',
301 branch_0 =
conv2d_bn(branch_0, 384, 3, strides=2, padding=
'valid')
303 branch_1 =
conv2d_bn(branch_1, 288, 3, strides=2, padding=
'valid')
306 branch_2 =
conv2d_bn(branch_2, 320, 3, strides=2, padding=
'valid')
307 branch_pool = MaxPooling2D(3, strides=2, padding=
'valid')(x)
308 branches = [branch_0, branch_1, branch_2, branch_pool]
309 x = Concatenate(axis=channel_axis, name=
'mixed_7a')(branches)
315 for block_idx
in range(1, 10):
330 x =
conv2d_bn(x, 1536, 1, name=
'conv_7b')
334 x = GlobalAveragePooling2D(name=
'avg_pool')(x)
335 x = Dense(classes, activation=
'softmax', name=
'predictions')(x)
338 x = GlobalAveragePooling2D()(x)
339 elif pooling ==
'max':
340 x = GlobalMaxPooling2D()(x)
344 if input_tensor
is not None:
345 inputs = get_source_inputs(input_tensor)
350 model = Model(inputs, x, name=
'se_inception_resnet_v2')
353 def squeeze_excite_block(input, ratio=16)
def _obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights=None)
def inception_resnet_block(x, scale, block_type, block_idx, activation='relu')
def conv2d_bn(x, filters, kernel_size, strides=1, padding='same', activation='relu', use_bias=False, name=None)