예제 #1
0
    def __init__(self, in_channels, pool_features, name=None):
        super(InceptionA, self).__init__()
        self.branch1x1 = ConvBNLayer(in_channels,
                                     64,
                                     1,
                                     name=name + '.branch1x1')

        self.branch5x5_1 = ConvBNLayer(in_channels,
                                       48,
                                       1,
                                       name=name + '.branch5x5_1')
        self.branch5x5_2 = ConvBNLayer(48,
                                       64,
                                       5,
                                       padding=2,
                                       name=name + '.branch5x5_2')

        self.branch3x3dbl_1 = ConvBNLayer(in_channels,
                                          64,
                                          1,
                                          name=name + '.branch3x3dbl_1')
        self.branch3x3dbl_2 = ConvBNLayer(64,
                                          96,
                                          3,
                                          padding=1,
                                          name=name + '.branch3x3dbl_2')
        self.branch3x3dbl_3 = ConvBNLayer(96,
                                          96,
                                          3,
                                          padding=1,
                                          name=name + '.branch3x3dbl_3')

        self.branch_pool0 = AvgPool2D(pool_size=3,
                                      pool_stride=1,
                                      pool_padding=1,
                                      exclusive=True)
        self.branch_pool = ConvBNLayer(in_channels,
                                       pool_features,
                                       1,
                                       name=name + '.branch_pool')
예제 #2
0
    def __init__(
        self,
        num_channels,
        num_filters,
        filter_size,
        stride=1,
        groups=1,
        is_vd_mode=False,
        act=None,
        name=None,
    ):
        super(ConvBNLayer, self).__init__()

        self.is_vd_mode = is_vd_mode
        self._pool2d_avg = AvgPool2D(kernel_size=2,
                                     stride=2,
                                     padding=0,
                                     ceil_mode=True)

        self._conv = Conv2D(in_channels=num_channels,
                            out_channels=num_filters,
                            kernel_size=filter_size,
                            stride=stride,
                            padding=(filter_size - 1) // 2,
                            groups=groups,
                            weight_attr=ParamAttr(name=name + "_weights"),
                            bias_attr=False)
        if name == "conv1":
            bn_name = "bn_" + name
        else:
            bn_name = "bn" + name[3:]
        self._batch_norm = BatchNorm(
            num_filters,
            act=act,
            param_attr=ParamAttr(name=bn_name + '_scale'),
            bias_attr=ParamAttr(bn_name + '_offset'),
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')
예제 #3
0
    def __init__(self, num_channels, pool_features):
        super().__init__()
        self.branch1x1 = ConvBNLayer(num_channels=num_channels,
                                     num_filters=64,
                                     filter_size=1,
                                     act="relu")
        self.branch5x5_1 = ConvBNLayer(num_channels=num_channels,
                                       num_filters=48,
                                       filter_size=1,
                                       act="relu")
        self.branch5x5_2 = ConvBNLayer(num_channels=48,
                                       num_filters=64,
                                       filter_size=5,
                                       padding=2,
                                       act="relu")

        self.branch3x3dbl_1 = ConvBNLayer(num_channels=num_channels,
                                          num_filters=64,
                                          filter_size=1,
                                          act="relu")
        self.branch3x3dbl_2 = ConvBNLayer(num_channels=64,
                                          num_filters=96,
                                          filter_size=3,
                                          padding=1,
                                          act="relu")
        self.branch3x3dbl_3 = ConvBNLayer(num_channels=96,
                                          num_filters=96,
                                          filter_size=3,
                                          padding=1,
                                          act="relu")
        self.branch_pool = AvgPool2D(kernel_size=3,
                                     stride=1,
                                     padding=1,
                                     exclusive=False)
        self.branch_pool_conv = ConvBNLayer(num_channels=num_channels,
                                            num_filters=pool_features,
                                            filter_size=1,
                                            act="relu")
예제 #4
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 groups=1,
                 is_tweaks_mode=False,
                 act=None,
                 name=None):
        super(ConvBNLayer, self).__init__()
        self.is_tweaks_mode = is_tweaks_mode
        #ResNet-D 1/2:add a 2×2 average pooling layer with a stride of 2 before the convolution,
        #             whose stride is changed to 1, works well in practice.
        self._pool2d_avg = AvgPool2D(kernel_size=2,
                                     stride=2,
                                     padding=0,
                                     ceil_mode=True)

        self._conv = Conv2D(in_channels=in_channels,
                            out_channels=out_channels,
                            kernel_size=kernel_size,
                            stride=stride,
                            padding=(kernel_size - 1) // 2,
                            groups=groups,
                            weight_attr=ParamAttr(name=name + "_weights"),
                            bias_attr=False)
        if name == "conv1":
            bn_name = "bn_" + name
        else:
            bn_name = "bn" + name[3:]

        self._act = act

        self._batch_norm = BatchNorm2D(
            out_channels,
            weight_attr=ParamAttr(name=bn_name + "_scale",
                                  regularizer=L2Decay(0.0)),
            bias_attr=ParamAttr(bn_name + "_offset", regularizer=L2Decay(0.0)))
예제 #5
0
 def __init__(self, name):
     super(InceptionA, self).__init__()
     self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
     self._conv1 = ConvBNLayer(
         384, 96, 1, act="relu", name="inception_a" + name + "_1x1")
     self._conv2 = ConvBNLayer(
         384, 96, 1, act="relu", name="inception_a" + name + "_1x1_2")
     self._conv3_1 = ConvBNLayer(
         384, 64, 1, act="relu", name="inception_a" + name + "_3x3_reduce")
     self._conv3_2 = ConvBNLayer(
         64,
         96,
         3,
         padding=1,
         act="relu",
         name="inception_a" + name + "_3x3")
     self._conv4_1 = ConvBNLayer(
         384,
         64,
         1,
         act="relu",
         name="inception_a" + name + "_3x3_2_reduce")
     self._conv4_2 = ConvBNLayer(
         64,
         96,
         3,
         padding=1,
         act="relu",
         name="inception_a" + name + "_3x3_2")
     self._conv4_3 = ConvBNLayer(
         96,
         96,
         3,
         padding=1,
         act="relu",
         name="inception_a" + name + "_3x3_3")
예제 #6
0
    def __init__(self, num_channels, ch1x1, ch3x3reduced, ch3x3, doublech3x3reduced, doublech3x3_1, doublech3x3_2, pool_proj):
        '''
        @Brief
            传入参数用于定义 `Inception` 结构
 
        @Parameters
            num_channels : 传入图片通道数
            ch1x1        : 1x1卷积操作的输出通道数
            ch3x3reduced : 3x3卷积之前的1x1卷积的通道数
            ch3x3        : 3x3卷积操作的输出通道数
            doublech3x3reduced : 两个3x3卷积叠加之前的1x1卷积的通道数
            doublech3x3_1        : 第一个3x3卷积操作的输出通道数
            doublech3x3_2        : 第而个3x3卷积操作的输出通道数
            pool_proj    : 池化操作之后1x1卷积的通道数

        @Return
            返回 `Inception` 网络模型

        @Examples
        ------------
        '''

        super(Inception, self).__init__()

        self.branch1 = ConvBNLayer(num_channels=num_channels,
                                    num_filters=ch1x1,
                                    filter_size=1,
                                    stride=1,
                                    padding=0)

        self.branch2 = paddle.nn.Sequential(
                        ConvBNLayer(num_channels=num_channels,
                                    num_filters=ch3x3reduced,
                                    filter_size=1,
                                    stride=1,
                                    padding=0),
                        ConvBNLayer(num_channels=ch3x3reduced,
                                    num_filters=ch3x3,
                                    filter_size=3,
                                    stride=1,
                                    padding=1)
                        )

        self.branch3 = paddle.nn.Sequential(
                        ConvBNLayer(num_channels=num_channels,
                                    num_filters=doublech3x3reduced,
                                    filter_size=1,
                                    stride=1,
                                    padding=0),
                        ConvBNLayer(num_channels=doublech3x3reduced,
                                    num_filters=doublech3x3_1,
                                    filter_size=3,
                                    stride=1,
                                    padding=1),
                        ConvBNLayer(num_channels=doublech3x3_1,
                                    num_filters=doublech3x3_2,
                                    filter_size=3,
                                    stride=1,
                                    padding=1)
                        )

        self.branch4 = paddle.nn.Sequential(
                        AvgPool2D(kernel_size=3,
                                    stride=1,
                                    padding=1),
                        ConvBNLayer(num_channels=num_channels,
                                    num_filters=pool_proj,
                                    filter_size=1,
                                    stride=1,
                                    padding=0)
                        )
예제 #7
0
    def __init__(self, class_num=1000):
        super(GoogLeNetDY, 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 = AdaptiveAvgPool2D(1)

        self._drop = Dropout(p=0.4, mode="downscale_in_infer")
        self._fc_out = Linear(1024,
                              class_num,
                              weight_attr=xavier(1024, 1, "out"),
                              bias_attr=ParamAttr(name="out_offset"))
        self._pool_o1 = AvgPool2D(kernel_size=5, stride=3)
        self._conv_o1 = ConvLayer(512, 128, 1, name="conv_o1")
        self._fc_o1 = Linear(1152,
                             1024,
                             weight_attr=xavier(2048, 1, "fc_o1"),
                             bias_attr=ParamAttr(name="fc_o1_offset"))
        self._drop_o1 = Dropout(p=0.7, mode="downscale_in_infer")
        self._out1 = Linear(1024,
                            class_num,
                            weight_attr=xavier(1024, 1, "out1"),
                            bias_attr=ParamAttr(name="out1_offset"))
        self._pool_o2 = AvgPool2D(kernel_size=5, stride=3)
        self._conv_o2 = ConvLayer(528, 128, 1, name="conv_o2")
        self._fc_o2 = Linear(1152,
                             1024,
                             weight_attr=xavier(2048, 1, "fc_o2"),
                             bias_attr=ParamAttr(name="fc_o2_offset"))
        self._drop_o2 = Dropout(p=0.7, mode="downscale_in_infer")
        self._out2 = Linear(1024,
                            class_num,
                            weight_attr=xavier(1024, 1, "out2"),
                            bias_attr=ParamAttr(name="out2_offset"))
예제 #8
0
    def __init__(self, num_channels, channels_7x7):
        super().__init__()
        self.branch1x1 = ConvNormActivation(in_channels=num_channels,
                                            out_channels=192,
                                            kernel_size=1,
                                            padding=0,
                                            activation_layer=nn.ReLU)

        self.branch7x7_1 = ConvNormActivation(in_channels=num_channels,
                                              out_channels=channels_7x7,
                                              kernel_size=1,
                                              stride=1,
                                              padding=0,
                                              activation_layer=nn.ReLU)
        self.branch7x7_2 = ConvNormActivation(in_channels=channels_7x7,
                                              out_channels=channels_7x7,
                                              kernel_size=(1, 7),
                                              stride=1,
                                              padding=(0, 3),
                                              activation_layer=nn.ReLU)
        self.branch7x7_3 = ConvNormActivation(in_channels=channels_7x7,
                                              out_channels=192,
                                              kernel_size=(7, 1),
                                              stride=1,
                                              padding=(3, 0),
                                              activation_layer=nn.ReLU)

        self.branch7x7dbl_1 = ConvNormActivation(in_channels=num_channels,
                                                 out_channels=channels_7x7,
                                                 kernel_size=1,
                                                 padding=0,
                                                 activation_layer=nn.ReLU)
        self.branch7x7dbl_2 = ConvNormActivation(in_channels=channels_7x7,
                                                 out_channels=channels_7x7,
                                                 kernel_size=(7, 1),
                                                 padding=(3, 0),
                                                 activation_layer=nn.ReLU)
        self.branch7x7dbl_3 = ConvNormActivation(in_channels=channels_7x7,
                                                 out_channels=channels_7x7,
                                                 kernel_size=(1, 7),
                                                 padding=(0, 3),
                                                 activation_layer=nn.ReLU)
        self.branch7x7dbl_4 = ConvNormActivation(in_channels=channels_7x7,
                                                 out_channels=channels_7x7,
                                                 kernel_size=(7, 1),
                                                 padding=(3, 0),
                                                 activation_layer=nn.ReLU)
        self.branch7x7dbl_5 = ConvNormActivation(in_channels=channels_7x7,
                                                 out_channels=192,
                                                 kernel_size=(1, 7),
                                                 padding=(0, 3),
                                                 activation_layer=nn.ReLU)

        self.branch_pool = AvgPool2D(kernel_size=3,
                                     stride=1,
                                     padding=1,
                                     exclusive=False)
        self.branch_pool_conv = ConvNormActivation(in_channels=num_channels,
                                                   out_channels=192,
                                                   kernel_size=1,
                                                   padding=0,
                                                   activation_layer=nn.ReLU)
예제 #9
0
    def __init__(self, num_channels, name=None):
        super(InceptionE, self).__init__()
        self.branch1x1 = ConvBNLayer(num_channels=num_channels,
                                     num_filters=320,
                                     filter_size=1,
                                     act="relu",
                                     name="inception_e_branch1x1_" + name)
        self.branch3x3_1 = ConvBNLayer(num_channels=num_channels,
                                       num_filters=384,
                                       filter_size=1,
                                       act="relu",
                                       name="inception_e_branch3x3_1_" + name)
        self.branch3x3_2a = ConvBNLayer(num_channels=384,
                                        num_filters=384,
                                        filter_size=(1, 3),
                                        padding=(0, 1),
                                        act="relu",
                                        name="inception_e_branch3x3_2a_" +
                                        name)
        self.branch3x3_2b = ConvBNLayer(num_channels=384,
                                        num_filters=384,
                                        filter_size=(3, 1),
                                        padding=(1, 0),
                                        act="relu",
                                        name="inception_e_branch3x3_2b_" +
                                        name)

        self.branch3x3dbl_1 = ConvBNLayer(num_channels=num_channels,
                                          num_filters=448,
                                          filter_size=1,
                                          act="relu",
                                          name="inception_e_branch3x3dbl_1_" +
                                          name)
        self.branch3x3dbl_2 = ConvBNLayer(num_channels=448,
                                          num_filters=384,
                                          filter_size=3,
                                          padding=1,
                                          act="relu",
                                          name="inception_e_branch3x3dbl_2_" +
                                          name)
        self.branch3x3dbl_3a = ConvBNLayer(
            num_channels=384,
            num_filters=384,
            filter_size=(1, 3),
            padding=(0, 1),
            act="relu",
            name="inception_e_branch3x3dbl_3a_" + name)
        self.branch3x3dbl_3b = ConvBNLayer(
            num_channels=384,
            num_filters=384,
            filter_size=(3, 1),
            padding=(1, 0),
            act="relu",
            name="inception_e_branch3x3dbl_3b_" + name)
        self.branch_pool = AvgPool2D(kernel_size=3,
                                     stride=1,
                                     padding=1,
                                     exclusive=False)
        self.branch_pool_conv = ConvBNLayer(num_channels=num_channels,
                                            num_filters=192,
                                            filter_size=1,
                                            act="relu",
                                            name="inception_e_branch_pool_" +
                                            name)
예제 #10
0
    def __init__(self, num_channels, channels_7x7, name=None):
        super(InceptionC, self).__init__()
        self.branch1x1 = ConvBNLayer(num_channels=num_channels,
                                     num_filters=192,
                                     filter_size=1,
                                     act="relu",
                                     name="inception_c_branch1x1_" + name)
        self.branch7x7_1 = ConvBNLayer(num_channels=num_channels,
                                       num_filters=channels_7x7,
                                       filter_size=1,
                                       stride=1,
                                       act="relu",
                                       name="inception_c_branch7x7_1_" + name)
        self.branch7x7_2 = ConvBNLayer(num_channels=channels_7x7,
                                       num_filters=channels_7x7,
                                       filter_size=(1, 7),
                                       stride=1,
                                       padding=(0, 3),
                                       act="relu",
                                       name="inception_c_branch7x7_2_" + name)
        self.branch7x7_3 = ConvBNLayer(num_channels=channels_7x7,
                                       num_filters=192,
                                       filter_size=(7, 1),
                                       stride=1,
                                       padding=(3, 0),
                                       act="relu",
                                       name="inception_c_branch7x7_3_" + name)

        self.branch7x7dbl_1 = ConvBNLayer(num_channels=num_channels,
                                          num_filters=channels_7x7,
                                          filter_size=1,
                                          act="relu",
                                          name="inception_c_branch7x7dbl_1_" +
                                          name)
        self.branch7x7dbl_2 = ConvBNLayer(num_channels=channels_7x7,
                                          num_filters=channels_7x7,
                                          filter_size=(7, 1),
                                          padding=(3, 0),
                                          act="relu",
                                          name="inception_c_branch7x7dbl_2_" +
                                          name)
        self.branch7x7dbl_3 = ConvBNLayer(num_channels=channels_7x7,
                                          num_filters=channels_7x7,
                                          filter_size=(1, 7),
                                          padding=(0, 3),
                                          act="relu",
                                          name="inception_c_branch7x7dbl_3_" +
                                          name)
        self.branch7x7dbl_4 = ConvBNLayer(num_channels=channels_7x7,
                                          num_filters=channels_7x7,
                                          filter_size=(7, 1),
                                          padding=(3, 0),
                                          act="relu",
                                          name="inception_c_branch7x7dbl_4_" +
                                          name)
        self.branch7x7dbl_5 = ConvBNLayer(num_channels=channels_7x7,
                                          num_filters=192,
                                          filter_size=(1, 7),
                                          padding=(0, 3),
                                          act="relu",
                                          name="inception_c_branch7x7dbl_5_" +
                                          name)

        self.branch_pool = AvgPool2D(kernel_size=3,
                                     stride=1,
                                     padding=1,
                                     exclusive=False)
        self.branch_pool_conv = ConvBNLayer(num_channels=num_channels,
                                            num_filters=192,
                                            filter_size=1,
                                            act="relu",
                                            name="inception_c_branch_pool_" +
                                            name)
예제 #11
0
파일: ECO.py 프로젝트: PaddlePaddle/Contrib
    def __init__(self, num_channels, ch1x1, ch3x3reduced, ch3x3,
                 doublech3x3reduced, doublech3x3_1, doublech3x3_2, pool_proj):
        '''
        @Brief
             `Inception` 
 
        @Parameters
            num_channels : channel numbers of input tensor
            ch1x1        : output channel numbers of 1x1 conv
            ch3x3reduced : channel numbers of 1x1 conv before 3x3 conv
            ch3x3        : output channel numbers of 3x3 conv
            doublech3x3reduced : channel numbers of 1x1 conv before the double 3x3 convs
            doublech3x3_1        : output channel numbers of the first 3x3 conv
            doublech3x3_2        : output channel numbers of the second 3x3 conv
            pool_proj    : output channel numbers of 1x1 conv after pool

        @Return
             `Inception` model

        '''

        super(Inception, self).__init__()

        self.branch1 = ConvBNLayer(num_channels=num_channels,
                                   num_filters=ch1x1,
                                   filter_size=1,
                                   stride=1,
                                   padding=0)

        self.branch2 = paddle.nn.Sequential(
            ConvBNLayer(num_channels=num_channels,
                        num_filters=ch3x3reduced,
                        filter_size=1,
                        stride=1,
                        padding=0),
            ConvBNLayer(num_channels=ch3x3reduced,
                        num_filters=ch3x3,
                        filter_size=3,
                        stride=1,
                        padding=1))

        self.branch3 = paddle.nn.Sequential(
            ConvBNLayer(num_channels=num_channels,
                        num_filters=doublech3x3reduced,
                        filter_size=1,
                        stride=1,
                        padding=0),
            ConvBNLayer(num_channels=doublech3x3reduced,
                        num_filters=doublech3x3_1,
                        filter_size=3,
                        stride=1,
                        padding=1),
            ConvBNLayer(num_channels=doublech3x3_1,
                        num_filters=doublech3x3_2,
                        filter_size=3,
                        stride=1,
                        padding=1))

        self.branch4 = paddle.nn.Sequential(
            AvgPool2D(kernel_size=3, stride=1, padding=1),
            ConvBNLayer(num_channels=num_channels,
                        num_filters=pool_proj,
                        filter_size=1,
                        stride=1,
                        padding=0))
예제 #12
0
파일: model.py 프로젝트: zhong110020/models
    def __init__(self, config):
        super(TSN_ResNet, self).__init__()
        self.layers = config.MODEL.num_layers
        self.seg_num = config.MODEL.seg_num
        self.class_dim = config.MODEL.num_classes
        supported_layers = [18, 34, 50, 101, 152]
        assert self.layers in supported_layers, \
            "supported layers are {} but input layer is {}".format(
                supported_layers, layers)

        if self.layers == 18:
            depth = [2, 2, 2, 2]
        elif self.layers == 34 or self.layers == 50:
            depth = [3, 4, 6, 3]
        elif self.layers == 101:
            depth = [3, 4, 23, 3]
        elif self.layers == 152:
            depth = [3, 8, 36, 3]
        in_channels = [64, 256, 512, 1024
                       ] if self.layers >= 50 else [64, 64, 128, 256]
        out_channels = [64, 128, 256, 512]

        self.conv = ConvBNLayer(in_channels=3,
                                out_channels=64,
                                kernel_size=7,
                                stride=2,
                                act="relu",
                                name="conv1")
        self.pool2D_max = MaxPool2D(kernel_size=3, stride=2, padding=1)

        self.block_list = []
        if self.layers >= 50:
            for block in range(len(depth)):
                shortcut = False
                for i in range(depth[block]):
                    if self.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(
                            in_channels=in_channels[block]
                            if i == 0 else out_channels[block] * 4,
                            out_channels=out_channels[block],
                            stride=2 if i == 0 and block != 0 else 1,
                            shortcut=shortcut,
                            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(
                        conv_name,
                        BasicBlock(in_channels=in_channels[block]
                                   if i == 0 else out_channels[block],
                                   out_channels=out_channels[block],
                                   stride=2 if i == 0 and block != 0 else 1,
                                   shortcut=shortcut,
                                   name=conv_name))
                    self.block_list.append(basic_block)
                    shortcut = True

        self.pool2D_avg = AvgPool2D(kernel_size=7)

        self.pool2D_avg_channels = in_channels[-1] * 2

        self.out = Linear(
            self.pool2D_avg_channels,
            self.class_dim,
            weight_attr=ParamAttr(initializer=paddle.nn.initializer.Normal(
                mean=0.0, std=0.01),
                                  name="fc_0.w_0"),
            bias_attr=ParamAttr(
                initializer=paddle.nn.initializer.Constant(value=0.0),
                name="fc_0.b_0"))
예제 #13
0
    def __init__(self, layers=50, num_classes=1000):
        super(ResNet, self).__init__()
        self.layers = layers
        supported_layers = [18, 34, 50, 101, 152]
        assert layers in supported_layers

        if layers == 18:
            depth = [2, 2, 2, 2]
        elif layers == 34:
            depth = [3, 4, 6, 3]
        elif layers == 50:
            depth = [3, 4, 6, 3]
        elif layers == 101:
            depth = [3, 4, 23, 3]
        elif layers == 152:
            depth = [3, 8, 36, 3]

        if layers < 50:
            num_channels = [64, 64, 128, 256]
        else:
            num_channels = [64, 256, 512, 1024]

        num_filters = [64, 128, 256, 512]

        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)
        if layers < 50:
            block = BasicBlock
            l1_shortcut = True
        else:
            block = BottleneckBlock
            l1_shortcut = False

        self.layer1 = Sequential(*self.make_layer(block,
                                                  num_channels[0],
                                                  num_filters[0],
                                                  depth[0],
                                                  stride=1,
                                                  shortcut=l1_shortcut,
                                                  name='layer1'))
        self.layer2 = Sequential(*self.make_layer(block,
                                                  num_channels[1],
                                                  num_filters[1],
                                                  depth[1],
                                                  stride=2,
                                                  name='layer2'))
        self.layer3 = Sequential(*self.make_layer(block,
                                                  num_channels[2],
                                                  num_filters[2],
                                                  depth[2],
                                                  stride=1,
                                                  name='layer3',
                                                  dilation=2))
        self.layer4 = Sequential(*self.make_layer(block,
                                                  num_channels[3],
                                                  num_filters[3],
                                                  depth[3],
                                                  stride=1,
                                                  name='layer4',
                                                  dilation=4))
        self.last_pool = AvgPool2D(
            kernel_size=7  # ignore if global_pooling is True
        )
        self.fc = Linear(in_features=num_filters[-1] * block.expansion,
                         out_features=num_classes)

        self.out_dim = num_filters[-1] * block.expansion
예제 #14
0
    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 radix=1,
                 cardinality=1,
                 bottleneck_width=64,
                 avd=False,
                 avd_first=False,
                 dilation=1,
                 is_first=False,
                 rectify_avg=False,
                 last_gamma=False,
                 avg_down=False,
                 name=None):
        super(BottleneckBlock, self).__init__()
        self.inplanes = inplanes
        self.planes = planes
        self.stride = stride
        self.radix = radix
        self.cardinality = cardinality
        self.avd = avd
        self.avd_first = avd_first
        self.dilation = dilation
        self.is_first = is_first
        self.rectify_avg = rectify_avg
        self.last_gamma = last_gamma
        self.avg_down = avg_down

        group_width = int(planes * (bottleneck_width / 64.)) * cardinality

        self.conv1 = ConvBNLayer(num_channels=self.inplanes,
                                 num_filters=group_width,
                                 filter_size=1,
                                 stride=1,
                                 groups=1,
                                 act="relu",
                                 name=name + "_conv1")

        if avd and avd_first and (stride > 1 or is_first):
            self.avg_pool2d_1 = AvgPool2D(kernel_size=3,
                                          stride=stride,
                                          padding=1)

        if radix >= 1:
            self.conv2 = SplatConv(in_channels=group_width,
                                   channels=group_width,
                                   kernel_size=3,
                                   stride=1,
                                   padding=dilation,
                                   dilation=dilation,
                                   groups=cardinality,
                                   bias=False,
                                   radix=radix,
                                   rectify_avg=rectify_avg,
                                   name=name + "_splat")
        else:
            self.conv2 = ConvBNLayer(num_channels=group_width,
                                     num_filters=group_width,
                                     filter_size=3,
                                     stride=1,
                                     dilation=dilation,
                                     groups=cardinality,
                                     act="relu",
                                     name=name + "_conv2")

        if avd and avd_first == False and (stride > 1 or is_first):
            self.avg_pool2d_2 = AvgPool2D(kernel_size=3,
                                          stride=stride,
                                          padding=1)

        self.conv3 = ConvBNLayer(num_channels=group_width,
                                 num_filters=planes * 4,
                                 filter_size=1,
                                 stride=1,
                                 groups=1,
                                 act=None,
                                 name=name + "_conv3")

        if stride != 1 or self.inplanes != self.planes * 4:
            if avg_down:
                if dilation == 1:
                    self.avg_pool2d_3 = AvgPool2D(kernel_size=stride,
                                                  stride=stride,
                                                  padding=0)
                else:
                    self.avg_pool2d_3 = AvgPool2D(kernel_size=1,
                                                  stride=1,
                                                  padding=0,
                                                  ceil_mode=True)

                self.conv4 = Conv2D(in_channels=self.inplanes,
                                    out_channels=planes * 4,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0,
                                    groups=1,
                                    weight_attr=ParamAttr(
                                        name=name + "_weights",
                                        initializer=KaimingNormal()),
                                    bias_attr=False)
            else:
                self.conv4 = Conv2D(in_channels=self.inplanes,
                                    out_channels=planes * 4,
                                    kernel_size=1,
                                    stride=stride,
                                    padding=0,
                                    groups=1,
                                    weight_attr=ParamAttr(
                                        name=name + "_shortcut_weights",
                                        initializer=KaimingNormal()),
                                    bias_attr=False)

            bn_decay = 0.0
            self._batch_norm = BatchNorm(
                planes * 4,
                act=None,
                param_attr=ParamAttr(name=name + "_shortcut_scale",
                                     regularizer=L2Decay(bn_decay)),
                bias_attr=ParamAttr(name + "_shortcut_offset",
                                    regularizer=L2Decay(bn_decay)),
                moving_mean_name=name + "_shortcut_mean",
                moving_variance_name=name + "_shortcut_variance")
예제 #15
0
    def __init__(self,
                 output_blocks=[DEFAULT_BLOCK_INDEX],
                 class_dim=1000,
                 aux_logits=False,
                 resize_input=True,
                 normalize_input=True):
        super(InceptionV3, self).__init__()
        self.resize_input = resize_input
        self.normalize_input = normalize_input
        self.output_blocks = sorted(output_blocks)
        self.last_needed_block = max(output_blocks)
        self.class_dim = class_dim
        self.aux_logits = aux_logits

        assert self.last_needed_block <= 3, 'Last possible output block index is 3'
        self.blocks = []

        self.Conv2d_1a_3x3 = ConvBNLayer(3,
                                         32,
                                         3,
                                         stride=2,
                                         name='Conv2d_1a_3x3')
        self.Conv2d_2a_3x3 = ConvBNLayer(32, 32, 3, name='Conv2d_2a_3x3')
        self.Conv2d_2b_3x3 = ConvBNLayer(32,
                                         64,
                                         3,
                                         padding=1,
                                         name='Conv2d_2b_3x3')
        self.maxpool1 = MaxPool2D(pool_size=3, pool_stride=2)

        block0 = [
            self.Conv2d_1a_3x3, self.Conv2d_2a_3x3, self.Conv2d_2b_3x3,
            self.maxpool1
        ]
        self.blocks.append(nn.Sequential(*block0))
        ### block1

        if self.last_needed_block >= 1:
            self.Conv2d_3b_1x1 = ConvBNLayer(64, 80, 1, name='Conv2d_3b_1x1')
            self.Conv2d_4a_3x3 = ConvBNLayer(80, 192, 3, name='Conv2d_4a_3x3')
            self.maxpool2 = MaxPool2D(pool_size=3, pool_stride=2)
            block1 = [self.Conv2d_3b_1x1, self.Conv2d_4a_3x3, self.maxpool2]
            self.blocks.append(nn.Sequential(*block1))

        ### block2
        ### Mixed_5b 5c 5d
        if self.last_needed_block >= 2:
            self.Mixed_5b = Fid_inceptionA(192,
                                           pool_features=32,
                                           name='Mixed_5b')
            self.Mixed_5c = Fid_inceptionA(256,
                                           pool_features=64,
                                           name='Mixed_5c')
            self.Mixed_5d = Fid_inceptionA(288,
                                           pool_features=64,
                                           name='Mixed_5d')

            ### Mixed_6
            self.Mixed_6a = InceptionB(288, name='Mixed_6a')
            self.Mixed_6b = Fid_inceptionC(768, c7=128, name='Mixed_6b')
            self.Mixed_6c = Fid_inceptionC(768, c7=160, name='Mixed_6c')
            self.Mixed_6d = Fid_inceptionC(768, c7=160, name='Mixed_6d')
            self.Mixed_6e = Fid_inceptionC(768, c7=192, name='Mixed_6e')

            block2 = [
                self.Mixed_5b, self.Mixed_5c, self.Mixed_5d, self.Mixed_6a,
                self.Mixed_6b, self.Mixed_6c, self.Mixed_6d, self.Mixed_6e
            ]
            self.blocks.append(nn.Sequential(*block2))

        if self.aux_logits:
            self.AuxLogits = InceptionAux(768,
                                          self.class_dim,
                                          name='AuxLogits')
        ### block3
        ### Mixed_7
        if self.last_needed_block >= 3:
            self.Mixed_7a = InceptionD(768, name='Mixed_7a')
            self.Mixed_7b = Fid_inceptionE_1(1280, name='Mixed_7b')
            self.Mixed_7c = Fid_inceptionE_2(2048, name='Mixed_7c')
            self.avgpool = AvgPool2D(global_pooling=True)

            block3 = [
                self.Mixed_7a, self.Mixed_7b, self.Mixed_7c, self.avgpool
            ]
            self.blocks.append(nn.Sequential(*block3))