1. def __init__(groups=1):-》group=1表示是普通卷积,group=2表示Depthwise(DW)卷积。
2.padding = (kernel_size - 1) // 2-》padding由kernel_size来决定。
3.然后定义网络结构:
super(ConvBNReLU, self).__init__(
nn.Conv2d(in_channel, out_channel, kernel_size, stride, padding, groups=groups, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU6(inplace=True)
)
到这里ConvBNReLU网络结构定义完毕!
定义倒参差网络结构
1.expand_ratio-》表示扩展因子。
2.hidden_channel = in_channel * expand_ratio中的hidden_channel表示输出深度也是卷积核数量。
3.use_shortcut-》判断是否在正向传播过程中使用Mobile的捷径分支。
4.stride == 1 and in_channel == out_channel-》判断使用捷径分支条件:stride == 1并且输入深度等于输出深度。
5. if expand_ratio != 1:-》判断扩展因子是不是等于1,不等于1就添加一个1x1的卷积层。等于1的话就没有1x1的卷积层。
接下来添加一系列的层结构:
layers.extend([
# 3x3 depthwise conv
ConvBNReLU(hidden_channel, hidden_channel, stride=stride, groups=hidden_channel),
# 1x1 pointwise conv(linear)
nn.Conv2d(hidden_channel, out_channel, kernel_size=1, bias=False),
nn.BatchNorm2d(out_channel),
])
forward前向传播:
def forward(self, x):
if self.use_shortcut:
return x + self.conv(x)
else:
return self.conv(x)
-》如果use_short