Exemplo n.º 1
0
    def __init__(self,
                 input_channels: int,
                 output_channels: int,
                 stride: int = 2,
                 name: str = None,
                 relu_first: bool = False):
        super(EntryFlowBottleneckBlock, self).__init__()
        self.relu_first = relu_first

        self._short = Conv2d(in_channels=input_channels,
                             out_channels=output_channels,
                             kernel_size=1,
                             stride=stride,
                             padding=0,
                             weight_attr=ParamAttr(name + "_branch1_weights"),
                             bias_attr=False)
        self._conv1 = SeparableConv(input_channels,
                                    output_channels,
                                    stride=1,
                                    name=name + "_branch2a_weights")
        self._conv2 = SeparableConv(output_channels,
                                    output_channels,
                                    stride=1,
                                    name=name + "_branch2b_weights")
        self._pool = MaxPool2d(kernel_size=3, stride=stride, padding=1)
Exemplo n.º 2
0
 def __init__(self):
     super(ReductionA, self).__init__()
     self._pool = MaxPool2d(kernel_size=3, stride=2, padding=0)
     self._conv2 = ConvBNLayer(384, 384, 3, stride=2, act="relu", name="reduction_a_3x3")
     self._conv3_1 = ConvBNLayer(384, 192, 1, act="relu", name="reduction_a_3x3_2_reduce")
     self._conv3_2 = ConvBNLayer(192, 224, 3, padding=1, act="relu", name="reduction_a_3x3_2")
     self._conv3_3 = ConvBNLayer(224, 256, 3, stride=2, act="relu", name="reduction_a_3x3_3")
Exemplo n.º 3
0
 def __init__(self):
     super(ReductionB, self).__init__()
     self._pool = MaxPool2d(kernel_size=3, stride=2, padding=0)
     self._conv2_1 = ConvBNLayer(1024, 192, 1, act="relu", name="reduction_b_3x3_reduce")
     self._conv2_2 = ConvBNLayer(192, 192, 3, stride=2, act="relu", name="reduction_b_3x3")
     self._conv3_1 = ConvBNLayer(1024, 256, 1, act="relu", name="reduction_b_1x7_reduce")
     self._conv3_2 = ConvBNLayer(256, 256, (1, 7), padding=(0, 3), act="relu", name="reduction_b_1x7")
     self._conv3_3 = ConvBNLayer(256, 320, (7, 1), padding=(3, 0), act="relu", name="reduction_b_7x1")
     self._conv3_4 = ConvBNLayer(320, 320, 3, stride=2, act="relu", name="reduction_b_3x3_2")
Exemplo n.º 4
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")
Exemplo n.º 5
0
    def __init__(self, input_channels: int, output_channels1: int, output_channels2: int, name: str):
        super(ExitFlowBottleneckBlock, self).__init__()

        self._short = Conv2d(
            in_channels=input_channels,
            out_channels=output_channels2,
            kernel_size=1,
            stride=2,
            padding=0,
            weight_attr=ParamAttr(name + "_branch1_weights"),
            bias_attr=False)
        self._conv_1 = SeparableConv(input_channels, output_channels1, stride=1, name=name + "_branch2a_weights")
        self._conv_2 = SeparableConv(output_channels1, output_channels2, stride=1, name=name + "_branch2b_weights")
        self._pool = MaxPool2d(kernel_size=3, stride=2, padding=1)
Exemplo n.º 6
0
 def __init__(self):
     super(InceptionStem, self).__init__()
     self._conv_1 = ConvBNLayer(3, 32, 3, stride=2, act="relu", name="conv1_3x3_s2")
     self._conv_2 = ConvBNLayer(32, 32, 3, act="relu", name="conv2_3x3_s1")
     self._conv_3 = ConvBNLayer(32, 64, 3, padding=1, act="relu", name="conv3_3x3_s1")
     self._pool = MaxPool2d(kernel_size=3, stride=2, padding=0)
     self._conv2 = ConvBNLayer(64, 96, 3, stride=2, act="relu", name="inception_stem1_3x3_s2")
     self._conv1_1 = ConvBNLayer(160, 64, 1, act="relu", name="inception_stem2_3x3_reduce")
     self._conv1_2 = ConvBNLayer(64, 96, 3, act="relu", name="inception_stem2_3x3")
     self._conv2_1 = ConvBNLayer(160, 64, 1, act="relu", name="inception_stem2_1x7_reduce")
     self._conv2_2 = ConvBNLayer(64, 64, (7, 1), padding=(3, 0), act="relu", name="inception_stem2_1x7")
     self._conv2_3 = ConvBNLayer(64, 64, (1, 7), padding=(0, 3), act="relu", name="inception_stem2_7x1")
     self._conv2_4 = ConvBNLayer(64, 96, 3, act="relu", name="inception_stem2_3x3_2")
     self._conv3 = ConvBNLayer(192, 192, 3, stride=2, act="relu", name="inception_stem3_3x3_s2")
Exemplo n.º 7
0
    def __init__(self,
                 input_channels: int,
                 output_channels: int,
                 filter1: int,
                 filter3R: int,
                 filter3: int,
                 filter5R: int,
                 filter5: int,
                 proj: int,
                 name: str = None):
        super(Inception, self).__init__()

        self._conv1 = ConvLayer(input_channels,
                                filter1,
                                1,
                                name="inception_" + name + "_1x1")
        self._conv3r = ConvLayer(input_channels,
                                 filter3R,
                                 1,
                                 name="inception_" + name + "_3x3_reduce")
        self._conv3 = ConvLayer(filter3R,
                                filter3,
                                3,
                                name="inception_" + name + "_3x3")
        self._conv5r = ConvLayer(input_channels,
                                 filter5R,
                                 1,
                                 name="inception_" + name + "_5x5_reduce")
        self._conv5 = ConvLayer(filter5R,
                                filter5,
                                5,
                                name="inception_" + name + "_5x5")
        self._pool = MaxPool2d(kernel_size=3, stride=1, padding=1)

        self._convprj = ConvLayer(input_channels,
                                  proj,
                                  1,
                                  name="inception_" + name + "_3x3_proj")
Exemplo n.º 8
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")
Exemplo n.º 9
0
    def __init__(self, class_dim: int = 1000, load_checkpoint: str = None):
        super(GoogleNet, self).__init__()
        self._conv = ConvLayer(3, 64, 7, 2, name="conv1")
        self._pool = MaxPool2d(kernel_size=3, stride=2)
        self._conv_1 = ConvLayer(64, 64, 1, name="conv2_1x1")
        self._conv_2 = ConvLayer(64, 192, 3, name="conv2_3x3")

        self._ince3a = Inception(192,
                                 192,
                                 64,
                                 96,
                                 128,
                                 16,
                                 32,
                                 32,
                                 name="ince3a")
        self._ince3b = Inception(256,
                                 256,
                                 128,
                                 128,
                                 192,
                                 32,
                                 96,
                                 64,
                                 name="ince3b")

        self._ince4a = Inception(480,
                                 480,
                                 192,
                                 96,
                                 208,
                                 16,
                                 48,
                                 64,
                                 name="ince4a")
        self._ince4b = Inception(512,
                                 512,
                                 160,
                                 112,
                                 224,
                                 24,
                                 64,
                                 64,
                                 name="ince4b")
        self._ince4c = Inception(512,
                                 512,
                                 128,
                                 128,
                                 256,
                                 24,
                                 64,
                                 64,
                                 name="ince4c")
        self._ince4d = Inception(512,
                                 512,
                                 112,
                                 144,
                                 288,
                                 32,
                                 64,
                                 64,
                                 name="ince4d")
        self._ince4e = Inception(528,
                                 528,
                                 256,
                                 160,
                                 320,
                                 32,
                                 128,
                                 128,
                                 name="ince4e")

        self._ince5a = Inception(832,
                                 832,
                                 256,
                                 160,
                                 320,
                                 32,
                                 128,
                                 128,
                                 name="ince5a")
        self._ince5b = Inception(832,
                                 832,
                                 384,
                                 192,
                                 384,
                                 48,
                                 128,
                                 128,
                                 name="ince5b")

        self._pool_5 = AvgPool2d(kernel_size=7, stride=7)

        self._drop = Dropout(p=0.4, mode="downscale_in_infer")
        self._fc_out = Linear(1024,
                              class_dim,
                              weight_attr=xavier(1024, 1, "out"),
                              bias_attr=ParamAttr(name="out_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,
                                      'googlenet_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/googlenet_imagenet.pdparams -O'
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")
Exemplo n.º 10
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")