[pytorch]亲测解决RuntimeError: CUDA out of memory

问题

当我在测试训练好的基于Pytorch框架的半监督视频目标分割模型时,我已经加上了Model.eval( ),用于测试,但是运行过程中报错:RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 3.95 GiB total capacity; 3.39 GiB already allocated; 9.88 MiB free; 3.39 GiB reserved in total by PyTorch)。

原因分析

是我用的自己的游戏本运行测试,显卡显存比较小,但是按理说我的图片帧数并不大,而且是逐帧处理的不会出现这个问题。

解决

(1)按照其它博客的方案加上 torch.cuda.empty_cache( ),及时清理不用的缓存变量,然而效果几乎没有,没什么用;

(2)调小Batch Size:我已经调到1了还是Out  of memory,我真的想把电脑砸了;

(3)最终解决:加上with torch.no_grad( ),这下真正能不报错运行了,我错怪了我的电脑,具体原因可以看我的另外一篇博客https://blog.csdn.net/weixin_39626452/article/details/104656971。

 

你可能感兴趣的:(pytorch,pytorch,神经网络,深度学习)