od_graph_def = tf.GraphDef() AttributeError: modul

2020-07-02 10:40发布

问题:

I have a mac and I am using tensorflow 2.0, python 3.7. I am following the tutorial for creating an object detection model for real-time application. but i am getting the following error:

"Downloads/models/research/object_detection/object_detection_tutorial.py", line 43, in od_graph_def = tf od_graph_def = tf.GraphDef()

AttributeError: module 'tensorflow' has no attribute 'GraphDef'

below is the tutorial link:

I checked the environment and I already have tensorflow environment in anaconda

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='')

回答1:

Yeah, the syntax has changed in T2.0. Here's the correct piece:

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


回答2:

I had similar issues, when upgraded to Python 3.7 and Tensorflow 1.2.0 to Tensorflow 2.0.0

If you don't want to touch your code, just add these 2 lines in the main.py file w/ Tensorflow code:

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

And that's it!!
NOW Everything should run seamlessly :)

But if you write new code, indeed (as was mentioned above) change, these calls:

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

To:

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


回答3:

from object_detection.utils import ops as utils_ops
utils_ops.tf = tf.compat.v1
tf.gfile = tf.io.gfile

Adding these lines might fix your problem