深度可分离卷积
深度可分离卷积 = depth-wise conv + point-wise conv。
depth-wise conv:输入特征图每个通道作为一个group,用一个kernel处理。
point-wise conv:kernel size =1 的普通卷积。
import torch
import torch.nn as nn
stride = 1
kernel_size = 3
in_ch = 10
out_ch = 3
conv_normal = nn.Conv2d(in_channels=in_ch,
out_channels=out_ch,
kernel_size=kernel_size,
stride=stride)
input = torch.ones(1, 10, 20, 20) # input feature map
out_conv = conv_normal(input)
print('normal conv out :', out_conv.size())
# depth-separable conv
# 1.depth-wise conv
group_size = input.size(1)
depthwise_conv = nn.Conv2d(in_channels=in_ch,
out_channels=group_size,
kernel_size=kernel_size,
stride=stride,
groups=group_size)
out_depth_conv = depthwise_conv(input)
print('depth-wise conv out :', out_depth_conv.size())
# 2.point-wise conv
in_ch = out_depth_conv.size(1)
point_conv = nn.Conv2d(in_channels=in_ch,
out_channels=out_ch,
kernel_size=1,
stride=stride)
out_point_conv = point_conv(out_depth_conv)
print('point conv out :', out_point_conv.size())
normal conv out : torch.Size([1, 3, 18, 18])
depth-wise conv out : torch.Size([1, 10, 18, 18])
point conv out : torch.Size([1, 3, 18, 18])