Keras 2 API 文档 / 层 API / 重塑层 / ZeroPadding2D 层

ZeroPadding2D 层

[源代码]

ZeroPadding2D

tf_keras.layers.ZeroPadding2D(padding=(1, 1), data_format=None, **kwargs)

用于二维输入(例如图像)的零填充层。

此层可以在图像张量的顶部、底部、左侧和右侧添加零行和零列。

示例

>>> input_shape = (1, 1, 2, 2)
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
>>> print(x)
[[[[0 1]
   [2 3]]]]
>>> y = tf.keras.layers.ZeroPadding2D(padding=1)(x)
>>> print(y)
tf.Tensor(
  [[[[0 0]
     [0 0]
     [0 0]
     [0 0]]
    [[0 0]
     [0 1]
     [2 3]
     [0 0]]
    [[0 0]
     [0 0]
     [0 0]
     [0 0]]]], shape=(1, 3, 4, 2), dtype=int64)

参数

  • padding: 整数,或包含 2 个整数的元组,或包含 2 个包含 2 个整数的元组的元组。
    • 如果为整数:则对高度和宽度应用相同的对称填充。
    • 如果为包含 2 个整数的元组:则解释为高度和宽度的两个不同的对称填充值:(symmetric_height_pad, symmetric_width_pad)
    • 如果为包含 2 个包含 2 个整数的元组的元组:则解释为 ((top_pad, bottom_pad), (left_pad, right_pad))
  • data_format: 字符串,取值 channels_last(默认)或 channels_first 之一。输入中维度顺序。channels_last 对应于形状为 (batch_size, height, width, channels) 的输入,而 channels_first 对应于形状为 (batch_size, channels, height, width) 的输入。如果未指定,则使用 TF-Keras 配置文件 ~/.keras/keras.json(如果存在)中找到的 image_data_format 值,否则为 'channels_last'。默认为 'channels_last'。

输入形状

4D 张量,形状为:- 如果 data_format"channels_last"(batch_size, rows, cols, channels) - 如果 data_format"channels_first"(batch_size, channels, rows, cols)

输出形状

4D 张量,形状为:- 如果 data_format"channels_last"(batch_size, padded_rows, padded_cols, channels) - 如果 data_format"channels_first"(batch_size, channels, padded_rows, padded_cols)