diff options
Diffstat (limited to 'tensorflow/contrib/layers/python/layers/layers_test.py')
-rw-r--r-- | tensorflow/contrib/layers/python/layers/layers_test.py | 150 |
1 files changed, 145 insertions, 5 deletions
diff --git a/tensorflow/contrib/layers/python/layers/layers_test.py b/tensorflow/contrib/layers/python/layers/layers_test.py index d4ee85b550..15809ea180 100644 --- a/tensorflow/contrib/layers/python/layers/layers_test.py +++ b/tensorflow/contrib/layers/python/layers/layers_test.py @@ -27,7 +27,6 @@ from tensorflow.contrib.framework.python.ops import arg_scope from tensorflow.contrib.framework.python.ops import variables from tensorflow.contrib.layers.python.layers import layers as _layers from tensorflow.contrib.layers.python.layers import regularizers -from tensorflow.contrib.losses.python.losses import loss_ops from tensorflow.python.client import session from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes @@ -49,6 +48,7 @@ from tensorflow.python.ops import state_ops from tensorflow.python.ops import template from tensorflow.python.ops import variable_scope from tensorflow.python.ops import variables as variables_lib +from tensorflow.python.ops.losses import losses from tensorflow.python.platform import test @@ -121,6 +121,76 @@ class AvgPool2DTest(test.TestCase): self.assertListEqual(output.get_shape().as_list(), [5, 1, 1, 3]) +class AvgPool3DTest(test.TestCase): + + def testInvalidDataFormat(self): + depth, height, width = 3, 6, 9 + images = np.random.uniform(size=(5, depth, height, width, 3)) + with self.assertRaisesRegexp(ValueError, + 'data_format has to be either NCDHW or NDHWC.'): + _layers.avg_pool3d(images, [3, 3, 3], data_format='CDHWN') + + def testCreateAvgPool(self): + depth, height, width = 3, 6, 9 + images = np.random.uniform(size=(5, depth, height, width, 3)) + output = _layers.avg_pool3d(images, [3, 3, 3]) + self.assertEqual(output.op.name, 'AvgPool3D/AvgPool3D') + self.assertListEqual(output.get_shape().as_list(), [5, 1, 2, 4, 3]) + + def testCreateAvgPoolNCDHW(self): + depth, height, width = 3, 6, 9 + images = np.random.uniform(size=(5, 2, depth, height, width)) + output = _layers.avg_pool3d(images, [3, 3, 3], data_format='NCDHW') + self.assertEquals(output.op.name, 'AvgPool3D/transpose_1') + self.assertListEqual(output.get_shape().as_list(), [5, 2, 1, 2, 4]) + + def testCollectOutputs(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.avg_pool3d(images, [3, 3, 3], outputs_collections='outputs') + output_collected = ops.get_collection('outputs')[0] + self.assertEqual(output_collected.aliases, ['AvgPool3D']) + self.assertEqual(output_collected, output) + + def testCreateSquareAvgPool(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.avg_pool3d(images, 3) + self.assertEqual(output.op.name, 'AvgPool3D/AvgPool3D') + self.assertListEqual(output.get_shape().as_list(), [5, 1, 2, 4, 3]) + + def testCreateAvgPoolWithScope(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.avg_pool3d(images, [3, 3, 3], scope='pool1') + self.assertEqual(output.op.name, 'pool1/AvgPool3D') + + def testCreateAvgPoolWithSamePadding(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.avg_pool3d(images, [3, 3, 3], padding='SAME') + self.assertListEqual(output.get_shape().as_list(), [5, 2, 3, 5, 3]) + + def testCreateAvgPoolWithSamePaddingNCDHW(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, 3, depth, height, width), seed=1) + output = _layers.avg_pool3d( + images, [3, 3, 3], padding='SAME', data_format='NCDHW') + self.assertListEqual(output.get_shape().as_list(), [5, 3, 2, 3, 5]) + + def testCreateAvgPoolStrideWithSamePadding(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.avg_pool3d(images, [3, 3, 3], stride=1, padding='SAME') + self.assertListEqual(output.get_shape().as_list(), [5, depth, height, width, 3]) + + def testGlobalAvgPool(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.avg_pool3d(images, images.get_shape()[1:4], stride=1) + self.assertListEqual(output.get_shape().as_list(), [5, 1, 1, 1, 3]) + + class PoolTest(test.TestCase): def testCreatePool(self): @@ -1559,23 +1629,23 @@ class FCTest(test.TestCase): inputs, 32, scope='fc1', weights_regularizer=regularizer) self.assertEqual( len(ops.get_collection(ops.GraphKeys.REGULARIZATION_LOSSES)), 1) - self.assertEqual(len(loss_ops.get_regularization_losses()), 1) + self.assertEqual(len(losses.get_regularization_losses()), 1) _layers.fully_connected( inputs, 32, scope='fc1', weights_regularizer=regularizer, reuse=True) self.assertEqual( len(ops.get_collection(ops.GraphKeys.REGULARIZATION_LOSSES)), 1) - self.assertEqual(len(loss_ops.get_regularization_losses()), 1) + self.assertEqual(len(losses.get_regularization_losses()), 1) with variable_scope.variable_scope('outer', reuse=False): _layers.fully_connected(inputs, 32, weights_regularizer=regularizer) self.assertEqual( len(ops.get_collection(ops.GraphKeys.REGULARIZATION_LOSSES)), 2) - self.assertEqual(len(loss_ops.get_regularization_losses()), 2) + self.assertEqual(len(losses.get_regularization_losses()), 2) with variable_scope.variable_scope('outer', reuse=True): _layers.fully_connected(inputs, 32, weights_regularizer=regularizer) self.assertEqual( len(ops.get_collection(ops.GraphKeys.REGULARIZATION_LOSSES)), 2) - self.assertEqual(len(loss_ops.get_regularization_losses()), 2) + self.assertEqual(len(losses.get_regularization_losses()), 2) def testCreateFCWithoutActivation(self): height, width = 3, 3 @@ -2771,6 +2841,76 @@ class MaxPool2DTest(test.TestCase): self.assertListEqual(output.get_shape().as_list(), [5, 1, 1, 3]) +class MaxPool3DTest(test.TestCase): + + def testInvalidDataFormat(self): + depth, height, width = 3, 6, 9 + images = np.random.uniform(size=(5, depth, height, width, 3)) + with self.assertRaisesRegexp(ValueError, + 'data_format has to be either NCDHW or NDHWC.'): + _layers.max_pool3d(images, [3, 3, 3], data_format='CDHWN') + + def testCreateMaxPool(self): + depth, height, width = 3, 6, 9 + images = np.random.uniform(size=(5, depth, height, width, 3)).astype(np.float32) + output = _layers.max_pool3d(images, [3, 3, 3]) + self.assertEqual(output.op.name, 'MaxPool3D/MaxPool3D') + self.assertListEqual(output.get_shape().as_list(), [5, 1, 2, 4, 3]) + + def testCreateMaxPoolNCDHW(self): + depth, height, width = 3, 6, 9 + images = np.random.uniform(size=(5, 3, depth, height, width)).astype(np.float32) + output = _layers.max_pool3d(images, [3, 3, 3], data_format='NCDHW') + self.assertEquals(output.op.name, 'MaxPool3D/transpose_1') + self.assertListEqual(output.get_shape().as_list(), [5, 3, 1, 2, 4]) + + def testCollectOutputs(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.max_pool3d(images, [3, 3, 3], outputs_collections='outputs') + output_collected = ops.get_collection('outputs')[0] + self.assertEqual(output_collected.aliases, ['MaxPool3D']) + self.assertEqual(output_collected, output) + + def testCreateSquareMaxPool(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.max_pool3d(images, 3) + self.assertEqual(output.op.name, 'MaxPool3D/MaxPool3D') + self.assertListEqual(output.get_shape().as_list(), [5, 1, 2, 4, 3]) + + def testCreateMaxPoolWithScope(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.max_pool3d(images, [3, 3, 3], scope='pool1') + self.assertEqual(output.op.name, 'pool1/MaxPool3D') + + def testCreateMaxPoolWithSamePadding(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.max_pool3d(images, [3, 3, 3], padding='SAME') + self.assertListEqual(output.get_shape().as_list(), [5, 2, 3, 5, 3]) + + def testCreateMaxPoolWithSamePaddingNCDHW(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, 3, depth, height, width), seed=1) + output = _layers.max_pool3d( + images, [3, 3, 3], padding='SAME', data_format='NCDHW') + self.assertListEqual(output.get_shape().as_list(), [5, 3, 2, 3, 5]) + + def testCreateMaxPoolStrideWithSamePadding(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.max_pool3d(images, [3, 3, 3], stride=1, padding='SAME') + self.assertListEqual(output.get_shape().as_list(), [5, depth, height, width, 3]) + + def testGlobalMaxPool(self): + depth, height, width = 3, 6, 9 + images = random_ops.random_uniform((5, depth, height, width, 3), seed=1) + output = _layers.max_pool3d(images, images.get_shape()[1:4], stride=1) + self.assertListEqual(output.get_shape().as_list(), [5, 1, 1, 1, 3]) + + class OneHotEncodingTest(test.TestCase): def testOneHotEncodingCreate(self): |