Keras 3 API文档 / Layers API / Reshaping layers / Cropping2D layer

Cropping2D 层

[源代码]

Cropping2D

keras.layers.Cropping2D(cropping=((0, 0), (0, 0)), data_format=None, **kwargs)

用于 2D 输入(例如图片)的裁剪层。

它沿着空间维度进行裁剪,即高度和宽度。

示例

>>> input_shape = (2, 28, 28, 3)
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
>>> y = keras.layers.Cropping2D(cropping=((2, 2), (4, 4)))(x)
>>> y.shape
(2, 24, 20, 3)

参数

  • cropping:整数,或 2 个整数的元组,或 2 个元组(包含 2 个整数)的元组。
    • 如果是整数:在高度和宽度上应用相同的对称裁剪。
    • 如果是 2 个整数的元组:解释为高度和宽度的两个不同的对称裁剪值:(symmetric_height_crop, symmetric_width_crop)
    • 如果是 2 个元组(包含 2 个整数)的元组:解释为((top_crop, bottom_crop), (left_crop, right_crop))
  • 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"

输入形状

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

输出形状

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