pytorch训练时cuda内存不足

在使用pytorch,使用resnet50训练voc2012数据集时遇到CUDA out of memory的问题!我的显卡是3060ti-8g。
具体错误如下:
RuntimeError: CUDA out of memory. Tried to allocate 30.00 MiB (GPU 0; 8.00 GiB total capacity; 7.22 GiB already allocated; 0 bytes free; 7.32 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
请各位朋友帮忙看看怎么解决!多谢

img

通常遇到OOM(out of memory)问题, 只有两种解决方案, 降低您网络训练时的batchsize, 或者选用更小的网络. 看到您这里用的resnet50, 8Gmemory够用了, 您可以将训练时的batchsize减小就可以了, 在训练参数里可调

  • 你可以参考下这篇文章:Pytorch CUDA 训练错误汇总
  • 除此之外, 这篇博客: PyTorch代码错误笔记一中的 3、RuntimeError: CUDA out of memory. Tried to allocate 392.00 MiB (GPU 0; 4.00 GiB total capacity; 2.18 GiB already allocated; 381.14 MiB free; 2.20 GiB reserved in total by PyTorch) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 错误分析:显示GPU可分配的内存不够,我的batch_size = 256
    解决方案:改成batch_size=128改成batch_size=64,或者更小