智能数字图像处理:MobileNetV2代码(pytorch)之model.py解读

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

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值