Ejemplo n.º 1
0
    def call(self,
             features: Mapping[str, tf.Tensor],
             boxes: tf.Tensor,
             training: bool = None):
        """Generates ROIs.

    Args:
      features: A dictionary with key as pyramid level and value as features.
        The features are in shape of
        [batch_size, height_l, width_l, num_filters].
      boxes: A 3-D `tf.Tensor` of shape [batch_size, num_boxes, 4]. Each row
        represents a box with [y1, x1, y2, x2] in un-normalized coordinates.
        from grid point.
      training: A `bool` of whether it is in training mode.

    Returns:
      A 5-D `tf.Tensor` representing feature crop of shape
      [batch_size, num_boxes, crop_size, crop_size, num_filters].
    """
        roi_features = spatial_transform_ops.multilevel_crop_and_resize(
            features,
            boxes,
            output_size=self._config_dict['crop_size'],
            sample_offset=self._config_dict['sample_offset'])
        return roi_features
Ejemplo n.º 2
0
    def test_multilevel_crop_and_resize_large_input(self):
        """Test 512 boxes on TPU."""
        input_size = 1408
        min_level = 2
        max_level = 6
        batch_size = 2
        num_boxes = 512
        num_filters = 256
        output_size = 7
        strategy = tf.distribute.experimental.TPUStrategy()
        with strategy.scope():
            features = {}
            for level in range(min_level, max_level + 1):
                feat_size = int(input_size / 2**level)
                features[level] = tf.constant(np.reshape(
                    np.arange(batch_size * feat_size * feat_size * num_filters,
                              dtype=np.float32),
                    [batch_size, feat_size, feat_size, num_filters]),
                                              dtype=tf.bfloat16)
            boxes = np.array([
                [[0, 0, 256, 256]] * num_boxes,
            ],
                             dtype=np.float32)
            boxes = np.tile(boxes, [batch_size, 1, 1])
            tf_boxes = tf.constant(boxes, dtype=tf.float32)

            tf_roi_features = spatial_transform_ops.multilevel_crop_and_resize(
                features, tf_boxes)
            roi_features = tf_roi_features.numpy()
            self.assertEqual(
                roi_features.shape,
                (batch_size, num_boxes, output_size, output_size, num_filters))
Ejemplo n.º 3
0
    def test_multilevel_crop_and_resize_two_boxes(self):
        """Test two boxes."""
        input_size = 4
        min_level = 0
        max_level = 0
        batch_size = 1
        output_size = 2
        num_filters = 1
        features = {}
        for level in range(min_level, max_level + 1):
            feat_size = int(input_size / 2**level)

            features[level] = tf.range(batch_size * feat_size * feat_size *
                                       num_filters,
                                       dtype=tf.float32)
            features[level] = tf.reshape(
                features[level],
                [batch_size, feat_size, feat_size, num_filters])
        boxes = tf.constant([
            [[0, 0, 2, 2], [0, 0, 2, 3]],
        ], dtype=tf.float32)
        tf_roi_features = spatial_transform_ops.multilevel_crop_and_resize(
            features, boxes, output_size)
        roi_features = tf_roi_features.numpy()
        self.assertAllClose(
            roi_features,
            np.array([[[2.5, 3.5], [6.5, 7.5]], [[2.75, 4.25], [6.75, 8.25]]
                      ]).reshape([batch_size, 2, output_size, output_size, 1]))
Ejemplo n.º 4
0
    def test_multilevel_crop_and_resize_feature_level_assignment(self):
        """Test feature level assignment."""
        input_size = 640
        min_level = 2
        max_level = 5
        batch_size = 1
        output_size = 2
        num_filters = 1
        features = {}
        for level in range(min_level, max_level + 1):
            feat_size = int(input_size / 2**level)

            features[level] = float(level) * tf.ones(
                [batch_size, feat_size, feat_size, num_filters],
                dtype=tf.float32)
        boxes = tf.constant(
            [
                [
                    [0, 0, 111, 111],  # Level 2.
                    [0, 0, 113, 113],  # Level 3.
                    [0, 0, 223, 223],  # Level 3.
                    [0, 0, 225, 225],  # Level 4.
                    [0, 0, 449, 449]
                ],  # Level 5.
            ],
            dtype=tf.float32)
        tf_roi_features = spatial_transform_ops.multilevel_crop_and_resize(
            features, boxes, output_size)
        roi_features = tf_roi_features.numpy()
        self.assertAllClose(roi_features[0, 0], 2 * np.ones((2, 2, 1)))
        self.assertAllClose(roi_features[0, 1], 3 * np.ones((2, 2, 1)))
        self.assertAllClose(roi_features[0, 2], 3 * np.ones((2, 2, 1)))
        self.assertAllClose(roi_features[0, 3], 4 * np.ones((2, 2, 1)))
        self.assertAllClose(roi_features[0, 4], 5 * np.ones((2, 2, 1)))
Ejemplo n.º 5
0
  def test_multilevel_crop_and_resize_square(self):
    """Example test case.

    Input =
    [
      [0, 1, 2, 3],
      [4, 5, 6, 7],
      [8, 9, 10, 11],
      [12, 13, 14, 15],
    ]
    output_size = 2x2
    box =
    [
      [[0, 0, 2, 2]]
    ]
    Gathered data =
    [
      [0, 1, 1, 2],
      [4, 5, 5, 6],
      [4, 5, 5, 6],
      [8, 9, 9, 10],
    ]
    Interpolation kernel =
    [
      [1, 1, 1, 1],
      [1, 1, 1, 1],
      [1, 1, 1, 1],
      [1, 1, 1, 1],
    ]
    Output =
    [
      [2.5, 3.5],
      [6.5, 7.5]
    ]
    """
    input_size = 4
    min_level = 0
    max_level = 0
    batch_size = 1
    output_size = 2
    num_filters = 1
    features = {}
    for level in range(min_level, max_level + 1):
      feat_size = int(input_size / 2**level)

      features[str(level)] = tf.range(
          batch_size * feat_size * feat_size * num_filters, dtype=tf.float32)
      features[str(level)] = tf.reshape(
          features[str(level)], [batch_size, feat_size, feat_size, num_filters])
    boxes = tf.constant([
        [[0, 0, 2, 2]],
    ], dtype=tf.float32)
    tf_roi_features = spatial_transform_ops.multilevel_crop_and_resize(
        features, boxes, output_size)
    roi_features = tf_roi_features.numpy()
    self.assertAllClose(
        roi_features,
        np.array([[2.5, 3.5],
                  [6.5,
                   7.5]]).reshape([batch_size, 1, output_size, output_size, 1]))