Tensorflow Error: OOM when allocating tensor with strange shape

Tensorflow Error: OOM when allocating tensor with strange shape

当我收到此错误时,我正在尝试使用一些 512x512 图像执行 VGG16 的一个小变体:

tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[131072,4096]

这是我的 model.summary()

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 512, 512, 64)      1792      
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 512, 512, 64)      36928     
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 256, 256, 64)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 256, 256, 128)     73856     
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 256, 256, 128)     147584    
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 128, 128, 128)     0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 128, 128, 256)     295168    
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 128, 128, 256)     590080    
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 128, 128, 256)     590080    
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 64, 64, 256)       0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 64, 64, 512)       1180160   
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 64, 64, 512)       2359808   
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 64, 64, 32)        147488    
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 32, 32, 32)        0         
_________________________________________________________________
flatten (Flatten)            (None, 32768)             0         
_________________________________________________________________
dense (Dense)                (None, 4096)              134221824 
_________________________________________________________________
dense_1 (Dense)              (None, 4096)              16781312  
_________________________________________________________________
dense_2 (Dense)              (None, 3)                 12291     
=================================================================
Total params: 156,438,371
Trainable params: 156,438,371
Non-trainable params: 0

这里奇怪的是错误信息中的shape[131072, 4096]

在我的模型中,正如我们在 model.summary() 中看到的那样,唯一的 4096 在最后,在 Dense 层中,在 flatten 层之后.但是展平层产生 32768 个神经元的输出。所以,这种情况下的错误应该是shape[32768, 4096]。我试着写 2048 或 1024 而不是 4096。但错误总是 shape[131072, 4096],而且这个形状永远不会改变。

问题是:

  1. 这个shape[131072, 4096]从哪里来的?不应该是shape[32768, 4096]吗?
  2. 如何解决?

我看了一些有同样问题的问题,比如我们:

Error: OOM when allocating tensor with shape

但其中 none 解释了形状不正确的原因。

由于您使用的是VGG16,因此您需要确保您的输入是224x224,而您并非如此。另外,你能告诉你正在使用的批量大小吗,错误是因为你没有足够的 GPU 内存。因此,考虑减小批量大小。鉴于您使用的是 512x512,GPU 内存不足是可以理解的。此外,作为最佳实践,请记住大多数 SOTA 模型的输入图像永远不会超过 300x300。最好不要使用这么大的尺寸。

至于你问的形状,我问你是不是批次大小是32。因为32 x 256 x 256 x 64 = 131072 x 4096。因此张量实际上是正在计算的初始张量已创建但由于内存不足无法创建。至于为什么是这样的形状,可能是因为TensorFlow的内部工作,我无法评论,因为我对它的了解有限,对此我深表歉意。但是,我希望我能帮助您了解形状的来源。