【问题标题】:Feeding a value for placeholder tensor in Tensorflow C++ API在 Tensorflow C++ API 中为占位符张量提供值
【发布时间】:2017-07-12 10:10:13
【问题描述】:

我已经使用 (Tensorflow) Python API 重新训练了 Inception-v3 模型,并通过修改 tensorflow/tensorflow/examples/image_retraining/retrain.py 并在分类前添加了一个 drop out 层,在 .pb 中保存了一个独立的 Graph层:

def nn_layer(input_tensor, input_dim, output_dim, layer_name, activation_name='activation', act=tf.nn.softmax):
# Adding a name scope ensures logical grouping of the layers in the graph.
with tf.name_scope(layer_name):
# This Variable will hold the state of the weights for the layer
with tf.name_scope('weights'):
weights = weight_variable([input_dim, output_dim])
variable_summaries(weights, layer_name + '/weights')
with tf.name_scope('dropout'):
keep_prob = tf.placeholder(tf.float32)
tf.scalar_summary('dropout_keep_probability', keep_prob)
drop = tf.nn.dropout(input_tensor, keep_prob)
variable_summaries(drop, layer_name + '/dropout')
with tf.name_scope('biases'):
biases = bias_variable([output_dim])
variable_summaries(biases, layer_name + '/biases')
preactivate = tf.matmul(drop, weights) + biases
tf.histogram_summary(layer_name + '/pre_activations', preactivate)
with tf.name_scope(activation_name):
activations = act(preactivate)
tf.histogram_summary(layer_name + '/activations', activations)
return preactivate, activations, keep_prob

python中生成预测的代码如下:

softmax_tensor = sess.graph.get_tensor_by_name('final_layer/final_result/Softmax:0')
predictions = sess.run(softmax_tensor, { 'DecodeJpeg/contents:0':image_data, 'final_layer/dropout/Placeholder:0': 1.})

C++对应的python代码如下:

string input_layer = "Mul";
string output_layer = "final_layer/dropout/Placeholder:0";
Status run_status = session->Run({{input_layer, resized_tensor}}, {output_layer}, {}, &outputs);

C++ 代码以以下错误消息结束:

Running model failed: Invalid argument: You must feed a value for placeholder tensor 'final_layer/dropout/Placeholder'

我应该在上面的 C++ 代码中进行哪些更改以消除此错误?换句话说,如何像在 python 代码中一样更改 C++ 代码中的占位符值。很多天以来,我都陷入了这个问题。任何帮助将不胜感激。

【问题讨论】:

    标签: python c++ machine-learning tensorflow deep-learning


    【解决方案1】:

    您的 C++ 代码与您的 Python 代码不同。

    在python中你得到了

    softmax_tensor = sess.graph.get_tensor_by_name('final_layer/final_result/Softmax:0')
    predictions = sess.run(softmax_tensor, { 'DecodeJpeg/contents:0':image_data, 'final_layer/dropout/Placeholder:0': 1.})
    

    因此,您的feed_dict{ 'DecodeJpeg/contents:0':image_data, 'final_layer/dropout/Placeholder:0': 1.}

    这意味着:用image_data 覆盖DecodeJpeg/contents:0 的值,用1. 覆盖final_layer/dropout/Placeholder:0 的值。

    在 C++ 中,你得到了:

    Status run_status = session->Run({{input_layer, resized_tensor}}, {output_layer}, {}, &outputs);
    

    这个,你的feed_dict相当于是第一个输入参数,即:

    {{input_layer, resized_tensor}}
    

    这意味着:用resized_tensor 覆盖input_layer

    第一个问题是您正在尝试覆盖节点Mul,而不是如上所述的节点DecodeJpeg/contents:0

    此外,缺少占位符的覆盖。

    但是,您的 C++ 代码有些混乱,因为您调用了 output_tensor,实际上是 placeholder

    TL;DR

    如果你的 Python 代码对应的应该是这样的

    Status run_status = session->Run({
        {"DecodeJpeg/contents", resized_tensor},
        {"final_layer/dropout/Placeholder", 1f}
    }, {"final_layer/final_result/Softmax"}, {}, &outputs);
    

    意思是:

    resize_tensor 覆盖DecodeJpeg/contents 的节点值。 用 1 覆盖 final_layer/dropout/Placeholder 的节点值。

    获取节点final_layer/final_result/Softmax 的值。 将结果放入outputs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2017-11-02
      • 2017-11-10
      相关资源
      最近更新 更多