od_graph_def = tf.GraphDef() AttributeError: 模块“tensorflow”没有属性“GraphDef”

Iya*_*man 28 import tensorflow object-detection-api

我有一台 mac,我使用的是 tensorflow 2.0、python 3.7。我正在关注为实时应用程序创建对象检测模型的教程。但我收到以下错误:

“下载/模型/研究/object_detection/object_detection_tutorial.py”,第 43 行,在 od_graph_def = tf od_graph_def = tf.GraphDef()

AttributeError: 模块“tensorflow”没有属性“GraphDef”

下面是教程链接:

我检查了环境,我已经在 anaconda 中有 tensorflow 环境

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='')
Run Code Online (Sandbox Code Playgroud)

Pra*_*ell 54

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

tf.compat.v1.GraphDef()   # -> instead of tf.GraphDef()
tf.compat.v2.io.gfile.GFile()   # -> instead of tf.gfile.GFile()
Run Code Online (Sandbox Code Playgroud)


Koh*_*001 20

我有类似的问题,当升级到 Python 3.7 和 Tensorflow 1.2.0 到 Tensorflow 2.0.0

如果您不想修改代码,只需在 main.py 文件中添加以下 2 行代码,并使用 Tensorflow 代码:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
Run Code Online (Sandbox Code Playgroud)

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

但是,如果您编写新代码,确实(如上所​​述)更改这些调用:

  with tf.gfile.GFile(path, 'r') as fid:
Run Code Online (Sandbox Code Playgroud)

到:

  with tf.io.gfile.GFile(path, 'r') as fid:
Run Code Online (Sandbox Code Playgroud)