od_graph_def = tf.GraphDef() AttributeError: module 'tensorflow' has no attribute 'GraphDef'

Iyad Alsulaiman picture Iyad Alsulaiman · Aug 22, 2019 · Viewed 36.9k times · Source

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

Answer

Pranzell picture Pranzell · Oct 3, 2019

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