object_detection的bounding box在graph中的操作例子(求IOU,排序等)

在物体检测(object detection)任务中,box的操作是一个比较麻烦的事情,特别是要在tensorflow的graph中实现这些操作。

tensorflow考到这点,也提供了一些工具函数。本篇文章就用一个例子来引导大家使用这些工具。

首先是 BoxList类:存储多个box信息的数据结构

box_list_ops:提供了很多操作BoxList的方法,比如计算iou,排序等等

这两个py文件地址:https://github.com/tensorflow/models/tree/master/research/object_detection/core

import tensorflow as tf
from core import box_list
from core import box_list_ops
boxes1=tf.convert_to_tensor([[0.25, 0.25, 0.5, 0.5],[0.25, 0.25, 0.4, 0.5]]) #cx, cy,width, height
xmin=boxes1[:,0]-boxes1[:,2]/2
xmax=boxes1[:,0]+boxes1[:,2]/2
ymin=boxes1[:,1]-boxes1[:,3]/2 #I just want to try the [:] function of tensorflow
ymax=boxes1[:,1]+boxes1[:,3]/2 #the simplest way of doing this is using slice
boxes1=tf.transpose(tf.stack([ymin, xmin, ymax, xmax]))
boxes2=tf.convert_to_tensor([[0.0, 0.0, 0.5, 0.5]])
boxes_obj1=box_list.BoxList(boxes1)
boxes_obj2=box_list.BoxList(boxes2)
iou_score=box_list_ops.iou(boxes_obj2, boxes_obj1)
boxes_obj1.add_field('scores', iou_score[0])
boxes_obj1=box_list_ops.sort_by_field(boxes_obj1, 'scores')
boxes_obj1=boxes_obj1.get()
matched_box=boxes_obj1[0]
with tf.Session() as sess:
    print(sess.run([boxes1, iou_score]))

上面代码,首先生成一组假想box(boxes1),目标是找出boxes1里面和boxes2重合最大的那个box(IOU最大)

因为 boxes1使用[cx, cy,width, height]方式表达box信息,所以首先需要转换成[xmin, xmax, ymin, ymax]形式。

使用box_list.BoxList(boxes1)将tensor形式的box信息转换为BoxList

猜你喜欢

转载自blog.csdn.net/ziliwangmoe/article/details/81328918