Пример #1
0
    def _mixup(self, one, two):
        """Blend together 2 images for the mixup data augmentation."""
        if self._mixup_frequency >= 1.0:
            domo = 1.0
        else:
            domo = preprocessing_ops.random_uniform_strong(0.0,
                                                           1.0,
                                                           dtype=tf.float32,
                                                           seed=self._seed)
            noop = one.copy()

        if domo >= (1 - self._mixup_frequency):
            sample = one
            otype = one['image'].dtype
            r = preprocessing_ops.random_uniform_strong(0.4,
                                                        0.6,
                                                        tf.float32,
                                                        seed=self._seed)
            sample['image'] = (r * tf.cast(one['image'], tf.float32) +
                               (1 - r) * tf.cast(two['image'], tf.float32))

            sample['image'] = tf.cast(sample['image'], otype)
            sample['groundtruth_boxes'] = tf.concat(
                [one['groundtruth_boxes'], two['groundtruth_boxes']], axis=0)
            sample['groundtruth_classes'] = tf.concat(
                [one['groundtruth_classes'], two['groundtruth_classes']],
                axis=0)
            sample['groundtruth_is_crowd'] = tf.concat(
                [one['groundtruth_is_crowd'], two['groundtruth_is_crowd']],
                axis=0)
            sample['groundtruth_area'] = tf.concat(
                [one['groundtruth_area'], two['groundtruth_area']], axis=0)
            return sample
        else:
            return self._add_param(noop)
Пример #2
0
    def _mosaic_crop_image(self, image, boxes, classes, is_crowd, area):
        """Process a patched image in preperation for final output."""
        if self._mosaic_crop_mode != 'crop':
            shape = tf.cast(preprocessing_ops.get_image_shape(image),
                            tf.float32)
            center = shape * self._mosaic_center

            # shift the center of the image by applying a translation to the whole
            # image
            ch = tf.math.round(
                preprocessing_ops.random_uniform_strong(-center[0],
                                                        center[0],
                                                        seed=self._seed))
            cw = tf.math.round(
                preprocessing_ops.random_uniform_strong(-center[1],
                                                        center[1],
                                                        seed=self._seed))

            # clip the boxes to those with in the image
            image = tfa.image.translate(image, [cw, ch],
                                        fill_value=self._pad_value)
            boxes = box_ops.denormalize_boxes(boxes, shape[:2])
            boxes = boxes + tf.cast([ch, cw, ch, cw], boxes.dtype)
            boxes = box_ops.clip_boxes(boxes, shape[:2])
            inds = box_ops.get_non_empty_box_indices(boxes)

            boxes = box_ops.normalize_boxes(boxes, shape[:2])
            boxes, classes, is_crowd, area = self._select_ind(
                inds,
                boxes,
                classes,  # pylint:disable=unbalanced-tuple-unpacking
                is_crowd,
                area)

        # warp and scale the fully stitched sample
        image, _, affine = preprocessing_ops.affine_warp_image(
            image, [self._output_size[0], self._output_size[1]],
            scale_min=self._aug_scale_min,
            scale_max=self._aug_scale_max,
            translate=self._aug_rand_translate,
            degrees=self._aug_rand_angle,
            perspective=self._aug_rand_perspective,
            random_pad=self._random_pad,
            seed=self._seed)
        height, width = self._output_size[0], self._output_size[1]
        image = tf.image.resize(image, (height, width))

        # clip and clean boxes
        boxes, inds = preprocessing_ops.transform_and_clip_boxes(
            boxes,
            None,
            affine=affine,
            area_thresh=self._area_thresh,
            seed=self._seed)
        classes, is_crowd, area = self._select_ind(inds, classes, is_crowd,
                                                   area)  # pylint:disable=unbalanced-tuple-unpacking
        return image, boxes, classes, is_crowd, area, area
Пример #3
0
 def _generate_cut(self):
   """Generate a random center to use for slicing and patching the images."""
   if self._mosaic_crop_mode == 'crop':
     min_offset = self._mosaic_center
     cut_x = preprocessing_ops.random_uniform_strong(
         self._output_size[1] * min_offset,
         self._output_size[1] * (1 - min_offset),
         seed=self._seed)
     cut_y = preprocessing_ops.random_uniform_strong(
         self._output_size[0] * min_offset,
         self._output_size[0] * (1 - min_offset),
         seed=self._seed)
     cut = [cut_y, cut_x]
     ishape = tf.convert_to_tensor(
         [self._output_size[0], self._output_size[1], 3])
   else:
     cut = None
     ishape = tf.convert_to_tensor(
         [self._output_size[0] * 2, self._output_size[1] * 2, 3])
   return cut, ishape
Пример #4
0
  def _mosaic(self, one, two, three, four):
    """Stitch together 4 images to build a mosaic."""
    if self._mosaic_frequency >= 1.0:
      domo = 1.0
    else:
      domo = preprocessing_ops.random_uniform_strong(
          0.0, 1.0, dtype=tf.float32, seed=self._seed)
      noop = one.copy()

    if domo >= (1 - self._mosaic_frequency):
      cut, ishape = self._generate_cut()
      one = self._process_image(one, 1.0, 1.0, cut, ishape)
      two = self._process_image(two, 0.0, 1.0, cut, ishape)
      three = self._process_image(three, 1.0, 0.0, cut, ishape)
      four = self._process_image(four, 0.0, 0.0, cut, ishape)
      patch1 = self._patch2(one, two)
      patch2 = self._patch2(three, four)
      stitched = self._patch(patch1, patch2)
      return stitched
    else:
      return self._add_param(noop)