Exemplo n.º 1
0
    def __init__(self, model_path=INCEPTION_V2_PATH, image_size=299):
        cache_dir = os.sep.join(model_path.split(os.sep)[:-1])
        get_file(fname=model_path.split(os.sep)[-1],
                 cache_dir=cache_dir,
                 cache_subdir='',
                 origin=INCEPTION_V2_URL,
                 untar=True)

        self._get_session()
        self.checkpoint_file = model_path
        self.image_size = image_size
        self._get_inception_preprocessing(inception_size=224)

        self.arg_scope = inception.inception_v2_arg_scope()
        with slim.arg_scope(self.arg_scope):
            self.logits, self.end_points = inception.inception_v2(
                self.scaled_input_tensor,
                is_training=False,
                num_classes=1001,
                reuse=False)
            end_point = list(self.end_points.values())[-3]
            kernel_size = end_point.get_shape().as_list()[-2]
            with variable_scope.variable_scope('InceptionV2/Logits',
                                               reuse=True):
                embedding = layers_lib.avg_pool2d(end_point,
                                                  kernel_size,
                                                  padding='VALID',
                                                  scope='AvgPool_1a_%dx%d' %
                                                  (kernel_size, kernel_size))
            self.end_points['PreLogits'] = embedding
        self._restore()
Exemplo n.º 2
0
def avg_pool2d(inputs,
               kernel_size=2,
               stride=2,
               padding='SAME',
               explicit_padding=True,
               scope=None,
               outputs_collections=None):
    """
    SAME PADDING equally
    :param inputs: [N, H, W, C]
    :param kernel_size: kernel size
    :param stride: pooling stride
    :param padding: padding mode
    :param scope: var_scope & operation name
    :param outputs_collections: add result to some collection
    :return:
    """
    kernel_size = [kernel_size, kernel_size
                   ] if type(kernel_size) is int else kernel_size
    stride = [stride, stride] if type(stride) is int else stride

    if padding == 'SAME' and explicit_padding:
        inputs = same_padding(inputs, kernel_size, [1, 1])
        padding = 'VALID'

    pool = layers_lib.avg_pool2d(inputs,
                                 kernel_size,
                                 stride=stride,
                                 padding=padding,
                                 data_format='NHWC',
                                 outputs_collections=outputs_collections,
                                 scope=scope)
    return pool
Exemplo n.º 3
0
def avg_pool2d(inputs,
               kernel_size=2,
               stride=2,
               padding='SAME',
               scope=None,
               outputs_collections=None):
    """
    SAME PADDING equally
    :param inputs: [N, H, W, C]
    :param kernel_size: kernel size
    :param stride: pooling stride
    :param padding: padding mode
    :param scope: var_scope & operation name
    :param outputs_collections: add result to some collection
    :return:
    """
    if padding == 'SAME':
        inputs = same_padding(inputs, [kernel_size, kernel_size], [1, 1])

    pool = layers_lib.avg_pool2d(inputs,
                                 kernel_size,
                                 stride=stride,
                                 padding='VALID',
                                 data_format='NHWC',
                                 outputs_collections=outputs_collections,
                                 scope=scope)
    return pool
Exemplo n.º 4
0
def inception_v1(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 prediction_fn=layers_lib.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='InceptionV1'):
  """Defines the Inception V1 architecture.

  This architecture is defined in:

    Going deeper with convolutions
    Christian Szegedy, Wei Liu, Yangqing Jia, Pierre Sermanet, Scott Reed,
    Dragomir Anguelov, Dumitru Erhan, Vincent Vanhoucke, Andrew Rabinovich.
    http://arxiv.org/pdf/1409.4842v1.pdf.

  The default image size used to train this network is 224x224.

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether is training or not.
    dropout_keep_prob: the percentage of activation values that are retained.
    prediction_fn: a function to get predictions out of logits.
    spatial_squeeze: if True, logits is of shape is [B, C], if false logits is
        of shape [B, 1, 1, C], where B is batch_size and C is number of classes.
    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.

  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.
  """
  # Final pooling and prediction
  with variable_scope.variable_scope(
      scope, 'InceptionV1', [inputs, num_classes], reuse=reuse) as scope:
    with arg_scope(
        [layers_lib.batch_norm, layers_lib.dropout], is_training=is_training):
      net, end_points = inception_v1_base(inputs, scope=scope)
      with variable_scope.variable_scope('Logits'):
        net = layers_lib.avg_pool2d(
            net, [7, 7], stride=1, scope='MaxPool_0a_7x7')
        net = layers_lib.dropout(net, dropout_keep_prob, scope='Dropout_0b')
        logits = layers.conv2d(
            net,
            num_classes, [1, 1],
            activation_fn=None,
            normalizer_fn=None,
            scope='Conv2d_0c_1x1')
        if spatial_squeeze:
          logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')

        end_points['Logits'] = logits
        end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
  return logits, end_points
Exemplo n.º 5
0
def inception():
    image = tf.placeholder(tf.float32, [None, 224, 224, 3], 'image')
    with slim.arg_scope(inception_arg_scope(is_training=False)):
        with variable_scope.variable_scope(
                'InceptionV1', 'InceptionV1', [image, 1000], reuse=None) as scope:
            with arg_scope(
                    [layers_lib.batch_norm, layers_lib.dropout], is_training=False):
                net, end_points = inception_v1_base(image, scope=scope)
                with variable_scope.variable_scope('Logits'):
                    net_conv = layers_lib.avg_pool2d(
                        net, [7, 7], stride=1, scope='MaxPool_0a_7x7')
    print(net_conv.shape)

    return net_conv, image
def inception_2d_fields(img,
                        fields,
                        num_classes=30,
                        is_training=True,
                        dropout_keep_prob=0.6,
                        prediction_fn=layers_lib.softmax,
                        spatial_squeeze=True,
                        reuse=None,
                        scope='InceptionV1_Fields'
                        ):
    with arg_scope([layers.conv2d, layers_lib.fully_connected],
                   weights_initializer=tf.contrib.layers.xavier_initializer(),
                   biases_initializer=tf.constant_initializer(0.2),
                   weights_regularizer=regularizers.l2_regularizer(0.0002),
                   biases_regularizer=regularizers.l2_regularizer(0.0002)):
        net, end_points = inception_2d.inception_v1_base(img, scope=scope, final_endpoint='Mixed_4b')
        with variable_scope.variable_scope('Logits'):
            net = layers_lib.avg_pool2d(net, [5, 5], stride=3, scope='AvgPool_0a_5x5')
            net = layers.conv2d(inputs=net, num_outputs=128, kernel_size=1)
            net = tf.reshape(net, [-1, 1, 1, 4 * 4 * 128])
            net = array_ops.squeeze(net,[1,2],name='Squeeze4Fields')
            net = tf.concat([net,fields],axis=1)
            net = layers.fully_connected(inputs=net, num_outputs=1024)
            net = layers_lib.dropout(net, dropout_keep_prob, scope='Dropout_0b')
            logits = layers.fully_connected(inputs=net,
                                            num_outputs=num_classes,
                                            activation_fn=None,
                                            weights_initializer=tf.contrib.layers.xavier_initializer(),
                                            biases_initializer=tf.constant_initializer(0.0),
                                            weights_regularizer=regularizers.l2_regularizer(0.0002),
                                            biases_regularizer=regularizers.l2_regularizer(0.0002),
                                            scope='InnerProduct')
            # logits = layers.conv2d(
            #     net,
            #     num_classes, [1, 1],
            #     activation_fn=None,
            #     normalizer_fn=None,
            #     scope='Conv2d_0c_1x1')
            if spatial_squeeze:
                logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')

            end_points['Logits'] = logits
            end_points['Predictions'] = prediction_fn(logits, scope='Predictions')


    return logits, end_points
Exemplo n.º 7
0
def inception_v3(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 min_depth=16,
                 depth_multiplier=1.0,
                 prediction_fn=layers_lib.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='InceptionV3'):
  """Inception model from http://arxiv.org/abs/1512.00567.

  "Rethinking the Inception Architecture for Computer Vision"

  Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jonathon Shlens,
  Zbigniew Wojna.

  With the default arguments this method constructs the exact model defined in
  the paper. However, one can experiment with variations of the inception_v3
  network by changing arguments dropout_keep_prob, min_depth and
  depth_multiplier.

  The default image size used to train this network is 299x299.

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether is training or not.
    dropout_keep_prob: the percentage of activation values that are retained.
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    prediction_fn: a function to get predictions out of logits.
    spatial_squeeze: if True, logits is of shape is [B, C], if false logits is
      of shape [B, 1, 1, C], where B is batch_size and C is number of classes.
      To use this parameter, the input images must be smaller
      than 300x300 pixels, in which case the output logit layer
      does not contain spatial information and can be removed.
    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.

  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.

  Raises:
    ValueError: if 'depth_multiplier' is less than or equal to zero.
  """
  if depth_multiplier <= 0:
    raise ValueError('depth_multiplier is not greater than zero.')
  depth = lambda d: max(int(d * depth_multiplier), min_depth)

  with variable_scope.variable_scope(
      scope, 'InceptionV3', [inputs, num_classes], reuse=reuse) as scope:
    with arg_scope(
        [layers_lib.batch_norm, layers_lib.dropout], is_training=is_training):
      net, end_points = inception_v3_base(
          inputs,
          scope=scope,
          min_depth=min_depth,
          depth_multiplier=depth_multiplier)

      # Auxiliary Head logits
      with arg_scope(
          [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
          stride=1,
          padding='SAME'):
        aux_logits = end_points['Mixed_6e']
        with variable_scope.variable_scope('AuxLogits'):
          aux_logits = layers_lib.avg_pool2d(
              aux_logits, [5, 5],
              stride=3,
              padding='VALID',
              scope='AvgPool_1a_5x5')
          aux_logits = layers.conv2d(
              aux_logits, depth(128), [1, 1], scope='Conv2d_1b_1x1')

          # Shape of feature map before the final layer.
          kernel_size = _reduced_kernel_size_for_small_input(aux_logits, [5, 5])
          aux_logits = layers.conv2d(
              aux_logits,
              depth(768),
              kernel_size,
              weights_initializer=trunc_normal(0.01),
              padding='VALID',
              scope='Conv2d_2a_{}x{}'.format(*kernel_size))
          aux_logits = layers.conv2d(
              aux_logits,
              num_classes, [1, 1],
              activation_fn=None,
              normalizer_fn=None,
              weights_initializer=trunc_normal(0.001),
              scope='Conv2d_2b_1x1')
          if spatial_squeeze:
            aux_logits = array_ops.squeeze(
                aux_logits, [1, 2], name='SpatialSqueeze')
          end_points['AuxLogits'] = aux_logits

      # Final pooling and prediction
      with variable_scope.variable_scope('Logits'):
        kernel_size = _reduced_kernel_size_for_small_input(net, [8, 8])
        net = layers_lib.avg_pool2d(
            net,
            kernel_size,
            padding='VALID',
            scope='AvgPool_1a_{}x{}'.format(*kernel_size))
        # 1 x 1 x 2048
        net = layers_lib.dropout(
            net, keep_prob=dropout_keep_prob, scope='Dropout_1b')
        end_points['PreLogits'] = net
        # 2048
        logits = layers.conv2d(
            net,
            num_classes, [1, 1],
            activation_fn=None,
            normalizer_fn=None,
            scope='Conv2d_1c_1x1')
        if spatial_squeeze:
          logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')
        # 1000
      end_points['Logits'] = logits
      end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
  return logits, end_points
Exemplo n.º 8
0
def inception_v3_base(inputs,
                      final_endpoint='Mixed_7c',
                      min_depth=16,
                      depth_multiplier=1.0,
                      scope=None):
  """Inception model from http://arxiv.org/abs/1512.00567.

  Constructs an Inception v3 network from inputs to the given final endpoint.
  This method can construct the network up to the final inception block
  Mixed_7c.

  Note that the names of the layers in the paper do not correspond to the names
  of the endpoints registered by this function although they build the same
  network.

  Here is a mapping from the old_names to the new names:
  Old name          | New name
  =======================================
  conv0             | Conv2d_1a_3x3
  conv1             | Conv2d_2a_3x3
  conv2             | Conv2d_2b_3x3
  pool1             | MaxPool_3a_3x3
  conv3             | Conv2d_3b_1x1
  conv4             | Conv2d_4a_3x3
  pool2             | MaxPool_5a_3x3
  mixed_35x35x256a  | Mixed_5b
  mixed_35x35x288a  | Mixed_5c
  mixed_35x35x288b  | Mixed_5d
  mixed_17x17x768a  | Mixed_6a
  mixed_17x17x768b  | Mixed_6b
  mixed_17x17x768c  | Mixed_6c
  mixed_17x17x768d  | Mixed_6d
  mixed_17x17x768e  | Mixed_6e
  mixed_8x8x1280a   | Mixed_7a
  mixed_8x8x2048a   | Mixed_7b
  mixed_8x8x2048b   | Mixed_7c

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    final_endpoint: specifies the endpoint to construct the network up to. It
      can be one of ['Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3',
      'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3', 'MaxPool_5a_3x3',
      'Mixed_5b', 'Mixed_5c', 'Mixed_5d', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c',
      'Mixed_6d', 'Mixed_6e', 'Mixed_7a', 'Mixed_7b', 'Mixed_7c'].
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    scope: Optional variable_scope.

  Returns:
    tensor_out: output tensor corresponding to the final_endpoint.
    end_points: a set of activations for external use, for example summaries or
                losses.

  Raises:
    ValueError: if final_endpoint is not set to one of the predefined values,
                or depth_multiplier <= 0
  """
  # end_points will collect relevant activations for external use, for example
  # summaries or losses.
  end_points = {}

  if depth_multiplier <= 0:
    raise ValueError('depth_multiplier is not greater than zero.')
  depth = lambda d: max(int(d * depth_multiplier), min_depth)

  with variable_scope.variable_scope(scope, 'InceptionV3', [inputs]):
    with arg_scope(
        [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
        stride=1,
        padding='VALID'):
      # 299 x 299 x 3
      end_point = 'Conv2d_1a_3x3'
      net = layers.conv2d(inputs, depth(32), [3, 3], stride=2, scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 149 x 149 x 32
      end_point = 'Conv2d_2a_3x3'
      net = layers.conv2d(net, depth(32), [3, 3], scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 147 x 147 x 32
      end_point = 'Conv2d_2b_3x3'
      net = layers.conv2d(
          net, depth(64), [3, 3], padding='SAME', scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 147 x 147 x 64
      end_point = 'MaxPool_3a_3x3'
      net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 73 x 73 x 64
      end_point = 'Conv2d_3b_1x1'
      net = layers.conv2d(net, depth(80), [1, 1], scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 73 x 73 x 80.
      end_point = 'Conv2d_4a_3x3'
      net = layers.conv2d(net, depth(192), [3, 3], scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 71 x 71 x 192.
      end_point = 'MaxPool_5a_3x3'
      net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 35 x 35 x 192.

      # Inception blocks
    with arg_scope(
        [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
        stride=1,
        padding='SAME'):
      # mixed: 35 x 35 x 256.
      end_point = 'Mixed_5b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(48), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(64), [5, 5], scope='Conv2d_0b_5x5')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(32), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points

      # mixed_1: 35 x 35 x 288.
      end_point = 'Mixed_5c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(48), [1, 1], scope='Conv2d_0b_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(64), [5, 5], scope='Conv_1_0c_5x5')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(64), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points

      # mixed_2: 35 x 35 x 288.
      end_point = 'Mixed_5d'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(48), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(64), [5, 5], scope='Conv2d_0b_5x5')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(64), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points

      # mixed_3: 17 x 17 x 768.
      end_point = 'Mixed_6a'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net,
              depth(384), [3, 3],
              stride=2,
              padding='VALID',
              scope='Conv2d_1a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_1 = layers.conv2d(
              branch_1,
              depth(96), [3, 3],
              stride=2,
              padding='VALID',
              scope='Conv2d_1a_1x1')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers_lib.max_pool2d(
              net, [3, 3], stride=2, padding='VALID', scope='MaxPool_1a_3x3')
        net = array_ops.concat([branch_0, branch_1, branch_2], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points

      # mixed4: 17 x 17 x 768.
      end_point = 'Mixed_6b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(128), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(128), [1, 7], scope='Conv2d_0b_1x7')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(128), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [7, 1], scope='Conv2d_0b_7x1')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [1, 7], scope='Conv2d_0c_1x7')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [7, 1], scope='Conv2d_0d_7x1')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points

      # mixed_5: 17 x 17 x 768.
      end_point = 'Mixed_6c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(160), [1, 7], scope='Conv2d_0b_1x7')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [7, 1], scope='Conv2d_0b_7x1')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [1, 7], scope='Conv2d_0c_1x7')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [7, 1], scope='Conv2d_0d_7x1')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # mixed_6: 17 x 17 x 768.
      end_point = 'Mixed_6d'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(160), [1, 7], scope='Conv2d_0b_1x7')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [7, 1], scope='Conv2d_0b_7x1')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [1, 7], scope='Conv2d_0c_1x7')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [7, 1], scope='Conv2d_0d_7x1')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points

      # mixed_7: 17 x 17 x 768.
      end_point = 'Mixed_6e'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [1, 7], scope='Conv2d_0b_1x7')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [7, 1], scope='Conv2d_0b_7x1')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [1, 7], scope='Conv2d_0c_1x7')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [7, 1], scope='Conv2d_0d_7x1')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points

      # mixed_8: 8 x 8 x 1280.
      end_point = 'Mixed_7a'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
          branch_0 = layers.conv2d(
              branch_0,
              depth(320), [3, 3],
              stride=2,
              padding='VALID',
              scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [1, 7], scope='Conv2d_0b_1x7')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
          branch_1 = layers.conv2d(
              branch_1,
              depth(192), [3, 3],
              stride=2,
              padding='VALID',
              scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers_lib.max_pool2d(
              net, [3, 3], stride=2, padding='VALID', scope='MaxPool_1a_3x3')
        net = array_ops.concat([branch_0, branch_1, branch_2], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # mixed_9: 8 x 8 x 2048.
      end_point = 'Mixed_7b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(320), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(384), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = array_ops.concat(
              [
                  layers.conv2d(
                      branch_1, depth(384), [1, 3], scope='Conv2d_0b_1x3'),
                  layers.conv2d(
                      branch_1, depth(384), [3, 1], scope='Conv2d_0b_3x1')
              ],
              3)
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(448), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(384), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = array_ops.concat(
              [
                  layers.conv2d(
                      branch_2, depth(384), [1, 3], scope='Conv2d_0c_1x3'),
                  layers.conv2d(
                      branch_2, depth(384), [3, 1], scope='Conv2d_0d_3x1')
              ],
              3)
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points

      # mixed_10: 8 x 8 x 2048.
      end_point = 'Mixed_7c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(320), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net, depth(384), [1, 1], scope='Conv2d_0a_1x1')
          branch_1 = array_ops.concat(
              [
                  layers.conv2d(
                      branch_1, depth(384), [1, 3], scope='Conv2d_0b_1x3'),
                  layers.conv2d(
                      branch_1, depth(384), [3, 1], scope='Conv2d_0c_3x1')
              ],
              3)
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net, depth(448), [1, 1], scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(384), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = array_ops.concat(
              [
                  layers.conv2d(
                      branch_2, depth(384), [1, 3], scope='Conv2d_0c_1x3'),
                  layers.conv2d(
                      branch_2, depth(384), [3, 1], scope='Conv2d_0d_3x1')
              ],
              3)
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
    raise ValueError('Unknown final endpoint %s' % final_endpoint)
Exemplo n.º 9
0
    def get_loss(self,
                 net_model,
                 masks,
                 batch_size=None,
                 return_segmentation_net=True,
                 return_decision_net=True,
                 output_resolution_reduction=8):
        """Adds all losses for the model.
    
      Note the final loss is not returned. Instead, the list of losses are collected
      by slim.losses. The losses are accumulated in tower_loss() and summed to
      calculate the total loss.
    
      Args:
        logits: List of logits from inference(). Each entry is a 2-D float Tensor.
        labels: Labels from distorted_inputs or inputs(). 1-D tensor
                of shape [batch_size]
        batch_size: integer
      """
        if not batch_size:
            raise Exception("Missing batch_size")

        net, decision_net, endpoints = net_model

        if output_resolution_reduction > 1:
            mask_blur_kernel = [
                output_resolution_reduction * 2 + 1,
                output_resolution_reduction * 2 + 1
            ]
            masks = layers_lib.avg_pool2d(
                masks,
                mask_blur_kernel,
                stride=output_resolution_reduction,
                padding='SAME',
                scope='pool_mask',
                outputs_collections='tower_0/_end_points')

        if self.use_corss_entropy_seg_net is False:
            masks = tf.greater(masks, tf.constant(0.5))

        predictions = net

        tf.summary.image('prediction', predictions)

        l1 = None
        l2 = None

        if return_segmentation_net:
            if self.positive_weight > 1:
                pos_pixels = tf.less(tf.constant(0.0), masks)
                neg_pixels = tf.greater_equal(tf.constant(0.0), masks)

                num_pos_pixels = tf.cast(tf.count_nonzero(pos_pixels),
                                         dtype=tf.float32)
                num_neg_pixels = tf.cast(tf.count_nonzero(neg_pixels),
                                         dtype=tf.float32)

                pos_pixels = tf.cast(pos_pixels, dtype=tf.float32)
                neg_pixels = tf.cast(neg_pixels, dtype=tf.float32)

                positive_weight = tf.cond(
                    num_pos_pixels > tf.constant(0, dtype=tf.float32),
                    lambda: tf.multiply(
                        tf.div(num_neg_pixels, num_pos_pixels),
                        tf.constant(self.positive_weight, dtype=tf.float32)),
                    lambda: tf.constant(self.positive_weight, dtype=tf.float32
                                        ))

                positive_weight = tf.reshape(positive_weight, [1])

                # weight positive samples more !!
                weights = tf.add(neg_pixels,
                                 tf.multiply(pos_pixels, positive_weight))

                # noramlize weights so that the sum of weights is always equal to the num of elements
                N = tf.constant(weights.shape[1]._value *
                                weights.shape[2]._value,
                                dtype=tf.float32)

                factor = tf.reduce_sum(weights, axis=[1, 2])
                factor = tf.divide(N, factor)

                weights = tf.multiply(weights,
                                      tf.reshape(factor, [-1, 1, 1, 1]))

                if self.use_corss_entropy_seg_net is False:
                    l1 = tf.losses.mean_squared_error(masks,
                                                      predictions,
                                                      weights=weights)
                else:
                    l1 = tf.losses.sigmoid_cross_entropy(
                        logits=predictions,
                        multi_class_labels=masks,
                        weights=weights
                    )  # NOTE: weights were added but not tested yet !!
            else:
                if self.use_corss_entropy_seg_net is False:
                    l1 = tf.losses.mean_squared_error(masks, predictions)
                else:
                    l1 = tf.losses.sigmoid_cross_entropy(
                        logits=predictions, multi_class_labels=masks)

        if return_decision_net:
            with tf.name_scope('decision'):
                masks = tf.cast(masks, tf.float32)
                label = tf.minimum(tf.reduce_sum(masks, [1, 2, 3]),
                                   tf.constant(1.0))

                if len(decision_net.shape) == 2:
                    decision_net = tf.squeeze(decision_net, [1])
                elif len(decision_net.shape) == 4:
                    decision_net = tf.squeeze(decision_net, [1, 2, 3])
                else:
                    raise Exception(
                        "Only 2 or 4 dimensional output expected for decision_net"
                    )

                decision_net = tf.reshape(decision_net, [-1, 1])
                label = tf.reshape(label, [-1, 1])

                l2 = tf.losses.sigmoid_cross_entropy(
                    logits=decision_net,
                    multi_class_labels=label,
                    weights=self.decision_positive_weight)

        return [l1, l2]
Exemplo n.º 10
0
def inception_v2(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 min_depth=16,
                 depth_multiplier=1.0,
                 prediction_fn=layers_lib.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='InceptionV2'):
  """Inception v2 model for classification.

  Constructs an Inception v2 network for classification as described in
  http://arxiv.org/abs/1502.03167.

  The default image size used to train this network is 224x224.

  Args:
    inputs: a tensor of shape [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether is training or not.
    dropout_keep_prob: the percentage of activation values that are retained.
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    prediction_fn: a function to get predictions out of logits.
    spatial_squeeze: if True, logits is of shape is [B, C], if false logits is
        of shape [B, 1, 1, C], where B is batch_size and C is number of classes.
    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.

  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.

  Raises:
    ValueError: if final_endpoint is not set to one of the predefined values,
                or depth_multiplier <= 0
  """
  if depth_multiplier <= 0:
    raise ValueError('depth_multiplier is not greater than zero.')

  # Final pooling and prediction
  with variable_scope.variable_scope(
      scope, 'InceptionV2', [inputs, num_classes], reuse=reuse) as scope:
    with arg_scope(
        [layers_lib.batch_norm, layers_lib.dropout], is_training=is_training):
      net, end_points = inception_v2_base(
          inputs,
          scope=scope,
          min_depth=min_depth,
          depth_multiplier=depth_multiplier)
      with variable_scope.variable_scope('Logits'):
        kernel_size = _reduced_kernel_size_for_small_input(net, [7, 7])
        net = layers_lib.avg_pool2d(
            net,
            kernel_size,
            padding='VALID',
            scope='AvgPool_1a_{}x{}'.format(*kernel_size))
        # 1 x 1 x 1024
        net = layers_lib.dropout(
            net, keep_prob=dropout_keep_prob, scope='Dropout_1b')
        logits = layers.conv2d(
            net,
            num_classes, [1, 1],
            activation_fn=None,
            normalizer_fn=None,
            scope='Conv2d_1c_1x1')
        if spatial_squeeze:
          logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')
      end_points['Logits'] = logits
      end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
  return logits, end_points
Exemplo n.º 11
0
def inception_v3_base(inputs,
                      final_endpoint='Mixed_7c',
                      min_depth=16,
                      depth_multiplier=1.0,
                      scope=None):
    """Inception model from http://arxiv.org/abs/1512.00567.

  Constructs an Inception v3 network from inputs to the given final endpoint.
  This method can construct the network up to the final inception block
  Mixed_7c.

  Note that the names of the layers in the paper do not correspond to the names
  of the endpoints registered by this function although they build the same
  network.

  Here is a mapping from the old_names to the new names:
  Old name          | New name
  =======================================
  conv0             | Conv2d_1a_3x3
  conv1             | Conv2d_2a_3x3
  conv2             | Conv2d_2b_3x3
  pool1             | MaxPool_3a_3x3
  conv3             | Conv2d_3b_1x1
  conv4             | Conv2d_4a_3x3
  pool2             | MaxPool_5a_3x3
  mixed_35x35x256a  | Mixed_5b
  mixed_35x35x288a  | Mixed_5c
  mixed_35x35x288b  | Mixed_5d
  mixed_17x17x768a  | Mixed_6a
  mixed_17x17x768b  | Mixed_6b
  mixed_17x17x768c  | Mixed_6c
  mixed_17x17x768d  | Mixed_6d
  mixed_17x17x768e  | Mixed_6e
  mixed_8x8x1280a   | Mixed_7a
  mixed_8x8x2048a   | Mixed_7b
  mixed_8x8x2048b   | Mixed_7c

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    final_endpoint: specifies the endpoint to construct the network up to. It
      can be one of ['Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3',
      'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3', 'MaxPool_5a_3x3',
      'Mixed_5b', 'Mixed_5c', 'Mixed_5d', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c',
      'Mixed_6d', 'Mixed_6e', 'Mixed_7a', 'Mixed_7b', 'Mixed_7c'].
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    scope: Optional variable_scope.

  Returns:
    tensor_out: output tensor corresponding to the final_endpoint.
    end_points: a set of activations for external use, for example summaries or
                losses.

  Raises:
    ValueError: if final_endpoint is not set to one of the predefined values,
                or depth_multiplier <= 0
  """
    # end_points will collect relevant activations for external use, for example
    # summaries or losses.
    end_points = {}

    if depth_multiplier <= 0:
        raise ValueError('depth_multiplier is not greater than zero.')
    depth = lambda d: max(int(d * depth_multiplier), min_depth)

    with variable_scope.variable_scope(scope, 'InceptionV3', [inputs]):
        with arg_scope(
            [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
                stride=1,
                padding='VALID'):
            # 299 x 299 x 3
            end_point = 'Conv2d_1a_3x3'
            net = layers.conv2d(inputs,
                                depth(32), [3, 3],
                                stride=2,
                                scope=end_point)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # 149 x 149 x 32
            end_point = 'Conv2d_2a_3x3'
            net = layers.conv2d(net, depth(32), [3, 3], scope=end_point)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # 147 x 147 x 32
            end_point = 'Conv2d_2b_3x3'
            net = layers.conv2d(net,
                                depth(64), [3, 3],
                                padding='SAME',
                                scope=end_point)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # 147 x 147 x 64
            end_point = 'MaxPool_3a_3x3'
            net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope=end_point)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # 73 x 73 x 64
            end_point = 'Conv2d_3b_1x1'
            net = layers.conv2d(net, depth(80), [1, 1], scope=end_point)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # 73 x 73 x 80.
            end_point = 'Conv2d_4a_3x3'
            net = layers.conv2d(net, depth(192), [3, 3], scope=end_point)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # 71 x 71 x 192.
            end_point = 'MaxPool_5a_3x3'
            net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope=end_point)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # 35 x 35 x 192.

        # Inception blocks
        with arg_scope(
            [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
                stride=1,
                padding='SAME'):
            # mixed: 35 x 35 x 256.
            end_point = 'Mixed_5b'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(48), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(64), [5, 5],
                                             scope='Conv2d_0b_5x5')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0c_3x3')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(32), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_1: 35 x 35 x 288.
            end_point = 'Mixed_5c'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(48), [1, 1],
                                             scope='Conv2d_0b_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(64), [5, 5],
                                             scope='Conv_1_0c_5x5')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0c_3x3')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_2: 35 x 35 x 288.
            end_point = 'Mixed_5d'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(48), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(64), [5, 5],
                                             scope='Conv2d_0b_5x5')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0c_3x3')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_3: 17 x 17 x 768.
            end_point = 'Mixed_6a'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(384), [3, 3],
                                             stride=2,
                                             padding='VALID',
                                             scope='Conv2d_1a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(96), [3, 3],
                                             stride=2,
                                             padding='VALID',
                                             scope='Conv2d_1a_1x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(net, [3, 3],
                                                     stride=2,
                                                     padding='VALID',
                                                     scope='MaxPool_1a_3x3')
                net = array_ops.concat_v2([branch_0, branch_1, branch_2], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed4: 17 x 17 x 768.
            end_point = 'Mixed_6b'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(128), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(128), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(128), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(128), [7, 1],
                                             scope='Conv2d_0b_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(128), [1, 7],
                                             scope='Conv2d_0c_1x7')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(128), [7, 1],
                                             scope='Conv2d_0d_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0e_1x7')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_5: 17 x 17 x 768.
            end_point = 'Mixed_6c'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(160), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(160), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(160), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [7, 1],
                                             scope='Conv2d_0b_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [1, 7],
                                             scope='Conv2d_0c_1x7')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [7, 1],
                                             scope='Conv2d_0d_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0e_1x7')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # mixed_6: 17 x 17 x 768.
            end_point = 'Mixed_6d'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(160), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(160), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(160), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [7, 1],
                                             scope='Conv2d_0b_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [1, 7],
                                             scope='Conv2d_0c_1x7')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [7, 1],
                                             scope='Conv2d_0d_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0e_1x7')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_7: 17 x 17 x 768.
            end_point = 'Mixed_6e'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0b_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0c_1x7')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0d_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0e_1x7')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_8: 8 x 8 x 1280.
            end_point = 'Mixed_7a'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_0 = layers.conv2d(branch_0,
                                             depth(320), [3, 3],
                                             stride=2,
                                             padding='VALID',
                                             scope='Conv2d_1a_3x3')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [3, 3],
                                             stride=2,
                                             padding='VALID',
                                             scope='Conv2d_1a_3x3')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(net, [3, 3],
                                                     stride=2,
                                                     padding='VALID',
                                                     scope='MaxPool_1a_3x3')
                net = array_ops.concat_v2([branch_0, branch_1, branch_2], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # mixed_9: 8 x 8 x 2048.
            end_point = 'Mixed_7b'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(320), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(384), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = array_ops.concat_v2([
                        layers.conv2d(branch_1,
                                      depth(384), [1, 3],
                                      scope='Conv2d_0b_1x3'),
                        layers.conv2d(branch_1,
                                      depth(384), [3, 1],
                                      scope='Conv2d_0b_3x1')
                    ], 3)
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(448), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(384), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = array_ops.concat_v2([
                        layers.conv2d(branch_2,
                                      depth(384), [1, 3],
                                      scope='Conv2d_0c_1x3'),
                        layers.conv2d(branch_2,
                                      depth(384), [3, 1],
                                      scope='Conv2d_0d_3x1')
                    ], 3)
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_10: 8 x 8 x 2048.
            end_point = 'Mixed_7c'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(320), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(384), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = array_ops.concat_v2([
                        layers.conv2d(branch_1,
                                      depth(384), [1, 3],
                                      scope='Conv2d_0b_1x3'),
                        layers.conv2d(branch_1,
                                      depth(384), [3, 1],
                                      scope='Conv2d_0c_3x1')
                    ], 3)
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(448), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(384), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = array_ops.concat_v2([
                        layers.conv2d(branch_2,
                                      depth(384), [1, 3],
                                      scope='Conv2d_0c_1x3'),
                        layers.conv2d(branch_2,
                                      depth(384), [3, 1],
                                      scope='Conv2d_0d_3x1')
                    ], 3)
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat_v2(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[end_point] = net
            if end_point == final_endpoint:
                return net, end_points
        raise ValueError('Unknown final endpoint %s' % final_endpoint)
Exemplo n.º 12
0
def ldnet_v1(inputs,
             num_classes=3,
             dropout_keep_prob=0.5,
             spatial_squeeze=True,
             scope="ldnet",
             use_deform_conv=True,
             print_current_tensor=False):
    """
    ldnet architecture:
        input: 32*32*3
                             input   depth   kenal   stride   padding
        conv0:   net = conv(input,   32,   [3, 3],    1,     "same")
                 --> 32*32*32
        conv1:    net = conv(net,    32,   [3, 3],    1,     "same")
                 --> 32*32*32
        conv2:   net = conv(net,    64,   [3, 3],    1,     "same")
                 --> 32*32*64
        maxpool1: net = pool(net,          [3, 3],    1,     "same")
                 --> 32*32*64
        conv3:    net = conv(net,    192,  [3, 3],    1,     "same")
                 --> 32*32*192
        maxpool2: net = pool(net,          [3, 3],    1,     "same")
                 --> 32*32*192

        ldnet blocks:
        mixed_1: 32 x 32 x 320 Feature extraction module
        mixed_2: 32 x 32 x 320 Feature extraction module
        mixed_res1: 32 x 32 x 320 Feature extraction module
        mixed_3: 16 x 16 x 640 Dimension reduction module
        mixed_4: 16 x 16 x 640 Feature extraction module
        mixed_res2: 16 x 16 x 640 Feature extraction module
        mixed_5: 8 x 8 x 1280 Dimension reduction module
        mixed_6: 8 x 8 x 1280 Feature extraction module
        mixed_res3: 8 x 8 x 1280 Feature extraction module
        Final pooling and prediction -> 3

    :param inputs: the size of imputs is [batch_num, width, height, channel].
    :param num_classes: num of classes predicted.
    :param dropout_keep_prob: dropout probability.
    :param spatial_squeeze: whether or not squeeze.
    :param scope: optional scope.
    :param use_deform_conv: whether to use deform conv.
    :param print_current_tensor: whether or not print current tenser shape, name and type.

    :return:
        logits: [batch_size, num_classes]
    """

    # end_points will collect relevant activations for the computation
    # of shortcuts.
    end_points = []

    with variable_scope.variable_scope(scope, "ldnet_v1", [inputs]):
        with arg_scope([layers.conv2d, layers_lib.max_pool2d],
                       kernel_size=[3, 3],
                       stride=1,
                       padding='SAME'):
            # input: 32 * 32 * 3
            net = inputs

            end_point = "conv0"
            # if use_deform_conv:
            #     net = ConvOffset2D(3, name='conv0_offset')(net)  # net offset
            net = layers.conv2d(net, 32, scope=end_point)
            if print_current_tensor:
                print(net)
            # --> 32 * 32 * 32

            end_point = "conv1"
            if use_deform_conv:
                net = ConvOffset2D(32, name='conv1_offset')(net)  # net offset
            net = layers.conv2d(net, 32, scope=end_point)
            if print_current_tensor: print(net)
            # --> 32 * 32 * 32

            end_point = "conv2"
            if use_deform_conv:
                net = ConvOffset2D(32, name='conv2_offset')(net)  # net offset
            net = layers.conv2d(net, 64, scope=end_point)
            if print_current_tensor: print(net)
            # --> 32 * 32 * 64

            end_point = "maxpool0"
            net = layers_lib.max_pool2d(net,
                                        kernel_size=[2, 2],
                                        scope=end_point)
            if print_current_tensor: print(net)
            # --> 32 * 32 * 64

            end_point = 'conv3'
            if use_deform_conv:
                net = ConvOffset2D(64, name='conv3_offset')(net)  # net offset
            net = layers.conv2d(net, 192, scope=end_point)
            if print_current_tensor: print(net)
            # end_points.append(net)
            # --> 32 * 32 * 192

            end_point = 'maxpool1'
            net = layers_lib.max_pool2d(net,
                                        kernel_size=[2, 2],
                                        scope=end_point)
            if print_current_tensor: print(net)
            # net.alias = end_point
            # end_points.append(net)
            # --> 32 * 32 * 192

        # ldnet blocks
        with arg_scope(
            [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
                stride=1,
                padding='SAME'):
            # mixed_1: 32 x 32 x 320 Feature extraction module
            end_point = 'mixed_1'
            with variable_scope.variable_scope(end_point):
                net = _feature_extraction_residual(net,
                                                   first_layer_depth=48,
                                                   second_layer_depth=64,
                                                   last_layer_depth=96,
                                                   scope='feature_extraction')
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

            # mixed_2: 32 x 32 x 320 Feature extraction module
            end_point = 'mixed_2'
            with variable_scope.variable_scope(end_point):
                net = _feature_extraction_residual(
                    net,
                    first_layer_depth=48,
                    second_layer_depth=64,
                    last_layer_depth=96,
                    scope='feature_extraction_residual')
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

            # mixed_res1: 32 x 32 x 320 Feature extraction module
            end_point = 'mixed_res1'
            with variable_scope.variable_scope(end_point):
                net = _feature_extraction_residual(
                    net,
                    first_layer_depth=48,
                    second_layer_depth=64,
                    last_layer_depth=96,
                    scope='feature_extraction_residual')
                net_linear = layers.conv2d(net,
                                           int(net.shape[3]), [1, 1],
                                           activation_fn=None,
                                           scope='net_linear_projection')
                shortcuts = _shortcuts_addition(net.shape,
                                                end_points[-1],
                                                end_points[-2],
                                                scope="shortcuts_addition")
                net = nn_ops.relu(net_linear + shortcuts)
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

            # mixed_3: 16 x 16 x 640 Dimension reduction module
            end_point = "mixed_3"
            with variable_scope.variable_scope(end_point):
                net = _dimension_reduction(net,
                                           branch_0_depth=224,
                                           branch_1_depth=96,
                                           scope='dimension_reduction')
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

            # mixed_4: 16 x 16 x 640 Feature extraction module
            end_point = "mixed_4"
            with variable_scope.variable_scope(end_point):
                net = _feature_extraction_residual(
                    net,
                    first_layer_depth=48 * 2,
                    second_layer_depth=64 * 2,
                    last_layer_depth=96 * 2,
                    scope='feature_extraction_residual')
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

            # mixed_res2: 16 x 16 x 640 Feature extraction module
            end_point = "mixed_res2"
            with variable_scope.variable_scope(end_point):
                net = _feature_extraction_residual(
                    net,
                    first_layer_depth=48 * 2,
                    second_layer_depth=64 * 2,
                    last_layer_depth=96 * 2,
                    scope='feature_extraction_residual')
                net_linear = layers.conv2d(net,
                                           int(net.shape[3]), [1, 1],
                                           activation_fn=None,
                                           scope='net_linear_projection')
                shortcuts = _shortcuts_addition(net.shape,
                                                end_points[-1],
                                                end_points[-2],
                                                scope="shortcuts_addition")
                net = nn_ops.relu(net_linear + shortcuts)
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

            # mixed_5: 8 x 8 x 1280 Dimension reduction module
            end_point = "mixed_5"
            with variable_scope.variable_scope(end_point):
                net = _dimension_reduction(net,
                                           branch_0_depth=224 * 2,
                                           branch_1_depth=96 * 2,
                                           scope='dimension_reduction')
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

            # mixed_6: 8 x 8 x 1280 Feature extraction module
            end_point = "mixed_6"
            with variable_scope.variable_scope(end_point):
                net = _feature_extraction_residual(
                    net,
                    first_layer_depth=48 * 4,
                    second_layer_depth=64 * 4,
                    last_layer_depth=96 * 4,
                    scope='feature_extraction_residual')
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

            # mixed_res3: 8 x 8 x 1280 Feature extraction module
            end_point = "mixed_res3"
            with variable_scope.variable_scope(end_point):
                net = _feature_extraction_residual(
                    net,
                    first_layer_depth=48 * 4,
                    second_layer_depth=64 * 4,
                    last_layer_depth=96 * 4,
                    scope='feature_extraction_residual')
                net_linear = layers.conv2d(net,
                                           int(net.shape[3]), [1, 1],
                                           activation_fn=None,
                                           scope='net_linear_projection')
                shortcuts = _shortcuts_addition(net.shape,
                                                end_points[-1],
                                                end_points[-2],
                                                scope="shortcuts_addition")
                net = nn_ops.relu(net_linear + shortcuts)
                end_points.append(net)
                if print_current_tensor: print(net, len(end_points))

        # Final pooling and prediction
        with variable_scope.variable_scope('Logits'):
            with arg_scope([layers.conv2d],
                           normalizer_fn=None,
                           normalizer_params=None):
                net = layers.conv2d(net,
                                    int(net.shape[3]), [3, 3],
                                    stride=2,
                                    scope='conv2d_1a_3x3')
                # 4 x 4 x 1280
                net = layers_lib.avg_pool2d(net, [4, 4],
                                            padding='VALID',
                                            scope='AvgPool_1b_4x4')
                # 1 x 1 x 1280

                # net = layers.conv2d(net, 640, [1, 1], scope='Conv2d_0c_1x1')
                # local1
                with variable_scope.variable_scope('local1') as scope:
                    # Move everything into depth so we can perform a single matrix multiply.
                    reshape = tf.reshape(net, [-1, 1280])
                    weights = _variable_with_weight_decay('weights',
                                                          shape=[1280, 640],
                                                          stddev=0.04,
                                                          wd=0.0001)
                    biases = _variable_on_cpu('biases', [640],
                                              tf.constant_initializer(0.1))
                    net = tf.nn.relu(tf.matmul(reshape, weights) + biases,
                                     name=scope.name)
                # 1 x 1 x 640

                net = layers_lib.dropout(net,
                                         keep_prob=dropout_keep_prob,
                                         scope='Dropout_0c')

                # net = layers.conv2d(net, 320, [1, 1], scope='Conv2d_0d_1x1')
                # local2
                with variable_scope.variable_scope('local2') as scope:
                    weights = _variable_with_weight_decay('weights',
                                                          shape=[640, 320],
                                                          stddev=0.04,
                                                          wd=0.0001)
                    biases = _variable_on_cpu('biases', [320],
                                              tf.constant_initializer(0.1))
                    net = tf.nn.relu(tf.matmul(net, weights) + biases,
                                     name=scope.name)
                # 1 x 1 x 320

                net = layers_lib.dropout(net,
                                         keep_prob=dropout_keep_prob,
                                         scope='Dropout_0d')
                net = tf.expand_dims(net, 1)
                net = tf.expand_dims(net, 1)

                logits = layers.conv2d(net,
                                       num_classes, [1, 1],
                                       activation_fn=None,
                                       normalizer_fn=None,
                                       scope='Conv2d_0e_1x1')
                # 1 x 1 x 3
                if spatial_squeeze:
                    logits = array_ops.squeeze(logits, [1, 2],
                                               name='SpatialSqueeze')
                    # 3

    return logits
Exemplo n.º 13
0
def inception_v3(inputs,
                 num_classes=12,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 prediction_fn=layers_lib.softmax,
                 reuse=None,
                 hparam_string=None,
                 global_pool=True,
                 scope='InceptionV3'):
    """Inception model from http://arxiv.org/abs/1512.00567.

  "Rethinking the Inception Architecture for Computer Vision"

  Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jonathon Shlens,
  Zbigniew Wojna.

  With the default arguments this method constructs the exact model defined in
  the paper. However, one can experiment with variations of the inception_v3
  network by changing arguments dropout_keep_prob, min_depth and
  depth_multiplier.

  The default image size used to train this network is 299x299.

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether is training or not.
    dropout_keep_prob: the percentage of activation values that are retained.
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    prediction_fn: a function to get predictions out of logits.
    spatial_squeeze: if True, logits is of shape is [B, C], if false logits is
        of shape [B, 1, 1, C], where B is batch_size and C is number of classes.
    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.

  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.

  Raises:
    ValueError: if 'depth_multiplier' is less than or equal to zero.
  """

    hparams = create_hparams(hparam_string=hparam_string)

    if hparams.depth_multiplier <= 0:
        raise ValueError('depth_multiplier is not greater than zero.')
    depth = lambda d: max(int(d * hparams.depth_multiplier), hparams.min_depth)

    with variable_scope.variable_scope(scope,
                                       'InceptionV3', [inputs, num_classes],
                                       reuse=reuse) as scope:
        with arg_scope([layers_lib.batch_norm, layers_lib.dropout],
                       is_training=is_training):
            net, end_points = inception_v3_base(inputs,
                                                scope=scope,
                                                **hparams.values())

            # Final pooling and prediction
            with variable_scope.variable_scope('Logits'):
                if global_pool:
                    # Global average pooling.
                    net_avg = math_ops.reduce_mean(net, [1, 2],
                                                   keep_dims=True,
                                                   name='global_avg_pool')
                    net_max = math_ops.reduce_max(net, [1, 2],
                                                  keep_dims=True,
                                                  name='global_max_pool')
                    net = array_ops.concat([net_avg, net_max], -1)
                else:
                    kernel_size = _reduced_kernel_size_for_small_input(
                        net, [8, 8])
                    net = layers_lib.avg_pool2d(
                        net,
                        kernel_size,
                        padding='VALID',
                        scope='AvgPool_1a_{}x{}'.format(*kernel_size))

                # 1 x 1 x 2048
                net = layers_lib.dropout(net,
                                         keep_prob=dropout_keep_prob,
                                         scope='Dropout_1b')
                end_points['PreLogits'] = net
                # 2048
                logits = layers.conv2d(net,
                                       num_classes, [1, 1],
                                       activation_fn=None,
                                       normalizer_fn=None,
                                       scope='Conv2d_1c_1x1')
                logits = layers_core.dense(array_ops.squeeze(
                    logits, [1, 2], name='SpatialSqueeze'),
                                           num_classes,
                                           name='logits')

            end_points['Logits'] = logits
            end_points['Predictions'] = prediction_fn(logits,
                                                      scope='Predictions')
    return logits, end_points
Exemplo n.º 14
0
def vgg_19_style_transfer(inputs,
                          num_classes=1000,
                          is_training=True,
                          dropout_keep_prob=0.5,
                          spatial_squeeze=True,
                          scope='vgg_19'):
    """Oxford Net VGG 19-Layers version E Example.

  Note: All the fully_connected layers have been transformed to conv2d layers.
        To use in classification mode, resize input to 224x224.

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether or not the model is being trained.
    dropout_keep_prob: the probability that activations are kept in the dropout
      layers during training.
    spatial_squeeze: whether or not should squeeze the spatial dimensions of the
      outputs. Useful to remove unnecessary dimensions for classification.
    scope: Optional scope for the variables.

  Returns:
    the last op containing the log predictions and end_points dict.
  """
    with variable_scope.variable_scope(scope, 'vgg_19', [inputs]) as sc:
        end_points_collection = sc.name + '_end_points'
        # Collect outputs for conv2d, fully_connected and max_pool2d.
        with arg_scope(
            [layers.conv2d, layers_lib.fully_connected, layers_lib.max_pool2d],
                outputs_collections=end_points_collection):
            net = layers_lib.repeat(inputs,
                                    2,
                                    layers.conv2d,
                                    64, [3, 3],
                                    scope='conv1')
            net = layers_lib.avg_pool2d(net, [2, 2], scope='pool1')
            net = layers_lib.repeat(net,
                                    2,
                                    layers.conv2d,
                                    128, [3, 3],
                                    scope='conv2')
            net = layers_lib.avg_pool2d(net, [2, 2], scope='pool2')
            net = layers_lib.repeat(net,
                                    4,
                                    layers.conv2d,
                                    256, [3, 3],
                                    scope='conv3')
            net = layers_lib.avg_pool2d(net, [2, 2], scope='pool3')
            net = layers_lib.repeat(net,
                                    4,
                                    layers.conv2d,
                                    512, [3, 3],
                                    scope='conv4')
            net = layers_lib.avg_pool2d(net, [2, 2], scope='pool4')
            net = layers_lib.repeat(net,
                                    4,
                                    layers.conv2d,
                                    512, [3, 3],
                                    scope='conv5')
            net = layers_lib.avg_pool2d(net, [2, 2], scope='pool5')
            # Use conv2d instead of fully_connected layers.
            net = layers.conv2d(net,
                                4096, [7, 7],
                                padding='VALID',
                                scope='fc6')
            net = layers_lib.dropout(net,
                                     dropout_keep_prob,
                                     is_training=is_training,
                                     scope='dropout6')
            net = layers.conv2d(net, 4096, [1, 1], scope='fc7')
            net = layers_lib.dropout(net,
                                     dropout_keep_prob,
                                     is_training=is_training,
                                     scope='dropout7')
            net = layers.conv2d(net,
                                num_classes, [1, 1],
                                activation_fn=None,
                                normalizer_fn=None,
                                scope='fc8')
            # Convert end_points_collection into a end_point dict.
            end_points = utils.convert_collection_to_dict(
                end_points_collection)
            if spatial_squeeze:
                net = array_ops.squeeze(net, [1, 2], name='fc8/squeezed')
                end_points[sc.name + '/fc8'] = net
            return net, end_points
Exemplo n.º 15
0
def _feature_extraction_residual(net,
                                 first_layer_depth=48,
                                 second_layer_depth=64,
                                 last_layer_depth=96,
                                 use_deform_conv=False,
                                 scope='feature_extraction_residual'):
    """
    Feature extraction module of ldnet-v1.
    :param net: the net input.
    :param first_layer_depth: first layer depth.
    :param second_layer_depth: second layer depth.
    :param last_layer_depth: last layer depth.
    :param scope: optional scope.
    :return:
        the size of returned net: [batch_size, height, width, channel], which
        channel = (second_layer_depth + last_layer_depth) * 2
    """
    with variable_scope.variable_scope(scope):
        with variable_scope.variable_scope('Branch_0'):
            branch_0 = layers.conv2d(net,
                                     first_layer_depth, [1, 1],
                                     scope='Conv2d_0a_1x1')
            if use_deform_conv:
                branch_0 = ConvOffset2D(first_layer_depth,
                                        name='conv3_offset')(
                                            branch_0)  # net offset
            branch_0 = layers.conv2d(branch_0,
                                     second_layer_depth, [3, 3],
                                     scope='Conv2d_0b_3x3')

        with variable_scope.variable_scope('Branch_1'):
            branch_1 = layers.conv2d(net,
                                     first_layer_depth, [1, 1],
                                     scope='Conv2d_0a_1x1')
            if use_deform_conv:
                branch_1 = ConvOffset2D(first_layer_depth,
                                        name='conv3_offset')(
                                            branch_1)  # net offset
            branch_1 = layers.conv2d(branch_1,
                                     second_layer_depth, [5, 5],
                                     scope='Conv2d_0b_5x5')
            if use_deform_conv:
                branch_1 = ConvOffset2D(second_layer_depth,
                                        name='conv3_offset')(
                                            branch_1)  # net offset
            branch_1 = layers.conv2d(branch_1,
                                     last_layer_depth, [5, 5],
                                     scope='Conv2d_0c_5x5')

        with variable_scope.variable_scope('Branch_2'):
            branch_2 = layers.conv2d(net,
                                     first_layer_depth, [1, 1],
                                     scope='Conv2d_0a_1x1')
            if use_deform_conv:
                branch_2 = ConvOffset2D(first_layer_depth,
                                        name='conv3_offset')(
                                            branch_2)  # net offset
            branch_2 = layers.conv2d(branch_2,
                                     second_layer_depth, [7, 7],
                                     scope='Conv2d_0b_7x7')

        with variable_scope.variable_scope('Branch_3'):
            branch_3 = layers_lib.avg_pool2d(net, [5, 5],
                                             scope='AvgPool_0a_5x5')
            branch_3 = layers.conv2d(branch_3,
                                     last_layer_depth, [1, 1],
                                     scope='Conv2d_0b_1x1')

        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

    return net
Exemplo n.º 16
0
def inception_v2(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 min_depth=16,
                 depth_multiplier=1.0,
                 prediction_fn=layers_lib.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='InceptionV2'):
  """Inception v2 model for classification.

  Constructs an Inception v2 network for classification as described in
  http://arxiv.org/abs/1502.03167.

  The recommended image size used to train this network is 224x224. For image
  sizes that differ substantially, it is recommended to use inception_v2_base()
  and connect custom final layers to the output.

  Args:
    inputs: a tensor of shape [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether is training or not.
    dropout_keep_prob: the percentage of activation values that are retained.
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    prediction_fn: a function to get predictions out of logits.
    spatial_squeeze: if True, logits is of shape [B, C], if false logits is
        of shape [B, 1, 1, C], where B is batch_size and C is number of classes.
        Note that input image sizes other than 224x224 might lead to different
        spatial dimensions, and hence cannot be squeezed. In this event,
        it is best to set spatial_squeeze as False, and perform a reduce_mean
        over the resulting spatial dimensions with sizes exceeding 1.
    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.

  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.

  Raises:
    ValueError: if depth_multiplier <= 0.
  """
  if depth_multiplier <= 0:
    raise ValueError('depth_multiplier is not greater than zero.')

  # Final pooling and prediction
  with variable_scope.variable_scope(
      scope, 'InceptionV2', [inputs, num_classes], reuse=reuse) as scope:
    with arg_scope(
        [layers_lib.batch_norm, layers_lib.dropout], is_training=is_training):
      net, end_points = inception_v2_base(
          inputs,
          scope=scope,
          min_depth=min_depth,
          depth_multiplier=depth_multiplier)
      with variable_scope.variable_scope('Logits'):
        kernel_size = _reduced_kernel_size_for_small_input(net, [7, 7])
        net = layers_lib.avg_pool2d(
            net,
            kernel_size,
            padding='VALID',
            scope='AvgPool_1a_{}x{}'.format(*kernel_size))
        # 1 x 1 x 1024
        net = layers_lib.dropout(
            net, keep_prob=dropout_keep_prob, scope='Dropout_1b')
        logits = layers.conv2d(
            net,
            num_classes, [1, 1],
            activation_fn=None,
            normalizer_fn=None,
            scope='Conv2d_1c_1x1')
        if spatial_squeeze:
          logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')
      end_points['Logits'] = logits
      end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
  return logits, end_points
Exemplo n.º 17
0
def inception_v2_base(inputs,
                      final_endpoint='Mixed_5c',
                      min_depth=16,
                      depth_multiplier=1.0,
                      scope=None):
  """Inception v2 (6a2).

  Constructs an Inception v2 network from inputs to the given final endpoint.
  This method can construct the network up to the layer inception(5b) as
  described in http://arxiv.org/abs/1502.03167.

  Args:
    inputs: a tensor of shape [batch_size, height, width, channels].
    final_endpoint: specifies the endpoint to construct the network up to. It
      can be one of ['Conv2d_1a_7x7', 'MaxPool_2a_3x3', 'Conv2d_2b_1x1',
      'Conv2d_2c_3x3', 'MaxPool_3a_3x3', 'Mixed_3b', 'Mixed_3c', 'Mixed_4a',
      'Mixed_4b', 'Mixed_4c', 'Mixed_4d', 'Mixed_4e', 'Mixed_5a', 'Mixed_5b',
      'Mixed_5c'].
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    scope: Optional variable_scope.

  Returns:
    tensor_out: output tensor corresponding to the final_endpoint.
    end_points: a set of activations for external use, for example summaries or
                losses.

  Raises:
    ValueError: if final_endpoint is not set to one of the predefined values,
                or depth_multiplier <= 0
  """

  # end_points will collect relevant activations for external use, for example
  # summaries or losses.
  end_points = {}

  # Used to find thinned depths for each layer.
  if depth_multiplier <= 0:
    raise ValueError('depth_multiplier is not greater than zero.')
  depth = lambda d: max(int(d * depth_multiplier), min_depth)

  with variable_scope.variable_scope(scope, 'InceptionV2', [inputs]):
    with arg_scope(
        [
            layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d,
            layers.separable_conv2d
        ],
        stride=1,
        padding='SAME'):

      # Note that sizes in the comments below assume an input spatial size of
      # 224x224, however, the inputs can be of any size greater 32x32.

      # 224 x 224 x 3
      end_point = 'Conv2d_1a_7x7'
      # depthwise_multiplier here is different from depth_multiplier.
      # depthwise_multiplier determines the output channels of the initial
      # depthwise conv (see docs for tf.nn.separable_conv2d), while
      # depth_multiplier controls the # channels of the subsequent 1x1
      # convolution. Must have
      #   in_channels * depthwise_multipler <= out_channels
      # so that the separable convolution is not overparameterized.
      depthwise_multiplier = min(int(depth(64) / 3), 8)
      net = layers.separable_conv2d(
          inputs,
          depth(64), [7, 7],
          depth_multiplier=depthwise_multiplier,
          stride=2,
          weights_initializer=trunc_normal(1.0),
          scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 112 x 112 x 64
      end_point = 'MaxPool_2a_3x3'
      net = layers_lib.max_pool2d(net, [3, 3], scope=end_point, stride=2)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 56 x 56 x 64
      end_point = 'Conv2d_2b_1x1'
      net = layers.conv2d(
          net,
          depth(64), [1, 1],
          scope=end_point,
          weights_initializer=trunc_normal(0.1))
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 56 x 56 x 64
      end_point = 'Conv2d_2c_3x3'
      net = layers.conv2d(net, depth(192), [3, 3], scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 56 x 56 x 192
      end_point = 'MaxPool_3a_3x3'
      net = layers_lib.max_pool2d(net, [3, 3], scope=end_point, stride=2)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 28 x 28 x 192
      # Inception module.
      end_point = 'Mixed_3b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(64), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(32), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 28 x 28 x 256
      end_point = 'Mixed_3c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 28 x 28 x 320
      end_point = 'Mixed_4a'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_0 = layers.conv2d(
              branch_0, depth(160), [3, 3], stride=2, scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], stride=2, scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers_lib.max_pool2d(
              net, [3, 3], stride=2, scope='MaxPool_1a_3x3')
        net = array_ops.concat([branch_0, branch_1, branch_2], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 14 x 14 x 576
      end_point = 'Mixed_4b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(224), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 14 x 14 x 576
      end_point = 'Mixed_4c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(128), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 14 x 14 x 576
      end_point = 'Mixed_4d'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(160), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points

      # 14 x 14 x 576
      end_point = 'Mixed_4e'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(96), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(160), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 14 x 14 x 576
      end_point = 'Mixed_5a'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_0 = layers.conv2d(
              branch_0, depth(192), [3, 3], stride=2, scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(192), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(256), [3, 3], scope='Conv2d_0b_3x3')
          branch_1 = layers.conv2d(
              branch_1, depth(256), [3, 3], stride=2, scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers_lib.max_pool2d(
              net, [3, 3], stride=2, scope='MaxPool_1a_3x3')
        net = array_ops.concat([branch_0, branch_1, branch_2], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 7 x 7 x 1024
      end_point = 'Mixed_5b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(352), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(192), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(320), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(160), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(224), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(224), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points

      # 7 x 7 x 1024
      end_point = 'Mixed_5c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(352), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(192), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(320), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(192), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(224), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(224), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
    raise ValueError('Unknown final endpoint %s' % final_endpoint)
Exemplo n.º 18
0
def inception_g(inputs,
                num_classes = 5,
                dropout_keep_prob = 0.8,
                prediction_fn = layers_lib.softmax,
                scope='InceptionG'
                ):
    end_points = {}

    # conv2d
    with arg_scope(
        [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
        stride=1,
        padding='VALID'):
        # 48 x 48 x 1 
        end_point = 'Conv2d_1a_3x3'
        net = layers.conv2d(inputs, 32, [3, 3], stride=2, scope=end_point)
        end_points[end_point] = net
        # 23 x 23 x 32
        end_point = 'Conv2d_2a_3x3'
        net = layers.conv2d(net, 64, [3, 3], scope=end_point)
        end_points[end_point] = net

        # 21 x 21 x 64
        end_point = 'MaxPool_3a_3x3'
        net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope=end_point)
        end_points[end_point] = net

        # 10 x 10 x 64
        end_point = 'Conv2d_3b_1x1'
        net = layers.conv2d(net, 80, [1, 1], scope=end_point)
        end_points[end_point] = net

        # 8 x 8 x 80
        end_point = 'Conv2d_4a_3x3'
        net = layers.conv2d(net, 128, [3, 3], scope=end_point)
        end_points[end_point] = net
    
    # Inception blocks
    with arg_scope(
        [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
        stride=1,
        padding='SAME'):
        # 8 x 8 x 144
        end_point = 'Mixed_5a'
        branch_0 = layers.conv2d(
            net, 64, [1, 1], scope='Conv2d_0a_1x1')
        branch_1 = layers.conv2d(
            net, 48, [1, 1], scope='Conv2d_1a_1x1')
        branch_1 = layers.conv2d(
            branch_1, 64, [5, 5], scope='Conv2d_1b_5x5')
        branch_2 = layers.conv2d(
            net, 64, [1, 1], scope='Conv2d_2a_1x1')
        branch_2 = layers.conv2d(
            branch_2, 96, [3, 3], scope='Conv2d_2b_3x3')
        branch_2 = layers.conv2d(
            branch_2, 96, [3, 3], scope='Conv2d_2c_3x3')
        branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_3a_3x3')
        branch_3 = layers.conv2d(
            branch_3, 64, [1, 1], scope='Conv2d_3c_1x1')
        net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
        end_points[end_point] = net
    
    # aver pooling and softmax
    net = layers_lib.avg_pool2d(
            net,
            [8,8],
            padding='VALID',
            scope='AvgPool_0a_7x7')
    net = layers_lib.dropout(
            net, keep_prob=dropout_keep_prob, scope='Dropout_1b')
    end_points['PreLogits'] = net
    logits = layers.conv2d(
            net,
            num_classes,    # num classes
            [1, 1],
            activation_fn=None,
            normalizer_fn=None,
            scope='Conv2d_1c_1x1')
    logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')

    end_points['Logits'] = logits
    end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
    return logits, end_points
Exemplo n.º 19
0
def inception_v3_branch(net,
                        end_points,
                        num_classes,
                        deformable,
                        attention_option,
                        dropout_keep_prob=0.8,
                        final_endpoint='Logits',
                        min_depth=16,
                        depth_multiplier=1.0,
                        spatial_squeeze=True,
                        scope=None):
    if depth_multiplier <= 0:
        raise ValueError('depth_multiplier is not greater than zero.')
    depth = lambda d: max(int(d * depth_multiplier), min_depth)

    with variable_scope.variable_scope(scope, 'InceptionV3',
                                       [net, end_points]):
        with arg_scope(
            [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
                stride=1,
                padding='SAME'):

            # Deformable blocks
            if deformable == '1':
                end_point = 'Deformation'
                with variable_scope.variable_scope(end_point):
                    with variable_scope.variable_scope('Deform'):
                        net_feature = end_points['Conv2d_4a_3x3']
                        net_shape = net_feature.get_shape().as_list()
                        b = net_shape[0]
                        h = net_shape[1]
                        w = net_shape[2]
                        c = net_shape[3]
                        net_offset = layers.conv2d(end_points['Conv2d_3b_1x1'],
                                                   depth(2 * c), [3, 3],
                                                   padding='VALID',
                                                   scope=end_point + '_offset')
                    net = df._to_b_h_w_c(
                        df.tf_batch_map_offsets(
                            df._to_bc_h_w(net_feature, net_shape),
                            df._to_bc_h_w_2(net_offset, net_shape)), net_shape)
                end_points[scope.name + '/' + end_point +
                           '_offset'] = net_offset
                end_points[scope.name + '/' + end_point] = net
                if end_point == final_endpoint:
                    return net, end_points

            end_point = 'MaxPool_5a_3x3'
            net = layers_lib.max_pool2d(net, [3, 3],
                                        stride=2,
                                        padding='VALID',
                                        scope=end_point)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # 35 x 35 x 192.

            # Attention blocks
            if attention_option[0] == '1':
                end_point = 'Attention_S'
                with variable_scope.variable_scope(end_point):
                    map_shape = net.get_shape().as_list()
                    b = map_shape[0]
                    h = map_shape[1]
                    w = map_shape[2]
                    c = map_shape[3]
                    with variable_scope.variable_scope('Spatial'):
                        initial_map = layers.conv2d(net,
                                                    depth(c), [3, 3],
                                                    scope='Conv2d_4a_3x3')
                        attention_map = df._to_b_h_w_c(
                            tf.nn.softmax(df._to_bc_hw(initial_map,
                                                       map_shape)), map_shape)
                    net = net * attention_map
                end_points[scope.name + '/' + end_point +
                           '_map'] = attention_map
                end_points[scope.name + '/' + end_point] = net

            if attention_option[1] == '1':
                end_point = 'Attention_C'
                with variable_scope.variable_scope(end_point):
                    map_shape = net.get_shape().as_list()
                    b = map_shape[0]
                    h = map_shape[1]
                    w = map_shape[2]
                    c = map_shape[3]
                    with variable_scope.variable_scope('Channel'):
                        initial_map = layers.conv2d(net,
                                                    depth(c), [3, 3],
                                                    scope='Conv2d_4a_3x3')
                        attention_map = tf.nn.softmax(
                            tf.reduce_mean(tf.reshape(initial_map,
                                                      [b * h * w, c]),
                                           axis=0))
                    net = net * attention_map
                end_points[scope.name + '/' + end_point +
                           '_map'] = attention_map
                end_points[scope.name + '/' + end_point] = net

            if attention_option[2] == '1':
                end_point = 'Attention_S'
                with variable_scope.variable_scope(end_point):
                    map_shape = net.get_shape().as_list()
                    b = map_shape[0]
                    h = map_shape[1]
                    w = map_shape[2]
                    c = map_shape[3]
                    with variable_scope.variable_scope('Spatial'):
                        initial_map = layers.conv2d(net,
                                                    depth(c), [3, 3],
                                                    scope='Conv2d_4a_3x3')
                        attention_map = df._to_b_h_w_c(
                            tf.nn.softmax(df.to_bc_hw(initial_map, map_shape)),
                            map_shape)
                    net = net * attention_map
                end_points[scope.name + '/' + end_point +
                           '_map'] = attention_map
                end_points[scope.name + '/' + end_point] = net

            if attention_option[3] == '1':
                end_point = 'Attention_M'
                with variable_scope.variable_scope(end_point):
                    map_shape = net.get_shape().as_list()
                    b = map_shape[0]
                    h = map_shape[1]
                    w = map_shape[2]
                    c = map_shape[3]
                    with variable_scope.variable_scope('Modulation'):
                        initial_map = layers.conv2d(net,
                                                    depth(c), [3, 3],
                                                    scope='Conv2d_4a_3x3')
                        attention_map = tf.clip_by_value(initial_map, 0, 1)
                    net = net * attention_map
                end_points[scope.name + '/' + end_point +
                           '_map'] = attention_map
                end_points[scope.name + '/' + end_point] = net

            # Inception blocks
            # mixed: 35 x 35 x 256.
            end_point = 'Mixed_5b'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(48), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(64), [5, 5],
                                             scope='Conv2d_0b_5x5')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0c_3x3')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(32), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_1: 35 x 35 x 288.
            end_point = 'Mixed_5c'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(48), [1, 1],
                                             scope='Conv2d_0b_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(64), [5, 5],
                                             scope='Conv_1_0c_5x5')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0c_3x3')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_2: 35 x 35 x 288.
            end_point = 'Mixed_5d'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(48), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(64), [5, 5],
                                             scope='Conv2d_0b_5x5')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0c_3x3')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_3: 17 x 17 x 768.
            end_point = 'Mixed_6a'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(384), [3, 3],
                                             stride=2,
                                             padding='VALID',
                                             scope='Conv2d_1a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(64), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(96), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(96), [3, 3],
                                             stride=2,
                                             padding='VALID',
                                             scope='Conv2d_1a_1x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(net, [3, 3],
                                                     stride=2,
                                                     padding='VALID',
                                                     scope='MaxPool_1a_3x3')
                net = array_ops.concat([branch_0, branch_1, branch_2], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed4: 17 x 17 x 768.
            end_point = 'Mixed_6b'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(128), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(128), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(128), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(128), [7, 1],
                                             scope='Conv2d_0b_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(128), [1, 7],
                                             scope='Conv2d_0c_1x7')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(128), [7, 1],
                                             scope='Conv2d_0d_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0e_1x7')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_5: 17 x 17 x 768.
            end_point = 'Mixed_6c'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(160), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(160), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(160), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [7, 1],
                                             scope='Conv2d_0b_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [1, 7],
                                             scope='Conv2d_0c_1x7')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [7, 1],
                                             scope='Conv2d_0d_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0e_1x7')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # mixed_6: 17 x 17 x 768.
            end_point = 'Mixed_6d'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(160), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(160), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(160), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [7, 1],
                                             scope='Conv2d_0b_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [1, 7],
                                             scope='Conv2d_0c_1x7')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(160), [7, 1],
                                             scope='Conv2d_0d_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0e_1x7')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_7: 17 x 17 x 768.
            end_point = 'Mixed_6e'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0b_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0c_1x7')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0d_7x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0e_1x7')
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_8: 8 x 8 x 1280.
            end_point = 'Mixed_7a'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_0 = layers.conv2d(branch_0,
                                             depth(320), [3, 3],
                                             stride=2,
                                             padding='VALID',
                                             scope='Conv2d_1a_3x3')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [1, 7],
                                             scope='Conv2d_0b_1x7')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [7, 1],
                                             scope='Conv2d_0c_7x1')
                    branch_1 = layers.conv2d(branch_1,
                                             depth(192), [3, 3],
                                             stride=2,
                                             padding='VALID',
                                             scope='Conv2d_1a_3x3')
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(net, [3, 3],
                                                     stride=2,
                                                     padding='VALID',
                                                     scope='MaxPool_1a_3x3')
                net = array_ops.concat([branch_0, branch_1, branch_2], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # mixed_9: 8 x 8 x 2048.
            end_point = 'Mixed_7b'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(320), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(384), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = array_ops.concat([
                        layers.conv2d(branch_1,
                                      depth(384), [1, 3],
                                      scope='Conv2d_0b_1x3'),
                        layers.conv2d(branch_1,
                                      depth(384), [3, 1],
                                      scope='Conv2d_0b_3x1')
                    ], 3)
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(448), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(384), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = array_ops.concat([
                        layers.conv2d(branch_2,
                                      depth(384), [1, 3],
                                      scope='Conv2d_0c_1x3'),
                        layers.conv2d(branch_2,
                                      depth(384), [3, 1],
                                      scope='Conv2d_0d_3x1')
                    ], 3)
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points

            # mixed_10: 8 x 8 x 2048.
            end_point = 'Mixed_7c'
            with variable_scope.variable_scope(end_point):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(net,
                                             depth(320), [1, 1],
                                             scope='Conv2d_0a_1x1')
                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(net,
                                             depth(384), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_1 = array_ops.concat([
                        layers.conv2d(branch_1,
                                      depth(384), [1, 3],
                                      scope='Conv2d_0b_1x3'),
                        layers.conv2d(branch_1,
                                      depth(384), [3, 1],
                                      scope='Conv2d_0c_3x1')
                    ], 3)
                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(net,
                                             depth(448), [1, 1],
                                             scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(branch_2,
                                             depth(384), [3, 3],
                                             scope='Conv2d_0b_3x3')
                    branch_2 = array_ops.concat([
                        layers.conv2d(branch_2,
                                      depth(384), [1, 3],
                                      scope='Conv2d_0c_1x3'),
                        layers.conv2d(branch_2,
                                      depth(384), [3, 1],
                                      scope='Conv2d_0d_3x1')
                    ], 3)
                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     scope='AvgPool_0a_3x3')
                    branch_3 = layers.conv2d(branch_3,
                                             depth(192), [1, 1],
                                             scope='Conv2d_0b_1x1')
                net = array_ops.concat(
                    [branch_0, branch_1, branch_2, branch_3], 3)
            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points
            # Final pooling and prediction
            end_point = 'Logits'
            with variable_scope.variable_scope(end_point):
                kernel_size = _reduced_kernel_size_for_small_input(net, [8, 8])
                net = layers_lib.avg_pool2d(
                    net,
                    kernel_size,
                    padding='VALID',
                    scope='AvgPool_1a_{}x{}'.format(*kernel_size))
                # 1 x 1 x 2048
                net = layers_lib.dropout(net,
                                         keep_prob=dropout_keep_prob,
                                         scope='Dropout_1b')

                # 2048
                net = layers.conv2d(net,
                                    num_classes, [1, 1],
                                    activation_fn=None,
                                    normalizer_fn=None,
                                    scope='Conv2d_1c_1x1')

                if spatial_squeeze:
                    net = array_ops.squeeze(net, [1, 2], name='SpatialSqueeze')

            end_points[scope.name + '/' + end_point] = net
            if end_point == final_endpoint:
                return net, end_points
        raise ValueError('Unknown final endpoint %s' % final_endpoint)
Exemplo n.º 20
0
def inception_v1(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 prediction_fn=layers_lib.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='InceptionV1',
                 use_5x5=None):
  """Defines the Inception V1 architecture.

  This architecture is defined in:

    Going deeper with convolutions
    Christian Szegedy, Wei Liu, Yangqing Jia, Pierre Sermanet, Scott Reed,
    Dragomir Anguelov, Dumitru Erhan, Vincent Vanhoucke, Andrew Rabinovich.
    http://arxiv.org/pdf/1409.4842v1.pdf.

  The default image size used to train this network is 224x224.

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether is training or not.
    dropout_keep_prob: the percentage of activation values that are retained.
    prediction_fn: a function to get predictions out of logits.
    spatial_squeeze: if True, logits is of shape is [B, C], if false logits is
        of shape [B, 1, 1, C], where B is batch_size and C is number of classes.
    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.
    use_5x5: If True, the inception filter sizes will be set to 5x5 
      as specified in Figure 2(b) of the paper. The default behavior is to continue
      with 3x3 filter sizes.
      Note: This invalidates all current checkpoints and the saved models will 
      be incompatible as well. The training would have to be done from the beginning.
  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.
  """
  # Final pooling and prediction
  with variable_scope.variable_scope(
      scope, 'InceptionV1', [inputs, num_classes], reuse=reuse) as scope:
    with arg_scope(
        [layers_lib.batch_norm, layers_lib.dropout], is_training=is_training):
      # If we want the same filters as in the original paper. The default is 3x3.
      if use_5x5:
        filter_size=[5, 5]
        filter_size_str=str(filter_size[0])+'x'+str(filter_size[1])
      net, end_points = inception_v1_base(inputs, scope=scope)
      with variable_scope.variable_scope('Logits'):
        net = layers_lib.avg_pool2d(
            net, [7, 7], stride=1, scope='MaxPool_0a_7x7')
        net = layers_lib.dropout(net, dropout_keep_prob, scope='Dropout_0b')
        logits = layers.conv2d(
            net,
            num_classes, [1, 1],
            activation_fn=None,
            normalizer_fn=None,
            scope='Conv2d_0c_1x1')
        if spatial_squeeze:
          logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')

        end_points['Logits'] = logits
        end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
  return logits, end_points
Exemplo n.º 21
0
def inception_v2_base(inputs,
                      final_endpoint='Mixed_5c',
                      min_depth=16,
                      depth_multiplier=1.0,
                      scope=None):
  """Inception v2 (6a2).

  Constructs an Inception v2 network from inputs to the given final endpoint.
  This method can construct the network up to the layer inception(5b) as
  described in http://arxiv.org/abs/1502.03167.

  Args:
    inputs: a tensor of shape [batch_size, height, width, channels].
    final_endpoint: specifies the endpoint to construct the network up to. It
      can be one of ['Conv2d_1a_7x7', 'MaxPool_2a_3x3', 'Conv2d_2b_1x1',
      'Conv2d_2c_3x3', 'MaxPool_3a_3x3', 'Mixed_3b', 'Mixed_3c', 'Mixed_4a',
      'Mixed_4b', 'Mixed_4c', 'Mixed_4d', 'Mixed_4e', 'Mixed_5a', 'Mixed_5b',
      'Mixed_5c'].
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    scope: Optional variable_scope.

  Returns:
    tensor_out: output tensor corresponding to the final_endpoint.
    end_points: a set of activations for external use, for example summaries or
                losses.

  Raises:
    ValueError: if final_endpoint is not set to one of the predefined values,
                or depth_multiplier <= 0
  """

  # end_points will collect relevant activations for external use, for example
  # summaries or losses.
  end_points = {}

  # Used to find thinned depths for each layer.
  if depth_multiplier <= 0:
    raise ValueError('depth_multiplier is not greater than zero.')
  depth = lambda d: max(int(d * depth_multiplier), min_depth)

  with variable_scope.variable_scope(scope, 'InceptionV2', [inputs]):
    with arg_scope(
        [
            layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d,
            layers.separable_conv2d
        ],
        stride=1,
        padding='SAME'):

      # Note that sizes in the comments below assume an input spatial size of
      # 224x224, however, the inputs can be of any size greater 32x32.

      # 224 x 224 x 3
      end_point = 'Conv2d_1a_7x7'
      # depthwise_multiplier here is different from depth_multiplier.
      # depthwise_multiplier determines the output channels of the initial
      # depthwise conv (see docs for tf.nn.separable_conv2d), while
      # depth_multiplier controls the # channels of the subsequent 1x1
      # convolution. Must have
      #   in_channels * depthwise_multipler <= out_channels
      # so that the separable convolution is not overparameterized.
      depthwise_multiplier = min(int(depth(64) / 3), 8)
      net = layers.separable_conv2d(
          inputs,
          depth(64), [7, 7],
          depth_multiplier=depthwise_multiplier,
          stride=2,
          weights_initializer=trunc_normal(1.0),
          scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 112 x 112 x 64
      end_point = 'MaxPool_2a_3x3'
      net = layers_lib.max_pool2d(net, [3, 3], scope=end_point, stride=2)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 56 x 56 x 64
      end_point = 'Conv2d_2b_1x1'
      net = layers.conv2d(
          net,
          depth(64), [1, 1],
          scope=end_point,
          weights_initializer=trunc_normal(0.1))
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 56 x 56 x 64
      end_point = 'Conv2d_2c_3x3'
      net = layers.conv2d(net, depth(192), [3, 3], scope=end_point)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 56 x 56 x 192
      end_point = 'MaxPool_3a_3x3'
      net = layers_lib.max_pool2d(net, [3, 3], scope=end_point, stride=2)
      end_points[end_point] = net
      if end_point == final_endpoint:
        return net, end_points
      # 28 x 28 x 192
      # Inception module.
      end_point = 'Mixed_3b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(64), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(32), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2, branch_3] )
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 28 x 28 x 256
      end_point = 'Mixed_3c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 28 x 28 x 320
      end_point = 'Mixed_4a'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_0 = layers.conv2d(
              branch_0, depth(160), [3, 3], stride=2, scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], scope='Conv2d_0b_3x3')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], stride=2, scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers_lib.max_pool2d(
              net, [3, 3], stride=2, scope='MaxPool_1a_3x3')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 14 x 14 x 576
      end_point = 'Mixed_4b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(224), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(64), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(96), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 14 x 14 x 576
      end_point = 'Mixed_4c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(128), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(128), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 14 x 14 x 576
      end_point = 'Mixed_4d'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(160), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(160), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points

      # 14 x 14 x 576
      end_point = 'Mixed_4e'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(96), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(192), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(160), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(192), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(96), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 14 x 14 x 576
      end_point = 'Mixed_5a'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_0 = layers.conv2d(
              branch_0, depth(192), [3, 3], stride=2, scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(192), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(256), [3, 3], scope='Conv2d_0b_3x3')
          branch_1 = layers.conv2d(
              branch_1, depth(256), [3, 3], stride=2, scope='Conv2d_1a_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers_lib.max_pool2d(
              net, [3, 3], stride=2, scope='MaxPool_1a_3x3')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
      # 7 x 7 x 1024
      end_point = 'Mixed_5b'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(352), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(192), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(320), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(160), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(224), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(224), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points

      # 7 x 7 x 1024
      end_point = 'Mixed_5c'
      with variable_scope.variable_scope(end_point):
        with variable_scope.variable_scope('Branch_0'):
          branch_0 = layers.conv2d(
              net, depth(352), [1, 1], scope='Conv2d_0a_1x1')
        with variable_scope.variable_scope('Branch_1'):
          branch_1 = layers.conv2d(
              net,
              depth(192), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_1 = layers.conv2d(
              branch_1, depth(320), [3, 3], scope='Conv2d_0b_3x3')
        with variable_scope.variable_scope('Branch_2'):
          branch_2 = layers.conv2d(
              net,
              depth(192), [1, 1],
              weights_initializer=trunc_normal(0.09),
              scope='Conv2d_0a_1x1')
          branch_2 = layers.conv2d(
              branch_2, depth(224), [3, 3], scope='Conv2d_0b_3x3')
          branch_2 = layers.conv2d(
              branch_2, depth(224), [3, 3], scope='Conv2d_0c_3x3')
        with variable_scope.variable_scope('Branch_3'):
          branch_3 = layers_lib.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
          branch_3 = layers.conv2d(
              branch_3,
              depth(128), [1, 1],
              weights_initializer=trunc_normal(0.1),
              scope='Conv2d_0b_1x1')
        net = array_ops.concat(3, [branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        if end_point == final_endpoint:
          return net, end_points
    raise ValueError('Unknown final endpoint %s' % final_endpoint)
Exemplo n.º 22
0
def model_fn(features, labels, mode, params):
  """
  Based on https://github.com/tensorflow/tpu/blob/master/models/experimental/inception/inception_v2_tpu_model.py
  :param features:
  :param labels:
  :param mode:
  :param params:
  :return:
  """
  tf.summary.image('0_input', features, max_outputs=4)
  training = mode == tf.estimator.ModeKeys.TRAIN

  # 224 x 224 x 3
  end_point = 'Conv2d_1a_7x7'
  net = layers.conv2d(features, 64, [7, 7], stride=2, weights_initializer=trunc_normal(1.0), activation_fn=None, scope=end_point)
  net = tf.layers.batch_normalization(net, training=training, name='{}_bn'.format(end_point))
  net = tf.nn.relu(net, name='{}_act'.format(end_point))
  tf.summary.image('1_{}'.format(end_point), net[:, :, :, 0:3], max_outputs=4)

  # 112 x 112 x 64
  end_point = 'MaxPool_2a_3x3'
  net = layers_lib.max_pool2d(net, [3, 3], scope=end_point, stride=2, padding='SAME')
  tf.summary.image('2_{}'.format(end_point), net[:, :, :, 0:3], max_outputs=4)

  # 56 x 56 x 64
  end_point = 'Conv2d_2b_1x1'
  net = layers.conv2d(net, 64, [1, 1], activation_fn=None, scope=end_point, weights_initializer=trunc_normal(0.1))
  net = tf.layers.batch_normalization(net, training=training, name='{}_bn'.format(end_point))
  net = tf.nn.relu(net, name='{}_act'.format(end_point))
  tf.summary.image('3_{}'.format(end_point), net[:, :, :, 0:3], max_outputs=4)

  # 56 x 56 x 64
  end_point = 'Conv2d_2c_3x3'
  net = layers.conv2d(net, 192, [3, 3], activation_fn=None, scope=end_point)
  net = tf.layers.batch_normalization(net, training=training, name='{}_bn'.format(end_point))
  net = tf.nn.relu(net, name='{}_act'.format(end_point))
  tf.summary.image('4_{}'.format(end_point), net[:, :, :, 0:3], max_outputs=4)

  # 56 x 56 x 192
  end_point = 'MaxPool_3a_3x3'
  net = layers_lib.max_pool2d(net, [3, 3], scope=end_point, stride=2, padding='SAME')
  tf.summary.image('5_{}'.format(end_point), net[:, :, :, 0:3], max_outputs=4)

  # 28 x 28 x 192
  # Inception module.
  end_point = 'Mixed_3b'
  with variable_scope.variable_scope(end_point):
    with variable_scope.variable_scope('Branch_0'):
      branch_0 = layers.conv2d(net, 64, [1, 1], activation_fn=None, scope='Conv2d_0a_1x1')
      branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
      branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))
    with variable_scope.variable_scope('Branch_1'):
      branch_1 = layers.conv2d(net, 64, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
      branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
      branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

      branch_1 = layers.conv2d(branch_1, 64, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
      branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
      branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))
    with variable_scope.variable_scope('Branch_2'):
      branch_2 = layers.conv2d(net, 64, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
      branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
      branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0a_1x1'))

      branch_2 = layers.conv2d(branch_2, 96, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
      branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
      branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0b_3x3'))

      branch_2 = layers.conv2d(branch_2, 96, [3, 3], activation_fn=None, scope='Conv2d_0c_3x3')
      branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0c_3x3'))
      branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0c_3x3'))
    with variable_scope.variable_scope('Branch_3'):
      branch_3 = layers_lib.avg_pool2d(net, [3, 3], padding='SAME', stride=1, scope='AvgPool_0a_3x3')
      branch_3 = layers.conv2d(branch_3, 32, [1, 1], weights_initializer=trunc_normal(0.1), activation_fn=None, scope='Conv2d_0b_1x1')
      branch_3 = tf.layers.batch_normalization(branch_3, training=training, name='{}_bn'.format('Conv2d_0b_1x1'))
      branch_3 = tf.nn.relu(branch_3, name='{}_act'.format('Conv2d_0b_1x1'))
    net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

    # 28 x 28 x 256
    end_point = 'Mixed_3c'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 64, [1, 1], activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 64, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 96, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers.conv2d(net, 64, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_2 = layers.conv2d(branch_2, 96, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_2 = layers.conv2d(branch_2, 96, [3, 3], activation_fn=None, scope='Conv2d_0c_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0c_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0c_3x3'))
      with variable_scope.variable_scope('Branch_3'):
        branch_3 = layers_lib.avg_pool2d(net, [3, 3], padding='SAME', stride=1, scope='AvgPool_0a_3x3')
        branch_3 = layers.conv2d(branch_3, 64, [1, 1], weights_initializer=trunc_normal(0.1), activation_fn=None, scope='Conv2d_0b_1x1')
        branch_3 = tf.layers.batch_normalization(branch_3, training=training, name='{}_bn'.format('Conv2d_0b_1x1'))
        branch_3 = tf.nn.relu(branch_3, name='{}_act'.format('Conv2d_0b_1x1'))
      net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

    # 28 x 28 x 320
    end_point = 'Mixed_4a'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 128, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_0 = layers.conv2d(branch_0, 160, [3, 3], stride=2, activation_fn=None, scope='Conv2d_1a_3x3')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_1a_3x3'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_1a_3x3'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 64, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 96, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_1 = layers.conv2d(branch_1, 96, [3, 3], stride=2, activation_fn=None, scope='Conv2d_1a_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_1a_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_1a_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers_lib.max_pool2d(net, [3, 3], stride=2, padding='SAME', scope='MaxPool_1a_3x3')
      net = array_ops.concat([branch_0, branch_1, branch_2], 3)

    # 14 x 14 x 576
    end_point = 'Mixed_4b'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 224, [1, 1], activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 64, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 96, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers.conv2d(net, 96, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_2 = layers.conv2d(branch_2, 128, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_2 = layers.conv2d(branch_2, 128, [3, 3], activation_fn=None, scope='Conv2d_0c_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0c_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0c_3x3'))
      with variable_scope.variable_scope('Branch_3'):
        branch_3 = layers_lib.avg_pool2d(net, [3, 3], padding='SAME', stride=1, scope='AvgPool_0a_3x3')
        branch_3 = layers.conv2d(branch_3, 128, [1, 1], weights_initializer=trunc_normal(0.1), activation_fn=None, scope='Conv2d_0b_1x1')
        branch_3 = tf.layers.batch_normalization(branch_3, training=training, name='{}_bn'.format('Conv2d_0b_1x1'))
        branch_3 = tf.nn.relu(branch_3, name='{}_act'.format('Conv2d_0b_1x1'))
      net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

    # 14 x 14 x 576
    end_point = 'Mixed_4c'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 192, [1, 1], activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 96, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 128, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers.conv2d(net, 96, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_2 = layers.conv2d(branch_2, 128, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_2 = layers.conv2d(branch_2, 128, [3, 3], activation_fn=None, scope='Conv2d_0c_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0c_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0c_3x3'))
      with variable_scope.variable_scope('Branch_3'):
        branch_3 = layers_lib.avg_pool2d(net, [3, 3], padding='SAME', stride=1, scope='AvgPool_0a_3x3')
        branch_3 = layers.conv2d(branch_3, 128, [1, 1], weights_initializer=trunc_normal(0.1), activation_fn=None, scope='Conv2d_0b_1x1')
        branch_3 = tf.layers.batch_normalization(branch_3, training=training, name='{}_bn'.format('Conv2d_0b_1x1'))
        branch_3 = tf.nn.relu(branch_3, name='{}_act'.format('Conv2d_0b_1x1'))
      net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

    # 14 x 14 x 576
    end_point = 'Mixed_4d'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 160, [1, 1], activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 128, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 160, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers.conv2d(net, 128, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_2 = layers.conv2d(branch_2, 160, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_2 = layers.conv2d(branch_2, 160, [3, 3], activation_fn=None, scope='Conv2d_0c_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0c_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0c_3x3'))
      with variable_scope.variable_scope('Branch_3'):
        branch_3 = layers_lib.avg_pool2d(net, [3, 3], padding='SAME', stride=1, scope='AvgPool_0a_3x3')
        branch_3 = layers.conv2d(branch_3, 96, [1, 1], weights_initializer=trunc_normal(0.1), activation_fn=None, scope='Conv2d_0b_1x1')
        branch_3 = tf.layers.batch_normalization(branch_3, training=training, name='{}_bn'.format('Conv2d_0b_1x1'))
        branch_3 = tf.nn.relu(branch_3, name='{}_act'.format('Conv2d_0b_1x1'))
      net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

    # 14 x 14 x 576
    end_point = 'Mixed_4e'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 96, [1, 1], activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 128, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 192, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers.conv2d(net, 160, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_2 = layers.conv2d(branch_2, 192, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_2 = layers.conv2d(branch_2, 192, [3, 3], activation_fn=None, scope='Conv2d_0c_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0c_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0c_3x3'))
      with variable_scope.variable_scope('Branch_3'):
        branch_3 = layers_lib.avg_pool2d(net, [3, 3], padding='SAME', stride=1, scope='AvgPool_0a_3x3')
        branch_3 = layers.conv2d(branch_3, 96, [1, 1], weights_initializer=trunc_normal(0.1), activation_fn=None, scope='Conv2d_0b_1x1')
        branch_3 = tf.layers.batch_normalization(branch_3, training=training, name='{}_bn'.format('Conv2d_0b_1x1'))
        branch_3 = tf.nn.relu(branch_3, name='{}_act'.format('Conv2d_0b_1x1'))
      net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

    # 14 x 14 x 576
    end_point = 'Mixed_5a'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 128, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_0 = layers.conv2d(branch_0, 192, [3, 3], stride=2, activation_fn=None, scope='Conv2d_1a_3x3')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_1a_3x3'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_1a_3x3'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 192, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 256, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_1 = layers.conv2d(branch_1, 256, [3, 3], stride=2, activation_fn=None, scope='Conv2d_1a_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_1a_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_1a_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers_lib.max_pool2d(net, [3, 3], stride=2, padding='SAME', scope='MaxPool_1a_3x3')
      net = array_ops.concat([branch_0, branch_1, branch_2], 3)

    # 7 x 7 x 1024
    end_point = 'Mixed_5b'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 352, [1, 1], activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 192, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 320, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers.conv2d(net, 160, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_2 = layers.conv2d(branch_2, 224, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_2 = layers.conv2d(branch_2, 224, [3, 3], activation_fn=None, scope='Conv2d_0c_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0c_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0c_3x3'))
      with variable_scope.variable_scope('Branch_3'):
        branch_3 = layers_lib.avg_pool2d(net, [3, 3], padding='SAME', stride=1, scope='AvgPool_0a_3x3')
        branch_3 = layers.conv2d(branch_3, 128, [1, 1], weights_initializer=trunc_normal(0.1), activation_fn=None, scope='Conv2d_0b_1x1')
        branch_3 = tf.layers.batch_normalization(branch_3, training=training, name='{}_bn'.format('Conv2d_0b_1x1'))
        branch_3 = tf.nn.relu(branch_3, name='{}_act'.format('Conv2d_0b_1x1'))
      net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

    # 7 x 7 x 1024
    end_point = 'Mixed_5c'
    with variable_scope.variable_scope(end_point):
      with variable_scope.variable_scope('Branch_0'):
        branch_0 = layers.conv2d(net, 352, [1, 1], activation_fn=None, scope='Conv2d_0a_1x1')
        branch_0 = tf.layers.batch_normalization(branch_0, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_0 = tf.nn.relu(branch_0, name='{}_act'.format('Conv2d_0a_1x1'))
      with variable_scope.variable_scope('Branch_1'):
        branch_1 = layers.conv2d(net, 192, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_1 = layers.conv2d(branch_1, 320, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_1 = tf.layers.batch_normalization(branch_1, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_1 = tf.nn.relu(branch_1, name='{}_act'.format('Conv2d_0b_3x3'))
      with variable_scope.variable_scope('Branch_2'):
        branch_2 = layers.conv2d(net, 192, [1, 1], weights_initializer=trunc_normal(0.09), activation_fn=None, scope='Conv2d_0a_1x1')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0a_1x1'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0a_1x1'))

        branch_2 = layers.conv2d(branch_2, 224, [3, 3], activation_fn=None, scope='Conv2d_0b_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0b_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0b_3x3'))

        branch_2 = layers.conv2d(branch_2, 224, [3, 3], activation_fn=None, scope='Conv2d_0c_3x3')
        branch_2 = tf.layers.batch_normalization(branch_2, training=training, name='{}_bn'.format('Conv2d_0c_3x3'))
        branch_2 = tf.nn.relu(branch_2, name='{}_act'.format('Conv2d_0c_3x3'))
      with variable_scope.variable_scope('Branch_3'):
        branch_3 = layers_lib.max_pool2d(net, [3, 3], padding='SAME', stride=1, scope='MaxPool_0a_3x3')
        branch_3 = layers.conv2d(branch_3, 128, [1, 1], weights_initializer=trunc_normal(0.1), activation_fn=None, scope='Conv2d_0b_1x1')
        branch_3 = tf.layers.batch_normalization(branch_3, training=training, name='{}_bn'.format('Conv2d_0b_1x1'))
        branch_3 = tf.nn.relu(branch_3, name='{}_act'.format('Conv2d_0b_1x1'))
      net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)

  with variable_scope.variable_scope('Logits'):
    kernel_size = util._reduced_kernel_size_for_small_input(net, [7, 7])
    net = layers_lib.avg_pool2d(net, kernel_size, stride=1, padding='VALID', scope='AvgPool_1a_{}x{}'.format(*kernel_size))

    # 1 x 1 x 1024
    net = layers_lib.dropout(net, keep_prob=params['dropout_keep_prob'], scope='Dropout_1b')
    logits = layers.conv2d(net, params['num_classes'], [1, 1], normalizer_fn=None, activation_fn=None, scope='Conv2d_1c_1x1')
    if params['spatial_squeeze']:
      logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')

  predictions = {
    'argmax': tf.argmax(logits, axis=1, name='prediction_classes'),
    'predictions': layers_lib.softmax(logits, scope='Predictions'),
  }

  if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

  loss = tf.losses.sparse_softmax_cross_entropy(logits=logits, labels=labels)
  tf.summary.scalar('loss', loss)

  eval_metric_ops = {
    'accuracy_val': tf.metrics.accuracy(labels=labels, predictions=predictions['argmax'])
  }

  if mode == tf.estimator.ModeKeys.EVAL:
    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

  optimizer = tf.train.GradientDescentOptimizer(learning_rate=params['learning_rate'])
  extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
  with tf.control_dependencies(extra_update_ops):
    train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())

  tf.summary.scalar('accuracy_train', eval_metric_ops['accuracy_val'][1])
  tf.summary.histogram('labels', labels)
  tf.summary.histogram('predictions', predictions['argmax'])

  return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
Exemplo n.º 23
0
def ldnet(inputs, num_classes=3, dropout_keep_prob=0.5, spatial_squeeze=True, scope="ldnet",
          print_current_tensor=False):
    """
    ldnet architecture:
        input: 32*32*3
                             input   depth   kenal   stride   padding
        conv0:   net = conv(input,   32,   [3, 3],    1,     "same")
                 --> 32*32*32
        conv1:    net = conv(net,    32,   [3, 3],    1,     "same")
                 --> 32*32*32
        conv2:   net = conv(net,    64,   [3, 3],    1,     "same")
                 --> 32*32*64
        maxpool1: net = pool(net,          [3, 3],    1,     "same")
                 --> 32*32*64
        conv3:    net = conv(net,    192,  [3, 3],    1,     "same")
                 --> 32*32*192
        maxpool2: net = pool(net,          [3, 3],    1,     "same")
                 --> 32*32*192

        ldnet blocks:
        mixed_1: 32 x 32 x 320 Feature extraction module
        mixed_2: 16 x 16 x 640 Dimension reduction module
        mixed_3: 16 x 16 x 640 Feature extraction module
        mixed_4: 8 x 8 x 1280 Dimension reduction module
        mixed_5: 4 x 4 x 1280 Dimension reduction module
        Final pooling and prediction -> 3

    :param inputs: the size of imputs is [batch_num, width, height, channel].
    :param num_classes: num of classes.
    :param dropout_keep_prob:
    :param spatial_squeeze:
    :param scope:
    :param print_current_tensor: whether print current tenser shape, name and type.

    :return:
        logits: [batch, num_classes]
    """

    with variable_scope.variable_scope(scope, "ldnet", [inputs]):
        with arg_scope(
                [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
                kernel_size=[3, 3],
                stride=1,
                padding='SAME'):
            # input: 32 * 32 * 3

            net = layers.conv2d(inputs, 32, scope="conv0")
            if print_current_tensor: print(net)
            # --> 32 * 32 * 32

            net = layers.conv2d(net, 32, scope="conv1")
            if print_current_tensor: print(net)
            # --> 32 * 32 * 32

            net = layers.conv2d(net, 64, scope="conv2")
            if print_current_tensor: print(net)
            # --> 32 * 32 * 64

            net = layers_lib.max_pool2d(net, kernel_size=[2, 2], scope="maxpool0")
            if print_current_tensor: print(net)
            # --> 32 * 32 * 64

            net = layers.conv2d(net, 192, scope="conv3")
            if print_current_tensor: print(net)
            # --> 32 * 32 * 192

            net = layers_lib.max_pool2d(net, kernel_size=[2, 2], scope="maxpool1")
            if print_current_tensor: print(net)
            # --> 32 * 32 * 192

        # ldnet blocks
        with arg_scope(
                [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
                stride=1,
                padding='SAME'):
            # mixed_1: 32 x 32 x 320 Feature extraction module
            with variable_scope.variable_scope("mixed_1"):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(
                        net, 48, [1, 1], scope='Conv2d_0a_1x1')
                    branch_0 = layers.conv2d(
                        branch_0, 64, [3, 3], scope='Conv2d_0b_3x3')

                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(
                        net, 48, [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(
                        branch_1, 64, [5, 5], scope='Conv2d_0b_5x5')
                    branch_1 = layers.conv2d(
                        branch_1, 96, [5, 5], scope='Conv2d_0c_5x5')

                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(
                        net, 48, [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(
                        branch_2, 64, [7, 7], scope='Conv2d_0b_7x7')

                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [5, 5], scope='AvgPool_0a_5x5')
                    branch_3 = layers.conv2d(
                        branch_3, 96, [1, 1], scope='Conv2d_0b_1x1')

                net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
                if print_current_tensor: print(net)

            # mixed_2: 16 x 16 x 640 Dimension reduction module
            with variable_scope.variable_scope("mixed_2"):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(
                        net,
                        224, [3, 3],
                        stride=2,
                        scope='Conv2d_1a_1x1')

                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(
                        net, 64, [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(
                        branch_1, 96, [3, 3], scope='Conv2d_0b_3x3')
                    branch_1 = layers.conv2d(
                        branch_1,
                        96, [3, 3],
                        stride=2,
                        scope='Conv2d_1a_1x1')

                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(
                        net, [3, 3], stride=2, scope='MaxPool_1a_3x3')

                net = array_ops.concat([branch_0, branch_1, branch_2], 3)
                if print_current_tensor: print(net)

            # mixed_3: 16 x 16 x 640 Feature extraction module
            with variable_scope.variable_scope("mixed_3"):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(
                        net, 96, [1, 1], scope='Conv2d_0a_1x1')
                    branch_0 = layers.conv2d(
                        branch_0, 128, [3, 3], scope='Conv2d_0b_3x3')

                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(
                        net, 96, [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(
                        branch_1, 128, [5, 5], scope='Conv2d_0b_5x5')
                    branch_1 = layers.conv2d(
                        branch_1, 192, [5, 5], scope='Conv2d_0c_5x5')

                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers.conv2d(
                        net, 96, [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = layers.conv2d(
                        branch_2, 128, [7, 7], scope='Conv2d_0b_7x7')

                with variable_scope.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [5, 5], scope='AvgPool_0a_5x5')
                    branch_3 = layers.conv2d(
                        branch_3, 192, [1, 1], scope='Conv2d_0b_1x1')

                net = array_ops.concat([branch_0, branch_1, branch_2, branch_3], 3)
                if print_current_tensor: print(net)

            # mixed_4: 8 x 8 x 1280 Dimension reduction module
            with variable_scope.variable_scope("mixed_4"):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(
                        net,
                        448, [3, 3],
                        stride=2,
                        scope='Conv2d_1a_1x1')

                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(
                        net, 64, [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(
                        branch_1, 96, [3, 3], scope='Conv2d_0b_3x3')
                    branch_1 = layers.conv2d(
                        branch_1,
                        192, [3, 3],
                        stride=2,
                        scope='Conv2d_1a_1x1')

                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(
                        net, [3, 3], stride=2, scope='MaxPool_1a_3x3')

                net = array_ops.concat([branch_0, branch_1, branch_2], 3)
                if print_current_tensor: print(net)

            # mixed_5: 4 x 4 x 1280 Dimension reduction module
            with variable_scope.variable_scope("mixed_5"):
                with variable_scope.variable_scope('Branch_0'):
                    branch_0 = layers.conv2d(
                        net,
                        448, [3, 3],
                        stride=2,
                        scope='Conv2d_1a_1x1')

                with variable_scope.variable_scope('Branch_1'):
                    branch_1 = layers.conv2d(
                        net, 64, [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = layers.conv2d(
                        branch_1, 96, [3, 3], scope='Conv2d_0b_3x3')
                    branch_1 = layers.conv2d(
                        branch_1,
                        192, [3, 3],
                        stride=2,
                        scope='Conv2d_1a_1x1')

                with variable_scope.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(
                        net, [3, 3], stride=2, scope='MaxPool_1a_3x3')
                    branch_2 = layers.conv2d(
                        branch_2, 640, [1, 1], scope='Conv2d_0b_1x1')

                net = array_ops.concat([branch_0, branch_1, branch_2], 3)
                if print_current_tensor: print(net)

            # Final pooling and prediction
            with variable_scope.variable_scope('Logits'):
                net = layers_lib.avg_pool2d(
                    net,
                    [4, 4],
                    padding='VALID',
                    scope='AvgPool_1a_4x4')
                # 1 x 1 x 1280

                # net = layers.conv2d(net, 640, [1, 1], scope='Conv2d_0b_1x1')
                # local1
                with variable_scope.variable_scope('local1') as scope:
                    # Move everything into depth so we can perform a single matrix multiply.
                    reshape = tf.reshape(net, [-1, 1280])
                    weights = _variable_with_weight_decay('weights', shape=[1280, 640],
                                                          stddev=0.04, wd=0.0001)
                    biases = _variable_on_cpu('biases', [640], tf.constant_initializer(0.1))
                    net = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name)
                # 1 x 1 x 640

                net = layers_lib.dropout(net, keep_prob=dropout_keep_prob, scope='Dropout_0c')

                # net = layers.conv2d(net, 320, [1, 1], scope='Conv2d_0d_1x1')
                # local2
                with variable_scope.variable_scope('local2') as scope:
                    weights = _variable_with_weight_decay('weights', shape=[640, 320],
                                                          stddev=0.04, wd=0.0001)
                    biases = _variable_on_cpu('biases', [320], tf.constant_initializer(0.1))
                    net = tf.nn.relu(tf.matmul(net, weights) + biases, name=scope.name)
                # 1 x 1 x 320

                net = layers_lib.dropout(net, keep_prob=dropout_keep_prob, scope='Dropout_0e')
                net = tf.expand_dims(net, 1)
                net = tf.expand_dims(net, 1)

                logits = layers.conv2d(
                    net,
                    num_classes, [1, 1],
                    activation_fn=None,
                    normalizer_fn=None,
                    scope='Conv2d_0f_1x1')
                # 1 x 1 x 3
                if spatial_squeeze:
                    logits = array_ops.squeeze(logits, [1, 2], name='SpatialSqueeze')
                    # 3

    return logits
Exemplo n.º 24
0
def inception_v3(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 min_depth=16,
                 depth_multiplier=1.0,
                 prediction_fn=layers_lib.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='InceptionV3'):
    """Inception model from http://arxiv.org/abs/1512.00567.

  "Rethinking the Inception Architecture for Computer Vision"

  Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jonathon Shlens,
  Zbigniew Wojna.

  With the default arguments this method constructs the exact model defined in
  the paper. However, one can experiment with variations of the inception_v3
  network by changing arguments dropout_keep_prob, min_depth and
  depth_multiplier.

  The default image size used to train this network is 299x299.

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether is training or not.
    dropout_keep_prob: the percentage of activation values that are retained.
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_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.
    prediction_fn: a function to get predictions out of logits.
    spatial_squeeze: if True, logits is of shape is [B, C], if false logits is
        of shape [B, 1, 1, C], where B is batch_size and C is number of classes.
    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.

  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.

  Raises:
    ValueError: if 'depth_multiplier' is less than or equal to zero.
  """
    if depth_multiplier <= 0:
        raise ValueError('depth_multiplier is not greater than zero.')
    depth = lambda d: max(int(d * depth_multiplier), min_depth)

    with variable_scope.variable_scope(scope,
                                       'InceptionV3', [inputs, num_classes],
                                       reuse=reuse) as scope:
        with arg_scope([layers_lib.batch_norm, layers_lib.dropout],
                       is_training=is_training):
            net, end_points = inception_v3_base(
                inputs,
                scope=scope,
                min_depth=min_depth,
                depth_multiplier=depth_multiplier)

            # Auxiliary Head logits
            with arg_scope(
                [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
                    stride=1,
                    padding='SAME'):
                aux_logits = end_points['Mixed_6e']
                with variable_scope.variable_scope('AuxLogits'):
                    aux_logits = layers_lib.avg_pool2d(aux_logits, [5, 5],
                                                       stride=3,
                                                       padding='VALID',
                                                       scope='AvgPool_1a_5x5')
                    aux_logits = layers.conv2d(aux_logits,
                                               depth(128), [1, 1],
                                               scope='Conv2d_1b_1x1')

                    # Shape of feature map before the final layer.
                    kernel_size = _reduced_kernel_size_for_small_input(
                        aux_logits, [5, 5])
                    aux_logits = layers.conv2d(
                        aux_logits,
                        depth(768),
                        kernel_size,
                        weights_initializer=trunc_normal(0.01),
                        padding='VALID',
                        scope='Conv2d_2a_{}x{}'.format(*kernel_size))
                    aux_logits = layers.conv2d(
                        aux_logits,
                        num_classes, [1, 1],
                        activation_fn=None,
                        normalizer_fn=None,
                        weights_initializer=trunc_normal(0.001),
                        scope='Conv2d_2b_1x1')
                    if spatial_squeeze:
                        aux_logits = array_ops.squeeze(aux_logits, [1, 2],
                                                       name='SpatialSqueeze')
                    end_points['AuxLogits'] = aux_logits

            # Final pooling and prediction
            with variable_scope.variable_scope('Logits'):
                kernel_size = _reduced_kernel_size_for_small_input(net, [8, 8])
                net = layers_lib.avg_pool2d(
                    net,
                    kernel_size,
                    padding='VALID',
                    scope='AvgPool_1a_{}x{}'.format(*kernel_size))
                # 1 x 1 x 2048
                net = layers_lib.dropout(net,
                                         keep_prob=dropout_keep_prob,
                                         scope='Dropout_1b')
                end_points['PreLogits'] = net
                # 2048
                logits = layers.conv2d(net,
                                       num_classes, [1, 1],
                                       activation_fn=None,
                                       normalizer_fn=None,
                                       scope='Conv2d_1c_1x1')
                if spatial_squeeze:
                    logits = array_ops.squeeze(logits, [1, 2],
                                               name='SpatialSqueeze')
                # 1000
            end_points['Logits'] = logits
            end_points['Predictions'] = prediction_fn(logits,
                                                      scope='Predictions')
    return logits, end_points
Exemplo n.º 25
0
    def ops(self, input, scope=None, reuse=False):
        self.scope = scope
        sipconv2d = SIP_Conv2d()
        sipconv2d.set_default(is_training=self.is_training)
        if self.depth_multiplier <= 0:
            raise ValueError('depth_multiplier is not greater than zero.')
        depth = lambda d: max(int(d * self.depth_multiplier), self.min_depth)
        with tf.variable_scope(scope, "Inception_v3", reuse=reuse):
            sipconv2d.set_default(stride=1, padding="VALID")
            #---------------------------------------
            end_point = "Conv2d_p1_3x3"
            # ---- ops
            net = sipconv2d.ops(input,
                                depth(32), [3, 3],
                                stride=2,
                                scope=end_point)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Conv2d_p2_3x3"
            # ---- ops
            net = sipconv2d.ops(net, depth(32), [3, 3], scope=end_point)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Conv2d_p3_3x3"
            # ---- ops
            net = sipconv2d.ops(net,
                                depth(64), [3, 3],
                                padding="SAME",
                                scope=end_point)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "MaxPool_p4_3x3"
            # ---- ops
            net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope=end_point)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Conv2d_p5_1x1"
            # ---- ops
            net = sipconv2d.ops(net, depth(80), [1, 1], scope=end_point)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Conv2d_p6_3x3"
            # ---- ops
            net = sipconv2d.ops(net, depth(192), [3, 3], scope=end_point)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "MaxPool_p7_3x3"
            # ---- ops
            net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope=end_point)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p8_a"
            # ---- ops
            sipconv2d.set_default(padding="SAME", stride=1)
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(64), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(48), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(64), [5, 5],
                                             scope='b_conv2d_2')

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(64), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(96), [3, 3],
                                             scope="c_conv2d_2")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(96), [3, 3],
                                             scope="c_conv2d_3")

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(32), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p8_b"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(64), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(48), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(64), [5, 5],
                                             scope='b_conv2d_2')

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(64), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(96), [3, 3],
                                             scope="c_conv2d_2")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(96), [3, 3],
                                             scope="c_conv2d_3")

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(64), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p8_c"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(64), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(48), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(64), [5, 5],
                                             scope='b_conv2d_2')

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(64), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(96), [3, 3],
                                             scope="c_conv2d_2")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(96), [3, 3],
                                             scope="c_conv2d_3")

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(64), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p9_a"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(384), [3, 3],
                                             stride=2,
                                             padding="VALID",
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(64), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(96), [3, 3],
                                             scope='b_conv2d_2')
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(96), [3, 3],
                                             stride=2,
                                             padding="VALID",
                                             scope='b_conv2d_3')

                with tf.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(net, [3, 3],
                                                     stride=2,
                                                     padding="VALID",
                                                     scope="c_Max_Pool")
                net = tf.concat([branch_0, branch_1, branch_2], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p9_b"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(192), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(128), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(128), [1, 7],
                                             scope='b_conv2d_2')
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(192), [7, 1],
                                             scope='b_conv2d_3')

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(128), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(128), [7, 1],
                                             scope="c_conv2d_2")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(128), [1, 7],
                                             scope="c_conv2d_3")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(128), [7, 1],
                                             scope="c_conv2d_4")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(192), [1, 7],
                                             scope="c_conv2d_5")

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(192), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p9_c"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(192), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(160), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(160), [1, 7],
                                             scope='b_conv2d_2')
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(192), [7, 1],
                                             scope='b_conv2d_3')

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(160), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(160), [7, 1],
                                             scope="c_conv2d_2")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(160), [1, 7],
                                             scope="c_conv2d_3")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(160), [7, 1],
                                             scope="c_conv2d_4")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(192), [1, 7],
                                             scope="c_conv2d_5")

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(192), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p9_d"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(192), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(160), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(160), [1, 7],
                                             scope='b_conv2d_2')
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(192), [7, 1],
                                             scope='b_conv2d_3')

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(160), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(160), [7, 1],
                                             scope="c_conv2d_2")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(160), [1, 7],
                                             scope="c_conv2d_3")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(160), [7, 1],
                                             scope="c_conv2d_4")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(192), [1, 7],
                                             scope="c_conv2d_5")

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(192), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p9_e"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(192), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(192), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(192), [1, 7],
                                             scope='b_conv2d_2')
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(192), [7, 1],
                                             scope='b_conv2d_3')

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(192), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(192), [7, 1],
                                             scope="c_conv2d_2")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(192), [1, 7],
                                             scope="c_conv2d_3")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(192), [7, 1],
                                             scope="c_conv2d_4")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(192), [1, 7],
                                             scope="c_conv2d_5")

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(192), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p10_a"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(192), [1, 1],
                                             scope="a_conv2d_1")
                    branch_0 = sipconv2d.ops(branch_0,
                                             depth(320), [3, 3],
                                             stride=2,
                                             padding="VALID",
                                             scope="a_conv2d_2")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(192), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(192), [1, 7],
                                             scope='b_conv2d_2')
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(192), [7, 1],
                                             scope='b_conv2d_3')
                    branch_1 = sipconv2d.ops(branch_1,
                                             depth(192), [3, 3],
                                             stride=2,
                                             padding="VALID",
                                             scope='b_conv2d_4')

                with tf.variable_scope('Branch_2'):
                    branch_2 = layers_lib.max_pool2d(net, [3, 3],
                                                     stride=2,
                                                     padding="VALID",
                                                     scope="c_Max_Pool")
                net = tf.concat([branch_0, branch_1, branch_2], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p10_b"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(320), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(384), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = tf.concat([
                        sipconv2d.ops(
                            branch_1, depth(384), [1, 3], scope='b_conv2d_2'),
                        sipconv2d.ops(
                            branch_1, depth(384), [3, 1], scope='b_conv2d_3')
                    ], 3)

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(448), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(384), [3, 3],
                                             scope="c_conv2d_2")
                    branch_2 = tf.concat([
                        sipconv2d.ops(
                            branch_2, depth(384), [1, 3], scope='c_conv2d_3'),
                        sipconv2d.ops(
                            branch_2, depth(384), [3, 1], scope='c_conv2d_4')
                    ], 3)

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(192), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net
            # ---------------------------------------
            end_point = "Mixed_p10_c"
            # ---- ops
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = sipconv2d.ops(net,
                                             depth(320), [1, 1],
                                             scope="a_conv2d")

                with tf.variable_scope('Branch_1'):
                    branch_1 = sipconv2d.ops(net,
                                             depth(384), [1, 1],
                                             scope="b_conv2d_1")
                    branch_1 = tf.concat([
                        sipconv2d.ops(
                            branch_1, depth(384), [1, 3], scope='b_conv2d_2'),
                        sipconv2d.ops(
                            branch_1, depth(384), [3, 1], scope='b_conv2d_3')
                    ], 3)

                with tf.variable_scope('Branch_2'):
                    branch_2 = sipconv2d.ops(net,
                                             depth(448), [1, 1],
                                             scope="c_conv2d_1")
                    branch_2 = sipconv2d.ops(branch_2,
                                             depth(384), [3, 3],
                                             scope="c_conv2d_2")
                    branch_2 = tf.concat([
                        sipconv2d.ops(
                            branch_2, depth(384), [1, 3], scope='c_conv2d_3'),
                        sipconv2d.ops(
                            branch_2, depth(384), [3, 1], scope='c_conv2d_4')
                    ], 3)

                with tf.variable_scope('Branch_3'):
                    branch_3 = layers_lib.avg_pool2d(net, [3, 3],
                                                     stride=1,
                                                     padding="SAME",
                                                     scope='d_AvgPool')
                    branch_3 = sipconv2d.ops(branch_3,
                                             depth(192), [1, 1],
                                             scope='d_conv2d')
                net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
            # ----
            self.key_point[end_point] = net
            if end_point == self.final_endpoint:
                self.get_variables()
                return net