如何在 get_tensor_by_name 中实施和预测 tensorflow.js

How to implement get_tensor_by_name and predict in 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 中实现这两部分代码吗?

请帮忙。谢谢!

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

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

至于预测功能,你在Js中也有。如果您使用 tf.modeltf.sequential 创建模型,则可以调用 predict 进行预测。