當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.keras.layers.Cropping2D用法及代碼示例

用於 2D 輸入的裁剪層(例如圖片)。

繼承自:LayerModule

用法

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

參數

  • cropping 整數,或 2 個整數的元組,或 2 個整數的 2 個元組。
    • 如果 int: 對高度和寬度應用相同的對稱裁剪。
    • 如果 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 配置文件中的 image_data_format~/.keras/keras.json 。如果您從未設置它,那麽它將是"channels_last"。

它沿空間維度進行裁剪,即高度和寬度。

例子:

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

輸入形狀:

具有形狀的 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, cropped_rows, cropped_cols, channels)
  • 如果 data_format"channels_first"(batch_size, channels, cropped_rows, cropped_cols)

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.keras.layers.Cropping2D。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。