faster-rcnn系列assert (boxes[:, 2] >= boxes[:, 0]).all()和loss偶尔为nan的问题

问问题综述

这些问题的根源都是faster-rcnn系列在处理生成pascal voc数据集时,为了使像素以0为起点,每个bbox的左上
右下坐标都减1,如果你的数据里有坐标为0,一般是x1或y1,这时x1 = 0-1 = 65535,就会出现下面的问题1,2。
# 代码在$faster-rcnn-root/lib/datasets/pascal.py
# Make pixel indexes 0-based
x1 = float(bbox.find('xmin').text) - 1
y1 = float(bbox.find('ymin').text) - 1
x2 = float(bbox.find('xmax').text) - 1
y2 = float(bbox.find('ymax').text) - 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

问题1解释

# 终端提示
File "/py-faster-rcnn/tools/../lib/datasets/imdb.py", line 112, in append_flipped_images
    assert (boxes[:, 2] >= boxes[:, 0]).all()
AssertionError
  • 1
  • 2
  • 3
  • 4
# $faster-rcnn-root/lib/datasets/imdb.py
oldx1 = boxes[:, 0].copy()
oldx2 = boxes[:, 2].copy()
boxes[:, 0] = widths[i] - oldx2 - 1
boxes[:, 2] = widths[i] - oldx1 - 1
assert (boxes[:, 2] >= boxes[:, 0]).all()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

问题综述里说明过了如果你的坐标里有0,那么oldx1就会为65535,boxes[:, 2]就会小于boxes[:, 0],assert就会Error

问题2解释

训练时loss偶尔出现nan(不是不收敛那种情况)
# 网上很多教程说在 boxes[:, 2] = widths[i] - oldx1 - 1加下面代码
# 但是治标不治本,只是在这里不提示错误了,但是你的x1还是65533,在计算loss时
# 就会出现很大偏差,出现nan的情况,为什么偶尔出现,因为你的数据里偶尔有0啊 哈哈哈
for b in range(len(boxes)):
    if boxes[b][2]< boxes[b][0]:
        boxes[b][0] = 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

最终解决方案

找到文章开头说的根源所在
# 代码在$faster-rcnn-root/lib/datasets/pascal.py
# Make pixel indexes 0-based
# 根据你的数据情况选择减不减1,一般都是左上角的(x1,y1)可能为0
x1 = float(bbox.find('xmin').text) #- 1  注释掉你数据里不能减1的地方
y1 = float(bbox.find('ymin').text) #- 1
x2 = float(bbox.find('xmax').text) - 1
y2 = float(bbox.find('ymax').text) - 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

来自爆米花好美踩过的坑 哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值