Пример #1
0
    def __init__(self,
                 input_channels: int,
                 num_squeezed_channels: int,
                 oup: int,
                 padding_type: str,
                 name: str = None,
                 model_name: str = None,
                 cur_stage: str = 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")
Пример #2
0
    def __init__(self, class_dim: int = 1000, load_checkpoint: str = None):
        super(ResNeXt50_32x4d, self).__init__()

        self.layers = 50
        self.cardinality = 32
        depth = [3, 4, 6, 3]
        num_channels = [64, 256, 512, 1024]
        num_filters = [128, 256, 512, 1024]

        self.conv = ConvBNLayer(num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu', name="res_conv1")
        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]):
                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] * int(64 // self.cardinality),
                        num_filters=num_filters[block],
                        stride=2 if i == 0 and block != 0 else 1,
                        cardinality=self.cardinality,
                        shortcut=shortcut,
                        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"))

        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory, 'resnext50_32x4d_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/resnext50_32x4d_imagenet.pdparams -O '
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")
Пример #3
0
    def __init__(self, class_dim: int):
        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_dim,
            weight_attr=ParamAttr(name="fc_weights", initializer=Uniform(-stdv, stdv)),
            bias_attr=ParamAttr(name="fc_offset"))
Пример #4
0
 def __init__(self, channel: int, reduction: int = 4, name: str = ""):
     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"))
Пример #5
0
    def __init__(self, class_dim: int = 1000, load_checkpoint: str = None):
        super(InceptionV4, 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"))

        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory, 'inceptionv4_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/inceptionv4_imagenet.pdparams -O'
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")
Пример #6
0
    def __init__(self, class_dim: int = 1000, load_checkpoint: str = None):
        super(MobileNet, self).__init__()

        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), filter_size=3, stride=2, padding=1, name="conv1_1")

        self.block_list = []
        i = 1
        in_c = int(32)
        for layer_setting in bottleneck_params_list:
            t, c, n, s = layer_setting
            i += 1
            block = self.add_sublayer(
                "conv" + str(i), sublayer=InversiBlocks(in_c=in_c, t=t, c=int(c), n=n, s=s, name="conv" + str(i)))
            self.block_list.append(block)
            in_c = int(c)

        self.out_c = 1280
        self.conv9 = ConvBNLayer(
            num_channels=in_c, num_filters=self.out_c, filter_size=1, stride=1, padding=0, name="conv9")

        self.pool2d_avg = AdaptiveAvgPool2d(1)

        self.out = Linear(
            self.out_c, class_dim, weight_attr=ParamAttr(name="fc10_weights"), bias_attr=ParamAttr(name="fc10_offset"))

        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory, 'mobilenet_v2_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/mobilenet_v2_imagenet.pdparams -O '
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")
Пример #7
0
    def __init__(self,
                 is_test: bool = True,
                 padding_type: str = 'DYNAMIC',
                 override_params: dict = None,
                 use_se: bool = False,
                 class_dim: int = 1000,
                 load_checkpoint: str = None):
        super(EfficientNet_B0, self).__init__()

        model_name = 'efficientnet-b0'
        self.name = "b0"
        self._block_args, self._global_params = get_model_params(model_name, override_params)
        self.padding_type = padding_type
        self.use_se = use_se
        self.is_test = is_test

        self._ef = ExtractFeatures(
            3,
            self._block_args,
            self._global_params,
            self.padding_type,
            self.use_se,
            self.is_test,
            model_name=self.name)

        output_channels = round_filters(1280, self._global_params)
        oup = 320

        self._conv = ConvBNLayer(
            oup,
            1,
            output_channels,
            bn_act="swish",
            padding_type=self.padding_type,
            name="",
            conv_name="_conv_head",
            bn_name="_bn1",
            model_name=self.name,
            cur_stage=7)
        self._pool = AdaptiveAvgPool2d(1)

        if self._global_params.dropout_rate:
            self._drop = Dropout(p=self._global_params.dropout_rate, mode="upscale_in_train")

        param_attr, bias_attr = init_fc_layer("_fc")
        self._fc = Linear(output_channels, class_dim, weight_attr=param_attr, bias_attr=bias_attr)

        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory, 'efficientnet_b0_small_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/efficientnet_b0_small_imagenet.pdparams -O '
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")
Пример #8
0
    def __init__(self, class_dim: int = 1000, load_checkpoint: str = None):
        super(MobileNetV1, self).__init__()
        self.block_list = []

        self.conv1 = ConvBNLayer(num_channels=3,
                                 filter_size=3,
                                 channels=3,
                                 num_filters=int(32),
                                 stride=2,
                                 padding=1,
                                 name="conv1")

        conv2_1 = self.add_sublayer("conv2_1",
                                    sublayer=DepthwiseSeparable(
                                        num_channels=int(32),
                                        num_filters1=32,
                                        num_filters2=64,
                                        num_groups=32,
                                        stride=1,
                                        scale=1,
                                        name="conv2_1"))
        self.block_list.append(conv2_1)

        conv2_2 = self.add_sublayer("conv2_2",
                                    sublayer=DepthwiseSeparable(
                                        num_channels=int(64),
                                        num_filters1=64,
                                        num_filters2=128,
                                        num_groups=64,
                                        stride=2,
                                        scale=1,
                                        name="conv2_2"))
        self.block_list.append(conv2_2)

        conv3_1 = self.add_sublayer("conv3_1",
                                    sublayer=DepthwiseSeparable(
                                        num_channels=int(128),
                                        num_filters1=128,
                                        num_filters2=128,
                                        num_groups=128,
                                        stride=1,
                                        scale=1,
                                        name="conv3_1"))
        self.block_list.append(conv3_1)

        conv3_2 = self.add_sublayer("conv3_2",
                                    sublayer=DepthwiseSeparable(
                                        num_channels=int(128),
                                        num_filters1=128,
                                        num_filters2=256,
                                        num_groups=128,
                                        stride=2,
                                        scale=1,
                                        name="conv3_2"))
        self.block_list.append(conv3_2)

        conv4_1 = self.add_sublayer("conv4_1",
                                    sublayer=DepthwiseSeparable(
                                        num_channels=int(256),
                                        num_filters1=256,
                                        num_filters2=256,
                                        num_groups=256,
                                        stride=1,
                                        scale=1,
                                        name="conv4_1"))
        self.block_list.append(conv4_1)

        conv4_2 = self.add_sublayer("conv4_2",
                                    sublayer=DepthwiseSeparable(
                                        num_channels=int(256),
                                        num_filters1=256,
                                        num_filters2=512,
                                        num_groups=256,
                                        stride=2,
                                        scale=1,
                                        name="conv4_2"))
        self.block_list.append(conv4_2)

        for i in range(5):
            conv5 = self.add_sublayer("conv5_" + str(i + 1),
                                      sublayer=DepthwiseSeparable(
                                          num_channels=int(512),
                                          num_filters1=512,
                                          num_filters2=512,
                                          num_groups=512,
                                          stride=1,
                                          scale=1,
                                          name="conv5_" + str(i + 1)))
            self.block_list.append(conv5)

        conv5_6 = self.add_sublayer("conv5_6",
                                    sublayer=DepthwiseSeparable(
                                        num_channels=int(512),
                                        num_filters1=512,
                                        num_filters2=1024,
                                        num_groups=512,
                                        stride=2,
                                        scale=1,
                                        name="conv5_6"))
        self.block_list.append(conv5_6)

        conv6 = self.add_sublayer("conv6",
                                  sublayer=DepthwiseSeparable(
                                      num_channels=int(1024),
                                      num_filters1=1024,
                                      num_filters2=1024,
                                      num_groups=1024,
                                      stride=1,
                                      scale=1,
                                      name="conv6"))
        self.block_list.append(conv6)

        self.pool2d_avg = AdaptiveAvgPool2d(1)

        self.out = Linear(int(1024),
                          class_dim,
                          weight_attr=ParamAttr(initializer=MSRA(),
                                                name="fc7_weights"),
                          bias_attr=ParamAttr(name="fc7_offset"))

        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory,
                                      'mobilenet_v1_ssld_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/mobilenet_v1_ssld_imagenet.pdparams -O '
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")
Пример #9
0
    def __init__(self, class_dim: int = 1000, load_checkpoint: str = None):
        super(ResNet18_vd, self).__init__()

        self.layers = 18
        depth = [2, 2, 2, 2]
        num_channels = [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',
                                   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]):
                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))
                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"))

        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory,
                                      'resnet18_vd_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/resnet18_vd_imagenet.pdparams -O '
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")
Пример #10
0
    def __init__(self,
                 dropout_prob: float = 0.2,
                 class_dim: int = 1000,
                 load_checkpoint: str = None):
        super(MobileNetV3Small, self).__init__()

        inplanes = 16
        self.cfg = [
            # k, exp, c,  se,     nl,  s,
            [3, 16, 16, True, "relu", 2],
            [3, 72, 24, False, "relu", 2],
            [3, 88, 24, False, "relu", 1],
            [5, 96, 40, True, "hard_swish", 2],
            [5, 240, 40, True, "hard_swish", 1],
            [5, 240, 40, True, "hard_swish", 1],
            [5, 120, 48, True, "hard_swish", 1],
            [5, 144, 48, True, "hard_swish", 1],
            [5, 288, 96, True, "hard_swish", 2],
            [5, 576, 96, True, "hard_swish", 1],
            [5, 576, 96, True, "hard_swish", 1],
        ]
        self.cls_ch_squeeze = 576
        self.cls_ch_expand = 1280

        self.conv1 = ConvBNLayer(in_c=3,
                                 out_c=make_divisible(inplanes),
                                 filter_size=3,
                                 stride=2,
                                 padding=1,
                                 num_groups=1,
                                 if_act=True,
                                 act="hard_swish",
                                 name="conv1")

        self.block_list = []
        i = 0
        inplanes = make_divisible(inplanes)
        for (k, exp, c, se, nl, s) in self.cfg:
            self.block_list.append(
                ResidualUnit(in_c=inplanes,
                             mid_c=make_divisible(exp),
                             out_c=make_divisible(c),
                             filter_size=k,
                             stride=s,
                             use_se=se,
                             act=nl,
                             name="conv" + str(i + 2)))
            self.add_sublayer(sublayer=self.block_list[-1],
                              name="conv" + str(i + 2))
            inplanes = make_divisible(c)
            i += 1

        self.last_second_conv = ConvBNLayer(in_c=inplanes,
                                            out_c=make_divisible(
                                                self.cls_ch_squeeze),
                                            filter_size=1,
                                            stride=1,
                                            padding=0,
                                            num_groups=1,
                                            if_act=True,
                                            act="hard_swish",
                                            name="conv_last")

        self.pool = AdaptiveAvgPool2d(1)

        self.last_conv = Conv2d(
            in_channels=make_divisible(self.cls_ch_squeeze),
            out_channels=self.cls_ch_expand,
            kernel_size=1,
            stride=1,
            padding=0,
            weight_attr=ParamAttr(name="last_1x1_conv_weights"),
            bias_attr=False)

        self.dropout = Dropout(p=dropout_prob, mode="downscale_in_infer")

        self.out = Linear(self.cls_ch_expand,
                          class_dim,
                          weight_attr=ParamAttr("fc_weights"),
                          bias_attr=ParamAttr(name="fc_offset"))

        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory,
                                      'mobilenet_v3_small_ssld.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/mobilenet_v3_small_ssld.pdparams -O '
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")
Пример #11
0
    def __init__(self, class_dim: int = 1000, load_checkpoint: str = None):
        super(ShuffleNet, self).__init__()
        self.scale = 1
        self.class_dim = class_dim
        stage_repeats = [4, 8, 4]
        stage_out_channels = [-1, 24, 116, 232, 464, 1024]

        # 1. conv1
        self._conv1 = ConvBNLayer(num_channels=3,
                                  num_filters=stage_out_channels[1],
                                  filter_size=3,
                                  stride=2,
                                  padding=1,
                                  if_act=True,
                                  act='relu',
                                  name='stage1_conv')
        self._max_pool = MaxPool2d(kernel_size=3, stride=2, padding=1)

        # 2. bottleneck sequences
        self._block_list = []
        i = 1
        in_c = int(32)
        for idxstage in range(len(stage_repeats)):
            numrepeat = stage_repeats[idxstage]
            output_channel = stage_out_channels[idxstage + 2]
            for i in range(numrepeat):
                if i == 0:
                    block = self.add_sublayer(
                        str(idxstage + 2) + '_' + str(i + 1),
                        InvertedResidualUnit(
                            num_channels=stage_out_channels[idxstage + 1],
                            num_filters=output_channel,
                            stride=2,
                            benchmodel=2,
                            act='relu',
                            name=str(idxstage + 2) + '_' + str(i + 1)))
                    self._block_list.append(block)
                else:
                    block = self.add_sublayer(
                        str(idxstage + 2) + '_' + str(i + 1),
                        InvertedResidualUnit(num_channels=output_channel,
                                             num_filters=output_channel,
                                             stride=1,
                                             benchmodel=1,
                                             act='relu',
                                             name=str(idxstage + 2) + '_' +
                                             str(i + 1)))
                    self._block_list.append(block)

        # 3. last_conv
        self._last_conv = ConvBNLayer(num_channels=stage_out_channels[-2],
                                      num_filters=stage_out_channels[-1],
                                      filter_size=1,
                                      stride=1,
                                      padding=0,
                                      if_act=True,
                                      act='relu',
                                      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_dim,
                          weight_attr=ParamAttr(name='fc6_weights'),
                          bias_attr=ParamAttr(name='fc6_offset'))
        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory,
                                      'shufflenet_v2_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/shufflenet_v2_imagenet.pdparams -O '
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")