Keras 2 API 文件 / 層 API / 形狀重塑層 / Cropping2D 層

Cropping2D 層

[原始碼]

Cropping2D 類別

tf_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 = tf.keras.layers.Cropping2D(cropping=((2, 2), (4, 4)))(x)
>>> print(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) 的輸入。如果未指定,則使用在您的 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, cropped_rows, cropped_cols, channels) - 如果 data_format"channels_first"(batch_size, channels, cropped_rows, cropped_cols)