def call(self, inputs, training=True, drop_connect_rate=None):
        """Implementation of call().
        Args:
          inputs: the inputs tensor.
          training: boolean, whether the model is constructed for training.
          drop_connect_rate: float, between 0 to 1, drop connect rate.
        Returns:
          A output tensor.
        """
        tf.logging.info('Block input: %s shape: %s' %
                        (inputs.name, inputs.shape))
        if self._block_args.expand_ratio != 1:
            x = self._relu_fn(
                self._bn0(self._expand_conv(inputs), training=training))
        else:
            x = inputs
        tf.logging.info('Expand: %s shape: %s' % (x.name, x.shape))

        x = self._relu_fn(self._bn1(self._depthwise_conv(x),
                                    training=training))
        tf.logging.info('DWConv: %s shape: %s' % (x.name, x.shape))

        if self._has_se:
            with tf.variable_scope('se'):
                x = self._call_se(x)

        self.endpoints = {'expansion_output': x}

        x = self._bn2(self._project_conv(x), training=training)

        reuse_binary_tensor = None

        if self._block_args.id_skip:
            if all(
                    s == 1 for s in self._block_args.strides
            ) and self._block_args.input_filters == self._block_args.output_filters:
                # only apply drop_connect if skip presents.
                if drop_connect_rate:
                    #  they don't mean the default drop connect here but the stochastic depth drop connect https://arxiv.org/pdf/1603.09382.pdf
                    x, reuse_binary_tensor = drop.dropout_v2(
                        x,
                        drop_connect_rate,
                        training,
                        noise_shape=[x.get_shape()[0].value, 1, 1, 1])
                    # x = utils.drop_connect(x, training, drop_connect_rate)
                x = tf.add(x, inputs)
        tf.logging.info('Project: %s shape: %s' % (x.name, x.shape))
        return x, reuse_binary_tensor
    def call(self, inputs, training=True, features_only=None):
        """Implementation of call().
        Args:
          inputs: input tensors.
          training: boolean, whether the model is constructed for training.
          features_only: build the base feature network only.
        Returns:
          output tensors.
        """
        outputs = None
        self.endpoints = {}
        # Calls Stem layers
        with tf.variable_scope('stem'):
            outputs = self._relu_fn(
                self._bn0(self._conv_stem(inputs), training=training))
        tf.logging.info('Built stem layers with output shape: %s' %
                        outputs.shape)
        self.endpoints['stem'] = outputs

        # Calls blocks.
        reduction_idx = 0
        switch_reuse_binary_tensor_vars = []
        for idx, block in enumerate(self._blocks):
            is_reduction = False
            if ((idx == len(self._blocks) - 1)
                    or self._blocks[idx + 1].block_args().strides[0] > 1):
                is_reduction = True
                reduction_idx += 1

            with tf.variable_scope('blocks_%s' % idx):
                drop_rate = self._global_params.drop_connect_rate
                if drop_rate:
                    drop_rate *= float(idx) / len(self._blocks)
                    tf.logging.info('block_%s drop_connect_rate: %s' %
                                    (idx, drop_rate))
                outputs, switch_reuse_binary_tensor_var = block.call(
                    outputs, training=training, drop_connect_rate=drop_rate)
                if switch_reuse_binary_tensor_var is not None:
                    switch_reuse_binary_tensor_vars.append(
                        switch_reuse_binary_tensor_var)
                self.endpoints['block_%s' % idx] = outputs
                if is_reduction:
                    self.endpoints['reduction_%s' % reduction_idx] = outputs
                if block.endpoints:
                    for k, v in six.iteritems(block.endpoints):
                        self.endpoints['block_%s/%s' % (idx, k)] = v
                        if is_reduction:
                            self.endpoints['reduction_%s/%s' %
                                           (reduction_idx, k)] = v
        self.endpoints['features'] = outputs
        if not features_only:
            # Calls final layers and returns logits.
            with tf.variable_scope('head'):
                outputs = self._relu_fn(
                    self._bn1(self._conv_head(outputs), training=training))
                outputs = self._avg_pooling(outputs)
                # if self._dropout:
                #  owm dropout layer

                if self._global_params.dropout_rate > 0:
                    outputs, switch_reuse_binary_tensor_var = drop.dropout_v2(
                        outputs,
                        self._global_params.dropout_rate,
                        is_training=training)
                    switch_reuse_binary_tensor_vars.append(
                        switch_reuse_binary_tensor_var)
                    # outputs = self._dropout(outputs, training=training)
                self.endpoints['global_pool'] = outputs
                if self._fc:
                    outputs = self._fc(outputs)
                self.endpoints['head'] = outputs
        return outputs, switch_reuse_binary_tensor_vars
Esempio n. 3
0
 def dense_bc_conv(net, is_train, growth_rate, dropout_keep_prob):
     net = DenseNet.dense_bn(net, is_train, growth_rate)
     net = DenseNet.dense_conv(net, is_train, growth_rate)
     net, switch_reuse_binary_tensor_var = dropout_v2(
         net, 1 - dropout_keep_prob, is_train)
     return net, switch_reuse_binary_tensor_var
Esempio n. 4
0
def mobilenet(inputs,
              num_classes=1001,
              prediction_fn=slim.softmax,
              reuse=None,
              scope='Mobilenet',
              base_only=False,
              dropout_keep_prob=0.8,
              **mobilenet_args):
    """Mobilenet model for classification, supports both V1 and V2.
    Note: default mode is inference, use mobilenet.training_scope to create
    training network.
    Args:
      inputs: a tensor of shape [batch_size, height, width, channels].
      num_classes: number of predicted classes. If 0 or None, the logits layer
        is omitted and the input features to the logits layer (before dropout)
        are returned instead.
      prediction_fn: a function to get predictions out of logits
        (default softmax).
      reuse: whether or not the network and its variables should be reused. To be
        able to reuse 'scope' must be given.
      scope: Optional variable_scope.
      base_only: if True will only create the base of the network (no pooling
      and no logits).
      **mobilenet_args: passed to mobilenet_base verbatim.
        - conv_defs: list of conv defs
        - multiplier: Float multiplier for the depth (number of channels)
        for all convolution ops. The value must be greater than zero. Typical
        usage will be to set this value in (0, 1) to reduce the number of
        parameters or computation cost of the model.
        - output_stride: will ensure that the last layer has at most total stride.
        If the architecture calls for more stride than that provided
        (e.g. output_stride=16, but the architecture has 5 stride=2 operators),
        it will replace output_stride with fractional convolutions using Atrous
        Convolutions.
    Returns:
      logits: the pre-softmax activations, a tensor of size
        [batch_size, num_classes]
      end_points: a dictionary from components of the network to the corresponding
        activation tensor.
    Raises:
      ValueError: Input rank is invalid.
    """
    is_training = mobilenet_args.get('is_training', False)
    input_shape = inputs.get_shape().as_list()
    if len(input_shape) != 4:
        raise ValueError('Expected rank 4 input, was: %d' % len(input_shape))

    with tf.variable_scope(scope, 'Mobilenet', reuse=reuse) as scope:
        inputs = tf.identity(inputs, 'input')
        net, end_points = mobilenet_base(inputs, scope=scope, **mobilenet_args)
        if base_only:
            return net, end_points

        net = tf.identity(net, name='embedding')

        with tf.variable_scope('Logits'):
            net = global_pool(net)
            end_points['global_pool'] = net
            if not num_classes:
                return net, end_points

            # net = slim.dropout(net, scope='Dropout', is_training=is_training)

            drop_rate = 1 - dropout_keep_prob
            net, reuse_binary_tensor = drop.dropout_v2(net,
                                                       rate=drop_rate,
                                                       is_training=is_training)
            # 1 x 1 x num_classes
            # Note: legacy scope name.
            logits = slim.conv2d(net,
                                 num_classes, [1, 1],
                                 activation_fn=None,
                                 normalizer_fn=None,
                                 biases_initializer=tf.zeros_initializer(),
                                 scope='Conv2d_1c_1x1')

            logits = tf.squeeze(logits, [1, 2])

            logits = tf.identity(logits, name='output')
        end_points['Logits'] = logits
        if prediction_fn:
            end_points['Predictions'] = prediction_fn(logits, 'Predictions')
    return logits, end_points, reuse_binary_tensor