def __init__(self, stages_repeats, stages_out_channels, num_classes=1000): super().__init__() if len(stages_repeats) != 3: raise ValueError('expected stages_repeats as list of 3 positive ints') if len(stages_out_channels) != 5: raise ValueError('expected stages_out_channels as list of 5 positive ints') self._stage_out_channels = stages_out_channels in_channels = 3 out_channels = self._stage_out_channels[0] self.stem = Sequential([ Conv2d(in_channels, out_channels, 3, stride=2, norm='def', act='def'), Pool2d(3, stride=2, type='max'), ]) in_channels = out_channels stage_names = ['stage{}'.format(i) for i in [2, 3, 4]] for name, repeats, out_channels in zip( stage_names, stages_repeats, self._stage_out_channels[1:]): seq = [InvertedResidual(in_channels, out_channels, 2)] for i in range(repeats - 1): seq.append(InvertedResidual(out_channels, out_channels, 1)) setattr(self, name, Sequential(seq)) in_channels = out_channels out_channels = self._stage_out_channels[-1] self.final_conv = Conv2d(in_channels, out_channels, kernel_size=1, norm='def', act='def') self.avgpool = GlobalAvgPool() self.fc = Linear(out_channels, num_classes)
def __init__(self, in_channels, out_channels, stride, dropout, use_se=False): super().__init__() self.use_se = use_se 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 self.use_se: self.se = SELayer(out_channels, reduction=8) 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()
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='def', act='def') self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, groups=groups, norm='def', act='def') if self.use_se: self.se = SELayer(out_channels, 4) self.conv3 = Sequential( Conv2d(out_channels, out_channels, kernel_size=1, bias=False), Norm(out_channels, gamma_init='zeros')) if stride != 1 or in_channels != out_channels: self.shortcut = Conv2d(in_channels, out_channels, kernel_size=1, norm='def') else: self.shortcut = Identity() self.act = Act()
def __init__(self): super().__init__() channels = 4 self.stem = Conv2d(1, channels, kernel_size=3, norm='def', act='def') self.normal1 = make_layer_choice(channels) self.reduce1 = Sequential([ Pool2d(kernel_size=3, stride=2), Conv2d(channels, channels * 2, 1) ]) channels = channels * 2 self.normal2 = make_layer_choice(channels) self.reduce2 = Sequential([ Pool2d(kernel_size=3, stride=2), Conv2d(channels, channels * 2, 1) ]) channels = channels * 2 self.normal3 = make_layer_choice(channels) self.avg_pool = GlobalAvgPool() self.fc = Linear(channels, 10) self.alpha_normal = self.add_weight( name='alpha_normal', shape=(self.normal1.n_choices, ), dtype=self.dtype, initializer=RandomNormal(stddev=0.01), trainable=True) self.tau = self.add_weight(name='tau', shape=(), dtype=self.dtype, initializer=Constant(1.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()
def __init__(self, in_channels, out_channels=256, num_extra_convs=2, extra_convs_on='input', norm='bn'): super().__init__() assert isinstance(in_channels, list) self.in_channels = in_channels self.out_channels = out_channels self.num_extra_convs = num_extra_convs assert extra_convs_on in ['input', 'output'] self.extra_convs_on = extra_convs_on self.lateral_convs = [] self.fpn_convs = [] for in_channels in self.in_channels: self.lateral_convs.append( Conv2d(in_channels, out_channels, 1)) self.fpn_convs.append( Conv2d(out_channels, out_channels, 3, norm=norm)) self.extra_convs = [] in_channels = self.in_channels[-1] if extra_convs_on == 'input' else out_channels for i in range(num_extra_convs): extra_conv = Conv2d(in_channels, out_channels, 3, stride=2, norm=norm) if i != 0: extra_conv = Sequential([Act(), extra_conv]) self.extra_convs.append(extra_conv) in_channels = out_channels num_levels = len(self.in_channels) + num_extra_convs self.feat_channels = [out_channels] * num_levels
def __init__(self, in_channels, channels, **kwargs): super().__init__(**kwargs) self.pool = GlobalAvgPool(keep_dim=True) self.fc = Sequential([ Conv2d(in_channels, channels, 1, act='def'), Conv2d(channels, in_channels, 1, act='sigmoid'), ])
def __init__(self, in_channels, channels, stride, erase_relu=False, zero_init_residual=False, dilation=None, avd=None): 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, norm='def') 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() if not erase_relu else Identity()
def __init__(self, in_channels, channels, stride, channels_per_group, cardinality, start_block=False, end_block=False, exclude_bn0=False): super().__init__() out_channels = channels * self.expansion width = channels_per_group * cardinality 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 = Conv2d(width, width, kernel_size=3, stride=stride, groups=cardinality, norm='def', act='def') 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(3, 2, type='max')) 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
def __init__(self, in_channels, channels, stride, dilation=1, base_width=26, scale=4, zero_init_residual=True, avd=False): super().__init__() out_channels = channels * self.expansion start_block = stride != 1 or in_channels != out_channels width = math.floor(channels * (base_width / 64)) * scale self.conv1 = Conv2d(in_channels, width, kernel_size=1, norm='def', act='def') if avd and stride != 1: self.conv2 = Sequential([ Pool2d(3, stride=stride, type='avg'), Res2Conv(width, width, kernel_size=3, stride=1, dilation=dilation, scale=scale, groups=1, start_block=start_block, norm='def', act='def'), ]) else: self.conv2 = Res2Conv(width, width, kernel_size=3, stride=stride, dilation=dilation, scale=scale, groups=1, start_block=start_block, 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()
def __init__(self, in_channels, out_channels, kernel_size, dilation=1, norm=None, act=None): super().__init__() self.depthwise_conv = Conv2d( in_channels, in_channels, kernel_size, groups=in_channels, dilation=dilation) self.piontwise_conv = Conv2d( in_channels, out_channels, kernel_size=1, norm=norm, act=act)
def __init__(self, in_channels, out_channels, stride, groups, reduction, zero_init_residual): super().__init__() se_channels = in_channels // reduction self.conv1 = Conv2d(in_channels, out_channels, kernel_size=1, norm='def', act='def') self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, groups=groups, norm='def', act='def') self.se = SELayer(out_channels, se_channels) self.conv3 = Sequential([ Conv2d(out_channels, out_channels, kernel_size=1, bias=False), 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()
def __init__(self, in_channels, out_channels, kernel_size, stride, expand_ratio, se_ratio, drop_connect): super().__init__() self._has_se = se_ratio is not None and 0 < se_ratio <= 1 channels = in_channels * expand_ratio self.expand = Conv2d(in_channels, channels, 1, norm='def', act='def') if expand_ratio != 1 else Identity() self.depthwise = Conv2d(channels, channels, kernel_size, stride, groups=channels, padding='SAME', norm='def', act='def') if self._has_se: self.se = SELayer(channels, se_channels=int(in_channels * se_ratio), min_se_channels=1) self.project = Conv2d(channels, out_channels, 1, norm='def') self._use_residual = in_channels == out_channels and stride == 1 if self._use_residual: self.drop_connect = DropPath( drop_connect) if drop_connect else Identity()
def __init__(self, in_channels, out_channels, stride, groups): super().__init__() self.conv1 = Conv2d(in_channels, out_channels, kernel_size=1, norm='def', act='def') self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, groups=groups, norm='def', act='def') self.conv3 = Sequential([ Conv2d(out_channels, out_channels, kernel_size=1, bias=False), Norm(out_channels) ]) self.eca = ECALayer() if stride != 1 or in_channels != out_channels: self.shortcut = Conv2d(in_channels, out_channels, stride=stride, kernel_size=1, norm='def') else: self.shortcut = Identity() self.act = Act()
def __init__(self, in_channels, channels, stride, cardinality, base_width): super().__init__() out_channels = channels * 4 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.shortcut = Conv2d( in_channels, out_channels, kernel_size=1, stride=stride, norm='def') if in_channels != out_channels else Identity() self.act = Act()
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, channels, kernel_size=1, norm='def', act='def') self.conv2 = Conv2d(channels, channels, kernel_size=3, stride=stride, norm='def', act='def') self.conv3 = Conv2d(channels, out_channels, kernel_size=1) self.bn3 = 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()
def __init__(self, in_channels, channels, groups, stride=1, se_reduction=4, drop_path=0.2): super().__init__() branch1 = [ Norm(in_channels), Conv2d(in_channels, channels, kernel_size=1, norm='default', act='default'), *([Pool2d(3, 2)] if stride != 1 else []), Conv2d(channels, channels, kernel_size=3, groups=groups, norm='default', act='default'), *([SELayer(channels, se_reduction, groups)] if se_reduction else []), Conv2d(channels, channels, kernel_size=1, norm='default'), *([DropPath(drop_path)] if drop_path and stride == 1 else []), ] self.branch1 = Sequential(branch1) self.branch2 = Shortcut(in_channels, channels, stride)
def __init__(self, in_channels, aspp_channels, num_classes): super(Decoder, self).__init__() self.conv1 = Conv2d(in_channels, 48, kernel_size=1, norm='def', act='def') self.conv2 = SeparableConv2d(aspp_channels + 48, 256, kernel_size=3, norm='def', act='def') self.conv3 = SeparableConv2d(256, 256, kernel_size=3, norm='def', act='def') self.conv = Conv2d(256, num_classes, kernel_size=1)
def __init__(self, C_in, C_out): super().__init__() assert C_out % 2 == 0 self.act = Act() self.slice = Slice([1, 1, 0], [-1, -1, -1]) self.conv1 = Conv2d(C_in, C_out // 2, 1, stride=2, bias=False) self.conv2 = Conv2d(C_in, C_out // 2, 1, stride=2, bias=False) self.norm = Norm(C_out) self.concat = Concatenate()
def __init__(self, in_channels, num_paths, reduction): super().__init__() self.num_paths = num_paths channels = min(max(in_channels // reduction, 32), in_channels) self.pool = GlobalAvgPool(keep_dim=True) self.fc = Sequential([ Conv2d(in_channels, channels, 1, norm='def', act='def'), Conv2d(channels, in_channels * num_paths, 1), ])
def __init__(self, in_channels, num_classes): layers = [ Norm(in_channels), Pool2d(5, 3, padding=0, type='avg'), Conv2d(in_channels, 128, 1, norm='def', act='def'), Conv2d(128, 768, 2, padding=0, norm='def', act='def'), Flatten(), Linear(768, num_classes), ] super().__init__(layers)
def __init__(self, in_channels, use_se): super().__init__() c = in_channels // 2 branch2 = [ Conv2d(c, c, kernel_size=1, norm='def', act='def'), Conv2d(c, c, kernel_size=3, groups=c, norm='def'), Conv2d(c, c, kernel_size=1, norm='def', act='def') ] if use_se: branch2.append(SELayer(c, reduction=2)) self.branch2 = Sequential(branch2)
def __init__(self, in_channels=1, num_classes=10): super().__init__([ Conv2d(in_channels, 6, kernel_size=5, norm='def', act='def'), Pool2d(2, 2, type='avg'), Conv2d(6, 16, kernel_size=5, norm='def', act='def'), Pool2d(2, 2, type='avg'), Flatten(), Linear(8 * 8 * 16, 120), Linear(120, 84), Linear(84, num_classes), ])
def __init__(self, in_channels, out_channels, dropout): layers = [ Norm(in_channels), Act(), Conv2d(in_channels, out_channels, kernel_size=3), Norm(out_channels), Act(), Conv2d(out_channels, out_channels, kernel_size=3), ] if dropout: layers.insert(5, Dropout(dropout)) super().__init__(layers)
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, 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(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
def __init__(self, C, num_classes): """assuming input size 8x8""" super().__init__() self.features = Sequential([ Act(), Pool2d(5, stride=3, padding=0, type='avg'), Conv2d(C, 128, 1, norm='def', act='def'), Conv2d(128, 768, 2, norm='def', act='def', padding=0), ]) self.classifier = Sequential([ GlobalAvgPool(), Linear(768, num_classes), ])
def __init__(self, in_channels, out_channels, dropout, reduction): layers = [ Norm(in_channels), Act(), Conv2d(in_channels, out_channels, kernel_size=3), Norm(out_channels), Act(), Conv2d(out_channels, out_channels, kernel_size=3), ] if dropout: layers.insert(5, Dropout(dropout)) layers.append(SELayer(out_channels, reduction=reduction)) super().__init__(layers)
def __init__(self, C_in, C_out, kernel_size, stride, dilation): super().__init__([ Act(), Conv2d(C_in, C_in, kernel_size, stride=stride, dilation=dilation, groups=C_in, bias=False), Conv2d(C_in, C_out, 1, bias=False), Norm(C_out), ])
def __init__(self, in_channels, channels, stride, base_width, scale, cardinality, 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(channels * (base_width / 64)) * scale * cardinality 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=cardinality, 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
def __init__(self, in_channels, channels, groups, stride=1, radix=1, drop_path=0.2): super().__init__() out_channels = channels * self.expansion branch1 = [ Norm(in_channels), Conv2d(in_channels, channels, kernel_size=1, norm='def', act='default'), *([Pool2d(3, 2)] if stride != 1 else []), SplAtConv2d(channels, channels, kernel_size=3, groups=groups, radix=radix) if radix != 0 else Conv2d(channels, channels, kernel_size=3, groups=groups, norm='def', act='default'), Conv2d(channels, out_channels, kernel_size=1, norm='def'), *([DropPath(drop_path)] if drop_path and stride == 1 else []), ] self.branch1 = Sequential(branch1) self.branch2 = Shortcut(in_channels, out_channels, stride)