MMdetection使用多卡训练时出现“ your module has parameters that were not used in producing loss”

MMdetection使用多卡训练时出现“ your module has parameters that were not used in producing loss”

使用mmdetection多卡训练命令时:

./tools/dist_train.py config_file num_gpus

出现下述错误:

RuntimeError: Expected to have finished reduction in the prior
iteration before starting a new one. This error indicates that your
module has parameters that were not used in producing loss. You can
enable unused parameter detection by (1) passing the keyword argument
find_unused_parameters=True to
torch.nn.parallel.DistributedDataParallel; (2) making sure all forward
function outputs participate in calculating loss. If you already have
done the above two steps, then the distributed data parallel module
wasn’t able to locate the output tensors in the return value of your
module’s forward function. Please include the loss function and the
structure of the return value of forward of your module when reporting
this issue (e.g. list, dict, iterable)

出现该问题的原因之一是网络中有没有参与loss计算的网络参数。一种解决方法就是按照错误提示,将分布式训练代码中的find_unused_parameters设置为True即可,具体位置在mmdet/apis/train.py中。

但一些额外且没用的网络参数会增加网络模型大小,所以另一种解决办法是直接把这些参数在网络定义中删掉:

  • 找到所安装的mmcv软件包位置,并找到optimizer.py(mmcv/runner/hooks中)文件。
  • 在该文件中找到after_train_iter函数。
  • 在该函数中的runner.outputs[‘loss’].backward()的下一行输入以下代码:
    for name, param in runner.model.named_parameters():
         if param.grad is None:
             print(name)   
  • 运行单卡训练代码:
python tools/train.py config_file 

此时会将没有参与loss计算的网络参数名字打印出来。

最后将这些网络参数在代码里注释掉即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值