Faster RCNN pytroch训练问题:Warning: NaN or Inf found in input tensor.

problem
  1. 在自己的数据(voc格式)上训练Faster RCNN(https://github.com/jwyang/faster-rcnn.pytorch)就出现了loss=nan的问题。
  2. 在Pascal voc和coco上训练Faster RCNN都正常。
reason
  1. 可能是learning rate太大,调小learning rate。最有效的方法是learning rate设为0,看看是不是还有nan的问题。
  2. 大概率是自己的数据有问题(我的数据是voc格式),voc获取左边后是要减1的,如果你的数据的坐标框本身就是从0开始的,那减1就会导致超出图像边界。
solution
  1. 设置lr=0,如果不在出现loss=nan的问题,说明是learning rate太大,导致了梯度爆炸或梯度消失。可调整learning rate和weight decay。
  2. 如果lr=0后,依然存在loss=nan的问题,就修改pascal_voc.py中获取坐标框的代码:
原代码
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
修改后
x1 = float(bbox.find('xmin').text) 
y1 = float(bbox.find('ymin').text) 
x2 = float(bbox.find('xmax').text) 
y2 = float(bbox.find('ymax').text) 

若设置了翻转(cfg.TRAIN.USE_FLIPPED = True),则需要在imdb.py中的def append_flipped_images(self)方法:

源代码
boxes[:, 0] = widths[i] - oldx2 - 1
boxes[:, 2] = widths[i] - oldx1 - 1
修改后
boxes[:, 0] = widths[i] - oldx2
boxes[:, 2] = widths[i] - oldx1
总结(可能导致loss=nan的情况)[2]
  1. Coordinates out of the image resolution------------> NaN Loss
  2. xmin=xmax-----------> Results in NaN Loss
  3. ymin==ymax-----------> Results in Nan Loss
  4. The size of bounding box was very small-----------> Results in NaN Loss
For the 4th case, we put a condition that the difference of |xmax -xmin| >= 20 and similarly |ymax- ymin| >=20

[1]https://github.com/VisionLearningGroup/DA_Detection/issues/11
[2]https://github.com/jwyang/faster-rcnn.pytorch/issues/136

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值