Keras 2 API 文档 / 层 API / 卷积层 / DepthwiseConv2D 层

DepthwiseConv2D 层

[源代码]

DepthwiseConv2D

tf_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
)

深度方向(Depthwise)2D 卷积。

深度卷积是一种卷积类型,其中每个输入通道都与不同的核(称为深度核)进行卷积。你可以将深度卷积理解为深度可分离卷积的第一步。

它通过以下步骤实现:

  • 将输入分割成单独的通道。
  • 将每个通道与具有 depth_multiplier 输出通道的单独深度核进行卷积。
  • 沿通道轴连接卷积输出。

与常规的 2D 卷积不同,深度方向卷积不会在不同的输入通道之间混合信息。

depth_multiplier 参数决定了每个输入通道应用多少个滤波器。因此,它控制在深度方向卷积步骤中为每个输入通道生成的输出通道数量。

参数

  • kernel_size:一个整数或包含 2 个整数的元组/列表,指定 2D 卷积窗口的高度和宽度。可以是一个整数,为所有空间维度指定相同的值。
  • strides:一个整数或包含 2 个整数的元组/列表,指定卷积在高度和宽度上的步长。可以是一个整数,为所有空间维度指定相同的值。当前实现仅支持行和列维度上具有相同长度的步长。指定任何大于 1 的步长值都与指定任何大于 1 的 dilation_rate 值不兼容。
  • padding'valid''same'(不区分大小写)之一。"valid" 表示不进行填充。"same" 会在输入的左/右或上/下均匀地填充零,以便输出在高度/宽度维度上与输入相同。
  • depth_multiplier:每个输入通道的深度方向卷积输出通道数。深度方向卷积的总输出通道数将等于 filters_in * depth_multiplier
  • 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'。
  • dilation_rate:一个整数或包含 2 个整数的元组/列表,指定用于空洞卷积的空洞率。当前,指定任何大于 1 的 dilation_rate 值都与指定任何大于 1 的 strides 值不兼容。
  • activation:要使用的激活函数。如果您不指定任何内容,则不应用激活(请参阅 keras.activations)。
  • use_bias:布尔值,表示该层是否使用偏置向量。
  • depthwise_initializer:深度方向卷积核矩阵的初始化器(参见 keras.initializers)。如果为 None,则将使用默认初始化器 ('glorot_uniform')。
  • bias_initializer:偏置向量的初始化器(参见 keras.initializers)。如果为 None,则将使用默认初始化器 ('zeros')。
  • depthwise_regularizer:应用于深度方向卷积核矩阵的正则化器函数(参见 keras.regularizers)。
  • bias_regularizer:应用于偏置向量的正则化函数(请参阅 keras.regularizers)。
  • activity_regularizer:应用于层输出(其“激活”)的正则化器函数(参见 keras.regularizers)。
  • depthwise_constraint:应用于深度方向卷积核矩阵的约束函数(参见 keras.constraints)。
  • bias_constraint:应用于偏置向量的约束函数(请参阅 keras.constraints)。

输入形状

4D 张量,形状为:如果 data_format='channels_first',则为 [batch_size, channels, rows, cols];如果 data_format='channels_last',则为 [batch_size, rows, cols, channels]

输出形状

4D 张量,形状为:如果 data_format='channels_first',则为 [batch_size, channels * depth_multiplier, new_rows, new_cols];如果 data_format='channels_last',则为 [batch_size, new_rows, new_cols, channels * depth_multiplier]。由于填充,rowscols 的值可能会发生变化。

返回

一个秩为 4 的张量,表示 activation(depthwiseconv2d(inputs, kernel) + bias)

引发

  • ValueError:如果 padding 为 "causal"。 ("因果")
  • ValueError:当 strides > 1 且 dilation_rate > 1 时。