python - 为什么在非最大抑制期间出现 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
解决方案
推荐阅读
- powershell - Visual Studio 2019 开发人员 PowerShell v16.9.4 不工作
- python - 在 Selenium 版本 3.7.0 无头驱动程序 phantomjs 版本 2.1 上启用 Cookie
- java - 如何修复询问用户是否要重新运行的程序?
- javascript - React JS 应用程序中的推荐糖果集成
- visual-studio - C++ 的 GetScreenSize 函数;“没有合适的用户定义转换”(Winforms)
- javascript - 如何根据从下拉列表中选择的值在显示和隐藏元素之间切换?
- go - 你如何将一个非常大的 (>max(int64)) 纳秒 unix 时间戳转换为 time.Time
- python - 计算行中每个纬度和经度之间的距离
- ansible - 异步模块不适用于 become/become_user | Ansible
- ms-access - 访问,如果 *any* 模块上存在 *any* 错误,则报告公式上的 #Name 错误