Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 29.14 KB | None | 0 0
  1. """MobileNet v1 models for Keras.
  2. MobileNet is a general architecture and can be used for multiple use cases.
  3. Depending on the use case, it can use different input layer size and
  4. different width factors. This allows different width models to reduce
  5. the number of multiply-adds and thereby
  6. reduce inference cost on mobile devices.
  7. MobileNets support any input size greater than 32 x 32, with larger image sizes
  8. offering better performance.
  9. The number of parameters and number of multiply-adds
  10. can be modified by using the `alpha` parameter,
  11. which increases/decreases the number of filters in each layer.
  12. By altering the image size and `alpha` parameter,
  13. all 16 models from the paper can be built, with ImageNet weights provided.
  14. The paper demonstrates the performance of MobileNets using `alpha` values of
  15. 1.0 (also called 100 % MobileNet), 0.75, 0.5 and 0.25.
  16. For each of these `alpha` values, weights for 4 different input image sizes
  17. are provided (224, 192, 160, 128).
  18. The following table describes the size and accuracy of the 100% MobileNet
  19. on size 224 x 224:
  20. ----------------------------------------------------------------------------
  21. Width Multiplier (alpha) | ImageNet Acc |  Multiply-Adds (M) |  Params (M)
  22. ----------------------------------------------------------------------------
  23. |   1.0 MobileNet-224    |    70.6 %     |        529        |     4.2     |
  24. |   0.75 MobileNet-224   |    68.4 %     |        325        |     2.6     |
  25. |   0.50 MobileNet-224   |    63.7 %     |        149        |     1.3     |
  26. |   0.25 MobileNet-224   |    50.6 %     |        41         |     0.5     |
  27. ----------------------------------------------------------------------------
  28. The following table describes the performance of
  29. the 100 % MobileNet on various input sizes:
  30. ------------------------------------------------------------------------
  31.      Resolution      | ImageNet Acc | Multiply-Adds (M) | Params (M)
  32. ------------------------------------------------------------------------
  33. |  1.0 MobileNet-224  |    70.6 %    |        529        |     4.2     |
  34. |  1.0 MobileNet-192  |    69.1 %    |        529        |     4.2     |
  35. |  1.0 MobileNet-160  |    67.2 %    |        529        |     4.2     |
  36. |  1.0 MobileNet-128  |    64.4 %    |        529        |     4.2     |
  37. ------------------------------------------------------------------------
  38. The weights for all 16 models are obtained and translated
  39. from TensorFlow checkpoints found at
  40. https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.md
  41. # Reference
  42. - [MobileNets: Efficient Convolutional Neural Networks for
  43.   Mobile Vision Applications](https://arxiv.org/pdf/1704.04861.pdf))
  44. """
  45. from __future__ import print_function
  46. from __future__ import absolute_import
  47. from __future__ import division
  48.  
  49. import warnings
  50.  
  51. from keras.models import Model
  52. from keras.layers import Input
  53. from keras.layers import Activation
  54. from keras.layers import Dropout
  55. from keras.layers import Reshape
  56. from keras.layers import BatchNormalization
  57. from keras.layers import GlobalAveragePooling2D
  58. from keras.layers import GlobalMaxPooling2D
  59. from keras.layers import Conv2D
  60. from keras import initializers
  61. from keras import regularizers
  62. from keras import constraints
  63. from keras.utils import conv_utils
  64. from keras.utils.data_utils import get_file
  65. from keras.engine.topology import get_source_inputs
  66. from keras.engine import InputSpec
  67. from . import imagenet_utils
  68. from .imagenet_utils import _obtain_input_shape
  69. from .imagenet_utils import decode_predictions
  70. from keras import backend as K
  71.  
  72.  
  73. BASE_WEIGHT_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.6/'
  74.  
  75.  
  76. def relu6(x):
  77.     return K.relu(x, max_value=6)
  78.  
  79.  
  80. def preprocess_input(x):
  81.     """Preprocesses a numpy array encoding a batch of images.
  82.    # Arguments
  83.        x: a 4D numpy array consists of RGB values within [0, 255].
  84.    # Returns
  85.        Preprocessed array.
  86.    """
  87.     return imagenet_utils.preprocess_input(x, mode='tf')
  88.  
  89.  
  90. class DepthwiseConv2D(Conv2D):
  91.     """Depthwise separable 2D convolution.
  92.    Depthwise Separable convolutions consists in performing
  93.    just the first step in a depthwise spatial convolution
  94.    (which acts on each input channel separately).
  95.    The `depth_multiplier` argument controls how many
  96.    output channels are generated per input channel in the depthwise step.
  97.    # Arguments
  98.        kernel_size: An integer or tuple/list of 2 integers, specifying the
  99.            width and height of the 2D convolution window.
  100.            Can be a single integer to specify the same value for
  101.            all spatial dimensions.
  102.        strides: An integer or tuple/list of 2 integers,
  103.            specifying the strides of the convolution along the width and height.
  104.            Can be a single integer to specify the same value for
  105.            all spatial dimensions.
  106.            Specifying any stride value != 1 is incompatible with specifying
  107.            any `dilation_rate` value != 1.
  108.        padding: one of `'valid'` or `'same'` (case-insensitive).
  109.        depth_multiplier: The number of depthwise convolution output channels
  110.            for each input channel.
  111.            The total number of depthwise convolution output
  112.            channels will be equal to `filters_in * depth_multiplier`.
  113.        data_format: A string,
  114.            one of `channels_last` (default) or `channels_first`.
  115.            The ordering of the dimensions in the inputs.
  116.            `channels_last` corresponds to inputs with shape
  117.            `(batch, height, width, channels)` while `channels_first`
  118.            corresponds to inputs with shape
  119.            `(batch, channels, height, width)`.
  120.            It defaults to the `image_data_format` value found in your
  121.            Keras config file at `~/.keras/keras.json`.
  122.            If you never set it, then it will be 'channels_last'.
  123.        activation: Activation function to use
  124.            (see [activations](../activations.md)).
  125.            If you don't specify anything, no activation is applied
  126.            (ie. 'linear' activation: `a(x) = x`).
  127.        use_bias: Boolean, whether the layer uses a bias vector.
  128.        depthwise_initializer: Initializer for the depthwise kernel matrix
  129.            (see [initializers](../initializers.md)).
  130.        bias_initializer: Initializer for the bias vector
  131.            (see [initializers](../initializers.md)).
  132.        depthwise_regularizer: Regularizer function applied to
  133.            the depthwise kernel matrix
  134.            (see [regularizer](../regularizers.md)).
  135.        bias_regularizer: Regularizer function applied to the bias vector
  136.            (see [regularizer](../regularizers.md)).
  137.        activity_regularizer: Regularizer function applied to
  138.            the output of the layer (its 'activation').
  139.            (see [regularizer](../regularizers.md)).
  140.        depthwise_constraint: Constraint function applied to
  141.            the depthwise kernel matrix
  142.            (see [constraints](../constraints.md)).
  143.        bias_constraint: Constraint function applied to the bias vector
  144.            (see [constraints](../constraints.md)).
  145.    # Input shape
  146.        4D tensor with shape:
  147.        `[batch, channels, rows, cols]` if data_format='channels_first'
  148.        or 4D tensor with shape:
  149.        `[batch, rows, cols, channels]` if data_format='channels_last'.
  150.    # Output shape
  151.        4D tensor with shape:
  152.        `[batch, filters, new_rows, new_cols]` if data_format='channels_first'
  153.        or 4D tensor with shape:
  154.        `[batch, new_rows, new_cols, filters]` if data_format='channels_last'.
  155.        `rows` and `cols` values might have changed due to padding.
  156.    """
  157.  
  158.     def __init__(self,
  159.                  kernel_size,
  160.                  strides=(2, 2),
  161.                  padding='valid',
  162.                  depth_multiplier=1,
  163.                  data_format=None,
  164.                  activation=None,
  165.                  use_bias=True,
  166.                  depthwise_initializer='glorot_uniform',
  167.                  bias_initializer='zeros',
  168.                  depthwise_regularizer=None,
  169.                  bias_regularizer=None,
  170.                  activity_regularizer=None,
  171.                  depthwise_constraint=None,
  172.                  bias_constraint=None,
  173.                  dilation_rate=(1, 1),
  174.                  ** kwargs):
  175.         super(DepthwiseConv2D, self).__init__(
  176.             filters=None,
  177.             kernel_size=kernel_size,
  178.             strides=strides,
  179.             padding=padding,
  180.             data_format=data_format,
  181.             activation=activation,
  182.             use_bias=use_bias,
  183.             bias_regularizer=bias_regularizer,
  184.             activity_regularizer=activity_regularizer,
  185.             bias_constraint=bias_constraint,
  186.             dilation_rate=dilation_rate,
  187.             ** kwargs)
  188.         self.depth_multiplier = depth_multiplier
  189.         self.depthwise_initializer = initializers.get(depthwise_initializer)
  190.         self.depthwise_regularizer = regularizers.get(depthwise_regularizer)
  191.         self.depthwise_constraint = constraints.get(depthwise_constraint)
  192.         self.bias_initializer = initializers.get(bias_initializer)
  193.  
  194.     def build(self, input_shape):
  195.         if len(input_shape) < 4:
  196.             raise ValueError('Inputs to `DepthwiseConv2D` should have rank 4. '
  197.                              'Received input shape:', str(input_shape))
  198.         if self.data_format == 'channels_first':
  199.             channel_axis = 1
  200.         else:
  201.             channel_axis = 3
  202.         if input_shape[channel_axis] is None:
  203.             raise ValueError('The channel dimension of the inputs to '
  204.                              '`DepthwiseConv2D` '
  205.                              'should be defined. Found `None`.')
  206.         input_dim = int(input_shape[channel_axis])
  207.         depthwise_kernel_shape = (self.kernel_size[0],
  208.                                   self.kernel_size[1],
  209.                                   input_dim,
  210.                                   self.depth_multiplier)
  211.  
  212.         self.depthwise_kernel = self.add_weight(
  213.             shape=depthwise_kernel_shape,
  214.             initializer=self.depthwise_initializer,
  215.             name='depthwise_kernel',
  216.             regularizer=self.depthwise_regularizer,
  217.             constraint=self.depthwise_constraint)
  218.  
  219.         if self.use_bias:
  220.             self.bias = self.add_weight(shape=(input_dim * self.depth_multiplier,),
  221.                                         initializer=self.bias_initializer,
  222.                                         name='bias',
  223.                                         regularizer=self.bias_regularizer,
  224.                                         constraint=self.bias_constraint)
  225.         else:
  226.             self.bias = None
  227.         # Set input spec.
  228.         self.input_spec = InputSpec(ndim=4, axes={channel_axis: input_dim})
  229.         self.built = True
  230.  
  231.     def call(self, inputs, training=None):
  232.         outputs = K.depthwise_conv2d(
  233.             inputs,
  234.             self.depthwise_kernel,
  235.             strides=self.strides,
  236.             padding=self.padding,
  237.             dilation_rate=self.dilation_rate,
  238.             data_format=self.data_format)
  239.  
  240.         if self.bias:
  241.             outputs = K.bias_add(
  242.                 outputs,
  243.                 self.bias,
  244.                 data_format=self.data_format)
  245.  
  246.         if self.activation is not None:
  247.             return self.activation(outputs)
  248.  
  249.         return outputs
  250.  
  251.     def compute_output_shape(self, input_shape):
  252.         if self.data_format == 'channels_first':
  253.             rows = input_shape[2]
  254.             cols = input_shape[3]
  255.             out_filters = input_shape[1] * self.depth_multiplier
  256.         elif self.data_format == 'channels_last':
  257.             rows = input_shape[1]
  258.             cols = input_shape[2]
  259.             out_filters = input_shape[3] * self.depth_multiplier
  260.  
  261.         rows = conv_utils.conv_output_length(rows, self.kernel_size[0],
  262.                                              self.padding,
  263.                                              self.strides[0])
  264.         cols = conv_utils.conv_output_length(cols, self.kernel_size[1],
  265.                                              self.padding,
  266.                                              self.strides[1])
  267.  
  268.         if self.data_format == 'channels_first':
  269.             return (input_shape[0], out_filters, rows, cols)
  270.         elif self.data_format == 'channels_last':
  271.             return (input_shape[0], rows, cols, out_filters)
  272.  
  273.     def get_config(self):
  274.         config = super(DepthwiseConv2D, self).get_config()
  275.         config.pop('filters')
  276.         config.pop('kernel_initializer')
  277.         config.pop('kernel_regularizer')
  278.         config.pop('kernel_constraint')
  279.         config['depth_multiplier'] = self.depth_multiplier
  280.         config['depthwise_initializer'] = initializers.serialize(
  281.             self.depthwise_initializer)
  282.         config['depthwise_regularizer'] = regularizers.serialize(
  283.             self.depthwise_regularizer)
  284.         config['depthwise_constraint'] = constraints.serialize(
  285.             self.depthwise_constraint)
  286.         return config
  287.  
  288.  
  289. def MobileNet(input_shape=None,
  290.               alpha=1.0,
  291.               depth_multiplier=1,
  292.               dropout=1e-3,
  293.               include_top=True,
  294.               weights='imagenet',
  295.               input_tensor=None,
  296.               pooling=None,
  297.               classes=1000,
  298.               output_stride=None):
  299.     """Instantiates the MobileNet architecture.
  300.    Note that only TensorFlow is supported for now,
  301.    therefore it only works with the data format
  302.    `image_data_format='channels_last'` in your Keras config
  303.    at `~/.keras/keras.json`.
  304.    To load a MobileNet model via `load_model`, import the custom
  305.    objects `relu6` and `DepthwiseConv2D` and pass them to the
  306.    `custom_objects` parameter.
  307.    E.g.
  308.    model = load_model('mobilenet.h5', custom_objects={
  309.                       'relu6': mobilenet.relu6,
  310.                       'DepthwiseConv2D': mobilenet.DepthwiseConv2D})
  311.    # Arguments
  312.        input_shape: optional shape tuple, only to be specified
  313.            if `include_top` is False (otherwise the input shape
  314.            has to be `(224, 224, 3)` (with `channels_last` data format)
  315.            or (3, 224, 224) (with `channels_first` data format).
  316.            It should have exactly 3 inputs channels,
  317.            and width and height should be no smaller than 32.
  318.            E.g. `(200, 200, 3)` would be one valid value.
  319.        alpha: controls the width of the network.
  320.            - If `alpha` < 1.0, proportionally decreases the number
  321.                of filters in each layer.
  322.            - If `alpha` > 1.0, proportionally increases the number
  323.                of filters in each layer.
  324.            - If `alpha` = 1, default number of filters from the paper
  325.                 are used at each layer.
  326.        depth_multiplier: depth multiplier for depthwise convolution
  327.            (also called the resolution multiplier)
  328.        dropout: dropout rate
  329.        include_top: whether to include the fully-connected
  330.            layer at the top of the network.
  331.        weights: `None` (random initialization) or
  332.            `imagenet` (ImageNet weights)
  333.        input_tensor: optional Keras tensor (i.e. output of
  334.            `layers.Input()`)
  335.            to use as image input for the model.
  336.        pooling: Optional pooling mode for feature extraction
  337.            when `include_top` is `False`.
  338.            - `None` means that the output of the model
  339.                will be the 4D tensor output of the
  340.                last convolutional layer.
  341.            - `avg` means that global average pooling
  342.                will be applied to the output of the
  343.                last convolutional layer, and thus
  344.                the output of the model will be a
  345.                2D tensor.
  346.            - `max` means that global max pooling will
  347.                be applied.
  348.        classes: optional number of classes to classify images
  349.            into, only to be specified if `include_top` is True, and
  350.            if no `weights` argument is specified.
  351.    # Returns
  352.        A Keras model instance.
  353.    # Raises
  354.        ValueError: in case of invalid argument for `weights`,
  355.            or invalid input shape.
  356.        RuntimeError: If attempting to run this model with a
  357.            backend that does not support separable convolutions.
  358.    """
  359.  
  360.     if K.backend() != 'tensorflow':
  361.         raise RuntimeError('Only TensorFlow backend is currently supported, '
  362.                            'as other backends do not support '
  363.                            'depthwise convolution.')
  364.  
  365.     if weights not in {'imagenet', None}:
  366.         raise ValueError('The `weights` argument should be either '
  367.                          '`None` (random initialization) or `imagenet` '
  368.                          '(pre-training on ImageNet).')
  369.  
  370.     if weights == 'imagenet' and include_top and classes != 1000:
  371.         raise ValueError('If using `weights` as ImageNet with `include_top` '
  372.                          'as true, `classes` should be 1000')
  373.  
  374.     # Determine proper input shape and default size.
  375.     if input_shape is None:
  376.         default_size = 224
  377.     else:
  378.         if K.image_data_format() == 'channels_first':
  379.             rows = input_shape[1]
  380.             cols = input_shape[2]
  381.         else:
  382.             rows = input_shape[0]
  383.             cols = input_shape[1]
  384.  
  385.         if rows == cols and rows in [128, 160, 192, 224]:
  386.             default_size = rows
  387.         else:
  388.             default_size = 224
  389.  
  390.     if output_stride is not None and output_stride not in [8, 16, 32]:
  391.         raise ValueError('Only allowed output_stride values are 8, 16, 32.')
  392.  
  393.     input_shape = _obtain_input_shape(input_shape,
  394.                                       default_size=default_size,
  395.                                       min_size=32,
  396.                                       data_format=K.image_data_format(),
  397.                                       require_flatten=include_top,
  398.                                       weights=weights)
  399.  
  400.     if K.image_data_format() == 'channels_last':
  401.         row_axis, col_axis = (0, 1)
  402.     else:
  403.         row_axis, col_axis = (1, 2)
  404.     rows = input_shape[row_axis]
  405.     cols = input_shape[col_axis]
  406.  
  407.     if weights == 'imagenet':
  408.         if depth_multiplier != 1:
  409.             raise ValueError('If imagenet weights are being loaded, '
  410.                              'depth multiplier must be 1')
  411.  
  412.         if alpha not in [0.25, 0.50, 0.75, 1.0]:
  413.             raise ValueError('If imagenet weights are being loaded, '
  414.                              'alpha can be one of'
  415.                              '`0.25`, `0.50`, `0.75` or `1.0` only.')
  416.  
  417.         # if rows != cols or rows not in [128, 160, 192, 224]:
  418.         #     raise ValueError('If imagenet weights are being loaded, '
  419.         #                      'input must have a static square shape (one of '
  420.         #                      '(128,128), (160,160), (192,192), or (224, 224)).'
  421.         #                      ' Input shape provided = %s' % (input_shape,))
  422.  
  423.     if K.image_data_format() != 'channels_last':
  424.         warnings.warn('The MobileNet family of models is only available '
  425.                       'for the input data format "channels_last" '
  426.                       '(width, height, channels). '
  427.                       'However your settings specify the default '
  428.                       'data format "channels_first" (channels, width, height).'
  429.                       ' You should set `image_data_format="channels_last"` '
  430.                       'in your Keras config located at ~/.keras/keras.json. '
  431.                       'The model being returned right now will expect inputs '
  432.                       'to follow the "channels_last" data format.')
  433.         K.set_image_data_format('channels_last')
  434.         old_data_format = 'channels_first'
  435.     else:
  436.         old_data_format = None
  437.  
  438.     if input_tensor is None:
  439.         img_input = Input(shape=input_shape)
  440.     else:
  441.         if not K.is_keras_tensor(input_tensor):
  442.             img_input = Input(tensor=input_tensor, shape=input_shape)
  443.         else:
  444.             img_input = input_tensor
  445.  
  446.     x = _conv_block(img_input, 32, alpha, strides=(2, 2))
  447.  
  448.     current_stride = 2
  449.     rate = 1
  450.  
  451.     _CHANNELS = [64, 128, 128, 256, 256, 512,
  452.                  512, 512, 512, 512, 512, 1024, 1024]
  453.  
  454.     _STRIDES = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]
  455.  
  456.     for i in range(13):
  457.         if output_stride is not None and current_stride == output_stride:
  458.             # If we have reached the target output_stride, then we need to employ
  459.             # atrous convolution with stride=1 and multiply the atrous rate by the
  460.             # current unit's stride for use in subsequent layers.
  461.             layer_stride = 1
  462.             layer_rate = rate
  463.             rate *= _STRIDES[i]
  464.         else:
  465.             layer_stride = _STRIDES[i]
  466.             layer_rate = 1
  467.             current_stride *= _STRIDES[i]
  468.         x = _depthwise_conv_block(
  469.             x, _CHANNELS[i], alpha, depth_multiplier, strides=layer_stride, dilation_rate=(layer_rate, layer_rate), block_id=i + 1)
  470.  
  471.     if include_top:
  472.         if K.image_data_format() == 'channels_first':
  473.             shape = (int(1024 * alpha), 1, 1)
  474.         else:
  475.             shape = (1, 1, int(1024 * alpha))
  476.  
  477.         x = GlobalAveragePooling2D()(x)
  478.         x = Reshape(shape, name='reshape_1')(x)
  479.         x = Dropout(dropout, name='dropout')(x)
  480.         x = Conv2D(classes, (1, 1),
  481.                    padding='same', name='conv_preds')(x)
  482.         x = Activation('softmax', name='act_softmax')(x)
  483.         x = Reshape((classes,), name='reshape_2')(x)
  484.     else:
  485.         if pooling == 'avg':
  486.             x = GlobalAveragePooling2D()(x)
  487.         elif pooling == 'max':
  488.             x = GlobalMaxPooling2D()(x)
  489.  
  490.     # Ensure that the model takes into account
  491.     # any potential predecessors of `input_tensor`.
  492.     if input_tensor is not None:
  493.         inputs = get_source_inputs(input_tensor)
  494.     else:
  495.         inputs = img_input
  496.  
  497.     # Create model.
  498.     model = Model(inputs, x, name='mobilenet_%0.2f_%s' % (alpha, rows))
  499.  
  500.     # load weights
  501.     if weights == 'imagenet':
  502.         if K.image_data_format() == 'channels_first':
  503.             raise ValueError('Weights for "channels_last" format '
  504.                              'are not available.')
  505.         if alpha == 1.0:
  506.             alpha_text = '1_0'
  507.         elif alpha == 0.75:
  508.             alpha_text = '7_5'
  509.         elif alpha == 0.50:
  510.             alpha_text = '5_0'
  511.         else:
  512.             alpha_text = '2_5'
  513.  
  514.         if include_top:
  515.             model_name = 'mobilenet_%s_%d_tf.h5' % (alpha_text, rows)
  516.             weigh_path = BASE_WEIGHT_PATH + model_name
  517.             weights_path = get_file(model_name,
  518.                                     weigh_path,
  519.                                     cache_subdir='models')
  520.         else:
  521.             model_name = 'mobilenet_%s_%d_tf_no_top.h5' % (
  522.                 alpha_text, rows or 224)
  523.             weigh_path = BASE_WEIGHT_PATH + model_name
  524.             weights_path = get_file(model_name,
  525.                                     weigh_path,
  526.                                     cache_subdir='models')
  527.         model.load_weights(weights_path)
  528.  
  529.     if old_data_format:
  530.         K.set_image_data_format(old_data_format)
  531.     return model
  532.  
  533.  
  534. def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1)):
  535.     """Adds an initial convolution layer (with batch normalization and relu6).
  536.    # Arguments
  537.        inputs: Input tensor of shape `(rows, cols, 3)`
  538.            (with `channels_last` data format) or
  539.            (3, rows, cols) (with `channels_first` data format).
  540.            It should have exactly 3 inputs channels,
  541.            and width and height should be no smaller than 32.
  542.            E.g. `(224, 224, 3)` would be one valid value.
  543.        filters: Integer, the dimensionality of the output space
  544.            (i.e. the number output of filters in the convolution).
  545.        alpha: controls the width of the network.
  546.            - If `alpha` < 1.0, proportionally decreases the number
  547.                of filters in each layer.
  548.            - If `alpha` > 1.0, proportionally increases the number
  549.                of filters in each layer.
  550.            - If `alpha` = 1, default number of filters from the paper
  551.                 are used at each layer.
  552.        kernel: An integer or tuple/list of 2 integers, specifying the
  553.            width and height of the 2D convolution window.
  554.            Can be a single integer to specify the same value for
  555.            all spatial dimensions.
  556.        strides: An integer or tuple/list of 2 integers,
  557.            specifying the strides of the convolution along the width and height.
  558.            Can be a single integer to specify the same value for
  559.            all spatial dimensions.
  560.            Specifying any stride value != 1 is incompatible with specifying
  561.            any `dilation_rate` value != 1.
  562.    # Input shape
  563.        4D tensor with shape:
  564.        `(samples, channels, rows, cols)` if data_format='channels_first'
  565.        or 4D tensor with shape:
  566.        `(samples, rows, cols, channels)` if data_format='channels_last'.
  567.    # Output shape
  568.        4D tensor with shape:
  569.        `(samples, filters, new_rows, new_cols)` if data_format='channels_first'
  570.        or 4D tensor with shape:
  571.        `(samples, new_rows, new_cols, filters)` if data_format='channels_last'.
  572.        `rows` and `cols` values might have changed due to stride.
  573.    # Returns
  574.        Output tensor of block.
  575.    """
  576.     channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
  577.     filters = int(filters * alpha)
  578.     x = Conv2D(filters, kernel,
  579.                padding='same',
  580.                use_bias=False,
  581.                strides=strides,
  582.                name='conv1')(inputs)
  583.     x = BatchNormalization(axis=channel_axis, name='conv1_bn')(x)
  584.     return Activation(relu6, name='conv1_relu')(x)
  585.  
  586.  
  587. def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha,
  588.                           depth_multiplier=1, strides=(1, 1), dilation_rate=(1, 1), block_id=1):
  589.     """Adds a depthwise convolution block.
  590.    A depthwise convolution block consists of a depthwise conv,
  591.    batch normalization, relu6, pointwise convolution,
  592.    batch normalization and relu6 activation.
  593.    # Arguments
  594.        inputs: Input tensor of shape `(rows, cols, channels)`
  595.            (with `channels_last` data format) or
  596.            (channels, rows, cols) (with `channels_first` data format).
  597.        pointwise_conv_filters: Integer, the dimensionality of the output space
  598.            (i.e. the number output of filters in the pointwise convolution).
  599.        alpha: controls the width of the network.
  600.            - If `alpha` < 1.0, proportionally decreases the number
  601.                of filters in each layer.
  602.            - If `alpha` > 1.0, proportionally increases the number
  603.                of filters in each layer.
  604.            - If `alpha` = 1, default number of filters from the paper
  605.                 are used at each layer.
  606.        depth_multiplier: The number of depthwise convolution output channels
  607.            for each input channel.
  608.            The total number of depthwise convolution output
  609.            channels will be equal to `filters_in * depth_multiplier`.
  610.        strides: An integer or tuple/list of 2 integers,
  611.            specifying the strides of the convolution along the width and height.
  612.            Can be a single integer to specify the same value for
  613.            all spatial dimensions.
  614.            Specifying any stride value != 1 is incompatible with specifying
  615.            any `dilation_rate` value != 1.
  616.        block_id: Integer, a unique identification designating the block number.
  617.    # Input shape
  618.        4D tensor with shape:
  619.        `(batch, channels, rows, cols)` if data_format='channels_first'
  620.        or 4D tensor with shape:
  621.        `(batch, rows, cols, channels)` if data_format='channels_last'.
  622.    # Output shape
  623.        4D tensor with shape:
  624.        `(batch, filters, new_rows, new_cols)` if data_format='channels_first'
  625.        or 4D tensor with shape:
  626.        `(batch, new_rows, new_cols, filters)` if data_format='channels_last'.
  627.        `rows` and `cols` values might have changed due to stride.
  628.    # Returns
  629.        Output tensor of block.
  630.    """
  631.     channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
  632.     pointwise_conv_filters = int(pointwise_conv_filters * alpha)
  633.  
  634.     x = DepthwiseConv2D((3, 3),
  635.                         padding='same',
  636.                         depth_multiplier=depth_multiplier,
  637.                         strides=strides,
  638.                         dilation_rate=dilation_rate,
  639.                         use_bias=False,
  640.                         name='conv_dw_%d' % block_id)(inputs)
  641.     x = BatchNormalization(
  642.         axis=channel_axis, name='conv_dw_%d_bn' % block_id)(x)
  643.     x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)
  644.  
  645.     #x = Dropout(0.5)(x)
  646.  
  647.     x = Conv2D(pointwise_conv_filters, (1, 1),
  648.                padding='same',
  649.                use_bias=False,
  650.                strides=(1, 1),
  651.                name='conv_pw_%d' % block_id)(x)
  652.     x = BatchNormalization(
  653.         axis=channel_axis, name='conv_pw_%d_bn' % block_id)(x)
  654.     #x = Dropout(0.5)(x)
  655.     return Activation(relu6, name='conv_pw_%d_relu' % block_id)(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement