Faster RCNN pytroch1.0 训练时:Warning: NaN or Inf found in input tensor.

文章目录

问题

  1. 在Pascal voc和coco上训练Faster RCNN都正常
  2. 在训练自己的数据集时(Pascal voc格式)训练Faster R-CNN pytorch1.0时出现Warning: NaN or Inf found in input tensor.

原因

  1. 可能是learning rate太大,调小learning rate。最有效的方法是learning rate设为0,看看是不是还有nan的问题。
  2. 因为自己的数据是从0开始的,但是源码中-1,如果这时候annotation中有为0的就会出现越界的问题。

解决

  1. 设置lr=0,如果不在出现loss=nan的问题,说明是learning rate太大,导致了梯度爆炸或梯度消失。可调整learning rate和weight decay。

  2. 如果lr=0后,依然存在loss=nan的问题,就修改pascal_voc.py中获取坐标框的代码:

    修改前
            bbox = obj.find('bndbox')
            # 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
    修改后
            bbox = obj.find('bndbox')
            # 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
    

    若设置了翻转(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  # - 1
        boxes[:, 2] = widths[i] - oldx1  # - 1
    
  3. 最后记得清理data文件里面的缓存文件cache

参考:
【1】https://blog.csdn.net/qq_29936933/article/details/111378275
【2】https://github.com/jwyang/faster-rcnn.pytorch/issues/136

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值