Esempio n. 1
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):
       output = tf.identity(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)
Esempio n. 2
0
 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):
       output = tf.identity(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)
Esempio n. 3
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.assertEquals(len(variables.get_variables('FC')), 2)
     ops.fc(inputs, 32)
     self.assertEquals(len(variables.get_variables('FC')), 4)
Esempio n. 4
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.assertEquals(len(variables.get_variables()), 2)
     ops.conv2d(images, 32, [3, 3])
     self.assertEquals(len(variables.get_variables()), 4)
Esempio n. 5
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.assertEquals(len(variables.get_variables('fc1')), 2)
     ops.fc(inputs, 32, scope='fc1', reuse=True)
     self.assertEquals(len(variables.get_variables('fc1')), 2)
 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'))
Esempio n. 7
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'))
Esempio n. 8
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.assertEquals(len(variables.get_variables()), 4)
     self.assertEquals(len(variables.get_variables('fc1/BatchNorm')), 3)
Esempio n. 9
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'))
Esempio n. 10
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.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)
Esempio n. 11
0
 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)
Esempio n. 12
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.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)
Esempio n. 13
0
 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 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())
 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())
 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())