def __init__(self, class_dim=1000): super(InceptionV4DY, self).__init__() self._inception_stem = InceptionStem() self._inceptionA_1 = InceptionA(name="1") self._inceptionA_2 = InceptionA(name="2") self._inceptionA_3 = InceptionA(name="3") self._inceptionA_4 = InceptionA(name="4") self._reductionA = ReductionA() self._inceptionB_1 = InceptionB(name="1") self._inceptionB_2 = InceptionB(name="2") self._inceptionB_3 = InceptionB(name="3") self._inceptionB_4 = InceptionB(name="4") self._inceptionB_5 = InceptionB(name="5") self._inceptionB_6 = InceptionB(name="6") self._inceptionB_7 = InceptionB(name="7") self._reductionB = ReductionB() self._inceptionC_1 = InceptionC(name="1") self._inceptionC_2 = InceptionC(name="2") self._inceptionC_3 = InceptionC(name="3") self.avg_pool = AdaptiveAvgPool2D(1) self._drop = Dropout(p=0.2, mode="downscale_in_infer") stdv = 1.0 / math.sqrt(1536 * 1.0) self.out = Linear( 1536, class_dim, weight_attr=ParamAttr( initializer=Uniform(-stdv, stdv), name="final_fc_weights"), bias_attr=ParamAttr(name="final_fc_offset"))
def __init__(self, input_channels, num_squeezed_channels, oup, padding_type, name=None, model_name=None, cur_stage=None): super(SEBlock, self).__init__() self._pool = AdaptiveAvgPool2D(1) self._conv1 = Conv2ds( input_channels, num_squeezed_channels, 1, use_bias=True, padding_type=padding_type, act="swish", name=name + "_se_reduce") self._conv2 = Conv2ds( num_squeezed_channels, oup, 1, act="sigmoid", use_bias=True, padding_type=padding_type, name=name + "_se_expand")
def __init__(self, num_channels, num_filters, reduction_ratio, name=None, data_format="NCHW"): super(SELayer, self).__init__() self.data_format = data_format self.pool2d_gap = AdaptiveAvgPool2D(1, data_format=self.data_format) self._num_channels = num_channels med_ch = int(num_channels / reduction_ratio) stdv = 1.0 / math.sqrt(num_channels * 1.0) self.squeeze = Linear(num_channels, med_ch, weight_attr=ParamAttr( initializer=Uniform(-stdv, stdv), name=name + "_sqz_weights"), bias_attr=ParamAttr(name=name + '_sqz_offset')) self.relu = nn.ReLU() stdv = 1.0 / math.sqrt(med_ch * 1.0) self.excitation = Linear( med_ch, num_filters, weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name=name + "_exc_weights"), bias_attr=ParamAttr(name=name + '_exc_offset')) self.sigmoid = nn.Sigmoid()
def __init__(self, num_classes, in_channels, loss_cfg=dict(name='CrossEntropyLoss'), drop_ratio=0.4, std=0.01, data_format="NCHW", fclr5=True, **kwargs): super().__init__(num_classes, in_channels, loss_cfg, **kwargs) self.drop_ratio = drop_ratio self.std = std # NOTE: global pool performance self.avgpool2d = AdaptiveAvgPool2D((1, 1), data_format=data_format) if self.drop_ratio != 0: self.dropout = Dropout(p=self.drop_ratio) else: self.dropout = None self.fc = Linear( self.in_channels, self.num_classes, weight_attr=ParamAttr(learning_rate=5.0 if fclr5 else 1.0, regularizer=L2Decay(1e-4)), bias_attr=ParamAttr(learning_rate=10.0 if fclr5 else 1.0, regularizer=L2Decay(0.0)))
def __init__(self, depth=50, cardinality=32, num_classes=1000, with_pool=True): super(ResNeXt, self).__init__() self.depth = depth self.cardinality = cardinality self.num_classes = num_classes self.with_pool = with_pool supported_depth = [50, 101, 152] assert depth in supported_depth, \ "supported layers are {} but input layer is {}".format( supported_depth, depth) supported_cardinality = [32, 64] assert cardinality in supported_cardinality, \ "supported cardinality is {} but input cardinality is {}" \ .format(supported_cardinality, cardinality) layer_cfg = {50: [3, 4, 6, 3], 101: [3, 4, 23, 3], 152: [3, 8, 36, 3]} layers = layer_cfg[depth] num_channels = [64, 256, 512, 1024] num_filters = [128, 256, 512, 1024 ] if cardinality == 32 else [256, 512, 1024, 2048] self.conv = ConvBNLayer(num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu') self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] for block in range(len(layers)): shortcut = False for i in range(layers[block]): bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock(num_channels=num_channels[block] if i == 0 else num_filters[block] * int(64 // self.cardinality), num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, cardinality=self.cardinality, shortcut=shortcut)) self.block_list.append(bottleneck_block) shortcut = True if with_pool: self.pool2d_avg = AdaptiveAvgPool2D(1) if num_classes > 0: self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear( self.pool2d_avg_channels, num_classes, weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)))
def __init__(self, class_dim=1000, scale=1.0, prefix_name="", **args): super(MobileNet, self).__init__() self.scale = scale self.class_dim = class_dim bottleneck_params_list = [ (1, 16, 1, 1), (6, 24, 2, 2), (6, 32, 3, 2), (6, 64, 4, 2), (6, 96, 3, 1), (6, 160, 3, 2), (6, 320, 1, 1), ] self.conv1 = ConvBNLayer( num_channels=3, num_filters=int(32 * scale), filter_size=3, stride=2, padding=1, name=prefix_name + "conv1_1") self.block_list = [] i = 1 in_c = int(32 * scale) for layer_setting in bottleneck_params_list: t, c, n, s = layer_setting i += 1 block = self.add_sublayer( prefix_name + "conv" + str(i), sublayer=InvresiBlocks( in_c=in_c, t=t, c=int(c * scale), n=n, s=s, name=prefix_name + "conv" + str(i))) self.block_list.append(block) in_c = int(c * scale) self.out_c = int(1280 * scale) if scale > 1.0 else 1280 self.conv9 = ConvBNLayer( num_channels=in_c, num_filters=self.out_c, filter_size=1, stride=1, padding=0, name=prefix_name + "conv9") self.pool2d_avg = AdaptiveAvgPool2D(1) self.out = Linear( self.out_c, class_dim, weight_attr=ParamAttr(name=prefix_name + "fc10_weights"), bias_attr=ParamAttr(name=prefix_name + "fc10_offset"))
def __init__(self, num_classes=1000, with_pool=True): super().__init__() self.num_classes = num_classes self.with_pool = with_pool self.layers_config = { "inception_a": [[192, 256, 288], [32, 64, 64]], "inception_b": [288], "inception_c": [[768, 768, 768, 768], [128, 160, 160, 192]], "inception_d": [768], "inception_e": [1280, 2048] } inception_a_list = self.layers_config["inception_a"] inception_c_list = self.layers_config["inception_c"] inception_b_list = self.layers_config["inception_b"] inception_d_list = self.layers_config["inception_d"] inception_e_list = self.layers_config["inception_e"] self.inception_stem = InceptionStem() self.inception_block_list = nn.LayerList() for i in range(len(inception_a_list[0])): inception_a = InceptionA(inception_a_list[0][i], inception_a_list[1][i]) self.inception_block_list.append(inception_a) for i in range(len(inception_b_list)): inception_b = InceptionB(inception_b_list[i]) self.inception_block_list.append(inception_b) for i in range(len(inception_c_list[0])): inception_c = InceptionC(inception_c_list[0][i], inception_c_list[1][i]) self.inception_block_list.append(inception_c) for i in range(len(inception_d_list)): inception_d = InceptionD(inception_d_list[i]) self.inception_block_list.append(inception_d) for i in range(len(inception_e_list)): inception_e = InceptionE(inception_e_list[i]) self.inception_block_list.append(inception_e) if with_pool: self.avg_pool = AdaptiveAvgPool2D(1) if num_classes > 0: self.dropout = Dropout(p=0.2, mode="downscale_in_infer") stdv = 1.0 / math.sqrt(2048 * 1.0) self.fc = Linear( 2048, num_classes, weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)), bias_attr=ParamAttr())
def __init__(self, class_num=1000, scale=1.0, dropout_prob=0.2, class_expand=1280): super().__init__() self.scale = scale self.class_num = class_num self.class_expand = class_expand stage_repeats = [3, 7, 3] stage_out_channels = [ -1, 24, make_divisible(116 * scale), make_divisible(232 * scale), make_divisible(464 * scale), 1024 ] self.conv1 = ConvBNLayer(in_channels=3, out_channels=stage_out_channels[1], kernel_size=3, stride=2) self.max_pool = MaxPool2D(kernel_size=3, stride=2, padding=1) block_list = [] for stage_id, num_repeat in enumerate(stage_repeats): for i in range(num_repeat): if i == 0: block = ESBlock2( in_channels=stage_out_channels[stage_id + 1], out_channels=stage_out_channels[stage_id + 2]) else: block = ESBlock1( in_channels=stage_out_channels[stage_id + 2], out_channels=stage_out_channels[stage_id + 2]) block_list.append(block) self.blocks = nn.Sequential(*block_list) self.conv2 = ConvBNLayer(in_channels=stage_out_channels[-2], out_channels=stage_out_channels[-1], kernel_size=1) self.avg_pool = AdaptiveAvgPool2D(1) self.last_conv = Conv2D(in_channels=stage_out_channels[-1], out_channels=self.class_expand, kernel_size=1, stride=1, padding=0, bias_attr=False) self.hardswish = nn.Hardswish() self.dropout = Dropout(p=dropout_prob, mode="downscale_in_infer") self.flatten = nn.Flatten(start_axis=1, stop_axis=-1) self.fc = Linear(self.class_expand, self.class_num)
def __init__(self, stages_pattern, scale=1.0, class_num=1000, return_patterns=None, return_stages=None): super().__init__() self.scale = scale self.conv = ConvBNLayer( num_channels=3, filter_size=3, num_filters=int(32 * scale), stride=2, padding=1) #num_channels, num_filters1, num_filters2, num_groups, stride self.cfg = [[int(32 * scale), 32, 64, 32, 1], [int(64 * scale), 64, 128, 64, 2], [int(128 * scale), 128, 128, 128, 1], [int(128 * scale), 128, 256, 128, 2], [int(256 * scale), 256, 256, 256, 1], [int(256 * scale), 256, 512, 256, 2], [int(512 * scale), 512, 512, 512, 1], [int(512 * scale), 512, 512, 512, 1], [int(512 * scale), 512, 512, 512, 1], [int(512 * scale), 512, 512, 512, 1], [int(512 * scale), 512, 512, 512, 1], [int(512 * scale), 512, 1024, 512, 2], [int(1024 * scale), 1024, 1024, 1024, 1]] self.blocks = nn.Sequential(* [ DepthwiseSeparable( num_channels=params[0], num_filters1=params[1], num_filters2=params[2], num_groups=params[3], stride=params[4], scale=scale) for params in self.cfg ]) self.avg_pool = AdaptiveAvgPool2D(1) self.flatten = Flatten(start_axis=1, stop_axis=-1) self.fc = Linear( int(1024 * scale), class_num, weight_attr=ParamAttr(initializer=KaimingNormal())) super().init_res( stages_pattern, return_patterns=return_patterns, return_stages=return_stages)
def __init__(self, config, stages_pattern, class_num=1000, return_patterns=None, return_stages=None): super().__init__() self.inception_a_list = config["inception_a"] self.inception_c_list = config["inception_c"] self.inception_b_list = config["inception_b"] self.inception_d_list = config["inception_d"] self.inception_e_list = config["inception_e"] self.inception_stem = InceptionStem() self.inception_block_list = nn.LayerList() for i in range(len(self.inception_a_list[0])): inception_a = InceptionA(self.inception_a_list[0][i], self.inception_a_list[1][i]) self.inception_block_list.append(inception_a) for i in range(len(self.inception_b_list)): inception_b = InceptionB(self.inception_b_list[i]) self.inception_block_list.append(inception_b) for i in range(len(self.inception_c_list[0])): inception_c = InceptionC(self.inception_c_list[0][i], self.inception_c_list[1][i]) self.inception_block_list.append(inception_c) for i in range(len(self.inception_d_list)): inception_d = InceptionD(self.inception_d_list[i]) self.inception_block_list.append(inception_d) for i in range(len(self.inception_e_list)): inception_e = InceptionE(self.inception_e_list[i]) self.inception_block_list.append(inception_e) self.avg_pool = AdaptiveAvgPool2D(1) self.dropout = Dropout(p=0.2, mode="downscale_in_infer") stdv = 1.0 / math.sqrt(2048 * 1.0) self.fc = Linear( 2048, class_num, weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)), bias_attr=ParamAttr()) super().init_res(stages_pattern, return_patterns=return_patterns, return_stages=return_stages)
def __init__(self, version, num_classes=1000, with_pool=True): super(SqueezeNet, self).__init__() self.version = version self.num_classes = num_classes self.with_pool = with_pool supported_versions = ['1.0', '1.1'] assert version in supported_versions, \ "supported versions are {} but input version is {}".format( supported_versions, version) if self.version == "1.0": self._conv = Conv2D( 3, 96, 7, stride=2, weight_attr=ParamAttr(), bias_attr=ParamAttr()) self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0) self._conv1 = MakeFire(96, 16, 64, 64) self._conv2 = MakeFire(128, 16, 64, 64) self._conv3 = MakeFire(128, 32, 128, 128) self._conv4 = MakeFire(256, 32, 128, 128) self._conv5 = MakeFire(256, 48, 192, 192) self._conv6 = MakeFire(384, 48, 192, 192) self._conv7 = MakeFire(384, 64, 256, 256) self._conv8 = MakeFire(512, 64, 256, 256) else: self._conv = Conv2D( 3, 64, 3, stride=2, padding=1, weight_attr=ParamAttr(), bias_attr=ParamAttr()) self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0) self._conv1 = MakeFire(64, 16, 64, 64) self._conv2 = MakeFire(128, 16, 64, 64) self._conv3 = MakeFire(128, 32, 128, 128) self._conv4 = MakeFire(256, 32, 128, 128) self._conv5 = MakeFire(256, 48, 192, 192) self._conv6 = MakeFire(384, 48, 192, 192) self._conv7 = MakeFire(384, 64, 256, 256) self._conv8 = MakeFire(512, 64, 256, 256) self._drop = Dropout(p=0.5, mode="downscale_in_infer") self._conv9 = Conv2D( 512, num_classes, 1, weight_attr=ParamAttr(), bias_attr=ParamAttr()) self._avg_pool = AdaptiveAvgPool2D(1)
def __init__(self, in_channels, channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, radix=2, reduction_factor=4, rectify_avg=False, name=None): super(SplatConv, self).__init__() self.radix = radix self.conv1 = ConvBNLayer(num_channels=in_channels, num_filters=channels * radix, filter_size=kernel_size, stride=stride, groups=groups * radix, act="relu", name=name + "_1_weights") self.avg_pool2d = AdaptiveAvgPool2D(1) inter_channels = int(max(in_channels * radix // reduction_factor, 32)) # to calc gap self.conv2 = ConvBNLayer(num_channels=channels, num_filters=inter_channels, filter_size=1, stride=1, groups=groups, act="relu", name=name + "_2_weights") # to calc atten self.conv3 = Conv2D(in_channels=inter_channels, out_channels=channels * radix, kernel_size=1, stride=1, padding=0, groups=groups, weight_attr=ParamAttr(name=name + "_weights", initializer=KaimingNormal()), bias_attr=False) self.rsoftmax = rSoftmax(radix=radix, cardinality=groups)
def __init__(self, version, class_num=1000): super(SqueezeNet, self).__init__() self.version = version if self.version == "1.0": self._conv = Conv2D(3, 96, 7, stride=2, weight_attr=ParamAttr(name="conv1_weights"), bias_attr=ParamAttr(name="conv1_offset")) self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0) self._conv1 = MakeFire(96, 16, 64, 64, name="fire2") self._conv2 = MakeFire(128, 16, 64, 64, name="fire3") self._conv3 = MakeFire(128, 32, 128, 128, name="fire4") self._conv4 = MakeFire(256, 32, 128, 128, name="fire5") self._conv5 = MakeFire(256, 48, 192, 192, name="fire6") self._conv6 = MakeFire(384, 48, 192, 192, name="fire7") self._conv7 = MakeFire(384, 64, 256, 256, name="fire8") self._conv8 = MakeFire(512, 64, 256, 256, name="fire9") else: self._conv = Conv2D(3, 64, 3, stride=2, padding=1, weight_attr=ParamAttr(name="conv1_weights"), bias_attr=ParamAttr(name="conv1_offset")) self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0) self._conv1 = MakeFire(64, 16, 64, 64, name="fire2") self._conv2 = MakeFire(128, 16, 64, 64, name="fire3") self._conv3 = MakeFire(128, 32, 128, 128, name="fire4") self._conv4 = MakeFire(256, 32, 128, 128, name="fire5") self._conv5 = MakeFire(256, 48, 192, 192, name="fire6") self._conv6 = MakeFire(384, 48, 192, 192, name="fire7") self._conv7 = MakeFire(384, 64, 256, 256, name="fire8") self._conv8 = MakeFire(512, 64, 256, 256, name="fire9") self._drop = Dropout(p=0.5, mode="downscale_in_infer") self._conv9 = Conv2D(512, class_num, 1, weight_attr=ParamAttr(name="conv10_weights"), bias_attr=ParamAttr(name="conv10_offset")) self._avg_pool = AdaptiveAvgPool2D(1)
def __init__(self, channel, reduction=4): super().__init__() self.avg_pool = AdaptiveAvgPool2D(1) self.conv1 = Conv2D(in_channels=channel, out_channels=channel // reduction, kernel_size=1, stride=1, padding=0) self.relu = nn.ReLU() self.conv2 = Conv2D(in_channels=channel // reduction, out_channels=channel, kernel_size=1, stride=1, padding=0) self.hardsigmoid = nn.Hardsigmoid()
def __init__(self, channels, reduction): super(SEModule, self).__init__() self.avg_pool = AdaptiveAvgPool2D(1) self.fc1 = Conv2D(channels, channels // reduction, kernel_size=1, padding=0, bias_attr=False) self.relu = ReLU() self.fc2 = Conv2D(channels // reduction, channels, kernel_size=1, padding=0, bias_attr=False) self.sigmoid = Sigmoid()
def __init__(self, num_classes=1000, with_pool=True): super(GoogLeNet, self).__init__() self.num_classes = num_classes self.with_pool = with_pool self._conv = ConvLayer(3, 64, 7, 2) self._pool = MaxPool2D(kernel_size=3, stride=2) self._conv_1 = ConvLayer(64, 64, 1) self._conv_2 = ConvLayer(64, 192, 3) self._ince3a = Inception(192, 192, 64, 96, 128, 16, 32, 32) self._ince3b = Inception(256, 256, 128, 128, 192, 32, 96, 64) self._ince4a = Inception(480, 480, 192, 96, 208, 16, 48, 64) self._ince4b = Inception(512, 512, 160, 112, 224, 24, 64, 64) self._ince4c = Inception(512, 512, 128, 128, 256, 24, 64, 64) self._ince4d = Inception(512, 512, 112, 144, 288, 32, 64, 64) self._ince4e = Inception(528, 528, 256, 160, 320, 32, 128, 128) self._ince5a = Inception(832, 832, 256, 160, 320, 32, 128, 128) self._ince5b = Inception(832, 832, 384, 192, 384, 48, 128, 128) if with_pool: # out self._pool_5 = AdaptiveAvgPool2D(1) # out1 self._pool_o1 = AvgPool2D(kernel_size=5, stride=3) # out2 self._pool_o2 = AvgPool2D(kernel_size=5, stride=3) if num_classes > 0: # out self._drop = Dropout(p=0.4, mode="downscale_in_infer") self._fc_out = Linear( 1024, num_classes, weight_attr=xavier(1024, 1)) # out1 self._conv_o1 = ConvLayer(512, 128, 1) self._fc_o1 = Linear(1152, 1024, weight_attr=xavier(2048, 1)) self._drop_o1 = Dropout(p=0.7, mode="downscale_in_infer") self._out1 = Linear(1024, num_classes, weight_attr=xavier(1024, 1)) # out2 self._conv_o2 = ConvLayer(528, 128, 1) self._fc_o2 = Linear(1152, 1024, weight_attr=xavier(2048, 1)) self._drop_o2 = Dropout(p=0.7, mode="downscale_in_infer") self._out2 = Linear(1024, num_classes, weight_attr=xavier(1024, 1))
def __init__(self, channel, reduction=4, name=""): super(SEModule, self).__init__() self.avg_pool = AdaptiveAvgPool2D(1) self.conv1 = Conv2D(in_channels=channel, out_channels=channel // reduction, kernel_size=1, stride=1, padding=0, weight_attr=ParamAttr(name=name + "_1_weights"), bias_attr=ParamAttr(name=name + "_1_offset")) self.conv2 = Conv2D(in_channels=channel // reduction, out_channels=channel, kernel_size=1, stride=1, padding=0, weight_attr=ParamAttr(name + "_2_weights"), bias_attr=ParamAttr(name=name + "_2_offset"))
def __init__(self, class_num): super(ExitFlow, self).__init__() name = "exit_flow" self._conv_0 = ExitFlowBottleneckBlock( 728, 728, 1024, name=name + "_1") self._conv_1 = SeparableConv(1024, 1536, stride=1, name=name + "_2") self._conv_2 = SeparableConv(1536, 2048, stride=1, name=name + "_3") self._pool = AdaptiveAvgPool2D(1) stdv = 1.0 / math.sqrt(2048 * 1.0) self._out = Linear( 2048, class_num, weight_attr=ParamAttr( name="fc_weights", initializer=Uniform(-stdv, stdv)), bias_attr=ParamAttr(name="fc_offset"))
def __init__(self, class_dim=1000): super(InceptionV3, self).__init__() self.inception_a_list = [[192, 256, 288], [32, 64, 64]] self.inception_c_list = [[768, 768, 768, 768], [128, 160, 160, 192]] self.inception_stem = InceptionStem() self.inception_block_list = [] for i in range(len(self.inception_a_list[0])): inception_a = self.add_sublayer( "inception_a_" + str(i + 1), InceptionA(self.inception_a_list[0][i], self.inception_a_list[1][i], name=str(i + 1))) self.inception_block_list.append(inception_a) inception_b = self.add_sublayer("nception_b_1", InceptionB(288, name="1")) self.inception_block_list.append(inception_b) for i in range(len(self.inception_c_list[0])): inception_c = self.add_sublayer( "inception_c_" + str(i + 1), InceptionC(self.inception_c_list[0][i], self.inception_c_list[1][i], name=str(i + 1))) self.inception_block_list.append(inception_c) inception_d = self.add_sublayer("inception_d_1", InceptionD(768, name="1")) self.inception_block_list.append(inception_d) inception_e = self.add_sublayer("inception_e_1", InceptionE(1280, name="1")) self.inception_block_list.append(inception_e) inception_e = self.add_sublayer("inception_e_2", InceptionE(2048, name="2")) self.inception_block_list.append(inception_e) self.gap = AdaptiveAvgPool2D(1) self.drop = Dropout(p=0.2, mode="downscale_in_infer") stdv = 1.0 / math.sqrt(2048 * 1.0) self.out = Linear(2048, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_weights"), bias_attr=ParamAttr(name="fc_offset"))
def __init__(self, num_channels, reduction_ratio=4, name=None): super(SEBlock, self).__init__() self.pool2d_gap = AdaptiveAvgPool2D(1) self._num_channels = num_channels stdv = 1.0 / math.sqrt(num_channels * 1.0) med_ch = num_channels // reduction_ratio self.squeeze = Linear(num_channels, med_ch, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name=name + "_1_weights"), bias_attr=ParamAttr(name=name + "_1_offset")) stdv = 1.0 / math.sqrt(med_ch * 1.0) self.excitation = Linear(med_ch, num_channels, weight_attr=ParamAttr( initializer=Uniform(-stdv, stdv), name=name + "_2_weights"), bias_attr=ParamAttr(name=name + "_2_offset"))
def __init__(self, num_channels, num_filters, reduction_ratio, name=None): super(SELayer, self).__init__() self.pool2d_gap = AdaptiveAvgPool2D(1) self._num_channels = num_channels med_ch = int(num_channels / reduction_ratio) stdv = 1.0 / math.sqrt(num_channels * 1.0) self.squeeze = Linear( num_channels, med_ch, weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv))) stdv = 1.0 / math.sqrt(med_ch * 1.0) self.excitation = Linear( med_ch, num_filters, weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)))
def __init__(self): super(GoogLeNet, self).__init__() # GoogLeNet包含五个模块,每个模块后面紧跟一个池化层 # 第一个模块包含1个卷积层 self.conv1 = Conv2D(in_channels=3, out_channels=64, kernel_size=7, padding=3, stride=1) # 3x3最大池化 self.pool1 = MaxPool2D(kernel_size=3, stride=2, padding=1) # 第二个模块包含2个卷积层 self.conv2_1 = Conv2D(in_channels=64, out_channels=64, kernel_size=1, stride=1) self.conv2_2 = Conv2D(in_channels=64, out_channels=192, kernel_size=3, padding=1, stride=1) # 3x3最大池化 self.pool2 = MaxPool2D(kernel_size=3, stride=2, padding=1) # 第三个模块包含2个Inception块 self.block3_1 = Inception(192, 64, (96, 128), (16, 32), 32) self.block3_2 = Inception(256, 128, (128, 192), (32, 96), 64) # 3x3最大池化 self.pool3 = MaxPool2D(kernel_size=3, stride=2, padding=1) # 第四个模块包含5个Inception块 self.block4_1 = Inception(480, 192, (96, 208), (16, 48), 64) self.block4_2 = Inception(512, 160, (112, 224), (24, 64), 64) self.block4_3 = Inception(512, 128, (128, 256), (24, 64), 64) self.block4_4 = Inception(512, 112, (144, 288), (32, 64), 64) self.block4_5 = Inception(528, 256, (160, 320), (32, 128), 128) # 3x3最大池化 self.pool4 = MaxPool2D(kernel_size=3, stride=2, padding=1) # 第五个模块包含2个Inception块 self.block5_1 = Inception(832, 256, (160, 320), (32, 128), 128) self.block5_2 = Inception(832, 384, (192, 384), (48, 128), 128) # 全局池化,尺寸用的是global_pooling,pool_stride不起作用 self.pool5 = AdaptiveAvgPool2D(output_size=1) self.fc = Linear(in_features=1024, out_features=1)
def __init__(self, num_classes, in_channels, loss_cfg=dict(name='CrossEntropyLoss'), drop_ratio=0.4, std=0.01, **kwargs): super().__init__(num_classes, in_channels, loss_cfg, **kwargs) self.drop_ratio = drop_ratio self.std = std #NOTE: global pool performance self.avgpool2d = AdaptiveAvgPool2D((1, 1)) if self.drop_ratio != 0: self.dropout = Dropout(p=self.drop_ratio) else: self.dropout = None self.fc = Linear(self.in_channels, self.num_classes)
def __init__(self, scale, class_num=1000): super(GhostNet, self).__init__() self.cfgs = [ # k, t, c, SE, s [3, 16, 16, 0, 1], [3, 48, 24, 0, 2], [3, 72, 24, 0, 1], [5, 72, 40, 1, 2], [5, 120, 40, 1, 1], [3, 240, 80, 0, 2], [3, 200, 80, 0, 1], [3, 184, 80, 0, 1], [3, 184, 80, 0, 1], [3, 480, 112, 1, 1], [3, 672, 112, 1, 1], [5, 672, 160, 1, 2], [5, 960, 160, 0, 1], [5, 960, 160, 1, 1], [5, 960, 160, 0, 1], [5, 960, 160, 1, 1] ] self.scale = scale output_channels = int(self._make_divisible(16 * self.scale, 4)) self.conv1 = ConvBNLayer(in_channels=3, out_channels=output_channels, kernel_size=3, stride=2, groups=1, act="relu", name="conv1") # build inverted residual blocks idx = 0 self.ghost_bottleneck_list = [] for k, exp_size, c, use_se, s in self.cfgs: in_channels = output_channels output_channels = int(self._make_divisible(c * self.scale, 4)) hidden_dim = int(self._make_divisible(exp_size * self.scale, 4)) ghost_bottleneck = self.add_sublayer( name="_ghostbottleneck_" + str(idx), sublayer=GhostBottleneck(in_channels=in_channels, hidden_dim=hidden_dim, output_channels=output_channels, kernel_size=k, stride=s, use_se=use_se, name="_ghostbottleneck_" + str(idx))) self.ghost_bottleneck_list.append(ghost_bottleneck) idx += 1 # build last several layers in_channels = output_channels output_channels = int(self._make_divisible(exp_size * self.scale, 4)) self.conv_last = ConvBNLayer(in_channels=in_channels, out_channels=output_channels, kernel_size=1, stride=1, groups=1, act="relu", name="conv_last") self.pool2d_gap = AdaptiveAvgPool2D(1) in_channels = output_channels self._fc0_output_channels = 1280 self.fc_0 = ConvBNLayer(in_channels=in_channels, out_channels=self._fc0_output_channels, kernel_size=1, stride=1, act="relu", name="fc_0") self.dropout = nn.Dropout(p=0.2) stdv = 1.0 / math.sqrt(self._fc0_output_channels * 1.0) self.fc_1 = Linear(self._fc0_output_channels, class_num, weight_attr=ParamAttr(name="fc_1_weights", initializer=Uniform( -stdv, stdv)), bias_attr=ParamAttr(name="fc_1_offset"))
def __init__(self, class_num=1000, scale=1.0, act="relu"): super(ShuffleNet, self).__init__() self.scale = scale self.class_num = class_num stage_repeats = [4, 8, 4] if scale == 0.25: stage_out_channels = [-1, 24, 24, 48, 96, 512] elif scale == 0.33: stage_out_channels = [-1, 24, 32, 64, 128, 512] elif scale == 0.5: stage_out_channels = [-1, 24, 48, 96, 192, 1024] elif scale == 1.0: stage_out_channels = [-1, 24, 116, 232, 464, 1024] elif scale == 1.5: stage_out_channels = [-1, 24, 176, 352, 704, 1024] elif scale == 2.0: stage_out_channels = [-1, 24, 224, 488, 976, 2048] else: raise NotImplementedError("This scale size:[" + str(scale) + "] is not implemented!") # 1. conv1 self._conv1 = ConvBNLayer(in_channels=3, out_channels=stage_out_channels[1], kernel_size=3, stride=2, padding=1, act=act, name='stage1_conv') self._max_pool = MaxPool2D(kernel_size=3, stride=2, padding=1) # 2. bottleneck sequences self._block_list = [] for stage_id, num_repeat in enumerate(stage_repeats): for i in range(num_repeat): if i == 0: block = self.add_sublayer( name=str(stage_id + 2) + '_' + str(i + 1), sublayer=InvertedResidualDS( in_channels=stage_out_channels[stage_id + 1], out_channels=stage_out_channels[stage_id + 2], stride=2, act=act, name=str(stage_id + 2) + '_' + str(i + 1))) else: block = self.add_sublayer( name=str(stage_id + 2) + '_' + str(i + 1), sublayer=InvertedResidual( in_channels=stage_out_channels[stage_id + 2], out_channels=stage_out_channels[stage_id + 2], stride=1, act=act, name=str(stage_id + 2) + '_' + str(i + 1))) self._block_list.append(block) # 3. last_conv self._last_conv = ConvBNLayer(in_channels=stage_out_channels[-2], out_channels=stage_out_channels[-1], kernel_size=1, stride=1, padding=0, act=act, name='conv5') # 4. pool self._pool2d_avg = AdaptiveAvgPool2D(1) self._out_c = stage_out_channels[-1] # 5. fc self._fc = Linear(stage_out_channels[-1], class_num, weight_attr=ParamAttr(name='fc6_weights'), bias_attr=ParamAttr(name='fc6_offset'))
def __init__(self, layers=50, class_num=1000, cardinality=32, input_image_channel=3, data_format="NCHW"): super(ResNeXt, self).__init__() self.layers = layers self.cardinality = cardinality self.reduction_ratio = 16 self.data_format = data_format self.input_image_channel = input_image_channel supported_layers = [50, 101, 152] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) supported_cardinality = [32, 64] assert cardinality in supported_cardinality, \ "supported cardinality is {} but input cardinality is {}" \ .format(supported_cardinality, cardinality) if layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] num_channels = [64, 256, 512, 1024] num_filters = [128, 256, 512, 1024 ] if cardinality == 32 else [256, 512, 1024, 2048] if layers < 152: self.conv = ConvBNLayer(num_channels=self.input_image_channel, num_filters=64, filter_size=7, stride=2, act='relu', name="conv1", data_format=self.data_format) else: self.conv1_1 = ConvBNLayer(num_channels=self.input_image_channel, num_filters=64, filter_size=3, stride=2, act='relu', name="conv1", data_format=self.data_format) self.conv1_2 = ConvBNLayer(num_channels=64, num_filters=64, filter_size=3, stride=1, act='relu', name="conv2", data_format=self.data_format) self.conv1_3 = ConvBNLayer(num_channels=64, num_filters=128, filter_size=3, stride=1, act='relu', name="conv3", data_format=self.data_format) self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1, data_format=self.data_format) self.block_list = [] n = 1 if layers == 50 or layers == 101 else 3 for block in range(len(depth)): n += 1 shortcut = False for i in range(depth[block]): bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock(num_channels=num_channels[block] if i == 0 else num_filters[block] * int(64 // self.cardinality), num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, cardinality=self.cardinality, reduction_ratio=self.reduction_ratio, shortcut=shortcut, if_first=block == 0, name=str(n) + '_' + str(i + 1), data_format=self.data_format)) self.block_list.append(bottleneck_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1, data_format=self.data_format) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear(self.pool2d_avg_channels, class_num, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc6_weights"), bias_attr=ParamAttr(name="fc6_offset"))
def __init__(self, layers=50, scales=4, width=26, class_dim=1000): super(Res2Net_vd, self).__init__() self.layers = layers self.scales = scales self.width = width basic_width = self.width * self.scales supported_layers = [50, 101, 152, 200] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] elif layers == 200: depth = [3, 12, 48, 3] num_channels = [64, 256, 512, 1024] num_channels2 = [256, 512, 1024, 2048] num_filters = [basic_width * t for t in [1, 2, 4, 8]] self.conv1_1 = ConvBNLayer(num_channels=3, num_filters=32, filter_size=3, stride=2, act='relu', name="conv1_1") self.conv1_2 = ConvBNLayer(num_channels=32, num_filters=32, filter_size=3, stride=1, act='relu', name="conv1_2") self.conv1_3 = ConvBNLayer(num_channels=32, num_filters=64, filter_size=3, stride=1, act='relu', name="conv1_3") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152, 200] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock(num_channels1=num_channels[block] if i == 0 else num_channels2[block], num_channels2=num_channels2[block], num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, scales=scales, shortcut=shortcut, if_first=block == i == 0, name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear(self.pool2d_avg_channels, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_weights"), bias_attr=ParamAttr(name="fc_offset"))
def __init__(self, layers=50, class_dim=1000, input_image_channel=3, data_format="NCHW"): super(ResNet, self).__init__() self.layers = layers self.data_format = data_format self.input_image_channel = input_image_channel supported_layers = [18, 34, 50, 101, 152] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if layers == 18: depth = [2, 2, 2, 2] elif layers == 34 or layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] num_channels = [64, 256, 512, 1024 ] if layers >= 50 else [64, 64, 128, 256] num_filters = [64, 128, 256, 512] self.conv = ConvBNLayer(num_channels=self.input_image_channel, num_filters=64, filter_size=7, stride=2, act="relu", name="conv1", data_format=self.data_format) self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1, data_format=self.data_format) self.block_list = [] if layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( conv_name, BottleneckBlock( num_channels=num_channels[block] if i == 0 else num_filters[block] * 4, num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name, data_format=self.data_format)) self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( conv_name, BasicBlock(num_channels=num_channels[block] if i == 0 else num_filters[block], num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name, data_format=self.data_format)) self.block_list.append(basic_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1, data_format=self.data_format) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear(self.pool2d_avg_channels, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_0.w_0"), bias_attr=ParamAttr(name="fc_0.b_0"))
def __init__(self, layers=50, class_dim=1000, lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0]): super(ResNet_vd, self).__init__() self.layers = layers supported_layers = [18, 34, 50, 101, 152, 200] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) self.lr_mult_list = lr_mult_list assert isinstance( self.lr_mult_list, (list, tuple )), "lr_mult_list should be in (list, tuple) but got {}".format( type(self.lr_mult_list)) assert len( self.lr_mult_list ) == 5, "lr_mult_list length should should be 5 but got {}".format( len(self.lr_mult_list)) if layers == 18: depth = [2, 2, 2, 2] elif layers == 34 or layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] elif layers == 200: depth = [3, 12, 48, 3] num_channels = [64, 256, 512, 1024 ] if layers >= 50 else [64, 64, 128, 256] num_filters = [64, 128, 256, 512] self.conv1_1 = ConvBNLayer(num_channels=3, num_filters=32, filter_size=3, stride=2, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_1") self.conv1_2 = ConvBNLayer(num_channels=32, num_filters=32, filter_size=3, stride=1, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_2") self.conv1_3 = ConvBNLayer(num_channels=32, num_filters=64, filter_size=3, stride=1, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_3") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] if layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152, 200] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock( num_channels=num_channels[block] if i == 0 else num_filters[block] * 4, num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, if_first=block == i == 0, lr_mult=self.lr_mult_list[block + 1], name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BasicBlock(num_channels=num_channels[block] if i == 0 else num_filters[block], num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, if_first=block == i == 0, name=conv_name, lr_mult=self.lr_mult_list[block + 1])) self.block_list.append(basic_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear(self.pool2d_avg_channels, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_0.w_0"), bias_attr=ParamAttr(name="fc_0.b_0"))
def __init__(self, layers=68, class_dim=1000): super(DPN, self).__init__() self._class_dim = class_dim args = self.get_net_args(layers) bws = args['bw'] inc_sec = args['inc_sec'] rs = args['r'] k_r = args['k_r'] k_sec = args['k_sec'] G = args['G'] init_num_filter = args['init_num_filter'] init_filter_size = args['init_filter_size'] init_padding = args['init_padding'] self.k_sec = k_sec self.conv1_x_1_func = ConvBNLayer(num_channels=3, num_filters=init_num_filter, filter_size=init_filter_size, stride=2, pad=init_padding, act='relu', name="conv1") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) num_channel_dpn = init_num_filter self.dpn_func_list = [] #conv2 - conv5 match_list, num = [], 0 for gc in range(4): bw = bws[gc] inc = inc_sec[gc] R = (k_r * bw) // rs[gc] if gc == 0: _type1 = 'proj' _type2 = 'normal' match = 1 else: _type1 = 'down' _type2 = 'normal' match = match + k_sec[gc - 1] match_list.append(match) self.dpn_func_list.append( self.add_sublayer( "dpn{}".format(match), DualPathFactory(num_channels=num_channel_dpn, num_1x1_a=R, num_3x3_b=R, num_1x1_c=bw, inc=inc, G=G, _type=_type1, name="dpn" + str(match)))) num_channel_dpn = [bw, 3 * inc] for i_ly in range(2, k_sec[gc] + 1): num += 1 if num in match_list: num += 1 self.dpn_func_list.append( self.add_sublayer( "dpn{}".format(num), DualPathFactory(num_channels=num_channel_dpn, num_1x1_a=R, num_3x3_b=R, num_1x1_c=bw, inc=inc, G=G, _type=_type2, name="dpn" + str(num)))) num_channel_dpn = [ num_channel_dpn[0], num_channel_dpn[1] + inc ] out_channel = sum(num_channel_dpn) self.conv5_x_x_bn = BatchNorm( num_channels=sum(num_channel_dpn), act="relu", param_attr=ParamAttr(name='final_concat_bn_scale'), bias_attr=ParamAttr('final_concat_bn_offset'), moving_mean_name='final_concat_bn_mean', moving_variance_name='final_concat_bn_variance') self.pool2d_avg = AdaptiveAvgPool2D(1) stdv = 0.01 self.out = Linear(out_channel, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_weights"), bias_attr=ParamAttr(name="fc_offset"))