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
)

深度可分离二维卷积。

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

其实现步骤如下:

  • 将输入拆分为单个通道。
  • 使用具有 depth_multiplier 个输出通道的单个深度卷积核对每个通道进行卷积。
  • 沿通道轴连接卷积后的输出。

与常规的二维卷积不同,深度可分离卷积不会混合不同输入通道的信息。

depth_multiplier 参数决定了应用于一个输入通道的滤波器数量。因此,它控制了深度步骤中每个输入通道生成的输出通道数量。

参数

  • kernel_size:整数或包含 2 个整数的元组/列表,指定二维卷积窗口的高度和宽度。可以是单个整数,为所有空间维度指定相同的值。
  • strides:整数或包含 2 个整数的元组/列表,指定沿高度和宽度的卷积步长。可以是单个整数,为所有空间维度指定相同的值。当前实现仅支持行和列维度上长度相等的步长。指定任何步长值 != 1 与指定任何 dilation_rate 值 != 1 不兼容。
  • 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 个整数的元组/列表,指定用于膨胀卷积的膨胀率。目前,指定任何 dilation_rate 值 != 1 与指定任何 strides 值 != 1 不兼容。
  • 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)。

输入形状

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

输出形状

如果 data_format='channels_first',则为形状为 [batch_size, channels * depth_multiplier, new_rows, new_cols] 的 4D 张量;如果 data_format='channels_last',则为形状为 [batch_size, new_rows, new_cols, channels * depth_multiplier] 的 4D 张量。由于填充,rowscols 值可能已更改。

返回值

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

引发异常

  • ValueError:如果 padding 为 "causal"。
  • ValueError:当 strides > 1 和 dilation_rate > 1 时。