Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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.
  """
    # Fetch real images.
    with tf.compat.v1.name_scope('inputs'):
        real_images, _ = data_provider.provide_data(
            'train', hparams.num_images_generated, hparams.dataset_dir)

    image_write_ops = None
    if hparams.eval_real_images:
        tf.compat.v1.summary.scalar(
            'MNIST_Classifier_score',
            util.mnist_score(real_images, hparams.classifier_filename))
    else:
        # In order for variables to load, use the same variable scope as in the
        # train job.
        with tf.compat.v1.variable_scope('Generator'):
            images = networks.unconditional_generator(tf.random.normal(
                [hparams.num_images_generated, hparams.noise_dims]),
                                                      is_training=False)
        tf.compat.v1.summary.scalar(
            'MNIST_Frechet_distance',
            util.mnist_frechet_distance(real_images, images,
                                        hparams.classifier_filename))
        tf.compat.v1.summary.scalar(
            'MNIST_Classifier_score',
            util.mnist_score(images, hparams.classifier_filename))
        if hparams.num_images_generated >= 100 and hparams.write_to_disk:
            reshaped_images = tfgan.eval.image_reshaper(images[:100, ...],
                                                        num_cols=10)
            uint8_images = data_provider.float_image_to_uint8(reshaped_images)
            image_write_ops = tf.io.write_file(
                '%s/%s' % (hparams.eval_dir, 'unconditional_gan.png'),
                tf.image.encode_png(uint8_images[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)
Ejemplo n.º 3
0
def _get_write_image_ops(eval_dir, filename, images):
    """Create Ops that write images to disk."""
    return tf.io.write_file(
        '%s/%s' % (eval_dir, filename),
        tf.image.encode_png(data_provider.float_image_to_uint8(images)))