张量流训练后如何使用模型(保存/负载图) [英] how to use model after trained in tensorflow (save/load graph)

查看:108
本文介绍了张量流训练后如何使用模型(保存/负载图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的张量流版本是0.11. 我想在训练后保存图形,或者保存其他可以加载张量流的图形.

My tensorflow version is 0.11. I want to save a graph after training or save something else which tensorflow can load it.

I/使用导出和导入元图

我已经阅读了这篇文章: Tensorflow:如何保存/恢复模型?

I already read this post: Tensorflow: how to save/restore a model?

我的 Save.py 文件:

X = tf.placeholder("float", [None, 28, 28, 1], name='X')
Y = tf.placeholder("float", [None, 10], name='Y')

tf.train.Saver()
with tf.Session() as sess:
     ...run something ...
     final_tensor = tf.nn.softmax(py_x, name='final_result')
     tf.add_to_collection("final_tensor", final_tensor)

     predict_op = tf.argmax(py_x, 1)
     tf.add_to_collection("predict_op", predict_op)

saver.save(sess, 'my_project') 

然后我运行load.py:

Then I run load.py:

with tf.Session() as sess:
   new_saver = tf.train.import_meta_graph('my_project.meta')
   new_saver.restore(sess, 'my_project')
   predict_op = tf.get_collection("predict_op")[0]
   for i in range(2):
        test_indices = np.arange(len(teX)) # Get A Test Batch
        np.random.shuffle(test_indices)
        test_indices = test_indices[0:test_size]

        print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
                         sess.run(predict_op, feed_dict={"X:0": teX[test_indices],
                                                         "p_keep_conv:0": 1.0,
                                                         "p_keep_hidden:0": 1.0})))

但返回错误

Traceback (most recent call last):
  File "load_05_convolution.py", line 62, in <module>
    "p_keep_hidden:0": 1.0})))
  File "/home/khoa/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/home/khoa/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 894, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (256, 784) for Tensor u'X:0', which has shape '(?, 28, 28, 1)'

我真的不知道为什么吗?

I really don't know why?

如果我添加final_tensor = tf.get_collection("final_result")[0]

它返回另一个错误:

Traceback (most recent call last):
  File "load_05_convolution.py", line 46, in <module>
    final_tensor = tf.get_collection("final_result")[0]
IndexError: list index out of range

是因为tf.add_to_collection仅包含一个占位符吗?

Is it because tf.add_to_collection only contains only one place holder ?

II/使用tf.train.write_graph

我将此行添加到save.py的末尾 tf.train.write_graph(graph, 'folder', 'train.pb')

I add this line to the end of the save.py tf.train.write_graph(graph, 'folder', 'train.pb')

成功创建了文件"train.pb"

It created file 'train.pb' successfully

我的 load.py :

with tf.gfile.FastGFile('folder/train.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')

with tf.Session() as sess:
  predict_op = sess.graph.get_tensor_by_name('predict_op:0')
  for i in range(2):
        test_indices = np.arange(len(teX)) # Get A Test Batch
        np.random.shuffle(test_indices)
        test_indices = test_indices[0:test_size]

        print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
                         sess.run(predict_op, feed_dict={"X:0": teX[test_indices],
                                                         "p_keep_conv:0": 1.0,
                                                         "p_keep_hidden:0": 1.0})))

然后返回错误:

Traceback (most recent call last):
  File "load_05_convolution.py", line 22, in <module>
    graph_def.ParseFromString(f.read())
  File "/home/khoa/tensorflow/lib/python2.7/site-packages/google/protobuf/message.py", line 185, in ParseFromString
    self.MergeFromString(serialized)
  File "/home/khoa/tensorflow/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 1085, in MergeFromString
    raise message_mod.DecodeError('Unexpected end-group tag.')
google.protobuf.message.DecodeError: Unexpected end-group tag.

您介意共享用于保存/加载模型的标准方法,代码或教程吗?我真的很困惑.

would you mind sharing the standard way, code or tutorial to save/load model ? I'm really confused.

推荐答案

您的第一个解决方案(使用MetaGraph)几乎可以使用,但是由于您正在喂一批 flattened MNIST培训示例,所以会出现错误.到tf.placeholder(),它期望一批MNIST训练示例为形状为batch_size x height(= 28)x width(= 28)x channels(= 1)的4-D张量.解决此问题的最简单方法是重塑输入数据.代替此语句:

Your first solution (using the MetaGraph) almost works, but the error arises because you are feeding a batch of flattened MNIST training examples to a tf.placeholder() that expects a batch of MNIST training examples as a 4-D tensor with shape batch_size x height (= 28) x width (= 28) x channels (= 1). The easiest way to solve this is to reshape your input data. Instead of this statement:

print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
                 sess.run(predict_op, feed_dict={
                     "X:0": teX[test_indices],
                     "p_keep_conv:0": 1.0,
                     "p_keep_hidden:0": 1.0})))

...尝试使用以下语句,该语句将适当地重塑输入数据:

...try the following statement, which reshapes your input data appropriately, instead:

print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
                 sess.run(predict_op, feed_dict={
                     "X:0": teX[test_indices].reshape(-1, 28, 28, 1),
                     "p_keep_conv:0": 1.0,
                     "p_keep_hidden:0": 1.0})))

这篇关于张量流训练后如何使用模型(保存/负载图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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