• MobileNet V1、V2网络详解及V2复现


    • MobileNet V1、V2网络详解:

    https://mp.weixin.qq.com/s?__biz=Mzk0MzIzODM5MA==&mid=2247485219&idx=1&sn=dcd5040edf753acd1f748470159ccce9&chksm=c337babaf44033aca18c9834652c7dc9e1808e47ef6249d3fdcd072a836a274feab2fa649dad#rd

    • V2复现:
    from torch import nn
    import torch
    
    class MobileNetV2(nn.Module):
        def __init__(self, num_classes=1000, alpha=1.0, round_nearest=8):
            super(MobileNetV2, self).__init__()
            block = InvertedResidual
            input_channel = _make_divisible(32 * alpha, round_nearest)
            last_channel = _make_divisible(1280 * alpha, round_nearest)
    
            inverted_residual_setting = [
                # t, c, n, s
                [1, 16, 1, 1],
                [6, 24, 2, 2],
                [6, 32, 3, 2],
                [6, 64, 4, 2],
                [6, 96, 3, 1],
                [6, 160, 3, 2],
                [6, 320, 1, 1],
            ]
    
            features = []
            # conv1 layer
            features.append(ConvBNReLU(3, input_channel, stride=2))
            # building inverted residual residual blockes
            for t, c, n, s in inverted_residual_setting:
                output_channel = _make_divisible(c * alpha, round_nearest)
                for i in range(n):
                    stride = s if i == 0 else 1
                    features.append(block(input_channel, output_channel, stride, expand_ratio=t))
                    input_channel = output_channel
            # building last several layers
            features.append(ConvBNReLU(input_channel, last_channel, 1))
            # combine feature layers
            self.features = nn.Sequential(*features)
    
            # building classifier
            self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
            self.classifier = nn.Sequential(
                nn.Dropout(0.2),
                nn.Linear(last_channel, num_classes)
            )
    
            # weight initialization
            for m in self.modules():
                if isinstance(m, nn.Conv2d):
                    nn.init.kaiming_normal_(m.weight, mode='fan_out')
                    if m.bias is not None:
                        nn.init.zeros_(m.bias)
                elif isinstance(m, nn.BatchNorm2d):
                    nn.init.ones_(m.weight)
                    nn.init.zeros_(m.bias)
                elif isinstance(m, nn.Linear):
                    nn.init.normal_(m.weight, 0, 0.01)
                    nn.init.zeros_(m.bias)
    
        def forward(self, x):
            x = self.features(x)
            x = self.avgpool(x)
            x = torch.flatten(x, 1)
            x = self.classifier(x)
            return x
    
    def _make_divisible(ch, divisor=8, min_ch=None):
        """
        This function is taken from the original tf repo.
        It ensures that all layers have a channel number that is divisible by 8
        It can be seen here:
        https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
        """
        if min_ch is None:
            min_ch = divisor
        new_ch = max(min_ch, int(ch + divisor / 2) // divisor * divisor)
        # Make sure that round down does not go down by more than 10%.
        if new_ch < 0.9 * ch:
            new_ch += divisor
        return new_ch
    
    class ConvBNReLU(nn.Sequential):
        def __init__(self, in_channel, out_channel, kernel_size=3, stride=1, groups=1):
            padding = (kernel_size - 1) // 2
            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)
            )
    
    class InvertedResidual(nn.Module):
        def __init__(self, in_channel, out_channel, stride, expand_ratio):
            super(InvertedResidual, self).__init__()
            hidden_channel = in_channel * expand_ratio
            self.use_shortcut = stride == 1 and in_channel == out_channel
    
            layers = []
            if expand_ratio != 1:
                # 1x1 pointwise conv
                layers.append(ConvBNReLU(in_channel, hidden_channel, kernel_size=1))
            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),
            ])
    
            self.conv = nn.Sequential(*layers)
    
        def forward(self, x):
            if self.use_shortcut:
                return x + self.conv(x)
            else:
                return self.conv(x)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
  • 相关阅读:
    一文了解Go语言的I/O接口设计
    linux(rhel7)内核参数优化
    1000W用户1Wqps高并发签到系统的架构和实操
    Qt5开发从入门到精通——第三篇(窗口篇——停靠窗口)
    AI实战营第二期 第八节 《MMSegmentation代码课》——笔记9
    如何查看网站的https的数字证书
    云计算计算资源池与存储池访问逻辑
    元器件降额设计标准与要点参考总结
    【附源码】Python计算机毕业设计软件工程在线学习平台
    【Unity Shader】顶点片元着色器01 基本命令介绍
  • 原文地址:https://blog.csdn.net/qq_42363032/article/details/126917884