示例#1
0
 def test_random_height_inference(self):
   with CustomObjectScope({'RandomHeight': image_preprocessing.RandomHeight}):
     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.RandomHeight(.5)
       actual_output = layer(input_images, training=0)
       self.assertAllClose(expected_output, actual_output)
示例#2
0
 def test_valid_random_height(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, 5, 8, 3))
       layer = image_preprocessing.RandomHeight(.4)
       img_out = layer(img, training=True)
       self.assertEqual(img_out.shape[1], 3)
示例#3
0
 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.RandomHeight(factor)
     img_out = layer(img, training=True)
     self.assertEqual(img_out.shape[0], 2)
     self.assertEqual(img_out.shape[2], 8)
     self.assertEqual(img_out.shape[3], 3)
示例#4
0
 def test_random_height_shorter_numeric(self):
   for dtype in (np.int64, np.float32):
     with tf_test_util.use_gpu():
       input_image = np.reshape(np.arange(0, 8), (4, 2, 1)).astype(dtype)
       layer = image_preprocessing.RandomHeight(
           factor=(-.5, -.5), interpolation='nearest')
       output_image = layer(np.expand_dims(input_image, axis=0))
       # pyformat: disable
       expected_output = np.asarray([
           [2, 3],
           [6, 7]
       ]).astype(dtype)
       # pyformat: enable
       expected_output = np.reshape(expected_output, (1, 2, 2, 1))
       self.assertAllEqual(expected_output, output_image)
示例#5
0
 def test_random_height_longer_numeric(self):
   for dtype in (np.int64, np.float32):
     with tf_test_util.use_gpu():
       input_image = np.reshape(np.arange(0, 6), (2, 3, 1)).astype(dtype)
       layer = image_preprocessing.RandomHeight(factor=(1., 1.))
       output_image = layer(np.expand_dims(input_image, axis=0))
       # pyformat: disable
       expected_output = np.asarray([
           [0, 1, 2],
           [0.75, 1.75, 2.75],
           [2.25, 3.25, 4.25],
           [3, 4, 5]
       ]).astype(dtype)
       # pyformat: enable
       expected_output = np.reshape(expected_output, (1, 4, 3, 1))
       self.assertAllEqual(expected_output, output_image)
示例#6
0
 def test_random_height_longer_numeric(self):
   for dtype in (np.int64, np.float32):
     with tf_test_util.use_gpu():
       input_image = np.reshape(np.arange(0, 6), (2, 3, 1)).astype(dtype)
       layer = image_preprocessing.RandomHeight(factor=(1., 1.))
       # Return type of RandomHeight() 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, 1, 2],
           [0.75, 1.75, 2.75],
           [2.25, 3.25, 4.25],
           [3, 4, 5]
       ]).astype(dtype)
       # pyformat: enable
       expected_output = np.reshape(expected_output, (1, 4, 3, 1))
       self.assertAllEqual(expected_output, output_image)
示例#7
0
 def test_config_with_custom_name(self):
   layer = image_preprocessing.RandomHeight(.5, name='image_preproc')
   config = layer.get_config()
   layer_1 = image_preprocessing.RandomHeight.from_config(config)
   self.assertEqual(layer_1.name, layer.name)
示例#8
0
 def test_random_height_invalid_factor(self):
   with self.assertRaises(ValueError):
     image_preprocessing.RandomHeight((-1.5, .4))