def testCreateConvNCHW(self): height, width = 7, 9 with self.cached_session(): images = np.random.uniform(size=(5, 4, height, width)).astype(np.float32) output = conv2d_ws.conv2d(images, 32, [3, 3], data_format='NCHW') self.assertEqual(output.op.name, 'Conv/Relu') self.assertListEqual(output.get_shape().as_list(), [5, 32, height, width]) weights = contrib_framework.get_variables_by_name('weights')[0] self.assertListEqual(weights.get_shape().as_list(), [3, 3, 4, 32]) biases = contrib_framework.get_variables_by_name('biases')[0] self.assertListEqual(biases.get_shape().as_list(), [32])
def testCreateVerticalConv(self): height, width = 7, 9 with self.cached_session(): images = tf.random_uniform((5, height, width, 4), seed=1) output = conv2d_ws.conv2d(images, 32, [3, 1]) self.assertEqual(output.op.name, 'Conv/Relu') self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32]) weights = contrib_framework.get_variables_by_name('weights')[0] self.assertListEqual(weights.get_shape().as_list(), [3, 1, 4, 32]) biases = contrib_framework.get_variables_by_name('biases')[0] self.assertListEqual(biases.get_shape().as_list(), [32])
def testLocalTrainOp(self): g = tf.Graph() with g.as_default(): tf.set_random_seed(0) tf_inputs = tf.constant(self._inputs, dtype=tf.float32) tf_labels = tf.constant(self._labels, dtype=tf.float32) model_fn = BatchNormClassifier model_args = (tf_inputs, tf_labels) deploy_config = model_deploy.DeploymentConfig(num_clones=2, clone_on_cpu=True) optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) self.assertEqual(slim.get_variables(), []) model = model_deploy.deploy(deploy_config, model_fn, model_args, optimizer=optimizer) update_ops = tf.compat.v1.get_collection( tf.compat.v1.GraphKeys.UPDATE_OPS) self.assertEqual(len(update_ops), 4) self.assertEqual(len(model.clones), 2) self.assertEqual(model.total_loss.op.name, 'total_loss') self.assertEqual(model.summary_op.op.name, 'summary_op/summary_op') self.assertEqual(model.train_op.op.name, 'train_op') with tf.Session() as sess: sess.run(tf.global_variables_initializer()) moving_mean = contrib_framework.get_variables_by_name( 'moving_mean')[0] moving_variance = contrib_framework.get_variables_by_name( 'moving_variance')[0] initial_loss = sess.run(model.total_loss) initial_mean, initial_variance = sess.run( [moving_mean, moving_variance]) self.assertAllClose(initial_mean, [0.0, 0.0, 0.0, 0.0]) self.assertAllClose(initial_variance, [1.0, 1.0, 1.0, 1.0]) for _ in range(10): sess.run(model.train_op) final_loss = sess.run(model.total_loss) self.assertLess(final_loss, initial_loss / 5.0) final_mean, final_variance = sess.run( [moving_mean, moving_variance]) expected_mean = np.array([0.125, 0.25, 0.375, 0.25]) expected_var = np.array([0.109375, 0.1875, 0.234375, 0.1875]) expected_var = self._addBesselsCorrection(16, expected_var) self.assertAllClose(final_mean, expected_mean) self.assertAllClose(final_variance, expected_var)
def testCreateFullyConv(self): height, width = 7, 9 with self.cached_session(): images = tf.random_uniform((5, height, width, 32), seed=1) output = conv2d_ws.conv2d( images, 64, images.get_shape()[1:3], padding='VALID') self.assertEqual(output.op.name, 'Conv/Relu') self.assertListEqual(output.get_shape().as_list(), [5, 1, 1, 64]) biases = contrib_framework.get_variables_by_name('biases')[0] self.assertListEqual(biases.get_shape().as_list(), [64])
def testCreateConvWithWD(self): height, width = 7, 9 weight_decay = 0.01 with self.cached_session() as sess: images = tf.random_uniform((5, height, width, 3), seed=1) regularizer = contrib_layers.l2_regularizer(weight_decay) conv2d_ws.conv2d(images, 32, [3, 3], weights_regularizer=regularizer) l2_loss = tf.nn.l2_loss( contrib_framework.get_variables_by_name('weights')[0]) wd = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)[0] self.assertEqual(wd.op.name, 'Conv/kernel/Regularizer/l2_regularizer') sess.run(tf.global_variables_initializer()) self.assertAlmostEqual(sess.run(wd), weight_decay * l2_loss.eval())
def _get_l2_loss_by_names(self, loss, names): for name in names: weights_list = get_variables_by_name(name) for weights in weights_list: loss += tf.nn.l2_loss(weights) * self.__penal_coef return loss