Classes | Functions
se_mobilenet Namespace Reference

Classes

class  DepthwiseConv2D
 

Functions

def relu6 (x)
 
def preprocess_input (x)
 
def SEMobileNet (input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
 
def _conv_block (inputs, filters, alpha, kernel=(3, 3), strides=(1, 1))
 
def _depthwise_conv_block (inputs, pointwise_conv_filters, alpha, depth_multiplier=1, strides=(1, 1), block_id=1)
 

Detailed Description

SE MobileNet v1 models for Keras.

# Reference
- [MobileNets: Efficient Convolutional Neural Networks for
   Mobile Vision Applications](https://arxiv.org/pdf/1704.04861.pdf))

Function Documentation

def se_mobilenet._conv_block (   inputs,
  filters,
  alpha,
  kernel = (3, 3),
  strides = (1, 1) 
)
private
Adds an initial convolution layer (with batch normalization and relu6).
# Arguments
    inputs: Input tensor of shape `(rows, cols, 3)`
        (with `channels_last` data format) or
        (3, rows, cols) (with `channels_first` data format).
        It should have exactly 3 inputs channels,
        and width and height should be no smaller than 32.
        E.g. `(224, 224, 3)` would be one valid value.
    filters: Integer, the dimensionality of the output space
        (i.e. the number output of filters in the convolution).
    alpha: controls the width of the network.
        - If `alpha` < 1.0, proportionally decreases the number
            of filters in each layer.
        - If `alpha` > 1.0, proportionally increases the number
            of filters in each layer.
        - If `alpha` = 1, default number of filters from the paper
             are used at each layer.
    kernel: An integer or tuple/list of 2 integers, specifying the
        width and height of the 2D convolution window.
        Can be a single integer to specify the same value for
        all spatial dimensions.
    strides: An integer or tuple/list of 2 integers,
        specifying the strides of the convolution along the width and height.
        Can be a single integer to specify the same value for
        all spatial dimensions.
        Specifying any stride value != 1 is incompatible with specifying
        any `dilation_rate` value != 1.
# Input shape
    4D tensor with shape:
    `(samples, channels, rows, cols)` if data_format='channels_first'
    or 4D tensor with shape:
    `(samples, rows, cols, channels)` if data_format='channels_last'.
# Output shape
    4D tensor with shape:
    `(samples, filters, new_rows, new_cols)` if data_format='channels_first'
    or 4D tensor with shape:
    `(samples, new_rows, new_cols, filters)` if data_format='channels_last'.
    `rows` and `cols` values might have changed due to stride.
# Returns
    Output tensor of block.

Definition at line 422 of file se_mobilenet.py.

422 def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1)):
423  """Adds an initial convolution layer (with batch normalization and relu6).
424  # Arguments
425  inputs: Input tensor of shape `(rows, cols, 3)`
426  (with `channels_last` data format) or
427  (3, rows, cols) (with `channels_first` data format).
428  It should have exactly 3 inputs channels,
429  and width and height should be no smaller than 32.
430  E.g. `(224, 224, 3)` would be one valid value.
431  filters: Integer, the dimensionality of the output space
432  (i.e. the number output of filters in the convolution).
433  alpha: controls the width of the network.
434  - If `alpha` < 1.0, proportionally decreases the number
435  of filters in each layer.
436  - If `alpha` > 1.0, proportionally increases the number
437  of filters in each layer.
438  - If `alpha` = 1, default number of filters from the paper
439  are used at each layer.
440  kernel: An integer or tuple/list of 2 integers, specifying the
441  width and height of the 2D convolution window.
442  Can be a single integer to specify the same value for
443  all spatial dimensions.
444  strides: An integer or tuple/list of 2 integers,
445  specifying the strides of the convolution along the width and height.
446  Can be a single integer to specify the same value for
447  all spatial dimensions.
448  Specifying any stride value != 1 is incompatible with specifying
449  any `dilation_rate` value != 1.
450  # Input shape
451  4D tensor with shape:
452  `(samples, channels, rows, cols)` if data_format='channels_first'
453  or 4D tensor with shape:
454  `(samples, rows, cols, channels)` if data_format='channels_last'.
455  # Output shape
456  4D tensor with shape:
457  `(samples, filters, new_rows, new_cols)` if data_format='channels_first'
458  or 4D tensor with shape:
459  `(samples, new_rows, new_cols, filters)` if data_format='channels_last'.
460  `rows` and `cols` values might have changed due to stride.
461  # Returns
462  Output tensor of block.
463  """
464  channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
465  filters = int(filters * alpha)
466  x = Conv2D(filters, kernel,
467  padding='same',
468  use_bias=False,
469  strides=strides,
470  name='conv1')(inputs)
471  x = BatchNormalization(axis=channel_axis, name='conv1_bn')(x)
472  return Activation(relu6, name='conv1_relu')(x)
473 
474 
def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1))
def se_mobilenet._depthwise_conv_block (   inputs,
  pointwise_conv_filters,
  alpha,
  depth_multiplier = 1,
  strides = (1, 1),
  block_id = 1 
)
private
Adds a depthwise convolution block.
A depthwise convolution block consists of a depthwise conv,
batch normalization, relu6, pointwise convolution,
batch normalization and relu6 activation.
# Arguments
    inputs: Input tensor of shape `(rows, cols, channels)`
        (with `channels_last` data format) or
        (channels, rows, cols) (with `channels_first` data format).
    pointwise_conv_filters: Integer, the dimensionality of the output space
        (i.e. the number output of filters in the pointwise convolution).
    alpha: controls the width of the network.
        - If `alpha` < 1.0, proportionally decreases the number
            of filters in each layer.
        - If `alpha` > 1.0, proportionally increases the number
            of filters in each layer.
        - If `alpha` = 1, default number of filters from the paper
             are used at each layer.
    depth_multiplier: The number of depthwise convolution output channels
        for each input channel.
        The total number of depthwise convolution output
        channels will be equal to `filters_in * depth_multiplier`.
    strides: An integer or tuple/list of 2 integers,
        specifying the strides of the convolution along the width and height.
        Can be a single integer to specify the same value for
        all spatial dimensions.
        Specifying any stride value != 1 is incompatible with specifying
        any `dilation_rate` value != 1.
    block_id: Integer, a unique identification designating the block number.
# Input shape
    4D tensor with shape:
    `(batch, channels, rows, cols)` if data_format='channels_first'
    or 4D tensor with shape:
    `(batch, rows, cols, channels)` if data_format='channels_last'.
# Output shape
    4D tensor with shape:
    `(batch, filters, new_rows, new_cols)` if data_format='channels_first'
    or 4D tensor with shape:
    `(batch, new_rows, new_cols, filters)` if data_format='channels_last'.
    `rows` and `cols` values might have changed due to stride.
# Returns
    Output tensor of block.

Definition at line 476 of file se_mobilenet.py.

476  depth_multiplier=1, strides=(1, 1), block_id=1):
477  """Adds a depthwise convolution block.
478  A depthwise convolution block consists of a depthwise conv,
479  batch normalization, relu6, pointwise convolution,
480  batch normalization and relu6 activation.
481  # Arguments
482  inputs: Input tensor of shape `(rows, cols, channels)`
483  (with `channels_last` data format) or
484  (channels, rows, cols) (with `channels_first` data format).
485  pointwise_conv_filters: Integer, the dimensionality of the output space
486  (i.e. the number output of filters in the pointwise convolution).
487  alpha: controls the width of the network.
488  - If `alpha` < 1.0, proportionally decreases the number
489  of filters in each layer.
490  - If `alpha` > 1.0, proportionally increases the number
491  of filters in each layer.
492  - If `alpha` = 1, default number of filters from the paper
493  are used at each layer.
494  depth_multiplier: The number of depthwise convolution output channels
495  for each input channel.
496  The total number of depthwise convolution output
497  channels will be equal to `filters_in * depth_multiplier`.
498  strides: An integer or tuple/list of 2 integers,
499  specifying the strides of the convolution along the width and height.
500  Can be a single integer to specify the same value for
501  all spatial dimensions.
502  Specifying any stride value != 1 is incompatible with specifying
503  any `dilation_rate` value != 1.
504  block_id: Integer, a unique identification designating the block number.
505  # Input shape
506  4D tensor with shape:
507  `(batch, channels, rows, cols)` if data_format='channels_first'
508  or 4D tensor with shape:
509  `(batch, rows, cols, channels)` if data_format='channels_last'.
510  # Output shape
511  4D tensor with shape:
512  `(batch, filters, new_rows, new_cols)` if data_format='channels_first'
513  or 4D tensor with shape:
514  `(batch, new_rows, new_cols, filters)` if data_format='channels_last'.
515  `rows` and `cols` values might have changed due to stride.
516  # Returns
517  Output tensor of block.
518  """
519  channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
520  pointwise_conv_filters = int(pointwise_conv_filters * alpha)
521 
522  x = DepthwiseConv2D((3, 3),
523  padding='same',
524  depth_multiplier=depth_multiplier,
525  strides=strides,
526  use_bias=False,
527  name='conv_dw_%d' % block_id)(inputs)
528  x = BatchNormalization(axis=channel_axis, name='conv_dw_%d_bn' % block_id)(x)
529  x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)
530 
531  x = Conv2D(pointwise_conv_filters, (1, 1),
532  padding='same',
533  use_bias=False,
534  strides=(1, 1),
535  name='conv_pw_%d' % block_id)(x)
536  x = BatchNormalization(axis=channel_axis, name='conv_pw_%d_bn' % block_id)(x)
537  x = Activation(relu6, name='conv_pw_%d_relu' % block_id)(x)
538 
539  # squeeze and excite block
540  x = squeeze_excite_block(x)
541  return x
542 
def squeeze_excite_block(input, ratio=16)
Definition: se.py:5
def se_mobilenet.preprocess_input (   x)
Preprocesses a numpy array encoding a batch of images.
# Arguments
    x: a 4D numpy array consists of RGB values within [0, 255].
# Returns
    Preprocessed array.

Definition at line 41 of file se_mobilenet.py.

42  """Preprocesses a numpy array encoding a batch of images.
43  # Arguments
44  x: a 4D numpy array consists of RGB values within [0, 255].
45  # Returns
46  Preprocessed array.
47  """
48  return imagenet_utils.preprocess_input(x, mode='tf')
49 
50 
def preprocess_input(x)
Definition: se_mobilenet.py:41
def preprocess_input(x, data_format=None, mode='caffe')
def se_mobilenet.relu6 (   x)

Definition at line 37 of file se_mobilenet.py.

37 def relu6(x):
38  return K.relu(x, max_value=6)
39 
40 
def relu6(x)
Definition: se_mobilenet.py:37
def se_mobilenet.SEMobileNet (   input_shape = None,
  alpha = 1.0,
  depth_multiplier = 1,
  dropout = 1e-3,
  include_top = True,
  weights = None,
  input_tensor = None,
  pooling = None,
  classes = 1000 
)
Instantiates the SE-MobileNet architecture.
Note that only TensorFlow is supported for now,
therefore it only works with the data format
`image_data_format='channels_last'` in your Keras config
at `~/.keras/keras.json`.
To load a MobileNet model via `load_model`, import the custom
objects `relu6` and `DepthwiseConv2D` and pass them to the
`custom_objects` parameter.
E.g.
model = load_model('mobilenet.h5', custom_objects={
                   'relu6': mobilenet.relu6,
                   'DepthwiseConv2D': mobilenet.DepthwiseConv2D})
# Arguments
    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,
        and width and height should be no smaller than 32.
        E.g. `(200, 200, 3)` would be one valid value.
    alpha: controls the width of the network.
        - If `alpha` < 1.0, proportionally decreases the number
            of filters in each layer.
        - If `alpha` > 1.0, proportionally increases the number
            of filters in each layer.
        - If `alpha` = 1, default number of filters from the paper
             are used at each layer.
    depth_multiplier: depth multiplier for depthwise convolution
        (also called the resolution multiplier)
    dropout: dropout rate
    include_top: whether to include the fully-connected
        layer at the top of the network.
    weights: `None` (random initialization) or
        `imagenet` (ImageNet weights)
    input_tensor: optional Keras tensor (i.e. output of
        `layers.Input()`)
        to use as image input for the model.
    pooling: Optional pooling mode for feature extraction
        when `include_top` is `False`.
        - `None` means that the output of the model
            will be the 4D tensor output of the
            last convolutional layer.
        - `avg` means that global average pooling
            will be applied to the output of the
            last convolutional layer, and thus
            the output of the model will be a
            2D tensor.
        - `max` means that global max pooling will
            be applied.
    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.
    RuntimeError: If attempting to run this model with a
        backend that does not support separable convolutions.

Definition at line 253 of file se_mobilenet.py.

253  classes=1000):
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.
262  E.g.
263  model = load_model('mobilenet.h5', custom_objects={
264  'relu6': mobilenet.relu6,
265  'DepthwiseConv2D': mobilenet.DepthwiseConv2D})
266  # Arguments
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
289  `layers.Input()`)
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
300  2D tensor.
301  - `max` means that global max pooling will
302  be applied.
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.
306  # Returns
307  A Keras model instance.
308  # Raises
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.
313  """
314 
315  if K.backend() != 'tensorflow':
316  raise RuntimeError('Only TensorFlow backend is currently supported, '
317  'as other backends do not support '
318  'depthwise convolution.')
319 
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).')
324 
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')
328 
329  # Determine proper input shape and default size.
330  if input_shape is None:
331  default_size = 224
332  else:
333  if K.image_data_format() == 'channels_first':
334  rows = input_shape[1]
335  cols = input_shape[2]
336  else:
337  rows = input_shape[0]
338  cols = input_shape[1]
339 
340  if rows == cols and rows in [128, 160, 192, 224]:
341  default_size = rows
342  else:
343  default_size = 224
344 
345  input_shape = _obtain_input_shape(input_shape,
346  default_size=default_size,
347  min_size=32,
348  data_format=K.image_data_format(),
349  require_flatten=include_top,
350  weights=weights)
351 
352  if K.image_data_format() == 'channels_last':
353  row_axis, col_axis = (0, 1)
354  else:
355  row_axis, col_axis = (1, 2)
356  rows = input_shape[row_axis]
357  cols = input_shape[col_axis]
358 
359  if input_tensor is None:
360  img_input = Input(shape=input_shape)
361  else:
362  if not K.is_keras_tensor(input_tensor):
363  img_input = Input(tensor=input_tensor, shape=input_shape)
364  else:
365  img_input = input_tensor
366 
367  x = _conv_block(img_input, 32, alpha, strides=(2, 2))
368  x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1)
369 
370  x = _depthwise_conv_block(x, 128, alpha, depth_multiplier,
371  strides=(2, 2), block_id=2)
372  x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3)
373 
374  x = _depthwise_conv_block(x, 256, alpha, depth_multiplier,
375  strides=(2, 2), block_id=4)
376  x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5)
377 
378  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier,
379  strides=(2, 2), block_id=6)
380  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7)
381  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8)
382  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9)
383  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10)
384  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11)
385 
386  x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier,
387  strides=(2, 2), block_id=12)
388  x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13)
389 
390  if include_top:
391  if K.image_data_format() == 'channels_first':
392  shape = (int(1024 * alpha), 1, 1)
393  else:
394  shape = (1, 1, int(1024 * alpha))
395 
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)
403  else:
404  if pooling == 'avg':
405  x = GlobalAveragePooling2D()(x)
406  elif pooling == 'max':
407  x = GlobalMaxPooling2D()(x)
408 
409  # Ensure that the model takes into account
410  # any potential predecessors of `input_tensor`.
411  if input_tensor is not None:
412  inputs = get_source_inputs(input_tensor)
413  else:
414  inputs = img_input
415 
416  # Create model.
417  model = Model(inputs, x, name='se_mobilenet_%0.2f_%s' % (alpha, rows))
418 
419  return model
420 
421 
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))