解决问题:RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB

网络上常用的三种方法

方法一:

仅需减小batchsize

改文件的配置cfg的batchsize=1,一般在cfg文件下的查找batch或batchsize,将batchsize调小后,再次运行,类似于改下面

解决问题:RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB

方法二 :

上述方法还没解决,不改batchsize,可以考虑下面的方法的链接

不计算梯度:

ps: 在报错的哪一行代码的上面,加上下面一行代码,不计算梯度

with torch.no_grad()

不计算梯度的方法

方法三:

释放内存:链接如下

释放内存

if hasattr(torch.cuda, ’empty_cache’):

torch.cuda.empty_cache()

ps: 在报错的那一行代码的上面,加上下面两行代码,释放无关的内存

if hasattr(torch.cuda, ’empty_cache’):

torch.cuda.empty_cache()

方法四: 我的解决方案

我没用上面的方法,最主要是我是新手不知道在哪改,所以在看了好多网上的解决方案后,试了一下这种方法,可以训练了,成功解决GPU内存不足的问题

解决方法:将img-size调小

解决问题:RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB

我把原本的[640,640]改为上图所示,成功解决问题

Original: https://blog.csdn.net/javahaoge/article/details/122890247
Author: 编程newbird
Title: 解决问题:RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/628812/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球