【问题标题】:Tensorflow 1 to Tensorflow 2 conversion code - detection_graph.as_default() Attribute ErrorTensorflow 1 到 Tensorflow 2 的转换代码 - detection_graph.as_default() 属性错误
【发布时间】:2021-09-21 20:07:58
【问题描述】:

我正在使用TensorFlow1 运行此代码

detection_graph = tf.Graph()
with detection_graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

由于它不适用于TensorFlow2,我将代码转换为According to this

detection_graph = tf.compat.v1.GraphDef()
with detection_graph.as_default():
  od_graph_def = tf.compat.v1.GraphDef()
  with tf.compat.v2.io.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

但它会触发以下错误:

with detection_graph.as_default():
AttributeError: as_default

有谁知道如何解决这个问题?

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    在 TensorFlow 1 中使用类似,

    import tensorflow as tf
    print(tf.__version__)
    detection_graph = tf.compat.v1.get_default_graph()
    with detection_graph.as_default():
      od_graph_def = tf.GraphDef()
    

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 2020-10-28
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 2022-11-24
      相关资源
      最近更新 更多