Tensorflow object_detection: 无法找到输入和输出张量

Tensorflow object_detection: unable to find input and output tensors

我已经使用他们的对象检测成功地训练并保存了更快的 RCNN 模型用于 tensorflow API。我现在正在尝试 运行 对代码进行一些推断,从 this tutorial.

中获取一些代码

但是,在我成功恢复元图和检查点后,系统找不到输入和输出节点,出现以下错误:

KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."

检查点和元图是由 train.py 脚本根据我自己的数据根据​​ here 给出的说明创建的。

这是我的代码:

OUTPUT_DIR = "my_path/models/SSD_v1/train"
CKPT_DIR = OUTPUT_DIR
LATEST_CKPT_FILENAME = "checkpoint"
LAST_CKPT_FILE = os.path.join(CKPT_DIR, LATEST_CKPT_FILENAME)
MODEL_FILENAME_PATH = os.path.join(OUTPUT_DIR, "model.ckpt.meta")
def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)


def test_model(images_list, path_to_ckpt=None,
               meta_graph=None):
    if path_to_ckpt is None:
        path_to_ckpt = tf.train.latest_checkpoint(CKPT_DIR, LATEST_CKPT_FILENAME)
    if meta_graph is None:
        meta_graph = MODEL_FILENAME_PATH
    print("test_model launched")

    tf.reset_default_graph()
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Restore graph
            saver = tf.train.import_meta_graph(meta_graph, clear_devices=True)
            print('metagraph restored')
            saver.restore(sess, path_to_ckpt)
            print('graph restored')

            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')  # This is where the error happens
            # Each box represents a part of the image where a particular object was detected.
            detected_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detected_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            detected_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = graph.get_tensor_by_name('num_detections:0')

            print("Output tensors: ")
            print(detected_boxes)
            print(detected_scores)
            print(detected_classes)
            print('')

            for i, image in enumerate(images_list):
                detected_boxes, detected_scores, detected_classes, num_detect = sess.run([detected_boxes, detected_scores, detected_classes, num_detections],
                         feed_dict={image_tensor: image})
                print(i, num_detect, detected_boxes, detected_scores, detected_classes)


def main():
    directory_path = "../data/samples/"
    image_files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_list = [ np.expand_dims(load_image_into_numpy_array(Image.open(os.path.join(directory_path, f))), axis=0) for f in image_files]
    test_model(images_list=image_list)

if __name__=="__main__":
    main()

完整的错误堆栈跟踪:

Traceback (most recent call last):   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/pano_faster_rcnn/src/run_faster_rcnn_inference.py", line 99, in <module>
    main()   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/pano_faster_rcnn/src/run_faster_rcnn_inference.py", line 95, in main
    test_model(images_list=image_list)   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/pano_faster_rcnn/src/run_faster_rcnn_inference.py", line 48, in test_model
    image_tensor = graph.get_tensor_by_name('image_tensor:0')   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2733, in get_tensor_by_name
    return self.as_graph_element(name, allow_tensor=True, allow_operation=False)   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2584, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2626, in _as_graph_element_locked
    "graph." % (repr(name), repr(op_name))) KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."

在训练图中,input/output 节点没有给出这些名称。您需要做的是通过 export_inference_graph.py 工具 "export" 您训练好的模型。我相信它目前将其导出到冻结图或 SavedModel,但在未来的版本中,它也会导出到普通检查点。

如果您想要用于查找图形节点名称的示例代码,请参考 object_detection_tutorial.ipynb,在 "Load a (frozen) Tensorflow model into memory." 块之后:

对于 od_graph_def.node 中的节点: 打印 node.name

这应该会列出您可以在后续块中输入的所有节点名称。