def test_predicting_with_mock_longer_width(self): np.random.seed(1337) height, width = 4, 6 inp = np.random.random((12, 8, 16, 3)) with tf_test_util.use_gpu(): layer = image_preprocessing.RandomCrop(height, width) actual_output = layer(inp, training=0) resized_inp = image_ops.resize_images_v2(inp, size=[4, 8]) expected_output = resized_inp[:, :, 1:7, :] self.assertAllClose(expected_output, actual_output)
def test_predicting_with_mock_longer_height(self): np.random.seed(1337) height, width = 3, 3 inp = np.random.random((12, 10, 6, 3)) with self.cached_session(use_gpu=True): layer = image_preprocessing.RandomCrop(height, width) actual_output = layer(inp, training=0) resized_inp = image_ops.resize_images_v2(inp, size=[5, 3]) expected_output = resized_inp[:, 1:4, :, :] self.assertAllClose(expected_output, actual_output)
def image_to_input_size(version, images, antialias=False): """Resizes images to size `version_input_shape`. This will convert grayscale to rgb. Note: Should be run on CPU. Preprocessing as in: https://github.com/tensorflow/models/blob/1af55e018eebce03fb61bba9959a04672536107d/research/slim/preprocessing/vgg_preprocessing.py#L319 Note: Does not preserve aspect ratio if resize is necessary. Args: version: A supported inception version. See `versions()`. images: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor of shape `[height, width, channels]`. Where channels can be `1` or `3`. antialias: Whether to use an anti-aliasing filter when downsampling an image. Returns: A tensor of shape `[batch] + version_input_shape` or `version_input_shape`. """ channels = images.shape[-1] assert channels == 1 or channels == 3 info = version_info(version) batch_input_shape = info["input_shape"] input_shape = info["input_shape"][1:] if not ( images.shape.is_compatible_with(batch_input_shape) or images.shape.is_compatible_with(input_shape) ): images = img_ops.resize_images_v2( images, size=input_shape[0:2], preserve_aspect_ratio=False, antialias=antialias, ) if channels == 1: rank = array_ops.rank(images) - 1 tile_shape = array_ops.concat( [array_ops.ones([rank], dtype=dtypes.int32), [3]], 0 ) images = gen_array_ops.tile(images, tile_shape) images = math_ops.cast(images, dtype=dtypes.float32) images -= array_ops.reshape( constant_op.constant([_R_MEAN, _G_MEAN, _B_MEAN]), [1, 1, 3] ) return images
def image_to_input_size(version, images, antialias=False): """Resizes images to size `version_input_shape`. This will convert grayscale to rgb. Note: Should be run on CPU. Preprocessing as in: https://github.com/tensorflow/models/blob/master/research/slim/preprocessing/inception_preprocessing.py#L253 Args: version: A supported inception version. See `versions()`. images: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor of shape `[height, width, channels]`. Where channels can be `1` or `3`. antialias: Whether to use an anti-aliasing filter when downsampling an image. Returns: A tensor of shape `[batch] + version_input_shape` or `version_input_shape`. """ channels = images.shape[-1] assert channels == 1 or channels == 3 info = version_info(version) batch_input_shape = info["input_shape"] input_shape = info["input_shape"][1:] images = img_ops.convert_image_dtype(images, dtype=dtypes.float32) if not (images.shape.is_compatible_with(batch_input_shape) or images.shape.is_compatible_with(input_shape)): images = img_ops.resize_images_v2( images, size=input_shape[0:2], preserve_aspect_ratio=False, antialias=antialias, ) if channels == 1: rank = array_ops.rank(images) - 1 tile_shape = array_ops.concat( [array_ops.ones([rank], dtype=dtypes.int32), [3]], 0) images = gen_array_ops.tile(images, tile_shape) images -= 0.5 images *= 2.0 return images
def call(self, inputs): outputs = image_ops.resize_images_v2( images=inputs, size=[self.target_height, self.target_width], method=self._interpolation_method) return outputs