Keras 的 ZeroPadding2D 在 TensorFlow v1 和 v2 中的区别?

Difference of Keras' ZeroPadding2D in TensorFlow v1 and v2?

我最近在 Coursera 完成了 Andrew Ng 的卷积网络深度学习课程。最后的作业结束人脸识别。我想将使用 TensorFlow 1.2.1 的作业中的代码迁移到最新版本(我使用的是 TensorFlow 2.2.0)。

让我们使用 tf 1.2.1 观察作业中的以下代码:

  from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate
  X_input = Input(input_shape)
  X = ZeroPadding2D((3, 3))(X_input)

其中 input_shape = (3, 96, 96)。 X_input和X的形状如下(在Coursera的Notebook中用print执行):

(?, 3, 96, 96)   # shape of X_input
(?, 3, 102, 102) # shape of X

然而,当我尝试 运行 我的笔记本中 Google Colab 和 tf 2.2.0 上完全相同的代码时,形状是:

(None, 3, 96, 96)  # shape of X_input
(None, 9, 102, 96) # shape of X

在我看来 ZeroPadding2D 的工作方式不同。那正确吗?如何执行零填充以实现与作业中相同的形状(不触及通道)?

谢谢!

编辑: 我个人笔记本中的代码块:

from tensorflow import keras
from tensorflow.keras.layers import Input, Conv2D, MaxPool2D, AveragePooling2D, ZeroPadding2D, BatchNormalization, Activation, Dense, Lambda

X_input = Input(input_shape)
print(X_input.shape)
X = ZeroPadding2D((3, 3))(X_input)
print(X.shape)

替换代码,

X = ZeroPadding2D((3, 3))(X_input)

X = ZeroPadding2D(padding = ((3, 3), (3,3)), data_format='channels_first')(X_input)

会给你预期的结果。

完整的工作代码如下所示:

from tensorflow import keras
from tensorflow.keras.layers import Input, Conv2D, MaxPool2D, AveragePooling2D, ZeroPadding2D, BatchNormalization, Activation, Dense, Lambda
input_shape = (3, 96, 96)
X_input = Input(input_shape)
print(X_input.shape)
X = ZeroPadding2D(padding = ((3, 3), (3,3)), data_format='channels_first')(X_input)
print(X.shape)