Example #1
0
    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 dilation=1,
                 kernel=3,
                 norm='bn',
                 use_se=False,
                 activation=nn.ReLU):
        super(BasicBlock, self).__init__()
        padding = (dilation * kernel - dilation) // 2
        self.inplanes, self.planes = int(inplanes), int(planes)

        self.conv1 = nn.Conv2d(inplanes,
                               inplanes,
                               kernel_size=kernel,
                               padding=padding,
                               stride=stride,
                               dilation=dilation,
                               groups=inplanes,
                               bias=False)
        self.bn1 = make_norm(inplanes, norm=norm)
        self.conv2 = nn.Conv2d(inplanes,
                               planes,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.bn2 = make_norm(planes, norm=norm)
        self.se = ops.Se2d(planes, reduction=4) if use_se else None
        try:
            self.activation = activation(inplace=True)
        except:
            self.activation = activation()
Example #2
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()
Example #3
0
    def _make_head(self, block, pre_stage_channels, outplanes=2048, conv='normal'):
        # Increasing the #channels on each resolution, from C, 2C, 4C, 8C to 128, 256, 512, 1024
        incre_modules = []
        for i, channels in enumerate(pre_stage_channels):
            self.inplanes = channels
            incre_module = self._make_layer(block, self.head_dim[i], 1, stride=1, dilation=1, conv=conv)
            incre_modules.append(incre_module)
        incre_modules = nn.ModuleList(incre_modules)

        # downsampling modules
        downsamp_modules = []
        for i in range(len(pre_stage_channels) - 1):
            in_channels = self.head_dim[i] * block.expansion
            out_channels = self.head_dim[i + 1] * block.expansion

            downsamp_module = nn.Sequential(
                nn.Conv2d(in_channels, out_channels, 3, 2, 1),  # official implementation forgets bias=False
                make_norm(out_channels, norm=self.norm),
                nn.ReLU(inplace=True)
            )
            downsamp_modules.append(downsamp_module)
        downsamp_modules = nn.ModuleList(downsamp_modules)

        final_layer = nn.Sequential(
            nn.Conv2d(self.head_dim[3] * block.expansion, outplanes, 1, 1, 0),
            make_norm(outplanes, norm=self.norm),
            nn.ReLU(inplace=True)
        )

        return incre_modules, downsamp_modules, final_layer
Example #4
0
    def __init__(self, inplanes, planes, stride=1, dilation=1, norm='bn', conv='normal', use_se=False,
                 stride_3x3=False, downsample=None):
        super(AlignedBasicBlock, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError('{} type conv operation is not supported.'.format(conv))

        self.conv1_1 = conv_op(inplanes, planes, kernel_size=3, stride=stride, dilation=dilation, padding=dilation,
                               bias=False)
        self.conv2_1 = conv_op(inplanes, planes // 2, kernel_size=3, stride=stride, dilation=dilation, padding=dilation,
                               bias=False)
        self.bn2_1 = make_norm(planes // 2, norm=norm)
        self.conv2_2 = conv_op(planes // 2, planes // 2, kernel_size=3, stride=1, dilation=dilation,
                               padding=dilation, bias=False)
        self.bn_concat = make_norm(planes + (planes // 2), norm=norm)

        self.conv = nn.Conv2d(planes + (planes // 2), planes, kernel_size=3, stride=1, dilation=dilation,
                              padding=dilation, bias=False)
        self.bn = make_norm(planes, norm=norm)

        self.se = ops.Se2d(planes, reduction=16) if use_se else None
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Example #5
0
    def __init__(self,
                 inplanes,
                 planes,
                 base_width=64,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 radix=1,
                 stride_3x3=False,
                 downsample=None):
        super(BasicBlock, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))
        assert context in ['none', 'se', 'gcb']
        width = int(planes * (base_width / 64.))

        self.conv1 = conv_op(inplanes,
                             width,
                             kernel_size=3,
                             stride=stride,
                             dilation=dilation,
                             padding=dilation,
                             bias=False)
        self.bn1 = make_norm(width, norm=norm, an_k=10 if planes < 256 else 20)
        self.conv2 = conv_op(width,
                             width,
                             kernel_size=3,
                             stride=1,
                             dilation=dilation,
                             padding=dilation,
                             bias=False)
        self.bn2 = make_norm(width, norm=norm, an_k=10 if planes < 256 else 20)

        if context == 'none':
            self.ctx = None
        elif context == 'se':
            self.ctx = ops.SeConv2d(width, int(width * ctx_ratio))
        elif context == 'gcb':
            self.ctx = ops.GlobalContextBlock(width, int(width * ctx_ratio))
        else:
            raise ValueError(
                '{} type context operation is not supported.'.format(context))

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Example #6
0
    def __init__(self, block, planes, norm='bn', conv='normal', use_se=False, use_global=False, stage=2,
                 output_branches=2):
        super(StageModule, self).__init__()
        self.use_global = use_global

        self.branches = nn.ModuleList()
        for i in range(stage):
            w = planes * (2 ** i)
            branch = nn.Sequential(
                block(w, w, 1, 1, norm, conv, use_se, True),
                block(w, w, 1, 1, norm, conv, use_se, True),
                block(w, w, 1, 1, norm, conv, use_se, True),
                block(w, w, 1, 1, norm, conv, use_se, True),
            )
            self.branches.append(branch)

        self.fuse_layers = nn.ModuleList()
        if self.use_global:
            self.global_layers = nn.ModuleList()
        # for each output_branches (i.e. each branch in all cases but the very last one)
        for i in range(output_branches):
            self.fuse_layers.append(nn.ModuleList())
            for j in range(stage):  # for each branch
                if i == j:
                    self.fuse_layers[-1].append(nn.Sequential())  # Used in place of "None" because it is callable
                elif i < j:
                    self.fuse_layers[-1].append(nn.Sequential(
                        nn.Conv2d(planes * (2 ** j), planes * (2 ** i), 1, 1, 0, bias=False),
                        make_norm(planes * (2 ** i), norm=norm),
                        nn.Upsample(scale_factor=(2.0 ** (j - i)), mode='nearest'),
                    ))
                elif i > j:
                    ops = []
                    for k in range(i - j - 1):
                        ops.append(nn.Sequential(
                            nn.Conv2d(planes * (2 ** j), planes * (2 ** j), 3, 2, 1, bias=False),
                            make_norm(planes * (2 ** j), norm=norm),
                            nn.ReLU(inplace=True),
                        ))
                    ops.append(nn.Sequential(
                        nn.Conv2d(planes * (2 ** j), planes * (2 ** i), 3, 2, 1, bias=False),
                        make_norm(planes * (2 ** i), norm=norm),
                    ))
                    self.fuse_layers[-1].append(nn.Sequential(*ops))
            if self.use_global:
                sum_planes = sum([planes * (2 ** k) for k in range(stage)])
                self.global_layers.append(
                    nn.Sequential(
                        nn.Conv2d(sum_planes, planes * (2 ** i), 1, 1, 0, bias=False),
                        make_norm(planes * (2 ** i), norm=norm),
                        nn.Sigmoid()
                    )
                )

        self.relu = nn.ReLU(inplace=True)
Example #7
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()
Example #8
0
    def _make_layer(self,
                    block,
                    planes,
                    blocks,
                    stride=1,
                    dilation=1,
                    conv='normal',
                    context='none'):
        """ Stack n bottleneck modules where n is inferred from the depth of the network.
        Args:
            block: block type used to construct ResNet
            planes: number of output channels (need to multiply by block.expansion)
            blocks: number of blocks to be built
            stride: factor to reduce the spatial dimensionality in the first bottleneck of the block.
        Returns: a Module consisting of n sequential bottlenecks.
        """
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            if self.avg_down:
                downsample = nn.Sequential(
                    nn.AvgPool2d(kernel_size=stride, stride=stride),
                    nn.Conv2d(self.inplanes,
                              planes * block.expansion,
                              kernel_size=1,
                              stride=1,
                              bias=False),
                    make_norm(planes * block.expansion,
                              norm=self.norm.split('_')[-1]),
                )
            else:
                downsample = nn.Sequential(
                    nn.Conv2d(self.inplanes,
                              planes * block.expansion,
                              kernel_size=1,
                              stride=stride,
                              bias=False),
                    make_norm(planes * block.expansion,
                              norm=self.norm.split('_')[-1]),
                )

        layers = []
        layers.append(
            block(self.inplanes, planes, self.base_width, stride, dilation,
                  self.norm, conv, context, self.ctx_ratio, self.radix,
                  self.stride_3x3, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(
                block(self.inplanes, planes, self.base_width, 1, dilation,
                      self.norm, conv, context, self.ctx_ratio, self.radix,
                      self.stride_3x3))

        return nn.Sequential(*layers)
Example #9
0
    def __init__(self, aligned=False, use_se=False, use_global=False, avg_down=False, base_width=32, norm='bn',
                 stage_with_conv=('normal', 'normal', 'normal', 'normal'), num_classes=1000):
        """ Constructor
        Args:
            layers: config of layers, e.g., (3, 4, 23, 3)
            num_classes: number of classes
        """
        super(HRNet, self).__init__()
        block_1 = AlignedBottleneck if aligned else Bottleneck
        block_2 = AlignedBasicBlock if aligned else BasicBlock

        self.use_se = use_se
        self.avg_down = avg_down
        self.base_width = base_width
        self.norm = norm
        self.head_dim = (32, 64, 128, 256)

        self.inplanes = 64  # default 64
        self.conv1 = nn.Conv2d(3, 64, 3, 2, 1, bias=False)
        self.bn1 = make_norm(64, norm=self.norm)
        self.conv2 = nn.Conv2d(64, 64, 3, 2, 1, bias=False)
        self.bn2 = make_norm(64, norm=self.norm)
        self.relu = nn.ReLU(inplace=True)

        self.layer1 = self._make_layer(block_1, 64, 4, 1, conv=stage_with_conv[0])  # 4 blocks without down sample
        self.transition1 = self._make_transition(index=1, stride=2)  # Fusion layer 1: create full and 1/2 resolution

        self.stage2 = nn.Sequential(
            StageModule(block_2, base_width, norm, stage_with_conv[1], use_se, False, stage=2, output_branches=2),
        )  # Stage 2 with 1 group of block modules, which has 2 branches
        self.transition2 = self._make_transition(index=2, stride=2)  # Fusion layer 2: create 1/4 resolution

        self.stage3 = nn.Sequential(
            StageModule(block_2, base_width, norm, stage_with_conv[2], use_se, use_global, stage=3, output_branches=3),
            StageModule(block_2, base_width, norm, stage_with_conv[2], use_se, use_global, stage=3, output_branches=3),
            StageModule(block_2, base_width, norm, stage_with_conv[2], use_se, use_global, stage=3, output_branches=3),
            StageModule(block_2, base_width, norm, stage_with_conv[2], use_se, use_global, stage=3, output_branches=3),
        )  # Stage 3 with 4 groups of block modules, which has 3 branches
        self.transition3 = self._make_transition(index=3, stride=2)  # Fusion layer 3: create 1/8 resolution

        self.stage4 = nn.Sequential(
            StageModule(block_2, base_width, norm, stage_with_conv[3], use_se, use_global, stage=4, output_branches=4),
            StageModule(block_2, base_width, norm, stage_with_conv[3], use_se, use_global, stage=4, output_branches=4),
            StageModule(block_2, base_width, norm, stage_with_conv[3], use_se, use_global, stage=4, output_branches=4),
        )  # Stage 4 with 3 groups of block modules, which has 4 branches

        pre_stage_channels = [base_width, base_width * 2, base_width * 4, base_width * 8]
        self.incre_modules, self.downsamp_modules, self.final_layer = \
            self._make_head(block_1, pre_stage_channels, outplanes=2048, conv=stage_with_conv[3])
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.classifier = nn.Linear(2048, num_classes)

        self._init_weights()
Example #10
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()
Example #11
0
    def _make_transition(self, index=1, stride=1):
        transition = nn.ModuleList()
        if index == 1:
            transition.append(nn.Sequential(
                nn.Conv2d(self.inplanes, self.base_width, kernel_size=3, stride=1, padding=1, bias=False),
                make_norm(self.base_width, norm=self.norm),
                nn.ReLU(inplace=True),
            ))
        else:
            transition.extend([nn.Sequential() for _ in range(index)])
        transition.append(nn.Sequential(
            nn.Sequential(  # Double Sequential to fit with official pre-trained weights
                nn.Conv2d(self.inplanes if index == 1 else self.base_width * (2 ** (index - 1)),
                          self.base_width * (2 ** index), kernel_size=3, stride=stride, padding=1, bias=False),
                make_norm(self.base_width * (2 ** index), norm=self.norm),
                nn.ReLU(inplace=True),
            )
        ))

        return transition
Example #12
0
    def __init__(self,
                 inplanes,
                 planes,
                 outplanes,
                 num_conv=5,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 identity=False):
        super(OSABlock, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))

        self.identity = identity
        self.layers = nn.ModuleList()
        dim_in = inplanes
        for i in range(num_conv):
            self.layers.append(
                nn.Sequential(
                    conv_op(dim_in,
                            planes,
                            kernel_size=3,
                            stride=1,
                            dilation=dilation,
                            padding=dilation,
                            bias=False), make_norm(planes, norm=norm),
                    nn.ReLU(inplace=True)))
            dim_in = planes

        # feature aggregation
        dim_in = inplanes + num_conv * planes
        self.concat = nn.Sequential(
            conv_op(dim_in, outplanes, kernel_size=1, stride=1, bias=False),
            make_norm(outplanes, norm=norm), nn.ReLU(inplace=True))
Example #13
0
 def __init__(self, inplanes, outplanes, stride=1, dilation=1, kernel=3, groups=(1, 1), t=6, norm='bn', se_ratio=0,
              activation=nn.ReLU6):
     super(LinearBottleneck, self).__init__()
     padding = (dilation * kernel - dilation) // 2
     self.stride = stride
     self.inplanes, self.outplanes, innerplanes = int(inplanes), int(outplanes), int(inplanes * abs(t))
     self.t = t
     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)
     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)
     self.se = ops.SeConv2d(innerplanes, int(self.inplanes * se_ratio), activation) 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)
     try:
         self.activation = activation(inplace=True)
     except:
         self.activation = activation()
Example #14
0
    def _make_layer(self, block, planes, blocks, stride=1, dilation=1, conv='normal'):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            if self.avg_down:
                downsample = nn.Sequential(
                    nn.AvgPool2d(kernel_size=stride, stride=stride),
                    nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=1, bias=False),
                    make_norm(planes * block.expansion, norm=self.norm),
                )
            else:
                downsample = nn.Sequential(
                    nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
                    make_norm(planes * block.expansion, norm=self.norm),
                )

        layers = []
        layers.append(block(self.inplanes, planes, stride, dilation, self.norm, conv, self.use_se, True,
                            downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes, 1, dilation, self.norm, conv, self.use_se, True))

        return nn.Sequential(*layers)
Example #15
0
    def __init__(self, inplanes, planes, stride=1, dilation=1, norm='bn', conv='normal', use_se=False,
                 stride_3x3=False, downsample=None):
        super(Bottleneck, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError('{} type conv operation is not supported.'.format(conv))
        (str1x1, str3x3) = (1, stride) if stride_3x3 else (stride, 1)

        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, stride=str1x1, bias=False)
        self.bn1 = make_norm(planes, norm=norm)
        self.conv2 = conv_op(planes, planes, kernel_size=3, stride=str3x3, dilation=dilation, padding=dilation,
                             bias=False)
        self.bn2 = make_norm(planes, norm=norm)
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = make_norm(planes * 4, norm=norm)
        self.se = ops.Se2d(planes * 4, reduction=16) if use_se else None
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Example #16
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()
Example #17
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()
Example #18
0
    def __init__(self, norm='bn', stride=32):
        """ Constructor
        """
        super(HRNet, self).__init__()
        block_1 = hr.AlignedBottleneck if cfg.BACKBONE.HRNET.USE_ALIGN else hr.Bottleneck
        block_2 = hr.AlignedBasicBlock if cfg.BACKBONE.HRNET.USE_ALIGN else hr.BasicBlock

        use_se = cfg.BACKBONE.HRNET.USE_SE
        self.use_se = use_se
        self.use_global = cfg.BACKBONE.HRNET.USE_GLOBAL
        self.avg_down = cfg.BACKBONE.HRNET.AVG_DOWN
        base_width = cfg.BACKBONE.HRNET.WIDTH
        self.base_width = base_width
        self.norm = norm
        self.stride = stride

        stage_with_conv = cfg.BACKBONE.HRNET.STAGE_WITH_CONV

        self.inplanes = 64  # default 64
        self.conv1 = nn.Conv2d(3, 64, 3, 2, 1, bias=False)
        self.bn1 = make_norm(64, norm=self.norm)
        self.conv2 = nn.Conv2d(64, 64, 3, 2, 1, bias=False)
        self.bn2 = make_norm(64, norm=self.norm)
        self.relu = nn.ReLU(inplace=True)

        self.layer1 = self._make_layer(
            block_1, 64, 4, 1,
            conv=stage_with_conv[0])  # 4 blocks without down sample
        self.transition1 = self._make_transition(
            index=1,
            stride=2)  # Fusion layer 1: create full and 1/2 resolution

        self.stage2 = nn.Sequential(
            hr.StageModule(block_2, base_width, norm, stage_with_conv[1],
                           use_se, False, 2, 2),
        )  # Stage 2 with 1 group of block modules, which has 2 branches
        self.transition2 = self._make_transition(
            index=2, stride=2)  # Fusion layer 2: create 1/4 resolution

        self.stage3 = nn.Sequential(
            hr.StageModule(block_2, base_width, norm, stage_with_conv[2],
                           use_se, self.use_global, 3, 3),
            hr.StageModule(block_2, base_width, norm, stage_with_conv[2],
                           use_se, self.use_global, 3, 3),
            hr.StageModule(block_2, base_width, norm, stage_with_conv[2],
                           use_se, self.use_global, 3, 3),
            hr.StageModule(block_2, base_width, norm, stage_with_conv[2],
                           use_se, self.use_global, 3, 3),
        )  # Stage 3 with 4 groups of block modules, which has 3 branches
        self.transition3 = self._make_transition(
            index=3, stride=2)  # Fusion layer 3: create 1/8 resolution

        self.stage4 = nn.Sequential(
            hr.StageModule(block_2, base_width, norm, stage_with_conv[3],
                           use_se, self.use_global, 4, 4),
            hr.StageModule(block_2, base_width, norm, stage_with_conv[3],
                           use_se, self.use_global, 4, 4),
            hr.StageModule(block_2, base_width, norm, stage_with_conv[3],
                           use_se, self.use_global, 4, 4),  # multi out
        )  # Stage 4 with 3 groups of block modules, which has 4 branches

        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.incre_modules
        del self.downsamp_modules
        del self.final_layer
        del self.avgpool
        del self.classifier
        self._init_weights()
        self._init_modules()
Example #19
0
    def __init__(self,
                 inplanes,
                 planes,
                 base_width=64,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 radix=1,
                 stride_3x3=False,
                 downsample=None):
        super(Bottleneck, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))
        (str1x1, str3x3) = (1, stride) if stride_3x3 else (stride, 1)
        width = int(planes * (base_width / 64.))
        self.radix = radix

        self.conv1 = nn.Conv2d(inplanes,
                               width,
                               kernel_size=1,
                               stride=str1x1,
                               bias=False)
        self.bn1 = make_norm(width, norm=norm.split('_')[-1])

        if radix > 1 and (str3x3 > 1 or dilation > 1):
            self.avd_layer = nn.AvgPool2d(3, str3x3, padding=1)
            str3x3 = 1
        else:
            self.avd_layer = None
        if radix > 1:
            self.conv2 = ops.SplAtConv2d(width,
                                         width,
                                         kernel_size=3,
                                         stride=str3x3,
                                         padding=dilation,
                                         dilation=dilation,
                                         bias=False,
                                         radix=radix)
        else:
            self.conv2 = conv_op(width,
                                 width,
                                 kernel_size=3,
                                 stride=str3x3,
                                 dilation=dilation,
                                 padding=dilation,
                                 bias=False)
            self.bn2 = make_norm(width,
                                 norm=norm,
                                 an_k=10 if planes < 256 else 20)
        self.conv3 = nn.Conv2d(width,
                               planes * self.expansion,
                               kernel_size=1,
                               bias=False)
        self.bn3 = make_norm(planes * self.expansion, norm=norm.split('_')[-1])

        if context == 'none':
            self.ctx = None
        elif context == 'se':
            self.ctx = ops.SeConv2d(planes * self.expansion,
                                    int(planes * self.expansion * ctx_ratio))
        elif context == 'gcb':
            self.ctx = ops.GlobalContextBlock(
                planes * self.expansion,
                int(planes * self.expansion * ctx_ratio))
        elif context == 'nonlocal':
            self.ctx = ops.NonLocal2d(planes * self.expansion,
                                      int(planes * self.expansion * ctx_ratio),
                                      planes * self.expansion,
                                      use_gn=True)
        elif context == 'msa':
            self.ctx = ops.MS_NonLocal2d(planes * self.expansion,
                                         int(planes * self.expansion *
                                             ctx_ratio),
                                         planes * self.expansion,
                                         use_gn=True)
        else:
            raise ValueError(
                '{} type context operation is not supported.'.format(context))

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Example #20
0
    def __init__(self,
                 base_width=64,
                 stage_dims=(128, 160, 192, 224),
                 concat_dims=(256, 512, 768, 1024),
                 layers=(1, 1, 2, 2),
                 num_conv=5,
                 norm='bn',
                 stage_with_conv=('normal', 'normal', 'normal', 'normal'),
                 num_classes=1000):
        """ Constructor
        Args:
            layers: config of layers, e.g., (1, 1, 2, 2)
            num_classes: number of classes
        """
        super(VoVNet, self).__init__()
        block = OSABlock
        self.num_conv = num_conv
        self.norm = norm
        self.channels = [base_width] + list(concat_dims)

        self.inplanes = base_width
        self.conv1 = nn.Conv2d(3, self.inplanes, 3, 2, 1, bias=False)
        self.bn1 = make_norm(self.inplanes, norm=self.norm)
        self.conv2 = nn.Conv2d(self.inplanes,
                               self.inplanes,
                               3,
                               1,
                               1,
                               bias=False)
        self.bn2 = make_norm(self.inplanes, norm=self.norm)
        self.conv3 = nn.Conv2d(self.inplanes,
                               self.inplanes * 2,
                               3,
                               2,
                               1,
                               bias=False)
        self.bn3 = make_norm(self.inplanes * 2, norm=self.norm)
        self.relu = nn.ReLU(inplace=True)
        self.inplanes = self.inplanes * 2

        self.layer1 = self._make_layer(block,
                                       stage_dims[0],
                                       concat_dims[0],
                                       layers[0],
                                       1,
                                       conv=stage_with_conv[0])
        self.layer2 = self._make_layer(block,
                                       stage_dims[1],
                                       concat_dims[1],
                                       layers[1],
                                       2,
                                       conv=stage_with_conv[1])
        self.layer3 = self._make_layer(block,
                                       stage_dims[2],
                                       concat_dims[2],
                                       layers[2],
                                       2,
                                       conv=stage_with_conv[2])
        self.layer4 = self._make_layer(block,
                                       stage_dims[3],
                                       concat_dims[3],
                                       layers[3],
                                       2,
                                       conv=stage_with_conv[3])

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

        self._init_weights()
Example #21
0
    def __init__(self,
                 bottleneck=True,
                 aligned=False,
                 use_3x3x3stem=False,
                 stride_3x3=False,
                 avg_down=False,
                 stem_width=64,
                 base_width=64,
                 layers=(3, 4, 6, 3),
                 norm='bn',
                 stage_with_conv=('normal', 'normal', 'normal', 'normal'),
                 stage_with_context=('none', 'none', 'none', 'none'),
                 ctx_ratio=16,
                 radix=1,
                 num_classes=1000):
        """ Constructor
        Args:
            layers: config of layers, e.g., (3, 4, 23, 3)
            num_classes: number of classes
        """
        super(ResNet, self).__init__()
        if aligned:
            block = AlignedBottleneck
        else:
            if bottleneck:
                block = Bottleneck
            else:
                block = BasicBlock
        self.expansion = block.expansion
        self.stride_3x3 = stride_3x3
        self.avg_down = avg_down
        self.base_width = base_width
        self.norm = norm
        self.ctx_ratio = ctx_ratio
        self.radix = radix

        self.inplanes = stem_width
        self.use_3x3x3stem = use_3x3x3stem
        if not self.use_3x3x3stem:
            self.conv1 = nn.Conv2d(3, self.inplanes, 7, 2, 3, bias=False)
            self.bn1 = make_norm(self.inplanes, norm=self.norm.split('_')[-1])
        else:
            self.conv1 = nn.Conv2d(3, self.inplanes // 2, 3, 2, 1, bias=False)
            self.bn1 = make_norm(self.inplanes // 2,
                                 norm=self.norm.split('_')[-1])
            self.conv2 = nn.Conv2d(self.inplanes // 2,
                                   self.inplanes // 2,
                                   3,
                                   1,
                                   1,
                                   bias=False)
            self.bn2 = make_norm(self.inplanes // 2,
                                 norm=self.norm.split('_')[-1])
            self.conv3 = nn.Conv2d(self.inplanes // 2,
                                   self.inplanes,
                                   3,
                                   1,
                                   1,
                                   bias=False)
            self.bn3 = make_norm(self.inplanes, norm=self.norm.split('_')[-1])
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.layer1 = self._make_layer(block,
                                       64,
                                       layers[0],
                                       1,
                                       conv=stage_with_conv[0],
                                       context=stage_with_context[0])
        self.layer2 = self._make_layer(block,
                                       128,
                                       layers[1],
                                       2,
                                       conv=stage_with_conv[1],
                                       context=stage_with_context[1])
        self.layer3 = self._make_layer(block,
                                       256,
                                       layers[2],
                                       2,
                                       conv=stage_with_conv[2],
                                       context=stage_with_context[2])
        self.layer4 = self._make_layer(block,
                                       512,
                                       layers[3],
                                       2,
                                       conv=stage_with_conv[3],
                                       context=stage_with_context[3])

        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(512 * self.expansion, num_classes)

        self._init_weights()
Example #22
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()
Example #23
0
    def __init__(self,
                 inplanes,
                 planes,
                 base_width=64,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 stride_3x3=False,
                 downsample=None):
        super(AlignedBottleneck, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))
        width = int(planes * (base_width / 64.))

        self.conv1_1 = nn.Conv2d(inplanes,
                                 width,
                                 kernel_size=1,
                                 stride=1,
                                 padding=0,
                                 bias=False)
        self.bn1_1 = make_norm(width, norm=norm.split('_')[-1])
        self.conv1_2 = conv_op(width,
                               width,
                               kernel_size=3,
                               stride=stride,
                               dilation=dilation,
                               padding=dilation,
                               bias=False)
        self.conv2_1 = nn.Conv2d(inplanes,
                                 width // 2,
                                 kernel_size=1,
                                 stride=1,
                                 padding=0,
                                 bias=False)
        self.bn2_1 = make_norm(width // 2, norm=norm.split('_')[-1])
        self.conv2_2 = conv_op(width // 2,
                               width // 2,
                               kernel_size=3,
                               stride=stride,
                               dilation=dilation,
                               padding=dilation,
                               bias=False)
        self.bn2_2 = make_norm(width // 2,
                               norm=norm,
                               an_k=10 if planes < 256 else 20)
        self.conv2_3 = conv_op(width // 2,
                               width // 2,
                               kernel_size=3,
                               stride=1,
                               dilation=dilation,
                               padding=dilation,
                               bias=False)
        self.bn_concat = make_norm(width + (width // 2),
                                   norm=norm,
                                   an_k=10 if planes < 256 else 20)

        self.conv = nn.Conv2d(width + (width // 2),
                              planes * self.expansion,
                              kernel_size=1,
                              stride=1,
                              padding=0,
                              bias=False)
        self.bn = make_norm(planes * self.expansion, norm=norm.split('_')[-1])

        if context == 'none':
            self.ctx = None
        elif context == 'se':
            self.ctx = ops.SeConv2d(planes * self.expansion,
                                    int(planes * self.expansion * ctx_ratio))
        elif context == 'gcb':
            self.ctx = ops.GlobalContextBlock(
                planes * self.expansion,
                int(planes * self.expansion * ctx_ratio))
        else:
            raise ValueError(
                '{} type context operation is not supported.'.format(context))

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Example #24
0
    def __init__(self,
                 inplanes,
                 planes,
                 base_width,
                 cardinality,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 downsample=None):
        """ Constructor
        Args:
            inplanes: input channel dimensionality
            planes: output channel dimensionality
            base_width: base width.
            cardinality: num of convolution groups.
            stride: conv stride. Replaces pooling layer.
        """
        super(Bottleneck, self).__init__()

        D = int(math.floor(planes * (base_width / 64.0)))
        C = cardinality

        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))

        self.conv1 = nn.Conv2d(inplanes,
                               D * C,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.bn1 = make_norm(D * C, norm=norm)
        self.conv2 = conv_op(D * C,
                             D * C,
                             kernel_size=3,
                             stride=stride,
                             dilation=dilation,
                             padding=dilation,
                             groups=C,
                             bias=False)
        self.bn2 = make_norm(D * C, norm=norm)
        self.conv3 = nn.Conv2d(D * C,
                               planes * 4,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.bn3 = make_norm(planes * 4, norm=norm)

        if context == 'none':
            self.ctx = None
        elif context == 'se':
            self.ctx = ops.SeConv2d(planes * 4, int(planes * 4 * ctx_ratio))
        elif context == 'gcb':
            self.ctx = ops.GlobalContextBlock(planes * 4,
                                              int(planes * 4 * ctx_ratio))
        else:
            raise ValueError(
                '{} type context operation is not supported.'.format(context))

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Example #25
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()
Example #26
0
    def __init__(self, norm='bn', stride=32):
        """ Constructor
        """
        super(ResNet, self).__init__()
        if cfg.BACKBONE.RESNET.USE_ALIGN:
            block = res.AlignedBottleneck
        else:
            if cfg.BACKBONE.RESNET.BOTTLENECK:
                block = res.Bottleneck  # not use the original Bottleneck module
            else:
                block = res.BasicBlock
        self.expansion = block.expansion
        self.stride_3x3 = cfg.BACKBONE.RESNET.STRIDE_3X3
        self.avg_down = cfg.BACKBONE.RESNET.AVG_DOWN
        self.norm = norm
        self.stride = stride

        layers = cfg.BACKBONE.RESNET.LAYERS[:int(math.log(stride, 2)) - 1]
        self.layers = layers
        self.base_width = cfg.BACKBONE.RESNET.WIDTH
        stage_with_context = cfg.BACKBONE.RESNET.STAGE_WITH_CONTEXT
        self.ctx_ratio = cfg.BACKBONE.RESNET.CTX_RATIO
        stage_with_conv = cfg.BACKBONE.RESNET.STAGE_WITH_CONV
        c5_dilation = cfg.BACKBONE.RESNET.C5_DILATION

        self.inplanes = 64
        self.use_3x3x3stem = cfg.BACKBONE.RESNET.USE_3x3x3HEAD
        if not self.use_3x3x3stem:
            self.conv1 = nn.Conv2d(3, self.inplanes, 7, 2, 3, bias=False)
            self.bn1 = make_norm(self.inplanes, norm=self.norm.split('_')[-1])
        else:
            self.conv1 = nn.Conv2d(3, self.inplanes // 2, 3, 2, 1, bias=False)
            self.bn1 = make_norm(self.inplanes // 2,
                                 norm=self.norm.split('_')[-1])
            self.conv2 = nn.Conv2d(self.inplanes // 2,
                                   self.inplanes // 2,
                                   3,
                                   1,
                                   1,
                                   bias=False)
            self.bn2 = make_norm(self.inplanes // 2,
                                 norm=self.norm.split('_')[-1])
            self.conv3 = nn.Conv2d(self.inplanes // 2,
                                   self.inplanes,
                                   3,
                                   1,
                                   1,
                                   bias=False)
            self.bn3 = make_norm(self.inplanes, norm=self.norm.split('_')[-1])
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.layer1 = self._make_layer(block,
                                       64,
                                       layers[0],
                                       1,
                                       conv=stage_with_conv[0],
                                       context=stage_with_context[0])
        self.layer2 = self._make_layer(block,
                                       128,
                                       layers[1],
                                       2,
                                       conv=stage_with_conv[1],
                                       context=stage_with_context[1])
        self.layer3 = self._make_layer(block,
                                       256,
                                       layers[2],
                                       2,
                                       conv=stage_with_conv[2],
                                       context=stage_with_context[2])

        if len(layers) == 4:
            if c5_dilation != 1:
                c5_stride = 1
            else:
                c5_stride = 2
            self.layer4 = self._make_layer(block,
                                           512,
                                           layers[3],
                                           c5_stride,
                                           dilation=c5_dilation,
                                           conv=stage_with_conv[3],
                                           context=stage_with_context[3])
            self.spatial_scale = [
                1 / 4., 1 / 8., 1 / 16., 1 / 32. * c5_dilation
            ]
        else:
            del self.layer4
            self.spatial_scale = [1 / 4., 1 / 8., 1 / 16.]

        self.dim_out = self.stage_out_dim[1:int(math.log(self.stride, 2))]

        del self.avgpool
        del self.fc
        self._init_weights()
        self._init_modules()
Example #27
0
    def __init__(self, norm='bn', stride=32):
        """ Constructor
        """
        super(VoVNet, self).__init__()
        block = vov.OSABlock
        self.num_conv = cfg.BACKBONE.VOV.NUM_CONV  # 5
        self.norm = norm
        self.stride = stride

        base_width = cfg.BACKBONE.VOV.WIDTH  # 64
        stage_dims = cfg.BACKBONE.VOV.STAGE_DIMS
        concat_dims = cfg.BACKBONE.VOV.CONCAT_DIMS
        layers = cfg.BACKBONE.VOV.LAYERS
        self.layers = layers
        stage_with_conv = cfg.BACKBONE.VOV.STAGE_WITH_CONV
        self.channels = [base_width] + list(concat_dims)

        self.inplanes = base_width
        self.conv1 = nn.Conv2d(3, self.inplanes, 3, 2, 1, bias=False)
        self.bn1 = make_norm(self.inplanes, norm=self.norm)
        self.conv2 = nn.Conv2d(self.inplanes,
                               self.inplanes,
                               3,
                               1,
                               1,
                               bias=False)
        self.bn2 = make_norm(self.inplanes, norm=self.norm)
        self.conv3 = nn.Conv2d(self.inplanes,
                               self.inplanes * 2,
                               3,
                               2,
                               1,
                               bias=False)
        self.bn3 = make_norm(self.inplanes * 2, norm=self.norm)
        self.relu = nn.ReLU(inplace=True)
        self.inplanes = self.inplanes * 2

        self.layer1 = self._make_layer(block,
                                       stage_dims[0],
                                       concat_dims[0],
                                       layers[0],
                                       1,
                                       conv=stage_with_conv[0])
        self.layer2 = self._make_layer(block,
                                       stage_dims[1],
                                       concat_dims[1],
                                       layers[1],
                                       2,
                                       conv=stage_with_conv[1])
        self.layer3 = self._make_layer(block,
                                       stage_dims[2],
                                       concat_dims[2],
                                       layers[2],
                                       2,
                                       conv=stage_with_conv[2])
        self.layer4 = self._make_layer(block,
                                       stage_dims[3],
                                       concat_dims[3],
                                       layers[3],
                                       2,
                                       conv=stage_with_conv[3])

        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.avgpool
        del self.fc
        self._init_weights()
        self._init_modules()