254 """Instantiates the SE-MobileNet architecture. 255 Note that only TensorFlow is supported for now, 256 therefore it only works with the data format 257 `image_data_format='channels_last'` in your Keras config 258 at `~/.keras/keras.json`. 259 To load a MobileNet model via `load_model`, import the custom 260 objects `relu6` and `DepthwiseConv2D` and pass them to the 261 `custom_objects` parameter. 263 model = load_model('mobilenet.h5', custom_objects={ 264 'relu6': mobilenet.relu6, 265 'DepthwiseConv2D': mobilenet.DepthwiseConv2D}) 267 input_shape: optional shape tuple, only to be specified 268 if `include_top` is False (otherwise the input shape 269 has to be `(224, 224, 3)` (with `channels_last` data format) 270 or (3, 224, 224) (with `channels_first` data format). 271 It should have exactly 3 inputs channels, 272 and width and height should be no smaller than 32. 273 E.g. `(200, 200, 3)` would be one valid value. 274 alpha: controls the width of the network. 275 - If `alpha` < 1.0, proportionally decreases the number 276 of filters in each layer. 277 - If `alpha` > 1.0, proportionally increases the number 278 of filters in each layer. 279 - If `alpha` = 1, default number of filters from the paper 280 are used at each layer. 281 depth_multiplier: depth multiplier for depthwise convolution 282 (also called the resolution multiplier) 283 dropout: dropout rate 284 include_top: whether to include the fully-connected 285 layer at the top of the network. 286 weights: `None` (random initialization) or 287 `imagenet` (ImageNet weights) 288 input_tensor: optional Keras tensor (i.e. output of 290 to use as image input for the model. 291 pooling: Optional pooling mode for feature extraction 292 when `include_top` is `False`. 293 - `None` means that the output of the model 294 will be the 4D tensor output of the 295 last convolutional layer. 296 - `avg` means that global average pooling 297 will be applied to the output of the 298 last convolutional layer, and thus 299 the output of the model will be a 301 - `max` means that global max pooling will 303 classes: optional number of classes to classify images 304 into, only to be specified if `include_top` is True, and 305 if no `weights` argument is specified. 307 A Keras model instance. 309 ValueError: in case of invalid argument for `weights`, 310 or invalid input shape. 311 RuntimeError: If attempting to run this model with a 312 backend that does not support separable convolutions. 315 if K.backend() !=
'tensorflow':
316 raise RuntimeError(
'Only TensorFlow backend is currently supported, ' 317 'as other backends do not support ' 318 'depthwise convolution.')
320 if weights
not in {
'imagenet',
None}:
321 raise ValueError(
'The `weights` argument should be either ' 322 '`None` (random initialization) or `imagenet` ' 323 '(pre-training on ImageNet).')
325 if weights ==
'imagenet' and include_top
and classes != 1000:
326 raise ValueError(
'If using `weights` as ImageNet with `include_top` ' 327 'as true, `classes` should be 1000')
330 if input_shape
is None:
333 if K.image_data_format() ==
'channels_first':
334 rows = input_shape[1]
335 cols = input_shape[2]
337 rows = input_shape[0]
338 cols = input_shape[1]
340 if rows == cols
and rows
in [128, 160, 192, 224]:
346 default_size=default_size,
348 data_format=K.image_data_format(),
349 require_flatten=include_top,
352 if K.image_data_format() ==
'channels_last':
353 row_axis, col_axis = (0, 1)
355 row_axis, col_axis = (1, 2)
356 rows = input_shape[row_axis]
357 cols = input_shape[col_axis]
359 if input_tensor
is None:
360 img_input = Input(shape=input_shape)
362 if not K.is_keras_tensor(input_tensor):
363 img_input = Input(tensor=input_tensor, shape=input_shape)
365 img_input = input_tensor
367 x =
_conv_block(img_input, 32, alpha, strides=(2, 2))
371 strides=(2, 2), block_id=2)
375 strides=(2, 2), block_id=4)
379 strides=(2, 2), block_id=6)
387 strides=(2, 2), block_id=12)
391 if K.image_data_format() ==
'channels_first':
392 shape = (
int(1024 * alpha), 1, 1)
394 shape = (1, 1,
int(1024 * alpha))
396 x = GlobalAveragePooling2D()(x)
397 x = Reshape(shape, name=
'reshape_n_1')(x)
398 x = Dropout(dropout, name=
'dropout')(x)
399 x = Conv2D(classes, (1, 1),
400 padding=
'same', name=
'conv_preds')(x)
401 x = Activation(
'softmax', name=
'act_softmax')(x)
402 x = Reshape((classes,), name=
'reshape_final')(x)
405 x = GlobalAveragePooling2D()(x)
406 elif pooling ==
'max':
407 x = GlobalMaxPooling2D()(x)
411 if input_tensor
is not None:
412 inputs = get_source_inputs(input_tensor)
417 model = Model(inputs, x, name=
'se_mobilenet_%0.2f_%s' % (alpha, rows))
def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha, depth_multiplier=1, strides=(1, 1), block_id=1)
def _obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights=None)
def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1))