od_graph_def = tf。GraphDef() AttributeError: module 'tensorflow' 没有属性'GraphDef'



我有一个Mac,我正在使用tensorflow 2.0,python 3.7。 我正在按照教程为实时应用程序创建对象检测模型。 但是我收到以下错误:

"Downloads/models/research/object_detection/object_detection_tutorial.py", line 43, in <module>
od_graph_def = tf
od_graph_def = tf.GraphDef()
AttributeError: module 'tensorflow' has no attribute 'GraphDef'

以下是教程链接:

我检查了环境,我已经在蟒蛇中拥有张量流环境

import tensorflow as tf
import zipfile

from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image

sys.path.append("..")
from object_detection.utils import ops as utils_ops

from utils import label_map_util

from utils import visualization_utils as vis_util
MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

NUM_CLASSES = 90

opener = urllib.request.URLopener()
opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():
file_name = os.path.basename(file.name)
if 'frozen_inference_graph.pb' in file_name:
tar_file.extract(file, os.getcwd())

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

是的,语法在 T2.0 中发生了变化。这是正确的部分:

tf.compat.v1.GraphDef()   # -> instead of tf.GraphDef()
tf.compat.v2.io.gfile.GFile()   # -> instead of tf.gfile.GFile()

升级到 Python 3.7 和 Tensorflow 1.2.0 到 Tensorflow 2.0.0 时,我遇到了类似的问题

如果您不想接触您的代码,只需在带有 Tensorflow 代码的 main.py 文件中添加以下 2 行:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

就是这样!!>NOW 一切都应该无缝运行:)

但是如果你写新代码,确实(如上所述(改变这些调用:

with tf.gfile.GFile(path, 'r') as fid:

自:

with tf.io.gfile.GFile(path, 'r') as fid:
from object_detection.utils import ops as utils_ops
utils_ops.tf = tf.compat.v1
tf.gfile = tf.io.gfile

添加这些行可能会解决您的问题

最新更新