Example #1
0
    def __init__(self, num_anchors, num_classes, in_channels_list, focal_init=False, lite=False, large_kernel=False):
        super().__init__()
        self.num_classes = num_classes
        num_anchors = tuplify(num_anchors, len(in_channels_list))
        kernel_size = 5 if (lite and large_kernel) else 3
        self.loc_heads = nn.ModuleList([
            nn.Sequential(
                Norm("default", c),
                Conv2d(c, n * 4, kernel_size=kernel_size,
                       depthwise_separable=lite, mid_norm_layer='default')
            )
            for c, n in zip(in_channels_list, num_anchors)
        ])
        self.cls_heads = nn.ModuleList([
            nn.Sequential(
                Norm("default", c),
                Conv2d(c, n * num_classes, kernel_size=kernel_size,
                       depthwise_separable=lite, mid_norm_layer='default')
            )
            for c, n in zip(in_channels_list, num_anchors)
        ])

        if focal_init:
            for p in self.cls_heads:
                get_last_conv(p).bias.data.fill_(inverse_sigmoid(0.01))
Example #2
0
 def __init__(self, in_channels, out_channels, use_se, drop_path):
     super().__init__()
     self.bn1 = Norm(in_channels)
     self.nl1 = Act("default")
     self.conv1 = Conv2d(in_channels, out_channels, kernel_size=3)
     self.bn2 = Norm(out_channels)
     self.nl2 = Act("default")
     self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3)
     if use_se:
         self.se = SEModule(out_channels, reduction=8)
     if drop_path:
         self.drop_path = DropPath(drop_path)
Example #3
0
    def __init__(self, in_channels, channels, stride=1, downsample=None):
        super(Bottleneck, self).__init__()

        self.bn1 = Norm('default', in_channels)
        self.conv1 = nn.Conv2d(in_channels, channels, kernel_size=1, bias=True)
        self.bn2 = Norm('default', channels)
        self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, stride=stride,
                               padding=1, bias=True)
        self.bn3 = Norm('default', channels)
        self.conv3 = nn.Conv2d(channels, channels * 2, kernel_size=1, bias=True)
        self.relu = Act('default')
        self.downsample = downsample
        self.stride = stride
Example #4
0
 def __init__(self, in_channels, out_channels, stride=1):
     super().__init__()
     self.conv = nn.Sequential(
         Norm(in_channels),
         Conv2d(in_channels,
                out_channels,
                kernel_size=3,
                stride=stride,
                bias=False),
         Norm(out_channels),
         Act(),
         Conv2d(out_channels, out_channels, kernel_size=3, bias=False),
         Norm(out_channels),
     )
     self.shortcut = Shortcut(in_channels, out_channels, stride)
Example #5
0
    def __init__(self, start_channels, num_classes, block, widening_fractor,
                 depth):
        super().__init__()

        if block == 'basic':
            block = BasicBlock
            num_layers = [(depth - 2) // 6] * 3
        elif block == 'bottleneck':
            block = Bottleneck
            num_layers = [(depth - 2) // 9] * 3
        else:
            raise ValueError("invalid block type: %s" % block)

        strides = [1, 2, 2]

        self.add_channel = widening_fractor / sum(num_layers)
        self.in_channels = start_channels
        self.channels = start_channels

        layers = [Conv2d(3, start_channels, kernel_size=3, norm='default')]

        for n, s in zip(num_layers, strides):
            layers.append(self._make_layer(block, n, stride=s))

        self.features = nn.Sequential(*layers)
        assert (start_channels +
                widening_fractor) * block.expansion == self.in_channels
        self.post_activ = nn.Sequential(
            Norm(self.in_channels),
            Act('default'),
        )
        self.final_pool = nn.AdaptiveAvgPool2d(1)
        self.output = nn.Linear(self.in_channels, num_classes)
Example #6
0
    def __init__(self,
                 stem_channels=64,
                 mid_channels=192,
                 growth_rate=48,
                 num_units=(6, 8, 8, 8)):
        super().__init__()

        self.features = nn.Sequential()
        self.features.add_module("init_block", StemBlock(stem_channels))
        in_channels = stem_channels * 2
        for i, n in enumerate(num_units):
            stage = nn.Sequential()
            if i != len(num_units) - 1:
                stage.add_module("trans", Transition(in_channels, in_channels))
            for j in range(n):
                stage.add_module(
                    "unit%d" % (j + 1),
                    DenseUnit(in_channels, mid_channels, growth_rate))
                in_channels += growth_rate
            self.features.add_module("stage%d" % (i + 1), stage)
        self.features.add_module(
            "post_activ",
            seq(
                ("bn", Norm("default", in_channels)),
                ("relu", Act("default")),
            ))
Example #7
0
    def __init__(self, depth, k, num_classes=10, use_se=False, drop_path=0):
        super().__init__()
        num_blocks = (depth - 4) // 6
        self.conv = Conv2d(3, self.stages[0], kernel_size=3)

        self.layer1 = self._make_layer(self.stages[0] * 1,
                                       self.stages[1] * k,
                                       num_blocks,
                                       stride=1,
                                       use_se=use_se,
                                       drop_path=drop_path)
        self.layer2 = self._make_layer(self.stages[1] * k,
                                       self.stages[2] * k,
                                       num_blocks,
                                       stride=2,
                                       use_se=use_se,
                                       drop_path=drop_path)
        self.layer3 = self._make_layer(self.stages[2] * k,
                                       self.stages[3] * k,
                                       num_blocks,
                                       stride=2,
                                       use_se=use_se,
                                       drop_path=drop_path)

        self.bn = Norm(self.stages[3] * k)
        self.nl = Act('default')
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(self.stages[3] * k, num_classes)
Example #8
0
 def _make_head(self, num_layers, f_channels, out_channels):
     layers = []
     for i in range(num_layers):
         layers.append(MBConv(f_channels, f_channels * self.expand_ratio, f_channels, kernel_size=5))
     layers.append(nn.Sequential(
         Norm('default', f_channels),
         Conv2d(f_channels, out_channels, kernel_size=1),
     ))
     return nn.Sequential(*layers)
Example #9
0
    def __init__(self, in_channels, out_channels, stride=1, use_se=False):
        super().__init__()
        self.use_se = use_se
        self.bn1 = Norm(in_channels)
        self.nl1 = Act("default")
        self.conv1 = Conv2d(in_channels,
                            out_channels,
                            kernel_size=3,
                            stride=stride)
        self.bn2 = Norm(out_channels)
        self.nl2 = Act("default")
        self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3)
        if self.use_se:
            self.se = SEModule(out_channels, reduction=8)

        self.shortcut = Conv2d(in_channels,
                               out_channels,
                               kernel_size=1,
                               stride=stride)
Example #10
0
    def __init__(self,
                 stem_channels=64,
                 mid_channels=(64, 80, 96, 112),
                 out_channels=(128, 256, 384, 512),
                 num_modules=(1, 1, 1, 1),
                 num_classes=1000):
        super().__init__()
        num_stages = 5
        assert len(mid_channels) == len(out_channels) == len(
            num_modules) == num_stages - 1

        self.features = nn.Sequential()
        self.features.add_module(
            "init_block",
            nn.Sequential(
                Conv2d(3,
                       stem_channels,
                       kernel_size=3,
                       stride=2,
                       norm='default',
                       act='default'),
                Conv2d(stem_channels,
                       stem_channels,
                       kernel_size=3,
                       norm='default',
                       act='default'),
                Conv2d(stem_channels,
                       stem_channels * 2,
                       kernel_size=3,
                       norm='default',
                       act='default'),
            ))
        in_channels = stem_channels * 2
        for i, m, o, n in zip(range(num_stages - 1), mid_channels,
                              out_channels, num_modules):
            stage = nn.Sequential()
            stage.add_module(
                "pool", nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
            for j in range(n):
                stage.add_module("unit%d" % (j + 1), OSA(in_channels, m, o))
                in_channels = o
            self.features.add_module("stage%d" % (i + 1), stage)

        self.features.add_module(
            "post_activ",
            nn.Sequential(
                Norm("default", in_channels),
                Act("default"),
            ))
        self.features.add_module("final_pool", nn.AdaptiveAvgPool2d(1))

        self.output = nn.Linear(in_features=in_channels,
                                out_features=num_classes)
Example #11
0
    def __init__(self, num_anchors, num_classes, in_channels_list, focal_init=False):
        super().__init__()
        self.num_classes = num_classes
        num_anchors = tuplify(num_anchors, len(in_channels_list))
        self.loc_heads = nn.ModuleList([
            nn.Sequential(
                Norm("default", c),
                Conv2d(c, n * 4, kernel_size=1)
            )
            for c, n in zip(in_channels_list, num_anchors)
        ])
        self.cls_heads = nn.ModuleList([
            nn.Sequential(
                Norm("default", c),
                Conv2d(c, n * num_classes, kernel_size=1)
            )
            for c, n in zip(in_channels_list, num_anchors)
        ])

        if focal_init:
            for p in self.cls_heads:
                get_last_conv(p).bias.data.fill_(inverse_sigmoid(0.01))
Example #12
0
    def __init__(self, in_channels, channels, out_channels, kernel_size, stride=1, se_ratio=1 / 16):
        super().__init__()

        self.bn = Norm('default', in_channels)
        if in_channels != channels:
            self.expand = Conv2d(in_channels, channels, kernel_size=1,
                                 norm='default', act='default')

        self.dwconv = Conv2d(channels, channels, kernel_size, stride=stride, groups=channels,
                             norm='default', act='default')

        if se_ratio:
            assert 0 < se_ratio < 1
            self.se = SEModule(channels, reduction=int(1 / se_ratio))

        if out_channels is not None:
            self.project = Conv2d(channels, out_channels, kernel_size=1,
                                  norm='default')
        self.use_res_connect = stride == 1 and in_channels == out_channels
Example #13
0
    def __init__(self,
                 stem_channels=64,
                 mid_channels=192,
                 growth_rate=48,
                 num_units=(6, 8, 8, 8)):
        super().__init__()

        self.init_block = StemBlock(stem_channels)
        in_channels = stem_channels * 2
        out_channels = [in_channels]
        for i, n in enumerate(num_units):
            stage = nn.Sequential()
            stage.add_module(
                "pool", nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
            for j in range(n):
                stage.add_module(
                    "unit%d" % (j + 1),
                    DenseUnit(in_channels, mid_channels, growth_rate))
                in_channels += growth_rate
            if i != len(num_units) - 1:
                stage.add_module("trans", Transition(in_channels, in_channels))
            out_channels.append(in_channels)
            self.add_module("stage%d" % (i + 1), stage)
        self.post_activ = seq(
            ("bn", Norm("default", in_channels)),
            ("relu", Act("default")),
        )

        del self.stage4.pool

        self.trans = Transition(out_channels[-1], out_channels[-1])
        self.proj = Transition(out_channels[-1], 512)

        self.extra1 = BasicBlock(512, 512)
        self.extra2 = BasicBlock(512, 256)
        self.extra3 = BasicBlock(256, 256)
        self.extra4 = BasicBlock(256, 256)
        self.out_channels = [out_channels[-3], 512, 512, 256, 256, 256]
Example #14
0
    def __init__(self, in_channels, out_channels, stride, groups, use_se):
        super().__init__()
        self.use_se = use_se

        self.conv1 = Conv2d(in_channels, out_channels, kernel_size=1,
                            norm='default', act='default')
        self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, groups=groups,
                            norm='default', act='default')
        if self.use_se:
            self.se = SE(out_channels, 4)
        self.conv3 = Conv2d(out_channels, out_channels, kernel_size=1,
                            norm='default')
        if stride != 1 or in_channels != out_channels:
            layers = []
            if stride != 1:
                layers.append(nn.AvgPool2d(kernel_size=(2, 2), stride=2))
            layers.extend([
                Conv2d(in_channels, out_channels, kernel_size=1, bias=False),
                Norm(out_channels),
            ])
            self.shortcut = nn.Sequential(*layers)
        else:
            self.shortcut = nn.Identity()
        self.relu = Act('default')