提交 48141423 编写于 作者: Y Yash Katariya 提交者: TensorFlower Gardener

Resize and crop when height and width of crop dimensions are greater than the input image.

PiperOrigin-RevId: 380869424
上级 e24363d8
......@@ -282,9 +282,14 @@ class RandomCrop(base_layer.Layer):
outputs = tf.slice(resized_inputs, bbox_begin, bbox_size)
return outputs
output = control_flow_util.smart_cond(training, random_cropped_inputs,
resize_and_center_cropped_inputs)
input_shape = inputs.shape.as_list()
if self.height > input_shape[H_AXIS] or self.width > input_shape[W_AXIS]:
output = resize_and_center_cropped_inputs()
else:
output = control_flow_util.smart_cond(training, random_cropped_inputs,
resize_and_center_cropped_inputs)
if unbatched:
output_shape = [self.height, self.width, input_shape[-1]]
else:
......
......@@ -299,16 +299,6 @@ class RandomCropTest(keras_parameterized.TestCase):
expected_output_shape=(None, expected_height, expected_width,
channels))
@parameterized.named_parameters(('random_crop_5_by_12', 5, 12),
('random_crop_10_by_8', 10, 8),
('random_crop_10_by_12', 10, 12))
def test_invalid_random_crop(self, expected_height, expected_width):
# InternelError is raised by tf.function MLIR lowering pass when TFRT
# is enabled.
with self.assertRaises((tf.errors.InvalidArgumentError, tf.errors.InternalError)):
with CustomObjectScope({'RandomCrop': image_preprocessing.RandomCrop}):
self._run_test(expected_height, expected_width)
def test_training_with_mock(self):
np.random.seed(1337)
height, width = 3, 4
......@@ -389,6 +379,16 @@ class RandomCropTest(keras_parameterized.TestCase):
actual_output = layer(inp, training=1)
self.assertAllClose(inp[2:10, 2:10, :], actual_output)
def test_input_smaller_than_crop_box(self):
np.random.seed(1337)
height, width = 10, 8
inp = np.random.random((12, 3, 3, 3))
with testing_utils.use_gpu():
layer = image_preprocessing.RandomCrop(height, width)
actual_output = layer(inp)
expected_output_shape = (12, 10, 8, 3)
self.assertEqual(expected_output_shape, actual_output.shape)
class RescalingTest(keras_parameterized.TestCase):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册