def testComputeMovingVars(self):
   height, width = 3, 3
   with self.test_session() as sess:
     image_shape = (10, height, width, 3)
     image_values = np.random.rand(*image_shape)
     expected_mean = np.mean(image_values, axis=(0, 1, 2))
     expected_var = np.var(image_values, axis=(0, 1, 2))
     images = tf.constant(image_values, shape=image_shape, dtype=tf.float32)
     output = ops.batch_norm(images, decay=0.1)
     update_ops = tf.get_collection(ops.UPDATE_OPS_COLLECTION)
     with tf.control_dependencies(update_ops):
       barrier = tf.no_op(name='gradient_barrier')
       output = control_flow_ops.with_dependencies([barrier], output)
     # Initialize all variables
     sess.run(tf.global_variables_initializer())
     moving_mean = variables.get_variables('BatchNorm/moving_mean')[0]
     moving_variance = variables.get_variables('BatchNorm/moving_variance')[0]
     mean, variance = sess.run([moving_mean, moving_variance])
     # After initialization moving_mean == 0 and moving_variance == 1.
     self.assertAllClose(mean, [0] * 3)
     self.assertAllClose(variance, [1] * 3)
     for _ in range(10):
       sess.run([output])
     mean = moving_mean.eval()
     variance = moving_variance.eval()
     # After 10 updates with decay 0.1 moving_mean == expected_mean and
     # moving_variance == expected_var.
     self.assertAllClose(mean, expected_mean)
     self.assertAllClose(variance, expected_var)
Exemple #2
0
 def testComputeMovingVars(self):
     height, width = 3, 3
     with self.test_session() as sess:
         image_shape = (10, height, width, 3)
         image_values = np.random.rand(*image_shape)
         expected_mean = np.mean(image_values, axis=(0, 1, 2))
         expected_var = np.var(image_values, axis=(0, 1, 2))
         images = tf.constant(image_values,
                              shape=image_shape,
                              dtype=tf.float32)
         output = ops.batch_norm(images, decay=0.1)
         update_ops = tf.get_collection(ops.UPDATE_OPS_COLLECTION)
         with tf.control_dependencies(update_ops):
             barrier = tf.no_op(name='gradient_barrier')
             output = control_flow_ops.with_dependencies([barrier], output)
         # Initialize all variables
         sess.run(tf.initialize_all_variables())
         moving_mean = variables.get_variables('BatchNorm/moving_mean')[0]
         moving_variance = variables.get_variables(
             'BatchNorm/moving_variance')[0]
         mean, variance = sess.run([moving_mean, moving_variance])
         # After initialization moving_mean == 0 and moving_variance == 1.
         self.assertAllClose(mean, [0] * 3)
         self.assertAllClose(variance, [1] * 3)
         for _ in range(10):
             sess.run([output])
         mean = moving_mean.eval()
         variance = moving_variance.eval()
         # After 10 updates with decay 0.1 moving_mean == expected_mean and
         # moving_variance == expected_var.
         self.assertAllClose(mean, expected_mean)
         self.assertAllClose(variance, expected_var)
 def testReuseVars(self):
   height, width = 3, 3
   with self.test_session() as sess:
     image_shape = (10, height, width, 3)
     image_values = np.random.rand(*image_shape)
     expected_mean = np.mean(image_values, axis=(0, 1, 2))
     expected_var = np.var(image_values, axis=(0, 1, 2))
     images = tf.constant(image_values, shape=image_shape, dtype=tf.float32)
     output = ops.batch_norm(images, decay=0.1, is_training=False)
     update_ops = tf.get_collection(ops.UPDATE_OPS_COLLECTION)
     with tf.control_dependencies(update_ops):
       barrier = tf.no_op(name='gradient_barrier')
       output = control_flow_ops.with_dependencies([barrier], output)
     # Initialize all variables
     sess.run(tf.global_variables_initializer())
     moving_mean = variables.get_variables('BatchNorm/moving_mean')[0]
     moving_variance = variables.get_variables('BatchNorm/moving_variance')[0]
     mean, variance = sess.run([moving_mean, moving_variance])
     # After initialization moving_mean == 0 and moving_variance == 1.
     self.assertAllClose(mean, [0] * 3)
     self.assertAllClose(variance, [1] * 3)
     # Simulate assigment from saver restore.
     init_assigns = [tf.assign(moving_mean, expected_mean),
                     tf.assign(moving_variance, expected_var)]
     sess.run(init_assigns)
     for _ in range(10):
       sess.run([output], {images: np.random.rand(*image_shape)})
     mean = moving_mean.eval()
     variance = moving_variance.eval()
     # Although we feed different images, the moving_mean and moving_variance
     # shouldn't change.
     self.assertAllClose(mean, expected_mean)
     self.assertAllClose(variance, expected_var)
 def testReuseVars(self):
   height, width = 3, 3
   with self.test_session() as sess:
     image_shape = (10, height, width, 3)
     image_values = np.random.rand(*image_shape)
     expected_mean = np.mean(image_values, axis=(0, 1, 2))
     expected_var = np.var(image_values, axis=(0, 1, 2))
     images = tf.constant(image_values, shape=image_shape, dtype=tf.float32)
     output = ops.batch_norm(images, decay=0.1, is_training=False)
     update_ops = tf.get_collection(ops.UPDATE_OPS_COLLECTION)
     with tf.control_dependencies(update_ops):
       barrier = tf.no_op(name='gradient_barrier')
       output = control_flow_ops.with_dependencies([barrier], output)
     # Initialize all variables
     sess.run(tf.initialize_all_variables())
     moving_mean = variables.get_variables('BatchNorm/moving_mean')[0]
     moving_variance = variables.get_variables('BatchNorm/moving_variance')[0]
     mean, variance = sess.run([moving_mean, moving_variance])
     # After initialization moving_mean == 0 and moving_variance == 1.
     self.assertAllClose(mean, [0] * 3)
     self.assertAllClose(variance, [1] * 3)
     # Simulate assigment from saver restore.
     init_assigns = [tf.assign(moving_mean, expected_mean),
                     tf.assign(moving_variance, expected_var)]
     sess.run(init_assigns)
     for _ in range(10):
       sess.run([output], {images: np.random.rand(*image_shape)})
     mean = moving_mean.eval()
     variance = moving_variance.eval()
     # Although we feed different images, the moving_mean and moving_variance
     # shouldn't change.
     self.assertAllClose(mean, expected_mean)
     self.assertAllClose(variance, expected_var)
Exemple #5
0
 def testNonReuseVars(self):
     height, width = 3, 3
     with self.test_session():
         images = tf.random_uniform((5, height, width, 3), seed=1)
         ops.conv2d(images, 32, [3, 3])
         self.assertEqual(len(variables.get_variables()), 2)
         ops.conv2d(images, 32, [3, 3])
         self.assertEqual(len(variables.get_variables()), 4)
Exemple #6
0
 def testNonReuseVars(self):
     height, width = 3, 3
     inputs = tf.random_uniform((5, height * width * 3), seed=1)
     with self.test_session():
         ops.fc(inputs, 32)
         self.assertEqual(len(variables.get_variables('FC')), 2)
         ops.fc(inputs, 32)
         self.assertEqual(len(variables.get_variables('FC')), 4)
 def testReuseVars(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     ops.conv2d(images, 32, [3, 3], scope='conv1')
     self.assertEquals(len(variables.get_variables()), 2)
     ops.conv2d(images, 32, [3, 3], scope='conv1', reuse=True)
     self.assertEquals(len(variables.get_variables()), 2)
Exemple #8
0
 def testGetVariablesSuffix(self):
     with self.test_session():
         with tf.variable_scope("A"):
             a = variables.variable("a", [5])
         with tf.variable_scope("A"):
             b = variables.variable("b", [5])
         self.assertEquals([a], variables.get_variables(suffix="a"))
         self.assertEquals([b], variables.get_variables(suffix="b"))
 def testNonReuseVars(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     ops.conv2d(images, 32, [3, 3])
     self.assertEquals(len(variables.get_variables()), 2)
     ops.conv2d(images, 32, [3, 3])
     self.assertEquals(len(variables.get_variables()), 4)
 def testReuseVars(self):
   height, width = 3, 3
   inputs = tf.random_uniform((5, height * width * 3), seed=1)
   with self.test_session():
     ops.fc(inputs, 32, scope='fc1')
     self.assertEquals(len(variables.get_variables('fc1')), 2)
     ops.fc(inputs, 32, scope='fc1', reuse=True)
     self.assertEquals(len(variables.get_variables('fc1')), 2)
 def testNonReuseVars(self):
   height, width = 3, 3
   inputs = tf.random_uniform((5, height * width * 3), seed=1)
   with self.test_session():
     ops.fc(inputs, 32)
     self.assertEquals(len(variables.get_variables('FC')), 2)
     ops.fc(inputs, 32)
     self.assertEquals(len(variables.get_variables('FC')), 4)
Exemple #12
0
 def testGetVariablesSuffix(self):
   with self.test_session():
     with tf.variable_scope('A'):
       a = variables.variable('a', [5])
     with tf.variable_scope('A'):
       b = variables.variable('b', [5])
     self.assertEquals([a], variables.get_variables(suffix='a'))
     self.assertEquals([b], variables.get_variables(suffix='b'))
Exemple #13
0
 def testGetVariablesSuffix(self):
     with self.test_session():
         with tf.variable_scope('A'):
             a = variables.variable('a', [5])
         with tf.variable_scope('A'):
             b = variables.variable('b', [5])
         self.assertEquals([a], variables.get_variables(suffix='a'))
         self.assertEquals([b], variables.get_variables(suffix='b'))
Exemple #14
0
 def testReuseVars(self):
     height, width = 3, 3
     inputs = tf.random_uniform((5, height * width * 3), seed=1)
     with self.test_session():
         ops.fc(inputs, 32, scope='fc1')
         self.assertEqual(len(variables.get_variables('fc1')), 2)
         ops.fc(inputs, 32, scope='fc1', reuse=True)
         self.assertEqual(len(variables.get_variables('fc1')), 2)
Exemple #15
0
 def testGetVariables(self):
     with self.test_session():
         with tf.variable_scope('A'):
             a = variables.variable('a', [5])
         with tf.variable_scope('B'):
             b = variables.variable('a', [5])
         self.assertEqual([a, b], variables.get_variables())
         self.assertEqual([a], variables.get_variables('A'))
         self.assertEqual([b], variables.get_variables('B'))
 def testCreateConvCreatesWeightsAndBiasesVars(self):
   height, width = 3, 3
   images = tf.random_uniform((5, height, width, 3), seed=1)
   with self.test_session():
     self.assertFalse(variables.get_variables('conv1/weights'))
     self.assertFalse(variables.get_variables('conv1/biases'))
     ops.conv2d(images, 32, [3, 3], scope='conv1')
     self.assertTrue(variables.get_variables('conv1/weights'))
     self.assertTrue(variables.get_variables('conv1/biases'))
 def testReuseFCWithBatchNorm(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height * width * 3), seed=1)
     with scopes.arg_scope([ops.fc], batch_norm_params={'decay': 0.9}):
       net = ops.fc(images, 27, scope='fc1')
       net = ops.fc(net, 27, scope='fc1', reuse=True)
     self.assertEquals(len(variables.get_variables()), 4)
     self.assertEquals(len(variables.get_variables('fc1/BatchNorm')), 3)
 def testCreateFcCreatesWeightsAndBiasesVars(self):
   height, width = 3, 3
   inputs = tf.random_uniform((5, height * width * 3), seed=1)
   with self.test_session():
     self.assertFalse(variables.get_variables('fc1/weights'))
     self.assertFalse(variables.get_variables('fc1/biases'))
     ops.fc(inputs, 32, scope='fc1')
     self.assertTrue(variables.get_variables('fc1/weights'))
     self.assertTrue(variables.get_variables('fc1/biases'))
Exemple #19
0
 def testCreateFcCreatesWeightsAndBiasesVars(self):
     height, width = 3, 3
     inputs = tf.random_uniform((5, height * width * 3), seed=1)
     with self.test_session():
         self.assertFalse(variables.get_variables('fc1/weights'))
         self.assertFalse(variables.get_variables('fc1/biases'))
         ops.fc(inputs, 32, scope='fc1')
         self.assertTrue(variables.get_variables('fc1/weights'))
         self.assertTrue(variables.get_variables('fc1/biases'))
Exemple #20
0
 def testCreateConvCreatesWeightsAndBiasesVars(self):
     height, width = 3, 3
     images = tf.random_uniform((5, height, width, 3), seed=1)
     with self.test_session():
         self.assertFalse(variables.get_variables('conv1/weights'))
         self.assertFalse(variables.get_variables('conv1/biases'))
         ops.conv2d(images, 32, [3, 3], scope='conv1')
         self.assertTrue(variables.get_variables('conv1/weights'))
         self.assertTrue(variables.get_variables('conv1/biases'))
Exemple #21
0
 def testReuseFCWithBatchNorm(self):
     height, width = 3, 3
     with self.test_session():
         images = tf.random_uniform((5, height * width * 3), seed=1)
         with scopes.arg_scope([ops.fc], batch_norm_params={'decay': 0.9}):
             net = ops.fc(images, 27, scope='fc1')
             net = ops.fc(net, 27, scope='fc1', reuse=True)
         self.assertEqual(len(variables.get_variables()), 4)
         self.assertEqual(len(variables.get_variables('fc1/BatchNorm')), 3)
 def testReuseConvWithBatchNorm(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height, width, 32), seed=1)
     with scopes.arg_scope([ops.conv2d], batch_norm_params={'decay': 0.9}):
       net = ops.conv2d(images, 32, [3, 3], scope='Conv')
       net = ops.conv2d(net, 32, [3, 3], scope='Conv', reuse=True)
     self.assertEquals(len(variables.get_variables()), 4)
     self.assertEquals(len(variables.get_variables('Conv/BatchNorm')), 3)
     self.assertEquals(len(variables.get_variables('Conv_1/BatchNorm')), 0)
Exemple #23
0
 def testFCWithBatchNorm(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height * width * 3), seed=1)
     with scopes.arg_scope([ops.fc], batch_norm_params={}):
       net = ops.fc(images, 32, scope='fc1')
       net = ops.fc(net, 32, scope='fc2')
     self.assertEquals(len(tf.get_collection('moving_vars')), 4)
     self.assertEquals(len(variables.get_variables('fc1/BatchNorm')), 3)
     self.assertEquals(len(variables.get_variables('fc2/BatchNorm')), 3)
Exemple #24
0
 def testFCWithBatchNorm(self):
     height, width = 3, 3
     with self.test_session():
         images = tf.random_uniform((5, height * width * 3), seed=1)
         with scopes.arg_scope([ops.fc], batch_norm_params={}):
             net = ops.fc(images, 27)
             net = ops.fc(net, 27)
         self.assertEqual(len(variables.get_variables()), 8)
         self.assertEqual(len(variables.get_variables('FC/BatchNorm')), 3)
         self.assertEqual(len(variables.get_variables('FC_1/BatchNorm')), 3)
Exemple #25
0
 def testConvWithBatchNorm(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     with scopes.arg_scope([ops.conv2d], batch_norm_params={}):
       net = ops.conv2d(images, 32, [3, 3], scope='conv1')
       net = ops.conv2d(net, 32, [3, 3], scope='conv2')
     self.assertEquals(len(tf.get_collection('moving_vars')), 4)
     self.assertEquals(len(variables.get_variables('conv1/BatchNorm')), 3)
     self.assertEquals(len(variables.get_variables('conv2/BatchNorm')), 3)
 def testFCWithBatchNorm(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height * width * 3), seed=1)
     with scopes.arg_scope([ops.fc], batch_norm_params={}):
       net = ops.fc(images, 27)
       net = ops.fc(net, 27)
     self.assertEquals(len(variables.get_variables()), 8)
     self.assertEquals(len(variables.get_variables('FC/BatchNorm')), 3)
     self.assertEquals(len(variables.get_variables('FC_1/BatchNorm')), 3)
Exemple #27
0
 def testFCWithBatchNorm(self):
     height, width = 3, 3
     with self.test_session():
         images = tf.random_uniform((5, height * width * 3), seed=1)
         with scopes.arg_scope([ops.fc], batch_norm_params={}):
             net = ops.fc(images, 32, scope='fc1')
             net = ops.fc(net, 32, scope='fc2')
         self.assertEquals(len(tf.get_collection('moving_vars')), 4)
         self.assertEquals(len(variables.get_variables('fc1/BatchNorm')), 3)
         self.assertEquals(len(variables.get_variables('fc2/BatchNorm')), 3)
 def testReuseConvWithBatchNorm(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height, width, 32), seed=1)
     with scopes.arg_scope([ops.conv2d], batch_norm_params={'decay': 0.9}):
       net = ops.conv2d(images, 32, [3, 3], scope='Conv')
       net = ops.conv2d(net, 32, [3, 3], scope='Conv', reuse=True)
     self.assertEquals(len(variables.get_variables()), 4)
     self.assertEquals(len(variables.get_variables('Conv/BatchNorm')), 3)
     self.assertEquals(len(variables.get_variables('Conv_1/BatchNorm')), 0)
Exemple #29
0
 def testConvWithBatchNorm(self):
     height, width = 3, 3
     with self.test_session():
         images = tf.random_uniform((5, height, width, 3), seed=1)
         with scopes.arg_scope([ops.conv2d], batch_norm_params={}):
             net = ops.conv2d(images, 32, [3, 3], scope='conv1')
             net = ops.conv2d(net, 32, [3, 3], scope='conv2')
         self.assertEquals(len(tf.get_collection('moving_vars')), 4)
         self.assertEquals(len(variables.get_variables('conv1/BatchNorm')),
                           3)
         self.assertEquals(len(variables.get_variables('conv2/BatchNorm')),
                           3)
Exemple #30
0
 def testReuseFCWithWD(self):
     height, width = 3, 3
     with self.test_session():
         inputs = tf.random_uniform((5, height * width * 3), seed=1)
         ops.fc(inputs, 32, weight_decay=0.01, scope='fc')
         self.assertEqual(len(variables.get_variables()), 2)
         self.assertEqual(
             len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
         ops.fc(inputs, 32, weight_decay=0.01, scope='fc', reuse=True)
         self.assertEqual(len(variables.get_variables()), 2)
         self.assertEqual(
             len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
 def testReuseFCWithWD(self):
   height, width = 3, 3
   with self.test_session():
     inputs = tf.random_uniform((5, height * width * 3), seed=1)
     ops.fc(inputs, 32, weight_decay=0.01, scope='fc')
     self.assertEquals(len(variables.get_variables()), 2)
     self.assertEquals(
         len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
     ops.fc(inputs, 32, weight_decay=0.01, scope='fc', reuse=True)
     self.assertEquals(len(variables.get_variables()), 2)
     self.assertEquals(
         len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
 def testReuseConvWithWD(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     ops.conv2d(images, 32, [3, 3], weight_decay=0.01, scope='conv1')
     self.assertEquals(len(variables.get_variables()), 2)
     self.assertEquals(
         len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
     ops.conv2d(images, 32, [3, 3], weight_decay=0.01, scope='conv1',
                reuse=True)
     self.assertEquals(len(variables.get_variables()), 2)
     self.assertEquals(
         len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
 def testReuseConvWithWD(self):
   height, width = 3, 3
   with self.test_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     ops.conv2d(images, 32, [3, 3], weight_decay=0.01, scope='conv1')
     self.assertEquals(len(variables.get_variables()), 2)
     self.assertEquals(
         len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
     ops.conv2d(images, 32, [3, 3], weight_decay=0.01, scope='conv1',
                reuse=True)
     self.assertEquals(len(variables.get_variables()), 2)
     self.assertEquals(
         len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
Exemple #34
0
 def testNoneGetVariablesToRestore(self):
     with self.test_session():
         with tf.variable_scope('A'):
             a = variables.variable('a', [5], restore=False)
         with tf.variable_scope('B'):
             b = variables.variable('a', [5], restore=False)
         self.assertEquals([], variables.get_variables_to_restore())
         self.assertEquals([a, b], variables.get_variables())
Exemple #35
0
 def testReuseVariable(self):
   with self.test_session():
     with tf.variable_scope('A'):
       a = variables.variable('a', [])
     with tf.variable_scope('A', reuse=True):
       b = variables.variable('a', [])
     self.assertEquals(a, b)
     self.assertListEqual([a], variables.get_variables())
Exemple #36
0
 def testReuseVariable(self):
     with self.test_session():
         with tf.variable_scope('A'):
             a = variables.variable('a', [])
         with tf.variable_scope('A', reuse=True):
             b = variables.variable('a', [])
         self.assertEquals(a, b)
         self.assertListEqual([a], variables.get_variables())
Exemple #37
0
 def testNoneGetVariablesToRestore(self):
   with self.test_session():
     with tf.variable_scope('A'):
       a = variables.variable('a', [5], restore=False)
     with tf.variable_scope('B'):
       b = variables.variable('a', [5], restore=False)
     self.assertEquals([], variables.get_variables_to_restore())
     self.assertEquals([a, b], variables.get_variables())
Exemple #38
0
 def testGetVariablesToRestorePartial(self):
   with self.test_session():
     with tf.variable_scope('A'):
       a = variables.variable('a', [5])
     with tf.variable_scope('B'):
       b = variables.variable('b', [5], restore=False)
     self.assertListEqual([a, b], variables.get_variables())
     self.assertListEqual([a],
                          tf.get_collection(variables.VARIABLES_TO_RESTORE))
Exemple #39
0
 def testGetMixedVariablesToRestore(self):
     with self.test_session():
         with tf.variable_scope("A"):
             a = variables.variable("a", [5])
             b = variables.variable("b", [5], restore=False)
         with tf.variable_scope("B"):
             c = variables.variable("c", [5])
             d = variables.variable("d", [5], restore=False)
         self.assertEquals([a, b, c, d], variables.get_variables())
         self.assertEquals([a, c], variables.get_variables_to_restore())
Exemple #40
0
 def testGetMixedVariablesToRestore(self):
     with self.test_session():
         with tf.variable_scope('A'):
             a = variables.variable('a', [5])
             b = variables.variable('b', [5], restore=False)
         with tf.variable_scope('B'):
             c = variables.variable('c', [5])
             d = variables.variable('d', [5], restore=False)
         self.assertEquals([a, b, c, d], variables.get_variables())
         self.assertEquals([a, c], variables.get_variables_to_restore())
Exemple #41
0
 def testGetMixedVariablesToRestore(self):
   with self.test_session():
     with tf.variable_scope('A'):
       a = variables.variable('a', [5])
       b = variables.variable('b', [5], restore=False)
     with tf.variable_scope('B'):
       c = variables.variable('c', [5])
       d = variables.variable('d', [5], restore=False)
     self.assertEquals([a, b, c, d], variables.get_variables())
     self.assertEquals([a, c], variables.get_variables_to_restore())