首页 > 解决方案 > 如何将 Python Tensorflow Session Run Call 转换为 Tensorflow 的 C++ API

问题描述

我想弄清楚如何调用 Tensorflow 的 C++ API。

我有以下 Python 代码:

with open("logfile.txt", "w") as logFile:
    logFile.write("detection_boxes=" + str(detection_boxes))
    logFile.write("detection_scores=" + str(detection_scores))
    logFile.write("detection_classes=" + str(detection_classes))
    logFile.write("num_detections=" + str(num_detections))
    logFile.write("image_tensor=" + str(image_tensor))

(boxes, scores, classes, num) = sess.run(
    [detection_boxes, detection_scores, detection_classes, num_detections],
    feed_dict={image_tensor: image_np_expanded})

其中 detection_boxes、detection_scores、detection_classes、num_detections 和 image_tensor 是张量。当 str() 应用于 print 语句时,它们如下所示:

detection_boxes=Tensor("detection_boxes:0", dtype=float32)
detection_scores=Tensor("detection_scores:0", dtype=float32)
detection_classes=Tensor("detection_classes:0", dtype=float32)
num_detections=Tensor("num_detections:0", dtype=float32)
image_tensor=Tensor("image_tensor:0", shape=(?, ?, ?, 3), dtype=uint8)

但是,似乎 Tensorflow 的 C++ API 的 Session Run() 调用具有不同的签名。在 session.h 中,

虚拟状态运行(const std::vector >& 输入,const std::vector& output_tensor_names,const std::vector& target_node_names,std::vector* 输出)= 0;

似乎 C++ API 需要向量而不是张量的列表/集合。

如何调用具有与 Python 代码等效参数的 C++ API?

非常感谢您提前提供的帮助!

标签: pythonc++tensorflow

解决方案


推荐阅读