RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.
Posted 啊菜来了
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.相关的知识,希望对你有一定的参考价值。
这是我遇到的问题,刚开始的时候怎么也解决不了。
然后我去搜了一下解决方法,具体方法如下:
方法一:
仅需减小batchsize
改文件的配置cfg的batchsize=1,一般在cfg文件下的查找batch或batchsize,将batchsize调小后,再次运行,类似于改下面
方法二 :
上述方法还没解决,不改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调小
把原本的[640,640]改为上图所示
方法5:
也就是我解决的方法,我仔细地看了一下的那个报错,发现并不是batchsize的问题,也不是储存的问题,因为我的batchsize=1,储存还有 3G左右,所以排除了。
我的pycharm,还有其他的所有东西都在D盘,所以就把D盘设置成100000-100000了,
然后问题就解决了,然后程序就跑起来了。
以上是关于RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.的主要内容,如果未能解决你的问题,请参考以下文章
使用VGG网络训练发生错误RuntimeError: CUDA out of memory解决方案:
AI绘画——使用stable-diffusion生成图片时提示RuntimeError: CUDA out of memory处理方法
RuntimeError: CUDA out of memory. Tried to allocate 600.00 MiB (GPU 0; 23.69 GiB total capacity)
Pytorch RuntimeError: CUDA error: out of memory at loss.backward() , 使用 CPU 时没有错误
RuntimeError: CUDA out of memory. Tried to allocate 600.00 MiB (GPU 0; 23.69 GiB total capacity)
RuntimeError: CUDA out of memory. Tried to allocate 600.00 MiB (GPU 0; 23.69 GiB total capacity)