在keras代码中我们看到有SeparableConv2D和DepthwiseConv2D卷积,我们来聊聊他们之间的差别。
理解
SeparableConv2D实现的是整个深度可分离卷积,包含深度卷积(DW)和逐点卷积(PW)这两个过程,
DepthwiseConv2D只是实现了深度卷积,可见SeparableConv2D是比DepthwiseConv2D多了一个逐点卷积的过程。
正如下图所示:
就是SeparableConv2D的实现过程,其中深层卷积把通道进行分离,然后逐点卷积(1x1 conv)进行通道连接。
代码实现
在keras中的实现方法:
from keras import layers
model = Sequential()
model.add(layers.SeparableConv2D(64, 3, activation='relu'))
用pytorch代码实现:
class DepthwiseConv(nn.Module):
def __init__(self, inp, oup):
super(DepthwiseConv, self).__init__()
self.depth_conv = nn.Sequential(
# dw
nn.Conv2d(inp, inp, kernel_size=3, stride=1, padding=1, groups=inp, bias=False),
nn.BatchNorm2d(inp),
nn.ReLU6(inplace=True),
# pw
nn.Conv2d(inp, oup, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU6(inplace=True)
)
def forward(self, x):
return self.depth_conv(x)
参考:https://blog.csdn.net/c_chuxin/article/details/88581411