def _parse_eval_image(self, decoded_tensors):
        """Parses image data for evaluation."""
        image_bytes = decoded_tensors[self._image_field_key]

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

            # Center crops.
            image = preprocess_ops.center_crop_image_v2(
                image_bytes, image_shape)
        else:
            # Decodes image.
            image = tf.io.decode_image(image_bytes, channels=3)
            image.set_shape([None, None, 3])

            # Center crops.
            if self._aug_crop:
                image = preprocess_ops.center_crop_image(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])

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

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

        return image
    def _parse_eval_data(self, decoded_tensors):
        """Parses data for evaluation."""
        image_bytes = decoded_tensors['image/encoded']
        image_shape = tf.image.extract_jpeg_shape(image_bytes)

        if self._test_crop:
            image = preprocess_ops.center_crop_image_v2(
                image_bytes, image_shape)
        else:
            image = tf.image.decode_jpeg(image_bytes, channels=3)
        # This line convert the image to float 0.0 - 1.0
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)

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

        image = tf.clip_by_value(image, 0., 1.)

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

        if self._parse_label:
            label = tf.cast(decoded_tensors['image/class/label'],
                            dtype=tf.int32)
            return image, label

        return image
    def _parse_eval_image(self, decoded_tensors):
        """Parses image data for evaluation."""
        image_bytes = decoded_tensors[self._image_field_key]

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

            # Center crops.
            image = preprocess_ops.center_crop_image_v2(
                image_bytes, image_shape)
        else:
            # Decodes image.
            image = tf.io.decode_image(image_bytes, channels=3)
            image.set_shape([None, None, 3])

            # Center crops.
            image = preprocess_ops.center_crop_image(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])

        # Convert image to self._dtype.
        image = tf.image.convert_image_dtype(image, self._dtype)
        image = image / 255.0
        return image
Exemple #4
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
 def test_center_crop_image_v2(self, input_height, input_width):
     image_bytes = tf.constant(_encode_image(np.uint8(
         np.random.rand(input_height, input_width, 3) * 255),
                                             fmt='JPEG'),
                               dtype=tf.string)
     cropped_image = preprocess_ops.center_crop_image_v2(
         image_bytes, tf.constant([input_height, input_width, 3], tf.int32))
     cropped_image_shape = tf.shape(cropped_image)
     self.assertAllEqual([350, 350, 3], cropped_image_shape.numpy())
    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