def test_single_example_correct(self): # The correct label should have low cross entropy. correct_xent = util.mnist_cross_entropy(real_digit(), one_hot_real()) # The incorrect label should have high cross entropy. wrong_xent = util.mnist_cross_entropy(real_digit(), one_hot1()) # A random digit should have medium cross entropy for any label. fake_xent1 = util.mnist_cross_entropy(fake_digit(), one_hot_real()) fake_xent6 = util.mnist_cross_entropy(fake_digit(), one_hot1()) with self.cached_session() as sess: self.assertNear(0.00996, sess.run(correct_xent), 1e-5) self.assertNear(18.63073, sess.run(wrong_xent), 1e-5) self.assertNear(2.2, sess.run(fake_xent1), 1e-1) self.assertNear(2.2, sess.run(fake_xent6), 1e-1)
def _disabled_test_minibatch_correct(self): """Tests correctness of the mnist_cross_entropy function with batches.""" # Disabled since it requires loading the tfhub MNIST module. # Reorded minibatches should have the same value. xent1 = util.mnist_cross_entropy( tf.concat([real_digit(), real_digit(), fake_digit()], 0), tf.concat([one_hot_real(), one_hot1(), one_hot1()], 0)) xent2 = util.mnist_cross_entropy( tf.concat([real_digit(), fake_digit(), real_digit()], 0), tf.concat([one_hot_real(), one_hot1(), one_hot1()], 0)) with self.cached_session() as sess: self.assertNear(6.972539, sess.run(xent1), 1e-5) self.assertNear(sess.run(xent1), sess.run(xent2), 1e-5)
def test_minibatch_correct(self): if tf.executing_eagerly(): # `run_image_classifier` doesn't work in eager. return # Reorded minibatches should have the same value. xent1 = util.mnist_cross_entropy( tf.concat([real_digit(), real_digit(), fake_digit()], 0), tf.concat([one_hot_real(), one_hot1(), one_hot1()], 0)) xent2 = util.mnist_cross_entropy( tf.concat([real_digit(), fake_digit(), real_digit()], 0), tf.concat([one_hot_real(), one_hot1(), one_hot1()], 0)) with self.cached_session() as sess: self.assertNear(6.972539, sess.run(xent1), 1e-5) self.assertNear(sess.run(xent1), sess.run(xent2), 1e-5)
def _disabled_test_single_example_correct(self): """Tests correctness of the mnist_cross_entropy function.""" # Disabled since it requires loading the tfhub MNIST module. # The correct label should have low cross entropy. correct_xent = util.mnist_cross_entropy(real_digit(), one_hot_real()) # The incorrect label should have high cross entropy. wrong_xent = util.mnist_cross_entropy(real_digit(), one_hot1()) # A random digit should have medium cross entropy for any label. fake_xent1 = util.mnist_cross_entropy(fake_digit(), one_hot_real()) fake_xent6 = util.mnist_cross_entropy(fake_digit(), one_hot1()) with self.cached_session() as sess: self.assertNear(0.00996, sess.run(correct_xent), 1e-5) self.assertNear(18.63073, sess.run(wrong_xent), 1e-5) self.assertNear(2.2, sess.run(fake_xent1), 1e-1) self.assertNear(2.2, sess.run(fake_xent6), 1e-1)
def test_minibatch_correct(self): # Reorded minibatches should have the same value. xent1 = util.mnist_cross_entropy( tf.concat([real_digit(), real_digit(), fake_digit()], 0), tf.concat([one_hot_real(), one_hot1(), one_hot1()], 0)) xent2 = util.mnist_cross_entropy( tf.concat([real_digit(), fake_digit(), real_digit()], 0), tf.concat([one_hot_real(), one_hot1(), one_hot1()], 0)) with self.cached_session() as sess: self.assertNear(6.972539, sess.run(xent1), 1e-5) self.assertNear(sess.run(xent1), sess.run(xent2), 1e-5)
def test_deterministic(self): xent = util.mnist_cross_entropy(real_digit(), one_hot_real()) with self.cached_session() as sess: ent1 = sess.run(xent) ent2 = sess.run(xent) self.assertEqual(ent1, ent2) with self.cached_session() as sess: ent3 = sess.run(xent) self.assertEqual(ent1, ent3)
def test_deterministic(self, mock_tfhub_load): mock_tfhub_load.return_value = fake_logit_fn xent = util.mnist_cross_entropy(real_digit(), one_hot_real()) with self.cached_session() as sess: ent1 = sess.run(xent) ent2 = sess.run(xent) self.assertEqual(ent1, ent2) with self.cached_session() as sess: ent3 = sess.run(xent) self.assertEqual(ent1, ent3)
def test_deterministic(self): if tf.executing_eagerly(): # `run_image_classifier` doesn't work in eager. return xent = util.mnist_cross_entropy(real_digit(), one_hot_real()) with self.cached_session() as sess: ent1 = sess.run(xent) ent2 = sess.run(xent) self.assertEqual(ent1, ent2) with self.cached_session() as sess: ent3 = sess.run(xent) self.assertEqual(ent1, ent3)
def evaluate(hparams, run_eval_loop=True): """Runs an evaluation loop. Args: hparams: An HParams instance containing the eval hyperparameters. run_eval_loop: Whether to run the full eval loop. Set to False for testing. """ with tf.compat.v1.name_scope('inputs'): noise, one_hot_labels = _get_generator_inputs( hparams.num_images_per_class, NUM_CLASSES, hparams.noise_dims) # Generate images. with tf.compat.v1.variable_scope( 'Generator'): # Same scope as in train job. images = networks.conditional_generator((noise, one_hot_labels), is_training=False) # Visualize images. reshaped_img = tfgan.eval.image_reshaper( images, num_cols=hparams.num_images_per_class) tf.compat.v1.summary.image('generated_images', reshaped_img, max_outputs=1) # Calculate evaluation metrics. tf.compat.v1.summary.scalar( 'MNIST_Classifier_score', util.mnist_score(images, hparams.classifier_filename)) tf.compat.v1.summary.scalar( 'MNIST_Cross_entropy', util.mnist_cross_entropy(images, one_hot_labels, hparams.classifier_filename)) # Write images to disk. image_write_ops = None if hparams.write_to_disk: image_write_ops = tf.io.write_file( '%s/%s' % (hparams.eval_dir, 'conditional_gan.png'), tf.image.encode_png( data_provider.float_image_to_uint8(reshaped_img[0]))) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return evaluation.evaluate_repeatedly( hparams.checkpoint_dir, hooks=[ evaluation.SummaryAtEndHook(hparams.eval_dir), evaluation.StopAfterNEvalsHook(1) ], eval_ops=image_write_ops, max_number_of_evaluations=hparams.max_number_of_evaluations)
def test_any_batch_size(self, mock_tfhub_load): mock_tfhub_load.return_value = fake_logit_fn # Create a graph since placeholders don't work in eager execution mode. with tf.Graph().as_default(): num_classes = 10 one_label = np.array([[1] + [0] * (num_classes - 1)]) inputs = tf.placeholder(tf.float32, shape=[None, 28, 28, 1]) one_hot_label = tf.placeholder(tf.int32, shape=[None, num_classes]) entropy = util.mnist_cross_entropy(inputs, one_hot_label) for batch_size in [4, 16, 30]: with self.cached_session() as sess: sess.run(entropy, feed_dict={ inputs: np.zeros([batch_size, 28, 28, 1]), one_hot_label: np.concatenate([one_label] * batch_size)})
def test_any_batch_size(self): if tf.executing_eagerly(): # Placeholders don't work in eager execution mode. return num_classes = 10 one_label = np.array([[1] + [0] * (num_classes - 1)]) inputs = tf.compat.v1.placeholder(tf.float32, shape=[None, 28, 28, 1]) one_hot_label = tf.compat.v1.placeholder( tf.int32, shape=[None, num_classes]) entropy = util.mnist_cross_entropy(inputs, one_hot_label) for batch_size in [4, 16, 30]: with self.cached_session() as sess: sess.run(entropy, feed_dict={ inputs: np.zeros([batch_size, 28, 28, 1]), one_hot_label: np.concatenate([one_label] * batch_size)})