Exemplo n.º 1
0
    def __init__(self, num_classes=10, classifier_activation=None):
        super(LeNetDygraph, self).__init__()
        self.num_classes = num_classes
        self.features = Sequential(Conv2d(1, 6, 3, stride=1,
                                          padding=1), ReLU(),
                                   Pool2D(2, 'max', 2),
                                   Conv2d(6, 16, 5, stride=1, padding=0),
                                   ReLU(), Pool2D(2, 'max', 2))

        if num_classes > 0:
            self.fc = Sequential(Linear(400, 120), Linear(120, 84),
                                 Linear(84, 10),
                                 Softmax())  #Todo: accept any activation
Exemplo n.º 2
0
    def __init__(self,
                 num_channels: int,
                 num_filters: int,
                 filter_size: int,
                 stride: int = 1,
                 groups: int = 1,
                 act: str = None,
                 name: str = None):
        super(ConvBNLayer, self).__init__()

        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')
Exemplo n.º 3
0
    def __init__(self,
                 num_channels: int,
                 filter_size: int,
                 num_filters: int,
                 stride: int,
                 padding: int,
                 channels: int = None,
                 num_groups: int = 1,
                 act: str = 'relu',
                 name: str = None):
        super(ConvBNLayer, self).__init__()

        self._conv = Conv2d(in_channels=num_channels,
                            out_channels=num_filters,
                            kernel_size=filter_size,
                            stride=stride,
                            padding=padding,
                            groups=num_groups,
                            weight_attr=ParamAttr(initializer=MSRA(),
                                                  name=name + "_weights"),
                            bias_attr=False)

        self._batch_norm = BatchNorm(num_filters,
                                     act=act,
                                     param_attr=ParamAttr(name + "_bn_scale"),
                                     bias_attr=ParamAttr(name + "_bn_offset"),
                                     moving_mean_name=name + "_bn_mean",
                                     moving_variance_name=name +
                                     "_bn_variance")
Exemplo n.º 4
0
Arquivo: layers.py Projeto: zdqf/hapi
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 padding=0,
                 stddev=0.02,
                 norm=True,
                 act='leaky_relu',
                 relufactor=0.0,
                 use_bias=False):
        super(ConvBN, self).__init__()

        pattr = paddle.ParamAttr(
            initializer=nn.initializer.Normal(loc=0.0, scale=stddev))
        self.conv = Conv2d(in_channels=num_channels,
                           out_channels=num_filters,
                           kernel_size=filter_size,
                           stride=stride,
                           padding=padding,
                           weight_attr=pattr,
                           bias_attr=use_bias)
        if norm:
            self.bn = BatchNorm(
                num_filters,
                param_attr=paddle.ParamAttr(
                    initializer=nn.initializer.Normal(1.0, 0.02)),
                bias_attr=paddle.ParamAttr(
                    initializer=nn.initializer.Constant(0.0)),
                is_test=False,
                trainable_statistics=True)
        self.relufactor = relufactor
        self.norm = norm
        self.act = act
Exemplo n.º 5
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.º 6
0
 def __init__(self,
              in_c: int,
              out_c: int,
              filter_size: int,
              stride: int,
              padding: int,
              num_groups: int = 1,
              if_act: bool = True,
              act: str = None,
              name: str = ""):
     super(ConvBNLayer, self).__init__()
     self.if_act = if_act
     self.act = act
     self.conv = Conv2d(in_channels=in_c,
                        out_channels=out_c,
                        kernel_size=filter_size,
                        stride=stride,
                        padding=padding,
                        groups=num_groups,
                        weight_attr=ParamAttr(name=name + "_weights"),
                        bias_attr=False)
     self.bn = BatchNorm(num_channels=out_c,
                         act=None,
                         param_attr=ParamAttr(name=name + "_bn_scale",
                                              regularizer=L2Decay(0.0)),
                         bias_attr=ParamAttr(name=name + "_bn_offset",
                                             regularizer=L2Decay(0.0)),
                         moving_mean_name=name + "_bn_mean",
                         moving_variance_name=name + "_bn_variance")
Exemplo n.º 7
0
def make_layers(cfg, batch_norm=False):
    layers = []
    in_channels = 3

    for v in cfg:
        if v == 'M':
            layers += [Pool2D(pool_size=2, pool_stride=2)]
        else:
            if batch_norm:
                conv2d = Conv2d(in_channels, v, kernel_size=3, padding=1)
                layers += [conv2d, BatchNorm(v), ReLU()]
            else:
                conv2d = Conv2d(in_channels, v, kernel_size=3, padding=1)
                layers += [conv2d, ReLU()]
            in_channels = v
    return Sequential(*layers)
Exemplo n.º 8
0
    def __init__(self,
                 num_channels: int,
                 filter_size: int,
                 num_filters: int,
                 stride: int,
                 padding: int,
                 channels: int = None,
                 num_groups: int = 1,
                 if_act: bool = True,
                 act: str = 'relu',
                 name: str = None):
        super(ConvBNLayer, self).__init__()
        self._if_act = if_act
        assert act in ['relu', 'swish'], \
            "supported act are {} but your act is {}".format(
                ['relu', 'swish'], act)
        self._act = act
        self._conv = Conv2d(in_channels=num_channels,
                            out_channels=num_filters,
                            kernel_size=filter_size,
                            stride=stride,
                            padding=padding,
                            groups=num_groups,
                            weight_attr=ParamAttr(initializer=MSRA(),
                                                  name=name + "_weights"),
                            bias_attr=False)

        self._batch_norm = BatchNorm(
            num_filters,
            param_attr=ParamAttr(name=name + "_bn_scale"),
            bias_attr=ParamAttr(name=name + "_bn_offset"),
            moving_mean_name=name + "_bn_mean",
            moving_variance_name=name + "_bn_variance")
Exemplo n.º 9
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"))
Exemplo n.º 10
0
    def __init__(self,
                 input_channels: int,
                 output_channels: int,
                 filter_size: int,
                 stride: int = 1,
                 padding: int = 0,
                 groups: int = None,
                 name: str = "conv2d",
                 act: str = None,
                 use_bias: bool = False,
                 padding_type: str = None,
                 model_name: str = None,
                 cur_stage: str = None):
        super(Conv2ds, self).__init__()
        assert act in [None, "swish", "sigmoid"]
        self.act = act

        param_attr, bias_attr = initial_type(name=name, use_bias=use_bias)

        def get_padding(filter_size, stride=1, dilation=1):
            padding = ((stride - 1) + dilation * (filter_size - 1)) // 2
            return padding

        inps = 1 if model_name == None and cur_stage == None else inp_shape[
            model_name][cur_stage]
        self.need_crop = False
        if padding_type == "SAME":
            top_padding, bottom_padding = cal_padding(inps, stride,
                                                      filter_size)
            left_padding, right_padding = cal_padding(inps, stride,
                                                      filter_size)
            height_padding = bottom_padding
            width_padding = right_padding
            if top_padding != bottom_padding or left_padding != right_padding:
                height_padding = top_padding + stride
                width_padding = left_padding + stride
                self.need_crop = True
            padding = [height_padding, width_padding]
        elif padding_type == "VALID":
            height_padding = 0
            width_padding = 0
            padding = [height_padding, width_padding]
        elif padding_type == "DYNAMIC":
            padding = get_padding(filter_size, stride)
        else:
            padding = padding_type

        groups = 1 if groups is None else groups
        self._conv = Conv2d(input_channels,
                            output_channels,
                            filter_size,
                            groups=groups,
                            stride=stride,
                            padding=padding,
                            weight_attr=param_attr,
                            bias_attr=bias_attr)
Exemplo n.º 11
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.º 12
0
    def __init__(self,
                 num_channels: int,
                 num_filters: int,
                 filter_size: int,
                 stride: int = 1,
                 groups: int = 1,
                 name: str = None):
        super(ConvLayer, self).__init__()

        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)
Exemplo n.º 13
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")