【问题标题】:Object detction api of Tensorflow does not workTensorflow的对象检测api不起作用
【发布时间】:2020-05-09 09:39:43
【问题描述】:

我一直在尝试使用tensorflow的object detection api。经过很多麻烦,我已经安装了所有必要的模块。然后,当我从 models/research/object_detection 运行“object_detection_tutorial”时,我在检测部分遇到如下错误:

检测

加载一个对象检测模型:

model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
detection_model = load_model(model_name)

tensorflow:Saver 未创建,因为图中没有要恢复的变量

print(detection_model.inputs)

tf.Tensor 'image_tensor:0' shape=(?, ?, ?, 3) dtype=uint8

我再次在下面的行中得到错误:

for image_path in TEST_IMAGE_PATHS:
  show_inference(detection_model, image_path)

错误:

* 在 run_inference_for_single_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   output_dict = {key:value[0, :num_detections].numpy() 
     16                  for key,value in output_dict.items()}

TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 'Tensor'*

请帮助我为什么会收到此错误。我是 tensorflow 的新手,只想成功运行本教程。我已经安装了“Tensorflow 2.2.0”和所有模块。我也对 Tensorflow 低版本做了同样的事情,但同样的问题。

【问题讨论】:

    标签: python tensorflow object-detection


    【解决方案1】:

    试试下面的代码:

    import tensorflow as tf
    import cv2
    import numpy as np
    import os
    import random
    from IPython import embed
    
    IMAGE_PATH = "/Users/vedanshu/tfrecord/dataset"
    
    input_names = ['image_tensor']
    
    def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
                   font_scale=0.5, thickness=2):
        size = cv2.getTextSize(label, font, font_scale, thickness)[0]
        x, y = point
        cv2.rectangle(image, (x, y - size[1]),
                      (x + size[0], y), (255, 0, 0), cv2.FILLED)
        cv2.putText(image, label, point, font, font_scale,
                    (255, 255, 255), thickness)
    
    detection_graph1 = tf.Graph()
    
    with detection_graph1.as_default():
        tf_sess1 = tf.Session(graph=detection_graph1)
        model = tf.saved_model.loader.load(tf_sess1, ["serve"], "/Users/vedanshu/saved_model")
    
    tf_input = tf_sess1.graph.get_tensor_by_name(input_names[0] + ':0')
    tf_scores = tf_sess1.graph.get_tensor_by_name('detection_scores:0')
    tf_boxes = tf_sess1.graph.get_tensor_by_name('detection_boxes:0')
    tf_classes = tf_sess1.graph.get_tensor_by_name('detection_classes:0')
    tf_num_detections = tf_sess1.graph.get_tensor_by_name('num_detections:0')
    
    for img in os.listdir(IMAGE_PATH):
        if img.endswith("jpg"):
            image = os.path.join(IMAGE_PATH, img)
            image = cv2.imread(image)
            _img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            scores, boxes, classes, num_detections = tf_sess1.run([tf_scores, tf_boxes, tf_classes, tf_num_detections], feed_dict={tf_input: _img[None, ...]})
    
            boxes = boxes[0]  # index by 0 to remove batch dimension
            scores = scores[0]
            classes = classes[0]
            num_detections = int(num_detections[0])
    
            # embed()
            boxes_pixels = []
            for i in range(num_detections):
                # scale box to image coordinates
                box = boxes[i] * np.array([image.shape[0], image.shape[1], image.shape[0], image.shape[1]])
                box = np.round(box).astype(int)
                boxes_pixels.append(box)
    
            boxes_pixels = np.array(boxes_pixels)
    
            for i in range(num_detections):
                if scores[i] > 0.15:
                    box = boxes_pixels[i]
                    box = np.round(box).astype(int)
                    (startY, startX, endY, endX) = box.astype("int")
                    boxW = endX - startX
                    boxH = endY - startY
    
                    image = cv2.rectangle(image, (box[1], box[0]), (box[3], box[2]), (0, 255, 0), 2)
                    label = "{}:{:.2f}".format(int(classes[i]), scores[i])
                    draw_label(image, (box[1], box[0]), label)
    
                    cv2.imwrite("/Users/vedanshu/tfrecord/out/"+img, image)
    

    您需要提供saved_model.pb 的位置和图片的位置。

    【讨论】:

    • 脚本在小改动后工作。但是,它不会替换用标签标识的对象。是否需要在某处引用*.pbtxt文件?
    • 相应地更改label = "{}:{:.2f}".format(int(classes[i]), scores[i])这一行。
    • 即使使用此代码,我仍然收到“未创建保护程序,因为图中没有要恢复的变量”以及“指定的 SavedModel 没有变量;没有恢复检查点。”。我正在使用“ssd_mobilenet_v1_coco_2018_01_28”。为什么会出现这个错误?
    • 您的saved_model.pb 有问题。尝试使用download.tensorflow.org/models/object_detection/…
    • @Vedanshu 不走运,即使使用模型链接,我仍然遇到同样的错误
    猜你喜欢
    • 2021-05-16
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2020-06-25
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    相关资源
    最近更新 更多