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

时间:2017-07-12 13:32:31

标签: tensorflow object-detection

我已成功使用其对象检测API训练并保存了更快的RCNN模型,用于张量流。我现在正试图对代码进行一些推断,从this tutorial中获取一些代码。

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

  

KeyError:“名称'image_tensor:0'指的是没有的Tensor   存在。图中不存在'image_tensor'操作。“

检查点和元图由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."

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

如果您想要示例代码来查找图形的节点名称,请参阅object_detection_tutorial.ipynb,在&#34;将(冻结的)Tensorflow模型加载到内存之后。&#34;块:

表示od_graph_def.node中的节点:     print node.name

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