当前位置:   article > 正文

RuntimeError: CUDA out of memory. Tried to allocate xxx MiB 报错处理方案_allocated memory try setting max_split_size_mb to

allocated memory try setting max_split_size_mb to avoid fragmentation. see d

论坛下载了Yolo的改进模型,运行代码开始报错:

RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB (GPU 0; 4.00 GiB total capacity; 3.49 GiB already allocated; 0 bytes free; 3.50 GiB reserved in total by PyTorch) If
 reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
 

网上有以下解决方案:

方法1: 修改batchsize

        改小batchsize数值,一般情况下设置为8,运行没有问题的话可以适当增加,一般为2的整数倍。

方法2:推理阶段/验证阶段加上with torch.no_grad():

        添加消除梯度代码,减少运算内存

方法3:代码清理内存

  1. import torch,gc
  2. del variables
  3. gc.collect()
  4. torch.cuda.empty_cache()

        报错代码上方添加以上内容,手动清理内存

方法4:检查是否使用GPU训练

其它方法:

        如果以上方法都未能解决这个问题,可以尝试使用较小的网络模型,例如原本使用yolov5l的模型可以适当减小,使用yolov5m或者s的模型,这样可以极大的减小运算量。改进模型中,有些不是特别必要的层级运算可以减少,释放内存。

        

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/220620
推荐阅读
相关标签
  

闽ICP备14008679号