将Tensorflow Object Detection API模型结果转换为掩码张量

时间:2017-11-29 10:26:18

标签: python tensorflow object-detection object-detection-api

任务:

我尝试将通过Tensorflow Object Detection API训练和导出的模型集成到一个更大的图形中,该图形将模型结果转换为检测模板;基本上是与输入图像相同(高度×宽度)尺寸的float32张量,其中每个值反映模型对相应像素位于检测到的对象的边界框内的置信度。这与分割网络的输出非常相似,除了输出我没有查找尺寸(高度x宽度x num_classes),它是尺寸(高度x宽度),就好像那里一样只检测到一个类。我想在Tensorflow图中完全这样做。

我是如何努力完成的:

我尝试使用tf.boolean_mask()和tf.foldl()从检测模型返回的框边界逐步构建蒙版。注意,在这个例子中,掩码值是二进制的;为了使问题简单,我拿出了通过检测置信度来扩展结果的代码。

 def __init__(self, graph_protobuf, threshold=0.6):
    #Import code omitted...
    #Grab the model detection results:
    boxes = tf.squeeze(detection_graph.get_tensor_by_name('detection_boxes:0'), axis=0)
    scores = tf.squeeze(detection_graph.get_tensor_by_name('detection_scores:0'), axis=0)
    classes = tf.squeeze(detection_graph.get_tensor_by_name('detection_classes:0'), axis=0)
    num_detect = detection_graph.get_tensor_by_name('num_detections:0')
    #Only consider boxes above a provided threshold:
    over_threshold = tf.greater(scores, threshold)
    detections = tf.logical_and(over_threshold, tf.equal(classes, 1))

    #Debugging: Print out what the model result dimensions are.
    test = detection_graph.get_tensor_by_name('detection_scores:0')
    print("Shape of mask: {}  Shape of scores: {}   Shape of classes: {}  Raw score shape: {}".format(str(detections.shape), str(scores.shape), str(classes.shape), str(test.shape)))
    print("Boxes: {}".format(str(test)))

    #Remove under-threshold results:
    detection_boxes = tf.boolean_mask(boxes, detections)
    #Build the mask:
    self.result = tf.foldl(processBox, detection_boxes, initializer=tf.zeros([720,1280]))

 def processBox(self, res, box):
    res[box[0]:box[2],box[1]:box[3]].assign(tf.ones(((box[2]-box[0]),(box[3]-box[1]))))

问题:

我认为Tensorflow在模型返回的未知数量的盒子方面遇到了麻烦。以下是尝试使用此图表的结果:

 Shape of mask: <unknown>  Shape of scores: <unknown>   Shape of classes: <unknown>  Raw score shape: <unknown>
 Boxes: Tensor("detection_scores:0", dtype=float32)

 <...uninteresting stack trace omitted...>

 File "/home/username/.local/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1121, in boolean_mask
"Number of mask dimensions must be specified, even if some dimensions"
ValueError: Number of mask dimensions must be specified, even if some dimensions are None.  E.g. shape=[None] is ok, but shape=None is not.

关于tf.boolean_mask()的抱怨。如您所见,在运行时我根本无法获取模型输出尺寸。 tf.boolean_mask()与tf.constants作为输入正常工作,因此这使我得出结论,这种方法可能存在一些基本问题。

有没有办法让这种方法有效?如果没有,建立掩码结果的正确方法是什么?我正在寻找?

0 个答案:

没有答案