实现tensorflow对象检测模型时出错

Error in implemeting tensorflow object detection model

我是使用tensorflow学习对象检测的新手,我正在参考这个网站实现一个模型https://www.mygreatlearning.com/blog/object-detection-using-tensorflow/我已经按照所有步骤操作但是有两个突出的错误一次又一次地出现,第一个是

"INFO:tensorflow:Saver 未创建,因为图中没有要恢复的变量" 当运行这两个命令

model_name = 'ssd_inception_v2_coco_2017_11_17'
detection_model = load_model(model_name)

另一个错误是 TypeError: int() 参数必须是字符串、类字节对象或数字,而不是 'Tensor' 在 运行 这些

for image_path in TEST_IMAGE_PATHS:
    print(image_path)
    show_inference(detection_model, image_path)

这是调用堆栈:

models\research\object_detection\test_images\image1.jpg
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-e230477d58de> in <module>
      1 for image_path in TEST_IMAGE_PATHS:
      2     print(image_path)
----> 3     show_inference(detection_model, image_path)

<ipython-input-18-a9efdd893038> in show_inference(model, image_path)
      7     # Actual detection.
      8 
----> 9     output_dict = run_inference_for_single_image(model, image_np)
     10     # Visualization of the results of a detection.
     11     vis_util.visualize_boxes_and_labels_on_image_array(

<ipython-input-17-7172365aecd9> in run_inference_for_single_image(model, image)
     12     # Convert to numpy arrays, and take index [0] to remove the batch dimension.
     13     # We're only interested in the first num_detections.
---> 14     num_detections = int(output_dict.pop('num_detections'))
     15 
     16     #num_detections = int(tf.get_static_value(output_dict.pop('num_detections'))[0])

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'

有些回答说要从 dict 中删除张量,但我不完全知道如果要从那里删除它们,应该从字典中删除它们,然后尝试降级或升级 tensorflow。 在注释第 14 行并在取消注释后使用第 16 行时会出现错误 None 不可订阅。我还将 tf.gfile 更改为 tf.io.gfile 。 降级后我使用的是tensorflow 1.15.0版

关于第一个错误: 您可能使用了您提到的 blog-post 中的以下代码:

def load_model(model_name):
  base_url = 'http://download.tensorflow.org/models/object_detection/'
  model_file = model_name + '.tar.gz'
  model_dir = tf.keras.utils.get_file(
    fname=model_name, 
    origin=base_url + model_file,
    untar=True)
 
  model_dir = pathlib.Path(model_dir)/"saved_model"
 
  model = tf.saved_model.load(str(model_dir))
 
  return model

使用调试器检查模型是否正在从 url 下载。

关于第二个错误:

output_dict = run_inference_for_single_image(model, image_np)

似乎是 return 一个以张量为值的字典。

尝试:

num_detections = int(output_dict.pop('num_detections').numpy())