Load tensorflow model from moved directory

南楼画角 提交于 2019-12-23 02:07:09

问题


saver = tf.train.import_meta_graph(filepath)
tf.reset_default_graph()
sess = tf.Session()
saver.restore(sess, tf.train.latest_checkpoint('/home/deep_learning_tests/tensorflow/'))

Ok, the code is simple. And loading tensorflow model with the original path works perfectly.

But the problem is that if I move the tensorflow model (including .index, .meta, checkpoint) to other path it gives error

tensorflow.python.framework.errors_impl.NotFoundError: /home/deep_learning_tests/tensorflow/d:/labtest/tensorflow; No such file or directory

It tries to find the original file path. If the original file path still has the model (meaning model is only copied to the new directory), it works. But if the original file directory is deleted and only new directory remains, it gives the above error.

How can I load the tensorflow model that is moved from the original directory?


回答1:


Just want to add something. I used the saver.restore(sess, 'path_to_dir') to restore, it didn't work. I don't have .ckpt file in path_to_dir, what I have are 3 files: model_name.index, model_name.meta, model_name.data. So the way I solved my problem is to use saver.restore(sess, 'path_to_dir/model_name').




回答2:


Ok, I finally managed how to load the moved/copied model.

If you are using

tf.train.latest_checkpoint

then the loading target file must be in the same directory as it was created. Otherwise you must open the file 'checkpoint' and modify the directory path in the file. It works but not recommended.

My recommendation is that DO NOT USE

tf.train.latest_checkpoint

to load the model when the saved model is moved or copied to another directory / system.

Just use this

saver.restore(sess, 'path/to/file')

Then it will load the model.

To be clear, if you are trying to load like below

saver = tf.train.import_meta_graph(filepath)
tf.reset_default_graph()
sess = tf.Session()
saver.restore(sess, tf.train.latest_checkpoint('file/path/to/new/directory'))

then you must modify 'checkpoint' file to the new directory path.

Otherwise, just do

saver = tf.train.import_meta_graph(filepath)
tf.reset_default_graph()
sess = tf.Session()
saver.restore(sess, 'file/path/to/new/directory')

========================================================== I found another problem that people should know. Somehow, if I train in windows (didn't check linux or mac osx), checkpoint file writes its path with absolute path.

So if you are trying to load the model from some other system it will not find the correct directory path to load since it is looking for the absolute directory path written by windows directory system (which starts with c:/ or d:/ etc)

My checkpoint example is as follow.

model_checkpoint_path: "d:/Projects_data/emulator_data/NEW/cnn_21category_char\tf_ckpt\_loss_1.357984_accuracy_0.5358-2700" all_model_checkpoint_paths: "d:/Projects_data/emulator_data/NEW/cnn_21category_char\tf_ckpt\_loss_1.403583_accuracy_0.5247-1500" all_model_checkpoint_paths: "d:/Projects_data/emulator_data/NEW/cnn_21category_char\tf_ckpt\_loss_1.385835_accuracy_0.5302-1800" all_model_checkpoint_paths: "d:/Projects_data/emulator_data/NEW/cnn_21category_char\tf_ckpt\_loss_1.375068_accuracy_0.5334-2100" all_model_checkpoint_paths: "d:/Projects_data/emulator_data/NEW/cnn_21category_char\tf_ckpt\_loss_1.359645_accuracy_0.5363-2400" all_model_checkpoint_paths: "d:/Projects_data/emulator_data/NEW/cnn_21category_char\tf_ckpt\_loss_1.357984_accuracy_0.5358-2700"

If you want it to be read, then you must change it to relative path like below:

model_checkpoint_path: "_loss_1.357984_accuracy_0.5358-2700" all_model_checkpoint_paths: "_loss_1.403583_accuracy_0.5247-1500" all_model_checkpoint_paths: "_loss_1.385835_accuracy_0.5302-1800" all_model_checkpoint_paths: "_loss_1.375068_accuracy_0.5334-2100" all_model_checkpoint_paths: "_loss_1.359645_accuracy_0.5363-2400" all_model_checkpoint_paths: "_loss_1.357984_accuracy_0.5358-2700"

Then it will work. So I would recommend to check the checkpoint file if it is written as the absolute path.



来源:https://stackoverflow.com/questions/50391464/load-tensorflow-model-from-moved-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!