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
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_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 _build_inputs(self, image): """Builds classification model inputs for serving.""" # Center crops and resizes image. image = preprocess_ops.center_crop_image(image) image = tf.image.resize( image, self._input_image_size, method=tf.image.ResizeMethod.BILINEAR) image = tf.reshape( image, [self._input_image_size[0], self._input_image_size[1], 3]) # Normalizes image with mean and std pixel values. image = preprocess_ops.normalize_image(image, offset=MEAN_RGB, scale=STDDEV_RGB) return image
def inference_fn(cls, image: tf.Tensor, input_image_size: List[int], num_channels: int = 3) -> tf.Tensor: """Builds image model inputs for serving.""" image = tf.cast(image, dtype=tf.float32) image = preprocess_ops.center_crop_image(image) image = tf.image.resize( image, input_image_size, method=tf.image.ResizeMethod.BILINEAR) # Normalizes image with mean and std pixel values. image = preprocess_ops.normalize_image( image, offset=MEAN_RGB, scale=STDDEV_RGB) image.set_shape(input_image_size + [num_channels]) return image
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 test_center_crop_image(self, input_height, input_width): image = tf.convert_to_tensor( np.random.rand(input_height, input_width, 3)) cropped_image = preprocess_ops.center_crop_image(image) cropped_image_shape = tf.shape(cropped_image) self.assertAllEqual([350, 350, 3], cropped_image_shape.numpy())