【问题标题】:how to get the shape of the input in each node in tensorflow graph?如何获取张量流图中每个节点的输入形状?
【发布时间】:2018-03-22 03:16:28
【问题描述】:

嗨:现在我正在将一个 tensorflow 检查点模型转换为一个 caffe 模型。我已成功读取图表并提取了每个节点中的 attr 值。我在“Conv2D”节点中获得了“dilations”、“strides”和“padding”属性的值以及“weights”节点中的形状,但我无法获得“shape”属性的值,它在 Conv2D 的输入中为空节点。但是,这些形状显示在 tensorboard 的图表中。 这是我的代码:

new_saver = tf.train.import_meta_graph(meta_path)          
new_saver.restore(sess, tf.train.latest_checkpoint(ckpt_path))
graph_def = sess.graph_def
node_list = graph_def.node

# conv_node, weight_node, from_node are all in node_list
# conv_node: the conv2d node in graph_def
# weight_node: the weights node of conv2d
# from_node: the input feature map node of conv2d

weight_shape_attr = weight_node.attr['shape']
weight_shapes = [dim.size for dim in weight_shape_attr.shape.dim]

strides = [ii for ii in conv_node.attr['strides'].list.i]
dilations = [ii for ii in conv_node.attr['dilations'].list.i]

shapes = from_node.attr['shape']  # this is empty

和张量板图: tensorboard_graph

请注意,Conv2D 节点的输入形状为 ?x79x79x32,它必须存储在模型文件的某个位置。有谁能帮忙吗?任何点击都会有帮助,谢谢。

【问题讨论】:

    标签: python tensorflow caffe


    【解决方案1】:

    Tensorflow 图有 as_graph_def 方法,该方法具有可选参数 add_shapes(默认为 False)。如果设置为True,则会产生节点的附加属性:_output_shapes

    所以你可以尝试通过这种方式获取 GraphDef:

    graph_def = sess.graph.as_graph_def(add_shapes=True)
    

    【讨论】:

    • 我看到属性_output_shape 现在存在但是如何打印所有节点的形状?它不起作用:print([n._output_shapes for n in tf.get_default_graph().as_graph_def(add_shapes=True).node])
    • @Primoz, _output_shapes 是一个属性,因此应该像shapes = node.attr['_output_shapes'] 一样访问它,它提供了另一个可以向下导航到整数的protobuf 对象,如下所示:shapes.list.shape[0].dim[0].size。可能有更方便的方式来访问值,但我不知道。
    • 我在 python API 中没有看到add_shapes
    猜你喜欢
    • 1970-01-01
    • 2018-12-13
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 2018-06-16
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多