google 与 GPU 的合作实验室“ResourceExhaustedError”

google colaboratory `ResourceExhaustedError` with GPU

我正在尝试使用 colaboratory 微调 Vgg16 模型,但我 运行 在使用 GPU 进行训练时出现此错误。

OOM when allocating tensor of shape [7,7,512,4096]

INFO:tensorflow:Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.ResourceExhaustedError'>, OOM when allocating tensor of shape [7,7,512,4096] and type float
     [[Node: vgg_16/fc6/weights/Momentum/Initializer/zeros = Const[_class=["loc:@vgg_16/fc6/weights"], dtype=DT_FLOAT, value=Tensor<type: float shape: [7,7,512,4096] values: [[[0 0 0]]]...>, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]

Caused by op 'vgg_16/fc6/weights/Momentum/Initializer/zeros', defined at:

我的虚拟机会话也有这个输出:

    --- colab vm info ---
python v=3.6.3
tensorflow v=1.4.1
tf device=/device:GPU:0
model name  : Intel(R) Xeon(R) CPU @ 2.20GHz
model name  : Intel(R) Xeon(R) CPU @ 2.20GHz
MemTotal:       13341960 kB
MemFree:         1541740 kB
MemAvailable:   10035212 kB

我的 tfrecord 只有 118 张 256x256 JPG file size <2MB

有解决办法吗?它在我使用 CPU 时有效,而不是 GPU

我未能重现最初报告的错误,但如果这是由 运行 GPU 内存(相对于主内存)不足引起的,这可能会有所帮助:

# See https://www.tensorflow.org/tutorials/using_gpu#allowing_gpu_memory_growth
config = tf.ConfigProto()
config.gpu_options.allow_growth = True

然后将 session_config=config 传递给例如slim.learning.train()(或您最终使用的任何会话 ctor)。

在我的情况下,我没有使用 Ami 提供的解决方案解决问题,即使它非常好,可能是因为 Colaboratory VM 无法提供更多资源。

我在检测阶段(不是模型训练)出现 OOM 错误。我解决了一个解决方法,禁用 GPU 进行检测:

config = tf.ConfigProto(device_count = {'GPU': 0})
sess = tf.Session(config=config)

我遇到了同样的问题,我发现我的问题是由以下代码引起的:

from tensorflow.python.framework.test_util import is_gpu_available as tf
if tf()==True:
  device='/gpu:0'
else:
  device='/cpu:0'

我用下面的代码检查GPU内存使用状态,发现在运行上面的代码之前使用率是0%,在上面的代码之后变成了95%。

# memory footprint support libraries/code
!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
!pip install psutil
!pip install humanize    
import psutil
import humanize
import os
import GPUtil as GPU
GPUs = GPU.getGPUs()
# XXX: only one GPU on Colab and isn't guaranteed
gpu = GPUs[0]

def printm():
process = psutil.Process(os.getpid())
print("Gen RAM Free: " + humanize.naturalsize( psutil.virtual_memory().available ), " I Proc size: " + humanize.naturalsize( process.memory_info().rss))
print('GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total {3:.0f}MB'.format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))

printm()

之前:

Gen RAM Free: 12.7 GB I Proc size: 139.1 MB

GPU RAM Free: 11438MB | Used: 1MB | Util 0% | Total 11439MB

之后:

Gen RAM Free: 12.0 GB I Proc size: 1.0 GB

GPU RAM Free: 564MB | Used: 10875MB | Util 95% | Total 11439MB

不知何故,is_gpu_available() 管理消耗了大部分 GPU 内存而没有释放它们,所以我使用下面的代码为我检测 gpu 状态,问题解决了

!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
try:
  import GPUtil as GPU
  GPUs = GPU.getGPUs()
  device='/gpu:0'
except:
  device='/cpu:0'

看到少量空闲 GPU 内存几乎总是表示您创建了一个没有 allow_growth = True 选项的 TensorFlow 会话。看: https://www.tensorflow.org/guide/using_gpu#allowing_gpu_memory_growth

如果您不设置此选项,默认情况下,TensorFlow 将在创建会话时保留几乎所有 GPU 内存。

好消息:截至本周,Colab 现在默认设置此选项,因此当您在 Colab 上使用多个笔记本时,您应该会看到更低的增长。而且,您还可以通过从运行时菜单中选择“管理会话”来检查每个笔记本的 GPU 内存使用情况。

选择后,您将看到一个对话框,其中列出了所有笔记本和每个笔记本正在消耗的 GPU 内存。要释放内存,您也可以从此对话框终止运行时。