Ejemplo n.º 1
0
    def _parse_train_image(self, decoded_tensors):
        """Parses image data for training."""
        image_bytes = decoded_tensors[self._image_field_key]

        if self._decode_jpeg_only:
            image_shape = tf.image.extract_jpeg_shape(image_bytes)

            # Crops image.
            cropped_image = preprocess_ops.random_crop_image_v2(
                image_bytes, image_shape)
            image = tf.cond(
                tf.reduce_all(tf.equal(tf.shape(cropped_image), image_shape)),
                lambda: preprocess_ops.center_crop_image_v2(
                    image_bytes, image_shape), lambda: cropped_image)
        else:
            # Decodes image.
            image = tf.io.decode_image(image_bytes, channels=3)
            image.set_shape([None, None, 3])

            # Crops image.
            cropped_image = preprocess_ops.random_crop_image(image)

            image = tf.cond(
                tf.reduce_all(
                    tf.equal(tf.shape(cropped_image), tf.shape(image))),
                lambda: preprocess_ops.center_crop_image(image),
                lambda: cropped_image)

        if self._aug_rand_hflip:
            image = tf.image.random_flip_left_right(image)

        # Color jitter.
        if self._color_jitter > 0:
            image = preprocess_ops.color_jitter(image, self._color_jitter,
                                                self._color_jitter,
                                                self._color_jitter)

        # Resizes image.
        image = tf.image.resize(image,
                                self._output_size,
                                method=tf.image.ResizeMethod.BILINEAR)
        image.set_shape([self._output_size[0], self._output_size[1], 3])

        # Apply autoaug or randaug.
        if self._augmenter is not None:
            image = self._augmenter.distort(image)

        # Normalizes image with mean and std pixel values.
        image = preprocess_ops.normalize_image(image,
                                               offset=MEAN_RGB,
                                               scale=STDDEV_RGB)

        # Random erasing after the image has been normalized
        if self._random_erasing is not None:
            image = self._random_erasing.distort(image)

        # Convert image to self._dtype.
        image = tf.image.convert_image_dtype(image, self._dtype)

        return image
Ejemplo n.º 2
0
    def _parse_train_image(self, decoded_tensors):
        """Parses image data for training."""
        image_bytes = decoded_tensors[self._image_field_key]

        if self._decode_jpeg_only:
            image_shape = tf.image.extract_jpeg_shape(image_bytes)

            # Crops image.
            cropped_image = preprocess_ops.random_crop_image_v2(
                image_bytes, image_shape)
            image = tf.cond(
                tf.reduce_all(tf.equal(tf.shape(cropped_image), image_shape)),
                lambda: preprocess_ops.center_crop_image_v2(
                    image_bytes, image_shape), lambda: cropped_image)
        else:
            # Decodes image.
            image = tf.io.decode_image(image_bytes, channels=3)
            image.set_shape([None, None, 3])

            # Crops image.
            cropped_image = preprocess_ops.random_crop_image(image)

            image = tf.cond(
                tf.reduce_all(
                    tf.equal(tf.shape(cropped_image), tf.shape(image))),
                lambda: preprocess_ops.center_crop_image(image),
                lambda: cropped_image)

        if self._aug_rand_hflip:
            image = tf.image.random_flip_left_right(image)

        # Resizes image.
        image = tf.image.resize(image,
                                self._output_size,
                                method=tf.image.ResizeMethod.BILINEAR)
        image.set_shape([self._output_size[0], self._output_size[1], 3])

        # Apply autoaug or randaug.
        if self._augmenter is not None:
            image = self._augmenter.distort(image)

        # Convert image to self._dtype.
        image = tf.image.convert_image_dtype(image, self._dtype)
        image = image / 255.0
        return image
 def testRandomCropImage(self, input_height, input_width):
     image = tf.convert_to_tensor(
         np.random.rand(input_height, input_width, 3))
     _ = preprocess_ops.random_crop_image(image)