Пример #1
0
    def _parse_eval_data(self, data):
        """Parses data for evaluation."""

        # Get the image shape constants and cast the image to the selcted datatype.
        image = tf.cast(data['image'], dtype=self._dtype)
        boxes = data['groundtruth_boxes']
        classes = data['groundtruth_classes']

        image, infos, _ = preprocessing_ops.resize_and_jitter_image(
            image, [self._image_h, self._image_w],
            letter_box=self._letter_box,
            random_pad=False,
            shiftx=0.5,
            shifty=0.5,
            jitter=0.0)

        # Clip and clean boxes.
        image = image / 255.0
        boxes, inds = preprocessing_ops.transform_and_clip_boxes(
            boxes,
            infos,
            shuffle_boxes=False,
            area_thresh=0.0,
            filter_and_clip_boxes=False)
        classes = tf.gather(classes, inds)
        info = infos[-1]

        image, labels = self._build_label(image,
                                          boxes,
                                          classes,
                                          info,
                                          inds,
                                          data,
                                          is_training=False)
        return image, labels
Пример #2
0
    def _parse_train_data(self, data):
        """Parses data for training."""

        # Initialize the shape constants.
        image = data['image']
        boxes = data['groundtruth_boxes']
        classes = data['groundtruth_classes']

        if self._random_flip:
            # Randomly flip the image horizontally.
            image, boxes, _ = preprocess_ops.random_horizontal_flip(
                image, boxes, seed=self._seed)

        if not data['is_mosaic']:
            image, infos, affine = self._jitter_scale(
                image, [self._image_h, self._image_w], self._letter_box,
                self._jitter, self._random_pad, self._aug_scale_min,
                self._aug_scale_max, self._aug_rand_translate,
                self._aug_rand_angle, self._aug_rand_perspective)

            # Clip and clean boxes.
            boxes, inds = preprocessing_ops.transform_and_clip_boxes(
                boxes,
                infos,
                affine=affine,
                shuffle_boxes=False,
                area_thresh=self._area_thresh,
                augment=True,
                seed=self._seed)
            classes = tf.gather(classes, inds)
            info = infos[-1]
        else:
            image = tf.image.resize(image, (self._image_h, self._image_w),
                                    method='nearest')
            output_size = tf.cast([640, 640], tf.float32)
            boxes_ = bbox_ops.denormalize_boxes(boxes, output_size)
            inds = bbox_ops.get_non_empty_box_indices(boxes_)
            boxes = tf.gather(boxes, inds)
            classes = tf.gather(classes, inds)
            info = self._pad_infos_object(image)

        # Apply scaling to the hue saturation and brightness of an image.
        image = tf.cast(image, dtype=self._dtype)
        image = image / 255.0
        image = preprocessing_ops.image_rand_hsv(image,
                                                 self._aug_rand_hue,
                                                 self._aug_rand_saturation,
                                                 self._aug_rand_brightness,
                                                 seed=self._seed,
                                                 darknet=self._darknet)

        # Cast the image to the selcted datatype.
        image, labels = self._build_label(image,
                                          boxes,
                                          classes,
                                          info,
                                          inds,
                                          data,
                                          is_training=True)
        return image, labels
Пример #3
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
Пример #4
0
    def _augment_image(self,
                       image,
                       boxes,
                       classes,
                       is_crowd,
                       area,
                       xs=0.0,
                       ys=0.0,
                       cut=None):
        """Process a single image prior to the application of patching."""
        if self._random_flip:
            # Randomly flip the image horizontally.
            image, boxes, _ = preprocess_ops.random_horizontal_flip(
                image, boxes, seed=self._seed)

        # Augment the image without resizing
        image, infos, crop_points = preprocessing_ops.resize_and_jitter_image(
            image, [self._output_size[0], self._output_size[1]],
            random_pad=False,
            letter_box=self._letter_box,
            jitter=self._random_crop,
            shiftx=xs,
            shifty=ys,
            cut=cut,
            seed=self._seed)

        # Clip and clean boxes.
        boxes, inds = preprocessing_ops.transform_and_clip_boxes(
            boxes,
            infos,
            area_thresh=self._area_thresh,
            shuffle_boxes=False,
            filter_and_clip_boxes=True,
            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, crop_points