如何在tensorflow.js中实现get_tensor_by_name并进行预测

时间:2018-10-03 08:49:08

标签: tensorflow.js

我想使用Faster rcnn inception v2在tensorflow.js中进行对象检测。但是我无法在tfjs中找到诸如get_tensor_by_name之类的方法,并且无法运行会话进行预测。

在tensorflow(python)中,code如下:

定义输入和输出节点:

# Definite input Tensors for detection_graph
self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')

# Definite output Tensors for detection_graph
self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')

预测:

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

有人知道如何在tfjs中实现这两部分代码吗?

请帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

tensorflow.Js中没有session.run函数,而Python中没有。在python中,您开始定义图形,然后在run函数中执行图形。张量和变量在图中被分配了值,但是图中仅定义了计算流程,不包含任何值。真正的计算在您运行会话时发生。一个人可以创建许多会话,每个会话可以为变量分配不同的值,这就是为什么图具有get_by_tensor_name的原因,该输出输出张量,其名称作为参数给出。

您在Js中没有相同的机制。您可以在定义变量后立即使用该变量。这意味着,每当您定义新的张量或变量时,都可以在下一行中打印它,而在python中,您只能在会话期间打印张量或变量。 get_by_tensor_name在Js中确实没有意义。

对于预测函数,您也确实有一个Js。如果您使用tf.modeltf.sequential创建模型,则可以调用predict进行预测。