Example #1
0
def fpn_top_down_feature_maps(image_features,
                              depth,
                              use_depthwise=False,
                              use_explicit_padding=False,
                              use_bounded_activations=False,
                              scope=None,
                              use_native_resize_op=False):
    """Generates `top-down` feature maps for Feature Pyramid Networks.

  See https://arxiv.org/abs/1612.03144 for details.

  Args:
    image_features: list of tuples of (tensor_name, image_feature_tensor).
      Spatial resolutions of succesive tensors must reduce exactly by a factor
      of 2.
    depth: depth of output feature maps.
    use_depthwise: whether to use depthwise separable conv instead of regular
      conv.
    use_explicit_padding: whether to use explicit padding.
    use_bounded_activations: Whether or not to clip activations to range
      [-ACTIVATION_BOUND, ACTIVATION_BOUND]. Bounded activations better lend
      themselves to quantized inference.
    scope: A scope name to wrap this op under.
    use_native_resize_op: If True, uses tf.image.resize_nearest_neighbor op for
      the upsampling process instead of reshape and broadcasting implementation.

  Returns:
    feature_maps: an OrderedDict mapping keys (feature map names) to
      tensors where each tensor has shape [batch, height_i, width_i, depth_i].
  """
    with tf.name_scope(scope, 'top_down'):
        num_levels = len(image_features)
        output_feature_maps_list = []
        output_feature_map_keys = []
        padding = 'VALID' if use_explicit_padding else 'SAME'
        kernel_size = 3
        with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
                            padding=padding,
                            stride=1):
            top_down = slim.conv2d(image_features[-1][1],
                                   depth, [1, 1],
                                   activation_fn=None,
                                   normalizer_fn=None,
                                   scope='projection_%d' % num_levels)
            if use_bounded_activations:
                top_down = tf.clip_by_value(top_down, -ACTIVATION_BOUND,
                                            ACTIVATION_BOUND)
            output_feature_maps_list.append(top_down)
            output_feature_map_keys.append('top_down_%s' %
                                           image_features[-1][0])

            for level in reversed(range(num_levels - 1)):
                if use_native_resize_op:
                    with tf.name_scope('nearest_neighbor_upsampling'):
                        top_down_shape = top_down.shape.as_list()
                        top_down = tf.image.resize_nearest_neighbor(
                            top_down,
                            [top_down_shape[1] * 2, top_down_shape[2] * 2])
                else:
                    top_down = ops.nearest_neighbor_upsampling(top_down,
                                                               scale=2)
                residual = slim.conv2d(image_features[level][1],
                                       depth, [1, 1],
                                       activation_fn=None,
                                       normalizer_fn=None,
                                       scope='projection_%d' % (level + 1))
                if use_bounded_activations:
                    residual = tf.clip_by_value(residual, -ACTIVATION_BOUND,
                                                ACTIVATION_BOUND)
                if use_explicit_padding:
                    # slice top_down to the same shape as residual
                    residual_shape = tf.shape(residual)
                    top_down = top_down[:, :residual_shape[1], :
                                        residual_shape[2], :]
                top_down += residual
                if use_bounded_activations:
                    top_down = tf.clip_by_value(top_down, -ACTIVATION_BOUND,
                                                ACTIVATION_BOUND)
                if use_depthwise:
                    conv_op = functools.partial(slim.separable_conv2d,
                                                depth_multiplier=1)
                else:
                    conv_op = slim.conv2d
                if use_explicit_padding:
                    top_down = ops.fixed_padding(top_down, kernel_size)
                output_feature_maps_list.append(
                    conv_op(top_down,
                            depth, [kernel_size, kernel_size],
                            scope='smoothing_%d' % (level + 1)))
                output_feature_map_keys.append('top_down_%s' %
                                               image_features[level][0])
            return collections.OrderedDict(
                reversed(
                    list(zip(output_feature_map_keys,
                             output_feature_maps_list))))
Example #2
0
def multi_resolution_feature_maps(feature_map_layout,
                                  depth_multiplier,
                                  min_depth,
                                  insert_1x1_conv,
                                  image_features,
                                  pool_residual=False):
    """Generates multi resolution feature maps from input image features.

  Generates multi-scale feature maps for detection as in the SSD papers by
  Liu et al: https://arxiv.org/pdf/1512.02325v2.pdf, See Sec 2.1.

  More specifically, it performs the following two tasks:
  1) If a layer name is provided in the configuration, returns that layer as a
     feature map.
  2) If a layer name is left as an empty string, constructs a new feature map
     based on the spatial shape and depth configuration. Note that the current
     implementation only supports generating new layers using convolution of
     stride 2 resulting in a spatial resolution reduction by a factor of 2.
     By default convolution kernel size is set to 3, and it can be customized
     by caller.

  An example of the configuration for Inception V3:
  {
    'from_layer': ['Mixed_5d', 'Mixed_6e', 'Mixed_7c', '', '', ''],
    'layer_depth': [-1, -1, -1, 512, 256, 128]
  }

  Args:
    feature_map_layout: Dictionary of specifications for the feature map
      layouts in the following format (Inception V2/V3 respectively):
      {
        'from_layer': ['Mixed_3c', 'Mixed_4c', 'Mixed_5c', '', '', ''],
        'layer_depth': [-1, -1, -1, 512, 256, 128]
      }
      or
      {
        'from_layer': ['Mixed_5d', 'Mixed_6e', 'Mixed_7c', '', '', ''],
        'layer_depth': [-1, -1, -1, 512, 256, 128]
      }
      If 'from_layer' is specified, the specified feature map is directly used
      as a box predictor layer, and the layer_depth is directly infered from the
      feature map (instead of using the provided 'layer_depth' parameter). In
      this case, our convention is to set 'layer_depth' to -1 for clarity.
      Otherwise, if 'from_layer' is an empty string, then the box predictor
      layer will be built from the previous layer using convolution operations.
      Note that the current implementation only supports generating new layers
      using convolutions of stride 2 (resulting in a spatial resolution
      reduction by a factor of 2), and will be extended to a more flexible
      design. Convolution kernel size is set to 3 by default, and can be
      customized by 'conv_kernel_size' parameter (similarily, 'conv_kernel_size'
      should be set to -1 if 'from_layer' is specified). The created convolution
      operation will be a normal 2D convolution by default, and a depthwise
      convolution followed by 1x1 convolution if 'use_depthwise' is set to True.
    depth_multiplier: Depth multiplier for convolutional layers.
    min_depth: Minimum depth for convolutional layers.
    insert_1x1_conv: A boolean indicating whether an additional 1x1 convolution
      should be inserted before shrinking the feature map.
    image_features: A dictionary of handles to activation tensors from the
      base feature extractor.
    pool_residual: Whether to add an average pooling layer followed by a
      residual connection between subsequent feature maps when the channel
      depth match. For example, with option 'layer_depth': [-1, 512, 256, 256],
      a pooling and residual layer is added between the third and forth feature
      map. This option is better used with Weight Shared Convolution Box
      Predictor when all feature maps have the same channel depth to encourage
      more consistent features across multi-scale feature maps.

  Returns:
    feature_maps: an OrderedDict mapping keys (feature map names) to
      tensors where each tensor has shape [batch, height_i, width_i, depth_i].

  Raises:
    ValueError: if the number entries in 'from_layer' and
      'layer_depth' do not match.
    ValueError: if the generated layer does not have the same resolution
      as specified.
  """
    depth_fn = get_depth_fn(depth_multiplier, min_depth)

    feature_map_keys = []
    feature_maps = []
    base_from_layer = ''
    use_explicit_padding = False
    if 'use_explicit_padding' in feature_map_layout:
        use_explicit_padding = feature_map_layout['use_explicit_padding']
    use_depthwise = False
    if 'use_depthwise' in feature_map_layout:
        use_depthwise = feature_map_layout['use_depthwise']
    for index, from_layer in enumerate(feature_map_layout['from_layer']):
        layer_depth = feature_map_layout['layer_depth'][index]
        conv_kernel_size = 3
        if 'conv_kernel_size' in feature_map_layout:
            conv_kernel_size = feature_map_layout['conv_kernel_size'][index]
        if from_layer:
            feature_map = image_features[from_layer]
            base_from_layer = from_layer
            feature_map_keys.append(from_layer)
        else:
            pre_layer = feature_maps[-1]
            pre_layer_depth = pre_layer.get_shape().as_list()[3]
            intermediate_layer = pre_layer
            if insert_1x1_conv:
                layer_name = '{}_1_Conv2d_{}_1x1_{}'.format(
                    base_from_layer, index, depth_fn(layer_depth / 2))
                intermediate_layer = slim.conv2d(pre_layer,
                                                 depth_fn(layer_depth / 2),
                                                 [1, 1],
                                                 padding='SAME',
                                                 stride=1,
                                                 scope=layer_name)
            layer_name = '{}_2_Conv2d_{}_{}x{}_s2_{}'.format(
                base_from_layer, index, conv_kernel_size, conv_kernel_size,
                depth_fn(layer_depth))
            stride = 2
            padding = 'SAME'
            if use_explicit_padding:
                padding = 'VALID'
                intermediate_layer = ops.fixed_padding(intermediate_layer,
                                                       conv_kernel_size)
            if use_depthwise:
                feature_map = slim.separable_conv2d(
                    intermediate_layer,
                    None, [conv_kernel_size, conv_kernel_size],
                    depth_multiplier=1,
                    padding=padding,
                    stride=stride,
                    scope=layer_name + '_depthwise')
                feature_map = slim.conv2d(feature_map,
                                          depth_fn(layer_depth), [1, 1],
                                          padding='SAME',
                                          stride=1,
                                          scope=layer_name)
                if pool_residual and pre_layer_depth == depth_fn(layer_depth):
                    feature_map += slim.avg_pool2d(pre_layer, [3, 3],
                                                   padding='SAME',
                                                   stride=2,
                                                   scope=layer_name + '_pool')
            else:
                feature_map = slim.conv2d(intermediate_layer,
                                          depth_fn(layer_depth),
                                          [conv_kernel_size, conv_kernel_size],
                                          padding=padding,
                                          stride=stride,
                                          scope=layer_name)
            feature_map_keys.append(layer_name)
        feature_maps.append(feature_map)
    return collections.OrderedDict([
        (x, y) for (x, y) in zip(feature_map_keys, feature_maps)
    ])
    def extract_features(self, preprocessed_inputs):
        """Extract features from preprocessed inputs.

    Args:
      preprocessed_inputs: a [batch, height, width, channels] float tensor
        representing a batch of images.

    Returns:
      feature_maps: a list of tensors where the ith tensor has shape
        [batch, height_i, width_i, depth_i]
    """
        preprocessed_inputs = shape_utils.check_min_image_dim(
            33, preprocessed_inputs)

        with tf.variable_scope('MobilenetV1',
                               reuse=self._reuse_weights) as scope:
            with slim.arg_scope(
                    mobilenet_v1.mobilenet_v1_arg_scope(
                        is_training=None, regularize_depthwise=True)):
                with (slim.arg_scope(self._conv_hyperparams_fn())
                      if self._override_base_feature_extractor_hyperparams else
                      context_manager.IdentityContextManager()):
                    _, image_features = mobilenet_v1.mobilenet_v1_base(
                        ops.pad_to_multiple(preprocessed_inputs,
                                            self._pad_to_multiple),
                        final_endpoint='Conv2d_13_pointwise',
                        min_depth=self._min_depth,
                        depth_multiplier=self._depth_multiplier,
                        conv_defs=self._conv_defs,
                        use_explicit_padding=self._use_explicit_padding,
                        scope=scope)

            depth_fn = lambda d: max(int(d * self._depth_multiplier), self.
                                     _min_depth)
            with slim.arg_scope(self._conv_hyperparams_fn()):
                with tf.variable_scope('fpn', reuse=self._reuse_weights):
                    feature_blocks = [
                        'Conv2d_3_pointwise', 'Conv2d_5_pointwise',
                        'Conv2d_11_pointwise', 'Conv2d_13_pointwise'
                    ]
                    base_fpn_max_level = min(self._fpn_max_level, 5)
                    feature_block_list = []
                    for level in range(self._fpn_min_level,
                                       base_fpn_max_level + 1):
                        feature_block_list.append(feature_blocks[level - 2])
                    fpn_features = feature_map_generators.fpn_top_down_feature_maps(
                        [(key, image_features[key])
                         for key in feature_block_list],
                        depth=depth_fn(self._additional_layer_depth),
                        use_depthwise=self._use_depthwise,
                        use_explicit_padding=self._use_explicit_padding,
                        use_native_resize_op=self._use_native_resize_op)
                    feature_maps = []
                    for level in range(self._fpn_min_level,
                                       base_fpn_max_level + 1):
                        feature_maps.append(fpn_features['top_down_{}'.format(
                            feature_blocks[level - 2])])
                    last_feature_map = fpn_features['top_down_{}'.format(
                        feature_blocks[base_fpn_max_level - 2])]
                    # Construct coarse features
                    padding = 'VALID' if self._use_explicit_padding else 'SAME'
                    kernel_size = 3
                    for i in range(base_fpn_max_level + 1,
                                   self._fpn_max_level + 1):
                        if self._use_depthwise:
                            conv_op = functools.partial(slim.separable_conv2d,
                                                        depth_multiplier=1)
                        else:
                            conv_op = slim.conv2d
                        if self._use_explicit_padding:
                            last_feature_map = ops.fixed_padding(
                                last_feature_map, kernel_size)
                        last_feature_map = conv_op(
                            last_feature_map,
                            num_outputs=depth_fn(self._additional_layer_depth),
                            kernel_size=[kernel_size, kernel_size],
                            stride=2,
                            padding=padding,
                            scope='bottom_up_Conv2d_{}'.format(
                                i - base_fpn_max_level + 13))
                        feature_maps.append(last_feature_map)
        return feature_maps
Example #4
0
 def fixed_padding(features, kernel_size=conv_kernel_size):
     return ops.fixed_padding(features, kernel_size)
Example #5
0
 def _FixedPaddingLayer(self, kernel_size):
     return tf.keras.layers.Lambda(
         lambda x: ops.fixed_padding(x, kernel_size))