maxpumperla / deep_learning_and_the_game_of_go

Code and other material for the book "Deep Learning and the Game of Go"
https://www.manning.com/books/deep-learning-and-the-game-of-go
987 stars 390 forks source link

_pywrap_file_io.RecursivelyCreateDir(compat.path_to_bytes(path)) tensorflow.python.framework.errors_impl.FailedPreconditionError: /tmp/tmp-kerasmodelnblehs96 is not a directory #92

Open furyhawk opened 3 years ago

furyhawk commented 3 years ago

Running in both windows and WSL

python3 init_ac_agent.py --board-size 9 ac_v1.hdf5

produce the following error message

Traceback (most recent call last): File "init_ac_agent.py", line 42, in main() File "init_ac_agent.py", line 38, in main new_agent.serialize(outf) File "/home/furyhawk/deep_learning_and_the_game_of_go/code/dlgo/rl/ac.py", line 100, in serialize kerasutil.save_model_to_hdf5_group(self.model, h5file['model']) File "/home/furyhawk/deep_learning_and_the_game_of_go/code/dlgo/kerasutil.py", line 17, in save_model_to_hdf5_group save_model(model, tempfname) File "/home/furyhawk/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py", line 150, in save_model saved_model_save.save(model, filepath, overwrite, include_optimizer, File "/home/furyhawk/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save.py", line 89, in save saved_nodes, node_paths = save_lib.save_and_return_nodes( File "/home/furyhawk/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py", line 1110, in save_and_return_nodes utils_impl.get_or_create_variables_dir(export_dir) File "/home/furyhawk/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/utils_impl.py", line 220, in get_or_create_variables_dir file_io.recursive_create_dir(variables_dir) File "/home/furyhawk/.local/lib/python3.8/site-packages/tensorflow/python/lib/io/file_io.py", line 499, in recursive_create_dir recursive_create_dir_v2(dirname) File "/home/furyhawk/.local/lib/python3.8/site-packages/tensorflow/python/lib/io/file_io.py", line 514, in recursive_create_dir_v2 _pywrap_file_io.RecursivelyCreateDir(compat.path_to_bytes(path)) tensorflow.python.framework.errors_impl.FailedPreconditionError: /tmp/tmp-kerasmodelnblehs96 is not a directory

furyhawk commented 3 years ago

Oh, it created the output file correctly. However it throw the error in the end.

frogstack commented 3 years ago

This is happening because H5 is no longer the default file type for keras.save_model in TensorFlow 2.0. The new default is to save in TensorFlow SavedModel format, and SavedModel uses a directory as a root, not a file. To update the code from the book to work with recent versions of Keras/TensorFlow, edit kerasutil.py and in the save_model_to_hdf5_group function, in the call to tempfile.mkstemp specify the suffix like so: tempfile.mkstemp(prefix='tmp-kerasmodel', suffix='.h5'). The suffix prompts the save_model function to use H5 instead of SavedModel. Alternately you can specify save_model(model, tempfname, save_format='h5') It only needs to be done in the save_model_to_hdf5_group function, loading will work fine without it.

With that change I was able to save and load agents with no problems. There's some more detail in the Keras Serialization and Saving guide.