How to implement get_tensor_by_name and predict in

2019-08-04 16:26发布

问题:

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.

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

Define input and output node:

# 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')

Predict:

(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})

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

Please help. Thank you!

回答1:

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.

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.

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.