从移动的目录加载tensorflow模型 [英] Load tensorflow model from moved directory

查看:123
本文介绍了从移动的目录加载tensorflow模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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/'))

好,代码很简单。
并使用原始路径加载tensorflow模型可以很好地工作。

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

但是问题是如果我移动tensorflow模型(包括.index,.meta,checkpoint )到其他路径会给出错误

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;没有这样的
文件或目录

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.

如何加载从原始目录移动的tensorflow模型?

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

推荐答案

好,我终于设法加载了移动/复制的模型。

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

如果您正在使用


tf.train.latest_checkpoint

tf.train.latest_checkpoint

,则加载目标文件必须与创建时所在的目录相同。否则,您必须打开文件 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.

我的建议是请勿使用


tf.train.latest_checkpoint

在加载模型时将保存的模型移动或复制到另一个目录/系统。

只需使用此


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

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

然后它将加载模型。

要清楚,如果您尝试像下面那样加载

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

然后必须将'checkpoint'文件修改为新的目录路径。

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

否则,只需

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

======================= = b = b = b = b = b = b
我发现了另一个人们应该知道的问题。
不知何故,如果我在Windows中训练(未检查linux或mac osx),则检查点文件会使用绝对路径写入其路径。

========================================================== 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.

如果尝试从其他系统加载模型时,由于找不到Windows目录系统(以c:/或d:/等开头)写入的绝对目录路径,因此找不到要加载的正确目录路径

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)

我的检查点示例如下。


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\__1。 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:
$ b d:/Projects_data/emulator_data/NEW/cnn_21category_char\tf_ckpt\_loss_1.357984_accuracy_0.5358-2700

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 $ b $ all_model_checkpoint_paths: _loss_1.359645_accuracy_0.5363-2400
all_model_checkpoint_paths: _loss_1.357984_accuracy_0.5358-2700

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.

这篇关于从移动的目录加载tensorflow模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆