Exemple #1
0
 def testUnknownShape(self):
     if tf.executing_eagerly():
         # Placeholders don't work in eager execution mode.
         return
     inputs = tf.compat.v1.placeholder(tf.float32)
     with self.assertRaisesRegexp(ValueError, 'undefined rank'):
         norm.instance_norm(inputs)
Exemple #2
0
 def testParamsShapeNotFullyDefinedNHWC(self):
   if tf.executing_eagerly():
     # Placeholders don't work in eager execution mode.
     return
   inputs = tf.compat.v1.placeholder(tf.float32, shape=(3, 4, None))
   with self.assertRaisesRegexp(ValueError, 'undefined channels dimension'):
     norm.instance_norm(inputs, data_format='NHWC')
Exemple #3
0
 def testCreateVariables(self):
     if tf.executing_eagerly():
         # Collections don't work with eager.
         return
     height, width = 3, 3
     images = tf.random.uniform((5, height, width, 3), seed=1)
     norm.instance_norm(images, center=True, scale=True)
     self.assertLen(contrib_utils.get_variables_by_name('beta'), 1)
     self.assertLen(contrib_utils.get_variables_by_name('gamma'), 1)
Exemple #4
0
 def testReuseVariables(self):
     if tf.executing_eagerly():
         # Variable reuse doesn't work with eager.
         return
     height, width = 3, 3
     images = tf.random.uniform((5, height, width, 3), seed=1)
     norm.instance_norm(images, scale=True, scope='IN')
     norm.instance_norm(images, scale=True, scope='IN', reuse=True)
     self.assertLen(contrib_utils.get_variables_by_name('beta'), 1)
     self.assertLen(contrib_utils.get_variables_by_name('gamma'), 1)
Exemple #5
0
 def testValueCorrectWithReuseVars(self):
     height, width = 3, 3
     image_shape = (10, height, width, 3)
     images = tf.random.uniform(image_shape, seed=1)
     output_train = norm.instance_norm(images, scope='IN')
     output_eval = norm.instance_norm(images, scope='IN', reuse=True)
     with self.cached_session() as sess:
         sess.run(tf.compat.v1.global_variables_initializer())
         # output_train and output_eval should be the same.
         train_np, eval_np = sess.run([output_train, output_eval])
         self.assertAllClose(train_np, eval_np)
Exemple #6
0
 def testCreateOpFloat64(self):
     height, width = 3, 3
     images = tf.random.uniform((5, height, width, 3),
                                dtype=tf.float64,
                                seed=1)
     output = norm.instance_norm(images)
     self.assertListEqual([5, height, width, 3], output.shape.as_list())
Exemple #7
0
    def doOutputTest(self, input_shape, data_format, tol=1e-3):
        axis = -1 if data_format == 'NHWC' else 1
        for mu in (0.0, 1e2):
            for sigma in (1.0, 0.1):
                # Determine shape of Tensor after norm.
                reduced_shape = (input_shape[0], input_shape[axis])
                expected_mean = np.zeros(reduced_shape)
                expected_var = np.ones(reduced_shape)

                # Determine axes that will be normalized.
                reduced_axes = list(range(len(input_shape)))
                del reduced_axes[axis]
                del reduced_axes[0]
                reduced_axes = tuple(reduced_axes)

                inputs = tf.random.uniform(input_shape, seed=0) * sigma + mu
                output_op = norm.instance_norm(inputs,
                                               center=False,
                                               scale=False,
                                               data_format=data_format)
                with self.cached_session() as sess:
                    sess.run(tf.compat.v1.global_variables_initializer())
                    outputs = sess.run(output_op)
                    # Make sure that there are no NaNs
                    self.assertFalse(np.isnan(outputs).any())
                    mean = np.mean(outputs, axis=reduced_axes)
                    var = np.var(outputs, axis=reduced_axes)
                    # The mean and variance of each example should be close to 0 and 1
                    # respectively.
                    self.assertAllClose(expected_mean,
                                        mean,
                                        rtol=tol,
                                        atol=tol)
                    self.assertAllClose(expected_var, var, rtol=tol, atol=tol)
Exemple #8
0
 def testCreateOpNoScaleCenter(self):
   if tf.executing_eagerly():
     # Collections don't work with eager.
     return
   height, width = 3, 3
   images = tf.random.uniform((5, height, width, 3), dtype=tf.float64, seed=1)
   output = norm.instance_norm(images, center=False, scale=False)
   self.assertListEqual([5, height, width, 3], output.shape.as_list())
   self.assertEmpty(contrib_utils.get_variables_by_name('beta'))
   self.assertEmpty(contrib_utils.get_variables_by_name('gamma'))
Exemple #9
0
 def testBadDataFormat(self):
     inputs = tf.zeros((2, 5, 5), dtype=tf.float32)
     with self.assertRaisesRegexp(
             ValueError, 'data_format has to be either NCHW or NHWC.'):
         norm.instance_norm(inputs, data_format='NHCW')