RuntimeError: CUDA out of memory 已解决

先说下我的电脑环境:win10 3060ti torch10 python3.8 cuda11.3

RuntimeError: CUDA out of memory. Tried to allocate 128.00 MiB (GPU 0; 23.70 GiB total capacity; 7.44 GiB already allocated; 87.88 MiB free; 7.71 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

训练一个很小的网络的时候,报了一个这个错,很奇怪,看了一下以为是显存不够的原因 ,但是很矛盾,之前训练过transformer,不可能是这个原因。

通过网络上找来找去,众说纷纭

  1. 降低Dataloader中的batch_size
  2. import gc 清除cuda显存中的缓存
  3. 不让图片送入cuda,这个很明显没有意义
  4. 降低torch版本
  5. 不采用预训练模型

上述在我这里都不通, 我最终的解决方案来自这个链接中的一个回复 CUDA out of memory

降低了图片的像素,最开始的像素是2400x2400,resize到400x400后就可以玩耍了
因为是目标检测任务,resize图片后,label位置也需要进行改变,这里附上一篇不错的博客,希望可以帮到你:label随着resize而改变

猜你喜欢

转载自blog.csdn.net/maoker_Voice/article/details/127249012