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)
Exemplo n.º 2
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)
 def test_get_image_grid(self):
     util.get_image_grid(tf.zeros([6, 28, 28, 1]),
                         batch_size=6,
                         num_classes=3,
                         num_images_per_class=1)
Exemplo n.º 4
0
def main(_, run_eval_loop=True):

  if FLAGS.conditional_eval:
    FLAGS.checkpoint_dir = os.path.join(FLAGS.checkpoint_dir, 'conditional')
  else:
    FLAGS.checkpoint_dir = os.path.join(FLAGS.checkpoint_dir, 'unconditional')

  print('Checkpoint dir: {}'.format(FLAGS.checkpoint_dir))

  # 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)

  model_name = 'gan_cifar_'
  if FLAGS.conditional_eval:
    model_name = model_name + 'cond'
  else:
    model_name = model_name + 'uncond'

  sess = tf.Session()
  saver = tf.train.Saver()
  if not restore_from_checkpoint(sess, saver):
    raise NotImplementedError

  options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
  run_metadata = tf.RunMetadata()
  sess.run(generated_data, options=options, run_metadata=run_metadata)
  cg = CompGraph(model_name, run_metadata, tf.get_default_graph())

  cg_tensor_dict = cg.get_tensors()
  cg_sorted_keys = sorted(cg_tensor_dict.keys())
  cg_sorted_items = []
  for cg_key in cg_sorted_keys:
    cg_sorted_items.append(tf.shape(cg_tensor_dict[cg_key]))

  cg_sorted_shape = sess.run(cg_sorted_items)
  cg.op_analysis(dict(zip(cg_sorted_keys, cg_sorted_shape)),
                 '{}.pickle'.format(model_name))

  exit(0)
  # 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:
    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:
      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)
Exemplo n.º 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))