SSSxCCC / AlphaZero-In-Unity

Several board game AI, which are trained based on AlphaZero, used in a Unity game.
MIT License
39 stars 15 forks source link

Models Save/Restore #3

Closed WishMasterGit closed 4 years ago

WishMasterGit commented 5 years ago

Have a question, how did you converted models from tensorflow to bytes files in unity? Can't figure out this step. Because this code saves 4 files.

    def save_model(self, model_path):
      self.saver.save(self.session, model_path)
SSSxCCC commented 5 years ago

Python Project/AlphaZero/utility/merge_model.py:

import tensorflow as tf

if __name__ == "__main__":
    with tf.Session() as sess:
        game_dir = "Gobang"
        model_dir = "model2_10_10_5"
        batch = "11000"
        sess.run(tf.global_variables_initializer())
        latest_ckpt = tf.train.latest_checkpoint("../" + game_dir + "/" + model_dir + "/" + batch)
        restore_saver = tf.train.import_meta_graph("../" + game_dir + "/" + model_dir + "/" + batch + "/policy_value_net.model.meta")
        restore_saver.restore(sess, latest_ckpt)
        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess, sess.graph_def, ["action_fc/LogSoftmax", "evaluation_fc2/Tanh"])
        tf.train.write_graph(output_graph_def, "../" + game_dir + "/" + model_dir + "/" + batch, "graph.bytes", as_text=False)

This is the code that converts those files into one byte file.