c++ - 如何使用 Session::Run (TensorFlow C++ API) 以批处理模式执行基于 CNN 的样本分类

标签 c++ tensorflow deep-learning

我想在批处理模式下使用 C++ 中的预训练 CNN 对多个样本进行分类。到目前为止:

  • 我已经在 Python 中使用 Keras 训练了一个 CNN 模型,并使用 is repo ( https://github.com/amir-abdi/keras_to_tensorflow ) 提供的小脚本转换了这个模型。

  • 我能够使用 ReadBinaryProto 函数加载模型并创建一个 Session 对象。

现在我想对 n 输入张量进行分类。以下代码有效,但速度很慢,因为使用 (~20 毫秒) 运行一次非常快,但必须执行大约 20.000 次。

std::vector<tensorflow::Tensor> outputs;
std::vector<tensorflow::Tensor> tensor(tensorflow::DT_FLOAT,
     tensorflow::TensorShape({1, winSize, winSize, nochannels }));;
for (unsigned int n = 0; n < noSamples; n++)
{
    copyDataToTensor(prevImg, prevPoints[n], input_tensor, 0);
    tensorflow::Status run_status = m_Session->Run({ { "conv2d_5_input", tensor} }, { "output_node0" }, {}, &outputs);
    ... evaluate outputs ...
}

winSizenoChannels 是一些与 CNN 输入形状相关的常量。

我正在寻找的是一种以批处理模式运行多个样本的方法,例如:

 std::vector<tensorflow::Tensor> outputs;
 std::vector<tensorflow::Tensor> tensor(tensorflow::DT_FLOAT, 
      tensorflow::TensorShape({noSamples, winSize, winSize, nochannels }));;
 for (unsigned int n = 0; n < noSamples; n++)
 {
    copyDataToTensor(prevImg, prevPoints[n], input_tensor, n);
 }
 tensorflow::Status run_status = m_Session->Run({ { "conv2d_5_input", tensor} }, { "output_node0" }, {}, &outputs);
 ... evaluate outputs ...

但是,这种方法不起作用,因为 outputs 只有一个元素,并且该张量的内容仅存储一个分类结果的结果。

提供 output_tensor_names vector 的 noSamples{"ouput_node0"} 也不起作用。然后 outputs 张量 vector 具有正确的大小,但每个张量都相似。

如何正确使用此函数以批处理模式执行预测?我必须使用另一个界面来解决这个问题吗?

最佳答案

据推测,第二个批处理模式的奇怪行为的原因是使用了来自此 repo (https://github.com/amir-abdi/keras_to_tensorflow) 的 Keras 到 Python 脚本的错误。

一个导致错误结果的小错误,因为它以错误的方式连接输出,而不是连接到 dense_2/Softmax 节点。有问题的代码是:

K.set_learning_phase(0) net_model = load_model(weight_file_path)

pred = [None]*num_output
pred_node_names = [None]*num_output
for i in range(num_output):
     pred_node_names[i] = prefix_output_node_names_of_final_network+str(i)
     pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i]),
print('output nodes names are: ', pred_node_names)
sess = K.get_session()
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
graph_io.write_graph(constant_graph, output_fld, output_graph_name, as_text=False)

在 net_model.output[i] 中似乎有一个小错别字。如果改为使用输出字段 (net_model.outputs[i]),则一切正常。 IE。修复将是:

K.set_learning_phase(0)
net_model = load_model(weight_file_path)

pred = [None]*num_output
pred_node_names = [None]*num_output
for i in range(num_output):
     pred_node_names[i] = prefix_output_node_names_of_final_network+str(i)
     pred[i] = tf.identity(net_model.outputs[i], name=pred_node_names[i]),
print('output nodes names are: ', pred_node_names)
sess = K.get_session()
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
graph_io.write_graph(constant_graph, output_fld, output_graph_name, as_text=False)

关于c++ - 如何使用 Session::Run (TensorFlow C++ API) 以批处理模式执行基于 CNN 的样本分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47391709/

相关文章:

C++ MIDL2025 : Expecting a type specification near ""

c++ - 为什么 "copying"复制构造函数中的引用仍然有效?

python-2.7 - tensorflow /核心/框架/op_kernel.cc :993 Invalid argument: Received a label value of 253 which is outside the valid range of [0, 3)

c++ - 多GPU模式下的tensorflow c++ SetDefaultDevice

python - 尝试连接两个模型并适合 Keras 时出现断言错误

python - 我使用带有 MNISt 数据库的 TenserFlow 进行深度神经网络的 PCA,在数据形状方面出现错误

deep-learning - HUGGINGFACE 类型错误 : '>' not supported between instances of 'NoneType' and 'int'

c++ - 如何使用RAII获取类资源?

c++ - 可以从 QXmlStreamWriter 获取 QDomElement 吗?

tensorflow - 自动编码器的 tensorflow 输入数据字符串