【Keras】ResourceExhaustedError: OOM when allocating tensor with shape[16,64,256,512]

問題点

学習時の画像サイズを256x256から、256x512に変更したところ、エラーが発生した。

tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[16,64,256,512] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
         [[Node: loss_1/conv2d_1_loss/model_1/vgg16/block1_conv2/convolution = Conv2D[T=DT_FLOAT, _class=["loc:@train...kpropInput"], data_format="NCHW", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](loss_1/conv2d_1_loss/model_1/vgg16/block1_conv1/Relu, block1_conv2/kernel/read)]]
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.

         [[Node: loss_1/mul/_1677 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_11171_loss_1/mul", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
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.

原因

[16,64,256,512]は、右からバッチ数、チャンネルサイズ、画像の高さ、画像の幅を表す。
この合計が、16x64x256x512= 134,217,728(単位はbyteではない)となり、これがメモリサイズを超えた値となっているためエラーとなっている。

解決策

画像サイズは変更できないのでバッチサイズを8に変更した。

参考