Esempio n. 1
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 stride,
                 dropout,
                 drop_path,
                 start_block=False,
                 end_block=False,
                 exclude_bn0=False):
        super().__init__()
        if not start_block and not exclude_bn0:
            self.bn0 = Norm(in_channels)

        if not start_block:
            self.act0 = Act()

        self.conv1 = Conv2d(in_channels,
                            out_channels,
                            kernel_size=3,
                            stride=stride)
        self.bn1 = Norm(out_channels)
        self.act1 = Act()
        self.dropout = Dropout(dropout) if dropout else Identity()
        self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3)

        if start_block:
            self.bn2 = Norm(out_channels)

        self.drop_path = DropPath(drop_path) if drop_path else Identity()

        if end_block:
            self.bn2 = Norm(out_channels)
            self.act2 = Act()

        if stride != 1 or in_channels != out_channels:
            shortcut = []
            if stride != 1:
                shortcut.append(Pool2d(2, 2, type='avg'))
            shortcut.append(
                Conv2d(in_channels, out_channels, kernel_size=1, norm='def'))
            self.shortcut = Sequential(shortcut)
        else:
            self.shortcut = Identity()
        self.start_block = start_block
        self.end_block = end_block
        self.exclude_bn0 = exclude_bn0
Esempio n. 2
0
    def __init__(self,
                 in_channels,
                 channels,
                 stride,
                 start_block=False,
                 end_block=False,
                 exclude_bn0=False,
                 conv_cls=Conv2d):
        super().__init__()
        out_channels = channels * self.expansion
        width = getattr(self, "width", channels)
        if not start_block and not exclude_bn0:
            self.bn0 = Norm(in_channels)
        if not start_block:
            self.act0 = Act()
        self.conv1 = Conv2d(in_channels, width, kernel_size=1)
        self.bn1 = Norm(width)
        self.act1 = Act()
        self.conv2 = conv_cls(in_channels=width,
                              out_channels=width,
                              kernel_size=3,
                              stride=stride,
                              norm='def',
                              act='def',
                              start_block=start_block)
        self.conv3 = Conv2d(width, out_channels, kernel_size=1)

        if start_block:
            self.bn3 = Norm(out_channels)

        if end_block:
            self.bn3 = Norm(out_channels)
            self.act3 = Act()

        if stride != 1 or in_channels != out_channels:
            shortcut = []
            if stride != 1:
                shortcut.append(Pool2d(2, 2, type='avg'))
            shortcut.append(
                Conv2d(in_channels, out_channels, kernel_size=1, norm='def'))
            self.shortcut = Sequential(shortcut)
        else:
            self.shortcut = Identity()
        self.start_block = start_block
        self.end_block = end_block
        self.exclude_bn0 = exclude_bn0
Esempio n. 3
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 stride,
                 askc_type='DirectAdd'):
        super().__init__()
        self.norm1 = Norm(in_channels)
        self.act1 = Act()
        self.conv1 = Conv2d(in_channels,
                            out_channels,
                            kernel_size=3,
                            stride=stride,
                            bias=False)
        self.norm2 = Norm(out_channels)
        self.act2 = Act()
        self.conv2 = Conv2d(out_channels,
                            out_channels,
                            kernel_size=3,
                            bias=False)

        if in_channels != out_channels or stride == 2:
            if stride == 2:
                self.shortcut = Sequential([
                    Pool2d(2, 2, type='avg'),
                    Conv2d(in_channels,
                           out_channels,
                           kernel_size=1,
                           bias=False),
                ])
            else:
                self.shortcut = Conv2d(in_channels,
                                       out_channels,
                                       kernel_size=1,
                                       bias=False)
        else:
            self.shortcut = Identity()

        if askc_type == 'DirectAdd':
            self.attention = DirectAddFuse()
        elif askc_type == 'iAFF':
            self.attention = iAFF(out_channels)
        elif askc_type == 'AFF':
            self.attention = AFF(out_channels)
        else:
            raise ValueError('Unknown askc_type')
Esempio n. 4
0
def ResNetvdStem(channels=64, pool=True):
    layers = [
        Conv2d(3,
               channels // 2,
               kernel_size=3,
               stride=2,
               norm='def',
               act='def'),
        Conv2d(channels // 2,
               channels // 2,
               kernel_size=3,
               norm='def',
               act='def'),
        Conv2d(channels // 2, channels, kernel_size=3, norm='def', act='def'),
    ]
    if pool:
        layers.append(Pool2d(kernel_size=3, stride=2, type='max'))
    return Sequential(layers)
Esempio n. 5
0
    def __init__(self, in_channels, channels, stride, zero_init_residual=True, reduction=16):
        super().__init__()
        out_channels = channels * self.expansion
        self.conv1 = Conv2d(in_channels, out_channels, kernel_size=3, stride=stride,
                            norm='def', act='def')
        self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3)
        self.bn2 = Norm(out_channels, gamma_init='zeros' if zero_init_residual else 'ones')
        self.se = SELayer(out_channels, reduction=reduction)

        if stride != 1 or in_channels != out_channels:
            shortcut = []
            if stride != 1:
                shortcut.append(Pool2d(2, 2, type='avg'))
            shortcut.append(
                Conv2d(in_channels, out_channels, kernel_size=1, norm='def'))
            self.shortcut = Sequential(shortcut)
        else:
            self.shortcut = Identity()

        self.act = Act()
Esempio n. 6
0
    def __init__(self,
                 in_channels,
                 channels,
                 stride,
                 cardinality,
                 base_width,
                 zero_init_residual=True):
        super().__init__()
        out_channels = channels * self.expansion

        D = math.floor(channels * (base_width / 64))
        C = cardinality

        self.conv1 = Conv2d(in_channels,
                            D * C,
                            kernel_size=1,
                            norm='def',
                            act='def')
        self.conv2 = Conv2d(D * C,
                            D * C,
                            kernel_size=3,
                            stride=stride,
                            groups=cardinality,
                            norm='def',
                            act='def')
        self.conv3 = Conv2d(D * C, out_channels, kernel_size=1, norm='def')
        self.conv3 = Conv2d(D * C, out_channels, kernel_size=1)
        self.bn3 = Norm(out_channels,
                        gamma_init='zeros' if zero_init_residual else 'ones')

        if stride != 1 or in_channels != out_channels:
            shortcut = []
            if stride != 1:
                shortcut.append(Pool2d(2, 2, type='avg'))
            shortcut.append(
                Conv2d(in_channels, out_channels, kernel_size=1, norm='def'))
            self.shortcut = Sequential(shortcut)
        else:
            self.shortcut = Identity()

        self.act = Act()
Esempio n. 7
0
 def __init__(self, in_channels, out_channels, stride, erase_relu):
     super().__init__()
     self.conv1 = Conv2d(in_channels,
                         out_channels,
                         kernel_size=3,
                         stride=stride,
                         norm='def',
                         act='def')
     self.conv2 = Conv2d(out_channels,
                         out_channels,
                         kernel_size=3,
                         norm='def')
     if stride != 1:
         assert in_channels != out_channels
         self.shortcut = Sequential([
             Pool2d(2, 2, type='avg'),
             Conv2d(in_channels, out_channels, kernel_size=1, norm='def'),
         ])
     else:
         self.shortcut = Identity()
     self.act = Act() if not erase_relu else Identity()
Esempio n. 8
0
    def __init__(self, in_channels, out_channels, stride, dropout):
        super().__init__()
        self.norm1 = Norm(in_channels)
        self.act1 = Act()
        self.conv1 = Conv2d(in_channels,
                            out_channels,
                            kernel_size=3,
                            stride=stride)
        self.norm2 = Norm(out_channels)
        self.act2 = Act()
        self.dropout = Dropout(dropout) if dropout else Identity()
        self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3)

        if stride != 1:
            assert in_channels != out_channels
            self.shortcut = Sequential([
                Pool2d(2, 2, type='avg'),
                Conv2d(in_channels, out_channels, kernel_size=1, norm='def'),
            ])
        else:
            self.shortcut = Identity()
Esempio n. 9
0
    def __init__(self, in_channels, channels, stride, base_width, splits,
                 zero_init_residual, genotype):
        super().__init__()
        self.stride = stride

        out_channels = channels * self.expansion
        width = math.floor(out_channels // self.expansion *
                           (base_width / 64)) * splits
        self.conv1 = Conv2d(in_channels,
                            width,
                            kernel_size=1,
                            norm='def',
                            act='def')
        if stride == 1:
            self.conv2 = PPConv(width, splits=splits, genotype=genotype)
        else:
            self.conv2 = Conv2d(width,
                                width,
                                kernel_size=3,
                                stride=2,
                                groups=splits,
                                norm='def',
                                act='def')
        self.conv3 = Conv2d(width, out_channels, kernel_size=1)
        self.bn3 = Norm(out_channels,
                        gamma_init='zeros' if zero_init_residual else 'ones')

        if stride != 1 or in_channels != out_channels:
            shortcut = []
            if stride != 1:
                shortcut.append(Pool2d(2, 2, type='avg'))
            shortcut.append(
                Conv2d(in_channels, out_channels, kernel_size=1, norm='def'))
            self.shortcut = Sequential(shortcut)
        else:
            self.shortcut = Identity()

        self.act = Act()
Esempio n. 10
0
    def __init__(self, in_channels, channels, stride, base_width, scale,
                 end_block=False, exclude_bn0=False):
        super().__init__()
        out_channels = channels * self.expansion
        start_block = stride != 1 or in_channels != out_channels
        width = math.floor(out_channels // self.expansion * (base_width / 64)) * scale
        if not start_block and not exclude_bn0:
            self.bn0 = Norm(in_channels)
        if not start_block:
            self.act0 = Act()
        self.conv1 = Conv2d(in_channels, width, kernel_size=1)
        self.bn1 = Norm(width)
        self.act1 = Act()
        self.conv2 = Res2Conv(width, width, kernel_size=3, stride=stride, scale=scale, groups=1,
                              norm='def', act='def', start_block=start_block)
        self.conv3 = Conv2d(width, out_channels, kernel_size=1)

        if start_block:
            self.bn3 = Norm(out_channels)

        if end_block:
            self.bn3 = Norm(out_channels)
            self.act3 = Act()

        if stride != 1 or in_channels != out_channels:
            shortcut = []
            if stride != 1:
                shortcut.append(Pool2d(2, 2, type='avg'))
            shortcut.append(
                Conv2d(in_channels, out_channels, kernel_size=1, norm='def'))
            self.shortcut = Sequential(shortcut)
        else:
            self.shortcut = Identity()
        self.start_block = start_block
        self.end_block = end_block
        self.exclude_bn0 = exclude_bn0
Esempio n. 11
0
    def __init__(self, layers=(3, 4, 6, 4), base_width=26, splits=4,
                 num_classes=1000, stages=(64, 64, 128, 256, 512)):
        super().__init__()
        self.stages = stages
        self.splits = splits
        block = Bottleneck
        self.num_stages = len(layers)

        self.stem = Sequential([
            Conv2d(3, self.stages[0] // 2, kernel_size=3, stride=2,
                   norm='def', act='def'),
            Conv2d(self.stages[0] // 2, self.stages[0] // 2, kernel_size=3,
                   norm='def', act='def'),
            Conv2d(self.stages[0] // 2, self.stages[0], kernel_size=3,
                   norm='def', act='def'),
        ])
        self.maxpool = Pool2d(kernel_size=3, stride=2, type='max')
        self.in_channels = self.stages[0]

        self.layer1 = self._make_layer(
            block, self.stages[1], layers[0], stride=1,
            base_width=base_width, splits=splits)
        self.layer2 = self._make_layer(
            block, self.stages[2], layers[1], stride=2,
            base_width=base_width, splits=splits)
        self.layer3 = self._make_layer(
            block, self.stages[3], layers[2], stride=2,
            base_width=base_width, splits=splits)
        self.layer4 = self._make_layer(
            block, self.stages[4], layers[3], stride=2,
            base_width=base_width, splits=splits)

        self.avgpool = GlobalAvgPool()
        self.fc = Linear(self.in_channels, num_classes)

        self._initialize_alphas()
Esempio n. 12
0
 def __init__(self,
              in_channels,
              channels,
              groups,
              stride=1,
              se_reduction=4,
              drop_path=0.2):
     super().__init__()
     assert groups == 1
     branch1 = [
         Norm(in_channels),
         Conv2d(in_channels,
                channels,
                kernel_size=3,
                norm='default',
                act='default'),
         *([Pool2d(3, 2)] if stride != 1 else []),
         Conv2d(channels, channels, kernel_size=3, norm='default'),
         *([SELayer(channels, se_reduction, groups)]
           if se_reduction else []),
         *([DropPath(drop_path)] if drop_path and stride == 1 else []),
     ]
     self.branch1 = Sequential(branch1)
     self.branch2 = Shortcut(in_channels, channels, stride)
Esempio n. 13
0
import tensorflow as tf
from tensorflow.keras import Model, Sequential
from tensorflow.keras.layers import Layer

from hanser.models.layers import Norm, Pool2d, Conv2d, Act, Identity

OPS = {
    # 'none': lambda C, stride: Zero(stride),
    'skip_connect':
    lambda C, stride: Identity()
    if stride == 1 else Pool2d(3, stride, type='avg'),
    'nor_conv_1x1':
    lambda C, stride: Conv2d(C, C, 1, stride, norm='def', act='def'),
    'nor_conv_3x3':
    lambda C, stride: Conv2d(C, C, 3, stride, norm='def', act='def'),
}

# class DilConv(Layer):
#
#     def __init__(self, C_in, C_out, kernel_size, stride, dilation):
#         super().__init__()
#         self.op = Sequential([
#             Act('relu'),
#             Conv2d(C_in, C_in, kernel_size=kernel_size, stride=stride, dilation=dilation, groups=C_in, bias=False),
#             Conv2d(C_in, C_out, kernel_size=1, bias=False),
#             Norm(C_out),
#         ])
#
#     def forward(self, x):
#         return self.op(x)
#
Esempio n. 14
0
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Layer, Concatenate, Lambda
from hanser.models.modules import Slice
from hanser.models.layers import Conv2d, Norm, Act, Pool2d

OPS = {
    'none':
    lambda C, stride: Zero(stride),
    'avg_pool_3x3':
    lambda C, stride: Pool2d(3, stride=stride, type='avg'),
    'max_pool_3x3':
    lambda C, stride: Pool2d(3, stride=stride, type='max'),
    'skip_connect':
    lambda C, stride: Identity() if stride == 1 else FactorizedReduce(C, C),
    'sep_conv_3x3':
    lambda C, stride: SepConv(C, C, 3, stride),
    'sep_conv_5x5':
    lambda C, stride: SepConv(C, C, 5, stride),
    'sep_conv_7x7':
    lambda C, stride: SepConv(C, C, 7, stride),
    'dil_conv_3x3':
    lambda C, stride: DilConv(C, C, 3, stride, 2),
    'dil_conv_5x5':
    lambda C, stride: DilConv(C, C, 5, stride, 2),
    'conv_7x1_1x7':
    lambda C, stride: Sequential([
        Act(),
        Conv2d(C, C, (7, 1), stride=(stride, 1), bias=False),
        Conv2d(C, C, (1, 7), stride=(1, stride), bias=False),
        Norm(C),
Esempio n. 15
0
    def __init__(self,
                 in_channels,
                 channels,
                 stride,
                 dropout=0,
                 drop_path=0,
                 avd=False,
                 start_block=False,
                 end_block=False,
                 exclude_bn0=False):
        super().__init__()
        out_channels = channels * self.expansion

        if not start_block and not exclude_bn0:
            self.bn0 = Norm(in_channels)
        if not start_block:
            self.act0 = Act()
        self.conv1 = Conv2d(in_channels, channels, kernel_size=1)
        self.bn1 = Norm(channels)
        self.act1 = Act()

        if avd and stride != 1:
            self.conv2 = Sequential([
                Pool2d(3, stride, type='avg'),
                Conv2d(channels, channels, kernel_size=3, stride=1),
            ])
        else:
            self.conv2 = Conv2d(channels,
                                channels,
                                kernel_size=3,
                                stride=stride)
        self.bn2 = Norm(channels)
        self.act2 = Act()
        self.dropout = Dropout(dropout) if dropout else Identity()

        self.conv3 = Conv2d(channels, out_channels, kernel_size=1)

        if start_block:
            self.bn3 = Norm(out_channels)

        self.drop_path = DropPath(drop_path) if drop_path else Identity()

        if end_block:
            self.bn3 = Norm(out_channels)
            self.act3 = Act()

        if stride != 1 or in_channels != out_channels:
            shortcut = []
            if stride != 1:
                shortcut.append(Pool2d(3, 2, type='max'))
            if in_channels != out_channels:
                shortcut.append(
                    Conv2d(in_channels,
                           out_channels,
                           kernel_size=1,
                           norm='def'))
            self.shortcut = Sequential(shortcut)
        else:
            self.shortcut = Identity()
        self.start_block = start_block
        self.end_block = end_block
        self.exclude_bn0 = exclude_bn0
Esempio n. 16
0
    def __init__(self, in_channels, out_channels, cardinality,
                 bottleneck_width, stride, use_se, askc_type):
        super().__init__()
        r = 4 * self.expansion
        channels = out_channels // self.expansion
        D = int(math.floor(channels * bottleneck_width / 64))
        group_width = cardinality * D

        self.body = Sequential([
            Conv2d(in_channels,
                   group_width,
                   kernel_size=1,
                   norm='def',
                   act='def'),
            Conv2d(group_width,
                   group_width,
                   kernel_size=3,
                   stride=stride,
                   groups=cardinality,
                   norm='def',
                   act='def'),
            Conv2d(group_width,
                   out_channels,
                   kernel_size=1,
                   norm='def',
                   act='def'),
        ])

        if use_se:
            self.se = Sequential([
                GlobalAvgPool(keep_dim=True),
                Conv2d(out_channels,
                       out_channels // r,
                       kernel_size=1,
                       act='def'),
                Conv2d(out_channels // r,
                       out_channels,
                       kernel_size=1,
                       act='sigmoid'),
            ])
        else:
            self.se = None

        if in_channels != out_channels or stride == 2:
            if stride == 2:
                self.shortcut = Sequential([
                    Pool2d(2, 2, type='avg'),
                    Conv2d(in_channels,
                           out_channels,
                           kernel_size=1,
                           norm='def'),
                ])
            else:
                self.shortcut = Conv2d(in_channels,
                                       out_channels,
                                       kernel_size=1,
                                       norm='def')
        else:
            self.shortcut = Identity()

        if askc_type == 'DirectAdd':
            self.attention = DirectAddFuse()
        elif askc_type == 'iAFF':
            self.attention = iAFF(out_channels, r=r)
        elif askc_type == 'AFF':
            self.attention = AFF(out_channels, r=r)
        else:
            raise ValueError('Unknown askc_type')

        self.post_activ = Act()