Tensorflow对象检测

Tensorflow对象检测

本文介绍了Tensorflow对象检测-避免重叠框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:
我是机器学习的新手,我和同事必须实现一种用于检测交通信号灯的算法。我下载了一个预先训练的模型(更快的rcnn),并运行了几个训练步骤(〜10000)。现在,当使用来自tensorflow git存储库的对象检测算法时,在一个区域中检测到了多个交通信号灯。

Intro:I'm new to machine learning and me and a colleague have to implement an algorithm for detecting traffic lights. I downloaded a pre trained model (faster rcnn) and ran several training steps (~10000). Now when using the object detection algorithm from the tensorflow git repository several traffic lights in one area are detected.

我做了一些研究,发现了函数 tf.image。 non_max_suppression,但我无法使其按预期运行(说实话,我什至无法运行它。)。

I did a little research and found the function "tf.image.non_max_suppression" but I cannot get it to work as intended (to be honest, I cannot even get it to run).

我假设您知道tf对象检测示例代码因此,您还知道所有盒子都使用字典(output_dict)返回。

I assume you know the tf object detection sample code so you also know that all boxes are returned using a dictionary (output_dict).

要清理我使用的盒子:

selected_indices = tf.image.non_max_suppression(
        boxes           = output_dict['detection_boxes'],
        scores          = output_dict['detection_scores'],
        max_output_size = 1,
        iou_threshold   = 0.5,
        score_threshold = float('-inf'),
        name            = None)

起初我以为可以将selected_indices用作新的盒子列表,所以我尝试了以下方法:

At first I thought I could use selected_indices as a new list of boxes so I tried this:

vis_util.visualize_boxes_and_labels_on_image_array(
      image                      = image_np,
      boxes                      = selected_indices,
      classes                    = output_dict['detection_classes'],
      scores                     = output_dict['detection_scores'],
      category_index             = category_index,
      instance_masks             = output_dict.get('detection_masks'),
      use_normalized_coordinates = True)

,但是当我注意到这行不通时,我发现了一个必需的方法: tf.gather()。然后我运行以下代码:

but when I noticed this wont work I found a required method: "tf.gather()". Then I ran the following code:

boxes = output_dict['detection_boxes']
selected_indices = tf.image.non_max_suppression(
    boxes           = boxes,
    scores          = output_dict['detection_scores'],
    max_output_size = 1,
    iou_threshold   = 0.5,
    score_threshold = float('-inf'),
    name            = None)

selected_boxes = tf.gather(boxes, selected_indices)

vis_util.visualize_boxes_and_labels_on_image_array(
      image                      = image_np,
      boxes                      = selected_boxes,
      classes                    = output_dict['detection_classes'],
      scores                     = output_dict['detection_scores'],
      category_index             = category_index,
      instance_masks             = output_dict.get('detection_masks'),
      use_normalized_coordinates = True)

但哪一个都行不通。我在第689行的visualisation_utils.py中收到AttributeError( Tensor对象没有属性 tolist)。

but not even that one works. I receive an AttributeError ('Tensor' object has no attribute 'tolist') in visualization_utils.py on Line 689.

推荐答案

所以它看起来像要以正确的格式显示框,您需要创建一个会话并评估张量,如下所示:

So it looks like to get the boxes in the right format, you need to create a session and evaluate the tensor as follows:

suppressed = tf.image.non_max_suppression(output_dict['detection_boxes'], output_dict['detection_scores'], 5) # Replace 5 with max num desired boxes

sboxes = tf.gather(output_dict['detection_boxes'], suppressed)
sscores = tf.gather(output_dict['detection_scores'], suppressed)
sclasses = tf.gather(output_dict['detection_classes'], suppressed)

sess = tf.Session()
with sess.as_default():
    boxes = sboxes.eval()
    scores =sscores.eval()
    classes = sclasses.eval()

vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      boxes,
      classes,
      scores,
      category_index,
      instance_masks=output_dict.get('detection_masks'),
      use_normalized_coordinates=True,
      line_thickness=8)

这篇关于Tensorflow对象检测-避免重叠框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:01