RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

这是我遇到的问题,刚开始的时候怎么也解决不了。

RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

然后我去搜了一下解决方法,具体方法如下:

方法一:

仅需减小batchsize

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

RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

方法二 :

上述方法还没解决,不改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()

方法四:

解决方法:将img-size调小

RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

把原本的[640,640]改为上图所示

方法5:

也就是我解决的方法,我仔细地看了一下的那个报错,发现并不是batchsize的问题,也不是储存的问题,因为我的batchsize=1,储存还有 3G左右,所以排除了。

RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

我的pycharm,还有其他的所有东西都在D盘,所以就把D盘设置成100000-100000了,

然后问题就解决了,然后程序就跑起来了。

Original: https://blog.csdn.net/m0_61985580/article/details/124370995
Author: 啊菜来了
Title: RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

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

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

(0)

大家都在看

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