2023年4月15日
DWConv
代码
YOLOX 版本
# 深度可分离卷积的作用:# 深度可分离卷积的计算量比普通卷积小,因为深度可分离卷积分为两步,第一步是深度卷积,第二步是点卷积,深度卷积的计算量比普通卷积小,点卷积的计算量比普通卷积小class DWConv(nn.Module): """Depthwise Conv + Conv"""
def __init__(self, in_channels, out_channels, ksize, stride=1, act="silu"): super().__init__() self.dconv = BaseConv( # dconv表示深度可分离卷积 in_channels, # 输入通道数 in_channels, # 输出通道数 ksize=ksize, # 卷积核大小 stride=stride, # 步长 groups=in_channels, # 分组卷积,当groups=in_channels时,表示使用深度可分离卷积 # 分组卷积的作用是将输入的通道数分成groups组,每组的通道数为in_channels//groups,然后对每组通道数进行卷积,最后将每组卷积后的特征图进行拼接 act=act, ) self.pconv = BaseConv( # pconv表示pointwise卷积 in_channels, out_channels, ksize=1, stride=1, groups=1, act=act # pointwise卷积的作用是将输入的特征图的通道数从in_channels变为out_channels )
def forward(self, x): x = self.dconv(x) return self.pconv(x)YOLOV8版本
class DWConv(Conv): """Depth-wise convolution."""
def __init__(self, c1, c2, k=1, s=1, d=1, act=True): # ch_in, ch_out, kernel, stride, dilation, activation """Initialize Depth-wise convolution with given parameters.""" super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), d=d, act=act)模型结构

对比分析
YOLOX中使用了DW+PW,YOLOV8中只使用了DW