示例#1
0
    def testTrainingAndInferenceGraphsAreCompatible(self,
                                                    mock_provide_custom_data,
                                                    unused_mock_gan_train):
        # Training and inference graphs can get out of sync if changes are made
        # to one but not the other. This test will keep them in sync.

        # Save the training graph
        train_sess = tf.Session()
        FLAGS.image_set_x_file_pattern = '/tmp/x/*.jpg'
        FLAGS.image_set_y_file_pattern = '/tmp/y/*.jpg'
        FLAGS.batch_size = 3
        FLAGS.patch_size = 128
        FLAGS.generator_lr = 0.02
        FLAGS.discriminator_lr = 0.3
        FLAGS.train_log_dir = self._export_dir
        FLAGS.master = 'master'
        FLAGS.task = 0
        FLAGS.cycle_consistency_loss_weight = 2.0
        FLAGS.max_number_of_steps = 1
        mock_provide_custom_data.return_value = (tf.zeros([
            3,
            4,
            4,
            3,
        ]), tf.zeros([3, 4, 4, 3]))
        train.main(None)
        init_op = tf.global_variables_initializer()
        train_sess.run(init_op)
        train_saver = tf.train.Saver()
        train_saver.save(train_sess, save_path=self._ckpt_path)

        # Create inference graph
        tf.reset_default_graph()
        FLAGS.patch_dim = FLAGS.patch_size
        logging.info('dir_path: %s', os.listdir(self._export_dir))
        FLAGS.checkpoint_path = self._ckpt_path
        FLAGS.image_set_x_glob = self._image_glob
        FLAGS.image_set_y_glob = self._image_glob
        FLAGS.generated_x_dir = self._genx_dir
        FLAGS.generated_y_dir = self._geny_dir

        inference_demo.main(None)
        logging.info('gen x: %s', os.listdir(self._genx_dir))

        # Check that the image names match
        self.assertSetEqual(set(_basenames_from_glob(FLAGS.image_set_x_glob)),
                            set(os.listdir(FLAGS.generated_y_dir)))
        self.assertSetEqual(set(_basenames_from_glob(FLAGS.image_set_y_glob)),
                            set(os.listdir(FLAGS.generated_x_dir)))

        # Check that each image in the directory looks as expected
        for directory in [FLAGS.generated_x_dir, FLAGS.generated_x_dir]:
            for base_name in os.listdir(directory):
                image_path = os.path.join(directory, base_name)
                self.assertRealisticImage(image_path)
示例#2
0
  def testTrainingAndInferenceGraphsAreCompatible(
      self, mock_provide_custom_data, unused_mock_gan_train):
    # Training and inference graphs can get out of sync if changes are made
    # to one but not the other. This test will keep them in sync.

    # Save the training graph
    train_sess = tf.Session()
    FLAGS.image_set_x_file_pattern = '/tmp/x/*.jpg'
    FLAGS.image_set_y_file_pattern = '/tmp/y/*.jpg'
    FLAGS.batch_size = 3
    FLAGS.patch_size = 128
    FLAGS.generator_lr = 0.02
    FLAGS.discriminator_lr = 0.3
    FLAGS.train_log_dir = self._export_dir
    FLAGS.master = 'master'
    FLAGS.task = 0
    FLAGS.cycle_consistency_loss_weight = 2.0
    FLAGS.max_number_of_steps = 1
    mock_provide_custom_data.return_value = (
        tf.zeros([3, 4, 4, 3,]), tf.zeros([3, 4, 4, 3]))
    train.main(None)
    init_op = tf.global_variables_initializer()
    train_sess.run(init_op)
    train_saver = tf.train.Saver()
    train_saver.save(train_sess, save_path=self._ckpt_path)

    # Create inference graph
    tf.reset_default_graph()
    FLAGS.patch_dim = FLAGS.patch_size
    logging.info('dir_path: %s', os.listdir(self._export_dir))
    FLAGS.checkpoint_path = self._ckpt_path
    FLAGS.image_set_x_glob = self._image_glob
    FLAGS.image_set_y_glob = self._image_glob
    FLAGS.generated_x_dir = self._genx_dir
    FLAGS.generated_y_dir = self._geny_dir

    inference_demo.main(None)
    logging.info('gen x: %s', os.listdir(self._genx_dir))

    # Check that the image names match
    self.assertSetEqual(
        set(_basenames_from_glob(FLAGS.image_set_x_glob)),
        set(os.listdir(FLAGS.generated_y_dir)))
    self.assertSetEqual(
        set(_basenames_from_glob(FLAGS.image_set_y_glob)),
        set(os.listdir(FLAGS.generated_x_dir)))

    # Check that each image in the directory looks as expected
    for directory in [FLAGS.generated_x_dir, FLAGS.generated_x_dir]:
      for base_name in os.listdir(directory):
        image_path = os.path.join(directory, base_name)
        self.assertRealisticImage(image_path)