Exemplo n.º 1
0
 def __init__(self, inplanes, outplanes, stride=1, dilation=1, kernel=3, groups=(1, 1), t=6, norm='bn', bn_eps=1e-5,
              se_ratio=0, activation=nn.ReLU6, se_reduce_mid=False, se_divisible=False, force_residual=False,
              sync_se_act=True):
     super(LinearBottleneck, self).__init__()
     padding = (dilation * kernel - dilation) // 2
     self.stride = stride
     self.inplanes, self.outplanes, innerplanes = int(inplanes), int(outplanes), make_divisible(inplanes * t, 8)
     self.t = t
     self.force_residual = force_residual
     if self.t != 1:
         self.conv1 = nn.Conv2d(self.inplanes, innerplanes, kernel_size=1, padding=0, stride=1, groups=groups[0],
                                bias=False)
         self.bn1 = make_norm(innerplanes, norm=norm, eps=bn_eps)
     self.conv2 = nn.Conv2d(innerplanes, innerplanes, kernel_size=kernel, padding=padding, stride=stride,
                            dilation=dilation, groups=innerplanes, bias=False)
     self.bn2 = make_norm(innerplanes, norm=norm, eps=bn_eps)
     se_base_chs = innerplanes if se_reduce_mid else self.inplanes
     se_innerplanse = make_divisible(se_base_chs * se_ratio, 8) if se_divisible else int(se_base_chs * se_ratio)
     self.se = SeConv2d(innerplanes, se_innerplanse, activation if sync_se_act else nn.ReLU) if se_ratio else None
     self.conv3 = nn.Conv2d(innerplanes, self.outplanes, kernel_size=1, padding=0, stride=1, groups=groups[1],
                            bias=False)
     self.bn3 = make_norm(self.outplanes, norm=norm, eps=bn_eps)
     try:
         self.activation = activation(inplace=True)
     except:
         self.activation = activation()
Exemplo n.º 2
0
    def _make_layer(self, block, lc, dilation=1):
        layers = []
        for i in range(0, len(lc)):
            layers.append(block(self.inplanes, make_divisible(lc[i][1] * self.widen_factor, 8),
                                stride=lc[i][4], dilation=dilation, kernel=lc[i][0], groups=(lc[i][5], lc[i][6]),
                                t=lc[i][7], norm=self.norm, se_ratio=lc[i][2],
                                activation=self.activation_type if lc[i][3] else nn.ReLU6))
            self.inplanes = make_divisible(lc[i][1] * self.widen_factor, 8)

        return nn.Sequential(*layers)
Exemplo n.º 3
0
    def __init__(self, setting='large', widen_factor=1.0, norm='bn', activation=H_Swish, drop_rate=0.0,
                 num_classes=1000):
        """ Constructor
        Args:
            widen_factor: config of widen_factor
            num_classes: number of classes
        """
        super(MobileNetV3, self).__init__()
        block = LinearBottleneck
        self.widen_factor = widen_factor
        self.norm = norm
        self.se_reduce_mid = True
        self.se_divisible = False
        self.head_use_bias = False
        self.force_residual = False
        self.sync_se_act = True
        self.bn_eps = 1e-5
        self.drop_rate = drop_rate
        self.activation_type = activation

        layers_cfg = MV3_CFG[setting]
        num_of_channels = [lc[-1][1] for lc in layers_cfg[1:-1]]
        self.channels = [make_divisible(ch * self.widen_factor, 8) for ch in num_of_channels]
        self.activation = activation() if layers_cfg[0][0][3] else nn.ReLU(inplace=True)

        self.inplanes = make_divisible(layers_cfg[0][0][1] * self.widen_factor, 8)
        self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=layers_cfg[0][0][0], stride=layers_cfg[0][0][4],
                               padding=layers_cfg[0][0][0] // 2, bias=False)
        self.bn1 = make_norm(self.inplanes, norm=self.norm, eps=self.bn_eps)

        self.layer0 = self._make_layer(block, layers_cfg[1], dilation=1) if layers_cfg[1][0][0] else None
        self.layer1 = self._make_layer(block, layers_cfg[2], dilation=1)
        self.layer2 = self._make_layer(block, layers_cfg[3], dilation=1)
        self.layer3 = self._make_layer(block, layers_cfg[4], dilation=1)
        self.layer4 = self._make_layer(block, layers_cfg[5], dilation=1)

        last_ch = make_divisible(layers_cfg[-1][0][1] * self.widen_factor, 8)
        self.last_stage = nn.Sequential(
            nn.Conv2d(self.inplanes, last_ch, kernel_size=layers_cfg[-1][0][0], stride=layers_cfg[-1][0][4],
                      padding=layers_cfg[-1][0][0] // 2, bias=False),
            make_norm(last_ch, norm=self.norm, eps=self.bn_eps),
            activation() if layers_cfg[-1][0][3] else nn.ReLU(inplace=True),
            SeConv2d(last_ch, int(last_ch * layers_cfg[-1][0][2]), activation) if layers_cfg[-1][0][2] else nn.Identity()
        )
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.conv_out = nn.Sequential(
            nn.Conv2d(last_ch, layers_cfg[-1][1][1], kernel_size=layers_cfg[-1][1][0], stride=layers_cfg[-1][1][4],
                      padding=layers_cfg[-1][1][0] // 2, bias=self.head_use_bias),
            activation() if layers_cfg[-1][1][3] else nn.ReLU(inplace=True)
        )
        self.fc = nn.Linear(layers_cfg[-1][1][1], num_classes)

        self._init_weights()
Exemplo n.º 4
0
    def __init__(self, norm='bn', activation=nn.ReLU6, stride=32):
        """ Constructor
        """
        super(MobileNetV2, self).__init__()
        block = mv2.LinearBottleneck
        self.use_se = cfg.BACKBONE.MV2.USE_SE
        self.widen_factor = cfg.BACKBONE.MV2.WIDEN_FACTOR
        self.norm = norm
        self.activation_type = activation
        try:
            self.activation = activation(inplace=True)
        except:
            self.activation = activation()
        self.stride = stride

        layers_cfg = mv2.model_se(
            mv2.MV2_CFG['A']) if self.use_se else mv2.MV2_CFG['A']
        num_of_channels = [lc[-1][1] for lc in layers_cfg[1:-1]]
        self.channels = [
            make_divisible(ch * self.widen_factor, 8) for ch in num_of_channels
        ]
        self.layers = [len(lc) for lc in layers_cfg[2:-1]]

        self.inplanes = make_divisible(layers_cfg[0][0][1] * self.widen_factor,
                                       8)
        self.conv1 = nn.Conv2d(3,
                               self.inplanes,
                               kernel_size=layers_cfg[0][0][0],
                               stride=layers_cfg[0][0][4],
                               padding=layers_cfg[0][0][0] // 2,
                               bias=False)
        self.bn1 = make_norm(self.inplanes, norm=self.norm)

        self.layer0 = self._make_layer(block, layers_cfg[1], dilation=1)
        self.layer1 = self._make_layer(block, layers_cfg[2], dilation=1)
        self.layer2 = self._make_layer(block, layers_cfg[3], dilation=1)
        self.layer3 = self._make_layer(block, layers_cfg[4], dilation=1)
        self.layer4 = self._make_layer(block, layers_cfg[5], dilation=1)

        self.spatial_scale = [1 / 4., 1 / 8., 1 / 16., 1 / 32.]
        self.dim_out = self.stage_out_dim[1:int(math.log(self.stride, 2))]

        del self.conv_out
        del self.bn_out
        del self.avgpool
        del self.fc
        self._init_weights()
        self._init_modules()
Exemplo n.º 5
0
    def __init__(self, norm='bn', activation=mv3.H_Swish, stride=32):
        """ Constructor
        """
        super(MobileNetV3, self).__init__()
        block = mv3.LinearBottleneck
        self.widen_factor = cfg.BACKBONE.MV3.WIDEN_FACTOR
        self.norm = norm
        self.se_reduce_mid = cfg.BACKBONE.MV3.SE_REDUCE_MID
        self.se_divisible = cfg.BACKBONE.MV3.SE_DIVISIBLE
        self.head_use_bias = cfg.BACKBONE.MV3.HEAD_USE_BIAS
        self.force_residual = cfg.BACKBONE.MV3.FORCE_RESIDUAL
        self.sync_se_act = cfg.BACKBONE.MV3.SYNC_SE_ACT
        self.bn_eps = cfg.BACKBONE.BN_EPS
        self.activation_type = activation
        self.stride = stride

        setting = cfg.BACKBONE.MV3.SETTING
        layers_cfg = mv3.MV3_CFG[setting]
        num_of_channels = [lc[-1][1] for lc in layers_cfg[1:-1]]
        self.channels = [make_divisible(ch * self.widen_factor, 8) for ch in num_of_channels]
        self.activation = activation() if layers_cfg[0][0][3] else nn.ReLU(inplace=True)
        self.layers = [len(lc) for lc in layers_cfg[2:-1]]

        self.inplanes = make_divisible(layers_cfg[0][0][1] * self.widen_factor, 8)
        self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=layers_cfg[0][0][0], stride=layers_cfg[0][0][4],
                               padding=layers_cfg[0][0][0] // 2, bias=False)
        self.bn1 = make_norm(self.inplanes, norm=self.norm, eps=self.bn_eps)

        self.layer0 = self._make_layer(block, layers_cfg[1], dilation=1) if layers_cfg[1][0][0] else None
        self.layer1 = self._make_layer(block, layers_cfg[2], dilation=1)
        self.layer2 = self._make_layer(block, layers_cfg[3], dilation=1)
        self.layer3 = self._make_layer(block, layers_cfg[4], dilation=1)
        self.layer4 = self._make_layer(block, layers_cfg[5], dilation=1)

        self.spatial_scale = [1 / 4., 1 / 8., 1 / 16., 1 / 32.]
        self.dim_out = self.stage_out_dim[1:int(math.log(self.stride, 2))]

        del self.last_stage
        del self.avgpool
        del self.conv_out
        del self.fc
        self._init_weights()
        self._init_modules()
Exemplo n.º 6
0
    def __init__(self, use_se=False, widen_factor=1.0, norm='bn', activation=nn.ReLU6, drop_rate=0.0, num_classes=1000):
        """ Constructor
        Args:
            widen_factor: config of widen_factor
            num_classes: number of classes
        """
        super(MobileNetV2, self).__init__()
        block = LinearBottleneck
        self.use_se = use_se
        self.widen_factor = widen_factor
        self.norm = norm
        self.drop_rate = drop_rate
        self.activation_type = activation
        try:
            self.activation = activation(inplace=True)
        except:
            self.activation = activation()

        layers_cfg = model_se(MV2_CFG['A']) if self.use_se else MV2_CFG['A']
        num_of_channels = [lc[-1][1] for lc in layers_cfg[1:-1]]
        self.channels = [make_divisible(ch * self.widen_factor, 8) for ch in num_of_channels]

        self.inplanes = make_divisible(layers_cfg[0][0][1] * self.widen_factor, 8)
        self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=layers_cfg[0][0][0], stride=layers_cfg[0][0][4],
                               padding=layers_cfg[0][0][0] // 2, bias=False)
        self.bn1 = make_norm(self.inplanes, norm=self.norm)

        self.layer0 = self._make_layer(block, layers_cfg[1], dilation=1)
        self.layer1 = self._make_layer(block, layers_cfg[2], dilation=1)
        self.layer2 = self._make_layer(block, layers_cfg[3], dilation=1)
        self.layer3 = self._make_layer(block, layers_cfg[4], dilation=1)
        self.layer4 = self._make_layer(block, layers_cfg[5], dilation=1)

        out_ch = layers_cfg[-1][-1][1]
        self.conv_out = nn.Conv2d(self.inplanes, out_ch, kernel_size=layers_cfg[-1][-1][0],
                                  stride=layers_cfg[-1][-1][4], padding=layers_cfg[-1][-1][0] // 2, bias=False)
        self.bn_out = make_norm(out_ch, norm=self.norm)

        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(out_ch, num_classes)

        self._init_weights()
Exemplo n.º 7
0
    def __init__(self, norm='bn', activation=nn.ReLU, stride=32):
        """ Constructor
        """
        super(MobileNetV1, self).__init__()
        block = mv1.BasicBlock
        self.use_se = cfg.BACKBONE.MV1.USE_SE
        self.norm = norm
        self.activation_type = activation
        try:
            self.activation = activation(inplace=True)
        except:
            self.activation = activation()
        self.stride = stride

        layers = cfg.BACKBONE.MV1.LAYERS[:int(math.log(stride, 2)) - 1]
        self.layers = layers
        kernel = cfg.BACKBONE.MV1.KERNEL
        c5_dilation = cfg.BACKBONE.MV1.C5_DILATION
        num_of_channels = cfg.BACKBONE.MV1.NUM_CHANNELS
        channels = [
            make_divisible(ch * cfg.BACKBONE.MV1.WIDEN_FACTOR, 8)
            for ch in num_of_channels
        ]
        self.channels = channels

        self.conv1 = nn.Conv2d(3,
                               channels[0],
                               kernel_size=3,
                               stride=2,
                               padding=1,
                               bias=False)
        self.bn1 = make_norm(channels[0], norm=self.norm)
        self.conv2 = nn.Conv2d(channels[0],
                               channels[0],
                               kernel_size=kernel,
                               stride=1,
                               padding=kernel // 2,
                               groups=channels[0],
                               bias=False)
        self.bn2 = make_norm(channels[0], norm=self.norm)
        self.conv3 = nn.Conv2d(channels[0],
                               channels[1],
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.bn3 = make_norm(channels[1], norm=self.norm)
        self.inplanes = channels[1]

        self.layer1 = self._make_layer(block,
                                       channels[2],
                                       layers[0],
                                       stride=2,
                                       dilation=1,
                                       kernel=kernel)
        self.layer2 = self._make_layer(block,
                                       channels[3],
                                       layers[1],
                                       stride=2,
                                       dilation=1,
                                       kernel=kernel)
        self.layer3 = self._make_layer(block,
                                       channels[4],
                                       layers[2],
                                       stride=2,
                                       dilation=1,
                                       kernel=kernel)
        self.layer4 = self._make_layer(block,
                                       channels[5],
                                       layers[3],
                                       stride=1 if c5_dilation != 1 else 2,
                                       dilation=c5_dilation,
                                       kernel=kernel)

        self.spatial_scale = [1 / 4., 1 / 8., 1 / 16., 1 / 32. * c5_dilation]
        self.dim_out = self.stage_out_dim[1:int(math.log(self.stride, 2))]

        self.dropblock = ops.DropBlock2D(
            keep_prob=0.9, block_size=7) if cfg.BACKBONE.MV1.USE_DP else None

        del self.avgpool
        del self.fc
        self._init_weights()
        self._init_modules()
Exemplo n.º 8
0
    def __init__(self,
                 use_se=False,
                 widen_factor=1.0,
                 kernel=3,
                 layers=(2, 2, 6, 2),
                 norm='bn',
                 activation=nn.ReLU,
                 drop_rate=0.0,
                 num_classes=1000):
        """ Constructor
        Args:
            widen_factor: config of widen_factor
            num_classes: number of classes
        """
        super(MobileNetV1, self).__init__()
        block = BasicBlock
        self.use_se = use_se
        self.norm = norm
        self.drop_rate = drop_rate
        self.activation_type = activation
        try:
            self.activation = activation(inplace=True)
        except:
            self.activation = activation()

        num_of_channels = [32, 64, 128, 256, 512, 1024]
        channels = [
            make_divisible(ch * widen_factor, 8) for ch in num_of_channels
        ]
        self.channels = channels

        self.conv1 = nn.Conv2d(3,
                               channels[0],
                               kernel_size=3,
                               stride=2,
                               padding=1,
                               bias=False)
        self.bn1 = make_norm(channels[0], norm=self.norm)
        self.conv2 = nn.Conv2d(channels[0],
                               channels[0],
                               kernel_size=kernel,
                               stride=1,
                               padding=kernel // 2,
                               groups=channels[0],
                               bias=False)
        self.bn2 = make_norm(channels[0], norm=self.norm)
        self.conv3 = nn.Conv2d(channels[0],
                               channels[1],
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.bn3 = make_norm(channels[1], norm=self.norm)
        self.inplanes = channels[1]

        self.layer1 = self._make_layer(block,
                                       channels[2],
                                       layers[0],
                                       stride=2,
                                       dilation=1,
                                       kernel=kernel)
        self.layer2 = self._make_layer(block,
                                       channels[3],
                                       layers[1],
                                       stride=2,
                                       dilation=1,
                                       kernel=kernel)
        self.layer3 = self._make_layer(block,
                                       channels[4],
                                       layers[2],
                                       stride=2,
                                       dilation=1,
                                       kernel=kernel)
        self.layer4 = self._make_layer(block,
                                       channels[5],
                                       layers[3],
                                       stride=2,
                                       dilation=1,
                                       kernel=kernel)

        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(channels[5], num_classes)

        self._init_weights()