对象检测教程中是否存在针对“ NameError:名称'detection_graph'未定义”的修复程序?

时间:2019-04-02 19:23:22

标签: tensorflow jupyter-notebook

我一直在以下github存储库中关注该教程:https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10

在开始运行Jupyter笔记本中的最后一段代码之前,我已经能够顺利完成所有步骤:

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    # Definite input and output Tensors for detection_graph
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    # Each box represents a part of the image where a particular object was detected.
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    # Each score represent how level of confidence for each of the objects.
    # Score is shown on the result image, together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')
    for image_path in TEST_IMAGE_PATHS:
      image = Image.open(image_path)
      # the array based representation of the image will be used later in order to prepare the
      # result image with boxes and labels on it.
      image_np = load_image_into_numpy_array(image)
      # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
      image_np_expanded = np.expand_dims(image_np, axis=0)
      # Actual detection.
      (boxes, scores, classes, num) = sess.run(
          [detection_boxes, detection_scores, detection_classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
      # Visualization of the results of a detection.
      vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          np.squeeze(boxes),
          np.squeeze(classes).astype(np.int32),
          np.squeeze(scores),
          category_index,
          use_normalized_coordinates=True,
          line_thickness=8)
      plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)

我收到此错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-7493eea60222> in <module>
----> 1 with detection_graph.as_default():
      2   with tf.Session(graph=detection_graph) as sess:
      3     # Definite input and output Tensors for detection_graph
      4     image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
      5     # Each box represents a part of the image where a particular object was detected.

NameError: name 'detection_graph' is not defined

不是正确输出带有两个检测到的狗,并且在它们周围有边框的图像。

我尝试在开头添加以下代码行:

detection_graph = tf.Graph()

但是那没有用,我最终遇到了另一个错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-88ea994f3b00> in <module>
      3   with tf.Session(graph=detection_graph) as sess:
      4     # Definite input and output Tensors for detection_graph
----> 5     image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
      6     # Each box represents a part of the image where a particular object was detected.
      7     detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

c:\users\ja230262\appdata\local\continuum\anaconda3\envs\tensorflow1\lib\site-packages\tensorflow\python\framework\ops.py in get_tensor_by_name(self, name)
   3497       raise TypeError("Tensor names are strings (or similar), not %s." %
   3498                       type(name).__name__)
-> 3499     return self.as_graph_element(name, allow_tensor=True, allow_operation=False)
   3500 
   3501   def _get_tensor_by_tf_output(self, tf_output):

c:\users\ja230262\appdata\local\continuum\anaconda3\envs\tensorflow1\lib\site-packages\tensorflow\python\framework\ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
   3321 
   3322     with self._lock:
-> 3323       return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
   3324 
   3325   def _as_graph_element_locked(self, obj, allow_tensor, allow_operation):

c:\users\ja230262\appdata\local\continuum\anaconda3\envs\tensorflow1\lib\site-packages\tensorflow\python\framework\ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
   3363           raise KeyError("The name %s refers to a Tensor which does not "
   3364                          "exist. The operation, %s, does not exist in the "
-> 3365                          "graph." % (repr(name), repr(op_name)))
   3366         try:
   3367           return op.outputs[out_n]

KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."

关于如何解决此问题的任何想法?

0 个答案:

没有答案