Keras 3 API 文档 / 层 API / 形状变换层 / UpSampling2D 层

UpSampling2D 层

[源码]

UpSampling2D

keras.layers.UpSampling2D(
    size=(2, 2), data_format=None, interpolation="nearest", **kwargs
)

用于 2D 输入的上采样层。

此实现使用插值法调整大小,具体方法由 interpolation 参数指定。使用 interpolation=nearest 可重复数据的行和列。

示例

>>> input_shape = (2, 2, 1, 3)
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
>>> print(x)
[[[[ 0  1  2]]
  [[ 3  4  5]]]
 [[[ 6  7  8]]
  [[ 9 10 11]]]]
>>> y = keras.layers.UpSampling2D(size=(1, 2))(x)
>>> print(y)
[[[[ 0  1  2]
   [ 0  1  2]]
  [[ 3  4  5]
   [ 3  4  5]]]
 [[[ 6  7  8]
   [ 6  7  8]]
  [[ 9 10 11]
   [ 9 10 11]]]]

参数

  • size: 整型或包含 2 个整型的元组。行和列的上采样因子。
  • data_format: 字符串,可选值包括 "channels_last" (默认) 或 "channels_first"。输入中各维度的顺序。"channels_last" 对应于形状为 (batch_size, height, width, channels) 的输入,而 "channels_first" 对应于形状为 (batch_size, channels, height, width) 的输入。如果未指定,则使用 Keras 配置文件 ~/.keras/keras.json 中的 image_data_format 值(如果存在),否则使用 "channels_last"。默认为 "channels_last"
  • interpolation: 字符串,可选值包括 "bicubic", "bilinear", "lanczos3", "lanczos5", "nearest"

输入形状

4D 张量,形状为: - 如果 data_format"channels_last": (批量大小, 行, 列, 通道) - 如果 data_format"channels_first": (批量大小, 通道, 行, 列)

输出形状

4D 张量,形状为: - 如果 data_format"channels_last": (批量大小, 上采样行, 上采样列, 通道) - 如果 data_format"channels_first": (批量大小, 通道, 上采样行, 上采样列)