Keras 3 API 文件 / 層 API / 卷積層 / DepthwiseConv2D 層

DepthwiseConv2D 層

[原始碼]

DepthwiseConv2D 類別

keras.layers.DepthwiseConv2D(
    kernel_size,
    strides=(1, 1),
    padding="valid",
    depth_multiplier=1,
    data_format=None,
    dilation_rate=(1, 1),
    activation=None,
    use_bias=True,
    depthwise_initializer="glorot_uniform",
    bias_initializer="zeros",
    depthwise_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    depthwise_constraint=None,
    bias_constraint=None,
    **kwargs
)

2D 深度可分離卷積層。

深度可分離卷積是一種卷積類型,其中每個輸入通道都與不同的核心(稱為深度核心)進行卷積。您可以將深度可分離卷積理解為深度可分離卷積的第一步。

它通過以下步驟實現

  • 將輸入分割成個別通道。
  • 將每個通道與具有 depth_multiplier 輸出通道的個別深度核心進行卷積。
  • 沿通道軸串聯卷積後的輸出。

與常規 2D 卷積不同,深度可分離卷積不會混合不同輸入通道之間的資訊。

depth_multiplier 參數決定了應用於一個輸入通道的濾波器數量。因此,它控制了在深度步驟中每個輸入通道產生的輸出通道數量。

引數

  • kernel_size:整數或 2 個整數的元組/列表,指定深度可分離卷積視窗的大小。
  • strides:整數或 2 個整數的元組/列表,指定深度可分離卷積的步幅長度。strides > 1dilation_rate > 1 不相容。
  • padding:字串,"valid""same"(不區分大小寫)。"valid" 表示不填充。"same" 會使輸入在左/右或上/下均勻填充。當 padding="same"strides=1 時,輸出與輸入大小相同。
  • depth_multiplier:每個輸入通道的深度可分離卷積輸出通道數。深度可分離卷積輸出通道總數將等於 input_channel * depth_multiplier
  • data_format:字串,"channels_last""channels_first"。輸入中維度的順序。"channels_last" 對應於形狀為 (batch, height, width, channels) 的輸入,而 "channels_first" 對應於形狀為 (batch, channels, height, width) 的輸入。它預設為在您的 Keras 設定檔 ~/.keras/keras.json 中找到的 image_data_format 值。如果您從未設定過,則將為 "channels_last"
  • dilation_rate:整數或 2 個整數的元組/列表,指定用於膨脹卷積的膨脹率。
  • activation:激活函數。如果為 None,則不應用激活函數。
  • use_bias:布林值,如果為 True,則會將偏置添加到輸出。
  • depthwise_initializer:卷積核心的初始化器。如果為 None,將使用預設初始化器 ("glorot_uniform")。
  • bias_initializer:偏置向量的初始化器。如果為 None,將使用預設初始化器 ("zeros")。
  • depthwise_regularizer:卷積核心的可選正規化器。
  • bias_regularizer:偏置向量的可選正規化器。
  • activity_regularizer:輸出的可選正規化函數。
  • depthwise_constraint:可選的投影函數,用於在 Optimizer 更新核心後應用於核心(例如,用於實作層權重的範數約束或值約束)。該函數必須將未投影的變數作為輸入,並且必須傳回投影的變數(必須具有相同的形狀)。在執行異步分散式訓練時,約束是不安全的。
  • bias_constraint:可選的投影函數,用於在 Optimizer 更新偏置後應用於偏置。

輸入形狀

  • 如果 data_format="channels_last":具有形狀的 4D 張量:(batch_size, height, width, channels)
  • 如果 data_format="channels_first":具有形狀的 4D 張量:(batch_size, channels, height, width)

輸出形狀

  • 如果 data_format="channels_last":具有形狀的 4D 張量:(batch_size, new_height, new_width, channels * depth_multiplier)
  • 如果 data_format="channels_first":具有形狀的 4D 張量:(batch_size, channels * depth_multiplier, new_height, new_width)

返回

表示 activation(depthwise_conv2d(inputs, kernel) + bias) 的 4D 張量。

引發

  • ValueError:當 strides > 1dilation_rate > 1 時。

範例

>>> x = np.random.rand(4, 10, 10, 12)
>>> y = keras.layers.DepthwiseConv2D(kernel_size=3, activation='relu')(x)
>>> print(y.shape)
(4, 8, 8, 12)