def test_get_frechet_inception_distance(self, mock_fid):
     mock_fid.return_value = 1.0
     util.get_frechet_inception_distance(
         tf.placeholder(tf.float32, shape=[None, 28, 28, 3]),
         tf.placeholder(tf.float32, shape=[None, 28, 28, 3]),
         batch_size=100,
         num_inception_images=10)
 def test_get_frechet_inception_distance(self):
     # Mock `frechet_inception_distance` which is expensive.
     with mock.patch.object(util.tfgan.eval,
                            'frechet_inception_distance') as mock_fid:
         mock_fid.return_value = 1.0
         util.get_frechet_inception_distance(
             tf.placeholder(tf.float32, shape=[None, 28, 28, 3]),
             tf.placeholder(tf.float32, shape=[None, 28, 28, 3]),
             batch_size=100,
             num_inception_images=10)
def main(_, run_eval_loop=True):
  # Fetch and generate images to run through Inception.
  with tf.name_scope('inputs'):
    real_data, num_classes = _get_real_data(
        FLAGS.num_images_generated, FLAGS.dataset_dir)
    generated_data = _get_generated_data(
        FLAGS.num_images_generated, FLAGS.conditional_eval, num_classes)

  # Compute Frechet Inception Distance.
  if FLAGS.eval_frechet_inception_distance:
    fid = util.get_frechet_inception_distance(
        real_data, generated_data, FLAGS.num_images_generated,
        FLAGS.num_inception_images)
    tf.summary.scalar('frechet_inception_distance', fid)

  # Compute normal Inception scores.
  if FLAGS.eval_real_images:
    inc_score = util.get_inception_scores(
        real_data, FLAGS.num_images_generated, FLAGS.num_inception_images)
  else:
    inc_score = util.get_inception_scores(
        generated_data, FLAGS.num_images_generated, FLAGS.num_inception_images)
  tf.summary.scalar('inception_score', inc_score)

  # If conditional, display an image grid of difference classes.
  if FLAGS.conditional_eval and not FLAGS.eval_real_images:
    reshaped_imgs = util.get_image_grid(
        generated_data, FLAGS.num_images_generated, num_classes,
        FLAGS.num_images_per_class)
    tf.summary.image('generated_data', reshaped_imgs, max_outputs=1)

  # Create ops that write images to disk.
  image_write_ops = None
  if FLAGS.conditional_eval and FLAGS.write_to_disk:
    reshaped_imgs = util.get_image_grid(
        generated_data, FLAGS.num_images_generated, num_classes,
        FLAGS.num_images_per_class)
    uint8_images = data_provider.float_image_to_uint8(reshaped_imgs)
    image_write_ops = tf.write_file(
        '%s/%s'% (FLAGS.eval_dir, 'conditional_cifar10.png'),
        tf.image.encode_png(uint8_images[0]))
  else:
    if FLAGS.num_images_generated >= 100 and FLAGS.write_to_disk:
      reshaped_imgs = tfgan.eval.image_reshaper(
          generated_data[:100], num_cols=FLAGS.num_images_per_class)
      uint8_images = data_provider.float_image_to_uint8(reshaped_imgs)
      image_write_ops = tf.write_file(
          '%s/%s'% (FLAGS.eval_dir, 'unconditional_cifar10.png'),
          tf.image.encode_png(uint8_images[0]))

  # For unit testing, use `run_eval_loop=False`.
  if not run_eval_loop: return
  tf.contrib.training.evaluate_repeatedly(
      FLAGS.checkpoint_dir,
      master=FLAGS.master,
      hooks=[tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir),
             tf.contrib.training.StopAfterNEvalsHook(1)],
      eval_ops=image_write_ops,
      max_number_of_evaluations=FLAGS.max_number_of_evaluations)
Exemple #4
0
def main(_, run_eval_loop=True):
  # Fetch and generate images to run through Inception.
  with tf.name_scope('inputs'):
    real_data, num_classes = _get_real_data(
        FLAGS.num_images_generated, FLAGS.dataset_dir)
    generated_data = _get_generated_data(
        FLAGS.num_images_generated, FLAGS.conditional_eval, num_classes)

  # Compute Frechet Inception Distance.
  if FLAGS.eval_frechet_inception_distance:
    fid = util.get_frechet_inception_distance(
        real_data, generated_data, FLAGS.num_images_generated,
        FLAGS.num_inception_images)
    tf.summary.scalar('frechet_inception_distance', fid)

  # Compute normal Inception scores.
  if FLAGS.eval_real_images:
    inc_score = util.get_inception_scores(
        real_data, FLAGS.num_images_generated, FLAGS.num_inception_images)
  else:
    inc_score = util.get_inception_scores(
        generated_data, FLAGS.num_images_generated, FLAGS.num_inception_images)
  tf.summary.scalar('inception_score', inc_score)

  # If conditional, display an image grid of difference classes.
  if FLAGS.conditional_eval and not FLAGS.eval_real_images:
    reshaped_imgs = util.get_image_grid(
        generated_data, FLAGS.num_images_generated, num_classes,
        FLAGS.num_images_per_class)
    tf.summary.image('generated_data', reshaped_imgs, max_outputs=1)

  # Create ops that write images to disk.
  image_write_ops = None
  if FLAGS.conditional_eval and FLAGS.write_to_disk:
    reshaped_imgs = util.get_image_grid(
        generated_data, FLAGS.num_images_generated, num_classes,
        FLAGS.num_images_per_class)
    uint8_images = data_provider.float_image_to_uint8(reshaped_imgs)
    image_write_ops = tf.write_file(
        '%s/%s'% (FLAGS.eval_dir, 'conditional_cifar10.png'),
        tf.image.encode_png(uint8_images[0]))
  else:
    if FLAGS.num_images_generated >= 100 and FLAGS.write_to_disk:
      reshaped_imgs = tfgan.eval.image_reshaper(
          generated_data[:100], num_cols=FLAGS.num_images_per_class)
      uint8_images = data_provider.float_image_to_uint8(reshaped_imgs)
      image_write_ops = tf.write_file(
          '%s/%s'% (FLAGS.eval_dir, 'unconditional_cifar10.png'),
          tf.image.encode_png(uint8_images[0]))

  # For unit testing, use `run_eval_loop=False`.
  if not run_eval_loop: return
  tf.contrib.training.evaluate_repeatedly(
      FLAGS.checkpoint_dir,
      master=FLAGS.master,
      hooks=[tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir),
             tf.contrib.training.StopAfterNEvalsHook(1)],
      eval_ops=image_write_ops,
      max_number_of_evaluations=FLAGS.max_number_of_evaluations)
Exemple #5
0
def main(_, run_eval_loop=True):
    # Fetch and generate images to run through Inception.
    with tf.name_scope('inputs'):
        real_data, num_classes = _get_real_data(
            # FLAGS.bs, FLAGS.dataset_dir)
            FLAGS.bs,
            FLAGS.dl)
        generated_data = _get_generated_data(FLAGS.bs, FLAGS.conditional_eval,
                                             num_classes)

    # Compute Frechet Inception Distance.
    if FLAGS.eval_frechet_inception_distance:
        fid = util.get_frechet_inception_distance(real_data, generated_data,
                                                  FLAGS.bs,
                                                  FLAGS.num_inception_images)
        tf.summary.scalar('frechet_inception_distance', fid)

    # Compute normal Inception scores.
    if FLAGS.eval_real_images:
        inc_score = util.get_inception_scores(real_data, FLAGS.bs,
                                              FLAGS.num_inception_images)
    else:
        inc_score = util.get_inception_scores(generated_data, FLAGS.bs,
                                              FLAGS.num_inception_images)
    tf.summary.scalar('inception_score', inc_score)

    # If conditional, display an image grid of difference classes.
    if FLAGS.conditional_eval and not FLAGS.eval_real_images:
        reshaped_imgs = util.get_image_grid(generated_data, FLAGS.bs,
                                            num_classes,
                                            FLAGS.num_images_per_class)
        tf.summary.image('generated_data', reshaped_imgs, max_outputs=1)

    # Create ops that write images to disk.
    image_write_ops = None
    if FLAGS.conditional_eval and FLAGS.write_to_disk:
        reshaped_imgs = util.get_image_grid(generated_data, FLAGS.bs,
                                            num_classes,
                                            FLAGS.num_images_per_class)
        uint8_images = data_provider.float_image_to_uint8(reshaped_imgs)
        image_write_ops = tf.write_file(
            '%s/%s' % (FLAGS.eval_dir, 'conditional_cifar10.png'),
            tf.image.encode_png(uint8_images[0]))
    else:
        if FLAGS.bs >= 100 and FLAGS.write_to_disk:
            reshaped_imgs = tfgan.eval.image_reshaper(
                generated_data[:100], num_cols=FLAGS.num_images_per_class)
            uint8_images = data_provider.float_image_to_uint8(reshaped_imgs)
            image_write_ops = tf.write_file(
                '%s/%s' % (FLAGS.eval_dir, 'unconditional_cifar10.png'),
                tf.image.encode_png(uint8_images[0]))

    # For unit testing, use `run_eval_loop=False`.
    if not run_eval_loop: return

    checkpoint_path = tf_saver.latest_checkpoint(FLAGS.ckpt)
    if (checkpoint_path is None):
        print("error, no checkpoint path")

    # set mkl env
    mkl_setup()
    sess_config = create_config_proto()

    session_creator = monitored_session.ChiefSessionCreator(
        scaffold=None,
        checkpoint_filename_with_path=checkpoint_path,
        master=FLAGS.master,
        config=sess_config)
    with monitored_session.MonitoredSession(session_creator=session_creator,
                                            hooks=None) as session:
        eval_ops = image_write_ops
        for warmup_i in range(FLAGS.nw):
            session.run(eval_ops, feed_dict=None)
        start = time.time()
        for batch_i in range(FLAGS.nb):
            session.run(eval_ops, feed_dict=None)
        end = time.time()
        inference_time = end - start

        print("Batch size:", FLAGS.bs, "\nBatches number:", FLAGS.nb)
        print("Time spent per BATCH: %.4f ms" % (inference_time * 1000 /
                                                 (FLAGS.nb)))
        print("Total samples/sec: %.4f samples/s" %
              (FLAGS.nb * FLAGS.bs / inference_time))