【问题标题】:Tensorflow object_detection: unable to find input and output tensorsTensorflow object_detection:无法找到输入和输出张量
【发布时间】:2017-12-16 23:15:34
【问题描述】:

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

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

KeyError:“名称‘image_tensor:0’指的是一个不 存在。图中不存在操作‘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."

【问题讨论】:

    标签: tensorflow object-detection


    【解决方案1】:

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

    【讨论】:

    • 非常感谢! :)
    • 我有冻结的模型。我正在尝试将它与 objectdetection api 一起使用。我得到同样的错误
    • 我尝试使用:export_inference_graph.py --input_type image_tensor --pipeline_config_path ./faster_rcnn_resnet101_coco_2018_01_28_VIOREL/pipeline_vio.config --trained_checkpoint_pr efix train_folder\model.ckpt-497555 --output_file train_folder\inference_graph\frozen_inference pb 和我在尝试使用导出的图形时遇到同样的错误
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 2019-08-30
      • 2017-02-06
      相关资源
      最近更新 更多