Tensorflow 1 到 Tensorflow 2 的转换代码 - detection_graph.as_default() 属性错误

Tensorflow 1 to Tensorflow 2 conversion code - detection_graph.as_default() Attribute Error

我是 运行 这个代码 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

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

在 Tensorflow 1 中使用 like,

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()