如何在 tensorflow.js 中实现 get_tensor_by_name 和预测 [英] How to implement get_tensor_by_name and predict in tensorflow.js

查看:27
本文介绍了如何在 tensorflow.js 中实现 get_tensor_by_name 和预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 更快的 rcnn inception v2在 tensorflow.js 中进行对象检测.但是我在 tfjs 中找不到一些方法,例如 get_tensor_by_name 和 session run 进行预测.

I want to use Faster rcnn inception v2 to do object detection in tensorflow.js. But i can't find some method in tfjs like get_tensor_by_name and session run for prediction.

在 tensorflow (python) 中,代码为以下内容:

In tensorflow (python), the code as the following:

定义输入输出节点:

# 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 中实现这两部分代码吗?

Do anyone know how to implement those two part of code in tfjs?

请帮忙.谢谢!

推荐答案

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

You don't have a session.run function in tensorflow.Js as there is in Python. In python, you start defining a graph and in the run function, you execute the graph. Tensors and Variables are assigned values in the graph, but the graph defines only the flow of computation, it does not hold any values. The real computation occurs when you run the session. One can create many session where each session can assign different values to the variable, that is why the graph has a get_by_tensor_name which outputs the tensor whose name is given as parameter.

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

You don't have the same mechanism in Js. You can use the variable as soon as you define them. It means that whenever you define a new tensor or variable, you can print it in the following line whereas in python, you can only print the tensor or variable only during a session. The get_by_tensor_name does not really have a sense in Js.

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

As for the predict function, you do have one in Js as well. if you create a model using tf.model or tf.sequential, you can invoke predict to make a prediction.

这篇关于如何在 tensorflow.js 中实现 get_tensor_by_name 和预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆