在faster-rcnn中涉及IOU的计算,而IOU是先计算交叉面积。
在detectron的源代码中,用少量几行代码实现了anchor集合和gt_boxes集合的交叉面积矩阵计算
代码的位置如下
# boxes1是gt_boxes # boxes2是由系统生成的数以万计的anchordefpairwise _ intersection (boxes 13360 boxes, boxes 2: boxes (-torch.tensor : ' ' giventwolistsofboxesofsizenandm,computetheintersectionareabetween _ all _ nxmm ymin,xmax,ymax ) args3360bbax ) ) ) ) ) Boxes2(Boxes ) :two`Boxes ).containsnmBoxes,respectives ) m].1 boxes2=boxes1.tensor,boxes2. tensor width _ height=torch.min (boxes1[ :None,2:],boxes2[ : ] 2 ] width _ height.clamp _ [ min=0] # [ n,m,2]intersection=wintersection
width _ height=torch.min (boxes1[ :None,2:],boxes2[ :23360 ] ) torch.max (boxes1[ 3: ] )
取boxes1的右下坐标,扩展了一个纬度。 给定有139个标记框,这里的shape为: [ 139,1,2 ]
取了boxes2右下角的坐标。 如果有319200个anchor,则此处的shape为: [ 319200,2 ]
此时,为了进行torch.min运算,首先必须对向量整体进行broadcasting,相当于如下。
boxes1[ 139,319200,2 ]
boxes2[ 139,319200,2 ]
这两个向量进行min运算。 有关详细信息,请参阅https://numpy.org/dev docs/user/theory.broadcasting.html
torch.min(boxes1[:None,2:],boxes2[ :23360 ] )此代码位于同一上方,但由于冒号的位置相反,因此左上角的坐标Torch.max
把宽度和高度相乘得到交叉面积
width_height.clamp_(min=0) # [N,m,2 ] intersection=width _ height.prod [ dim=2] # [ n,M]结果图像