【问题标题】:Tensorflow : How to get tensor name from Tensorboard?Tensorflow:如何从 Tensorboard 获取张量名称?
【发布时间】:2020-01-05 20:46:21
【问题描述】:

我从张量流检测模型动物园下载了一个ssd_mobilenet_v2_coco。我使用import_pb_to_tensorboard.py 在 Tensorboard 上显示结构。

我找到了一个名为“image_tensor”的节点this is the picture discribed in Tensorboard

我想使用函数 'get_tensor_by_name()' 输入新图像并获取输出。但是,它失败了。

我试过 'get_operation_by_name()' ,也没有用。

代码如下:

import tensorflow as tf

def one_image(im_path, model_path):
    sess= tf.Session()
    with sess.as_default():
        image_tensor = tf.image.decode_jpeg(tf.read_file(im_path), channels=3)
        saver = tf.train.import_meta_graph(model_path + "/model.ckpt.meta")
        saver.restore(sess, tf.train.latest_checkpoint(model_path))
        graph = tf.get_default_graph()

        # x = graph.get_tensor_by_name("import/image_tensor:0")
        # out_put = graph.get_tensor_by_name("import/detection_classes:0")

        x = graph.get_operation_by_name("import/image_tensor").outputs[0]
        outputs = graph.get_operation_by_name("import/detection_classes").outputs[0]
        out_put = sess.run(outputs, feed_dict={x: image_tensor.eval()})

        print(out_put)
        sess.close()

if __name__ == "__main__":
    one_image("testimg-4-resize.jpg", "ssd_mobilenet_v2_coco_2018_03_29")

这是 KeyError:

KeyError: "The name 'import/image_tensor' refers to an Operation not in the graph."

我想知道如何从 Tensorboard 获取张量名称,以及是否有另一种方法可以从“only-ckpts”加载模型。

'only-ckpts' 表示文件仅包含 'model.ckpt.data-00000-of-00001' , 'model.ckpt.index' 和 'model.ckpt.meta'。

任何建议将不胜感激。提前谢谢你。

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案1】:

    工具import_pb_to_tensorboard.pyuses tf.import_graph_def to import the graph 并使用默认的name 参数,即"import" as documented

    您的代码通过tf.train.import_meta_graph 导入图形并使用默认的import_scope 参数,该参数不会为导入的张量或操作名称添加前缀。很明显,您有两个选项可以纠正此错误:

    1. 执行以下操作代替您的 import_meta_graph 行:

      saver = tf.train.import_meta_graph(model_path + "/model.ckpt.meta",
                                         import_scope='import')
      
    2. 在尝试按名称获取张量或操作时删除 import/ 前缀,如下所示:

      x = graph.get_tensor_by_name("image_tensor:0")
      out_put = graph.get_tensor_by_name("detection_classes:0")
      
      x = graph.get_operation_by_name("image_tensor").outputs[0]
      outputs = graph.get_operation_by_name("detection_classes").outputs[0]
      

    【讨论】:

      猜你喜欢
      • 2016-08-05
      • 2020-04-12
      • 1970-01-01
      • 2019-08-27
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多