def test_random_width_inference(self): with CustomObjectScope({'RandomWidth': image_preprocessing.RandomWidth}): input_images = np.random.random((2, 5, 8, 3)).astype(np.float32) expected_output = input_images with tf_test_util.use_gpu(): layer = image_preprocessing.RandomWidth(.5) actual_output = layer(input_images, training=0) self.assertAllClose(expected_output, actual_output)
def test_valid_random_width(self): # need (maxval - minval) * rnd + minval = 0.6 mock_factor = 0 with test.mock.patch.object( gen_stateful_random_ops, 'stateful_uniform', return_value=mock_factor): with tf_test_util.use_gpu(): img = np.random.random((12, 8, 5, 3)) layer = image_preprocessing.RandomWidth(.4) img_out = layer(img, training=True) self.assertEqual(img_out.shape[2], 3)
def _run_test(self, factor): np.random.seed(1337) num_samples = 2 orig_height = 5 orig_width = 8 channels = 3 with tf_test_util.use_gpu(): img = np.random.random((num_samples, orig_height, orig_width, channels)) layer = image_preprocessing.RandomWidth(factor) img_out = layer(img, training=True) self.assertEqual(img_out.shape[0], 2) self.assertEqual(img_out.shape[1], 5) self.assertEqual(img_out.shape[3], 3)
def test_random_width_shorter_numeric(self): for dtype in (np.int64, np.float32): with tf_test_util.use_gpu(): input_image = np.reshape(np.arange(0, 8), (2, 4, 1)).astype(dtype) layer = image_preprocessing.RandomWidth( factor=(-.5, -.5), interpolation='nearest') output_image = layer(np.expand_dims(input_image, axis=0)) # pyformat: disable expected_output = np.asarray([ [1, 3], [5, 7] ]).astype(dtype) # pyformat: enable expected_output = np.reshape(expected_output, (1, 2, 2, 1)) self.assertAllEqual(expected_output, output_image)
def test_random_width_longer_numeric(self): for dtype in (np.int64, np.float32): with tf_test_util.use_gpu(): input_image = np.reshape(np.arange(0, 6), (3, 2, 1)).astype(dtype) layer = image_preprocessing.RandomWidth(factor=(1., 1.)) output_image = layer(np.expand_dims(input_image, axis=0)) # pyformat: disable expected_output = np.asarray([ [0, 0.25, 0.75, 1], [2, 2.25, 2.75, 3], [4, 4.25, 4.75, 5] ]).astype(dtype) # pyformat: enable expected_output = np.reshape(expected_output, (1, 3, 4, 1)) self.assertAllEqual(expected_output, output_image)
def test_random_width_longer_numeric(self): for dtype in (np.int64, np.float32): with tf_test_util.use_gpu(): input_image = np.reshape(np.arange(0, 6), (3, 2, 1)).astype(dtype) layer = image_preprocessing.RandomWidth(factor=(1., 1.)) # Return type of RandomWidth() is float32 if `interpolation` is not # set to `ResizeMethod.NEAREST_NEIGHBOR`; cast `layer` to desired dtype. output_image = math_ops.cast(layer(np.expand_dims(input_image, axis=0)), dtype=dtype) # pyformat: disable expected_output = np.asarray([ [0, 0.25, 0.75, 1], [2, 2.25, 2.75, 3], [4, 4.25, 4.75, 5] ]).astype(dtype) # pyformat: enable expected_output = np.reshape(expected_output, (1, 3, 4, 1)) self.assertAllEqual(expected_output, output_image)
def test_config_with_custom_name(self): layer = image_preprocessing.RandomWidth(.5, name='image_preproc') config = layer.get_config() layer_1 = image_preprocessing.RandomWidth.from_config(config) self.assertEqual(layer_1.name, layer.name)
def test_random_width_invalid_factor(self): with self.assertRaises(ValueError): image_preprocessing.RandomWidth((-1.5, .4))