解决重复运行Tensorflow程序GPU内存占用的小白方法

作为一个刚入门CV的新手,最近在运行一个生成人脸图像的程序时遇到了问题,程序是可以运行的,但是运行两次后就会出错,具体如下:

ResourceExhaustedError: 2 root error(s) found.
  (0) Resource exhausted: OOM when allocating tensor with shape[1,3,3,512,512] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
     [[{{node G_synthesis_5/_Run/G_synthesis/64x64/Conv0_up/Square}}]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

     [[G_synthesis_5/_Run/saturate_cast/_4577]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

  (1) Resource exhausted: OOM when allocating tensor with shape[1,3,3,512,512] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
     [[{{node G_synthesis_5/_Run/G_synthesis/64x64/Conv0_up/Square}}]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

0 successful operations.
0 derived errors ignored.

先介绍下我的运行环境:

  • Win 10
  • Spyder
  • Tensorflow-GPU == 1.14.0

查到的原因是GPU显存不够了。

 

第一次运行,专用GPU内存:0 ===> 1.2,第二次运行:1.2 ===> 1.6,第三次就没有办法运行了,由于自己的电脑是渣渣显卡,显存太小,而我的程序是需要不断改变参数重复运行的,但网上查了一遍说显存是不会运行一次就释放资源的,所以我不得不每运行两次就重启Spyder这样来释放显存。也尝试过运行一次后就杀掉显卡中的这个程序,但结果是直接导致Spyder内核连接中断,结果还是重启。

最后,灵机一动,直接在控制台重启内核就可以释放掉程序以及所占显存资源,这样就不用重启麻烦了。

如果是一段连续程序显存不够,那就要么换大一点显存的显卡,或者设置每次占用GPU资源的值了。这个可以看看其他博主的博客。

方法不高级,但挺实用,如果有更好的方法,欢迎留言评论。

posted @ 2020-08-01 17:40  MK_筱雨  阅读(1951)  评论(0编辑  收藏  举报