首页 > 解决方案 > 为什么在非最大抑制期间出现 zerdivision 错误?

问题描述

我在进行非最大抑制时遇到问题。当我在某些图像中执行给定代码时会发生下面给出的错误,而在其他图像中则不会。我该如何解决这个问题?

def bbox_iou(box1, box2):
    intersect_w = _interval_overlap([box1.xmin, box1.xmax], [box2.xmin, box2.xmax])
    intersect_h = _interval_overlap([box1.ymin, box1.ymax], [box2.ymin, box2.ymax])
    intersect = intersect_w * intersect_h
    w1, h1 = box1.xmax-box1.xmin, box1.ymax-box1.ymin
    w2, h2 = box2.xmax-box2.xmin, box2.ymax-box2.ymin
    union = w1*h1 + w2*h2 - intersect
    return float(intersect) / union
    

def do_nms(boxes, nms_thresh):
    if len(boxes) > 0:
        nb_class = len(boxes[0].classes)
    else:
        return
    for c in range(nb_class):
        sorted_indices = np.argsort([-box.classes[c] for box in boxes])
        for i in range(len(sorted_indices)):
            index_i = sorted_indices[i]
            if boxes[index_i].classes[c] == 0: continue
            for j in range(i+1, len(sorted_indices)):
                index_j = sorted_indices[j]
                if bbox_iou(boxes[index_i], boxes[index_j]) >= nms_thresh:
                    boxes[index_j].classes[c] = 0
do_nms(boxes, 0.5)
print("nb boxes remaining; ",len(boxes))

我得到错误:

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-71-13d8e73384c6> in <module>()
----> 1 do_nms(boxes, 0.5)
      2 print("nb boxes remaining; ",len(boxes))

1 frames
<ipython-input-70-a2bd5f888f81> in bbox_iou(box1, box2)
     21     w2, h2 = box2.xmax-box2.xmin, box2.ymax-box2.ymin
     22     union = w1*h1 + w2*h2 - intersect
---> 23     return intersect / union
     24 
     25 

ZeroDivisionError: division by zero

标签: pythonyolo

解决方案


推荐阅读