Esempio n. 1
0
    def __init__(self,
                 block,
                 layers,
                 num_classes=65,
                 groups=1,
                 width_per_group=64):
        super(MyCNN, self).__init__()
        norm_layer = dg.BatchNorm
        self._norm_layer = norm_layer

        self.inplanes = 64
        self.dilation = 1

        self.groups = groups
        self.base_width = width_per_group
        self.conv1 = dg.Conv2D(1,
                               self.inplanes,
                               filter_size=3,
                               stride=1,
                               padding=1)
        self.bn1 = norm_layer(self.inplanes,
                              param_attr=fluid.ParamAttr(
                                  initializer=fluid.initializer.Constant(1.0)))
        self.maxpool = dg.Pool2D(pool_size=3, pool_stride=2, pool_padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        # self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        # self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = dg.Pool2D(5, pool_type="avg")
        self.fc = Linear(128 * block.expansion, num_classes)
Esempio n. 2
0
    def __init__(self,
                 pool_size=-1,
                 pool_type='max',
                 pool_stride=1,
                 pool_padding=0,
                 global_pooling=False,
                 use_cudnn=True,
                 ceil_mode=False,
                 exclusive=True,
                 data_format='NCT'):
        super(Pool1D, self).__init__()
        self.pool_size = pool_size
        self.pool_type = pool_type
        self.pool_stride = pool_stride
        self.pool_padding = pool_padding
        self.global_pooling = global_pooling
        self.use_cudnn = use_cudnn
        self.ceil_mode = ceil_mode
        self.exclusive = exclusive
        self.data_format = data_format

        self.pool2d = dg.Pool2D(
            [1, pool_size],
            pool_type=pool_type,
            pool_stride=[1, pool_stride],
            pool_padding=[0, pool_padding],
            global_pooling=global_pooling,
            use_cudnn=use_cudnn,
            ceil_mode=ceil_mode,
            exclusive=exclusive)
Esempio n. 3
0
 def __init__(self, in_channels=384):
     super().__init__()
     self.pool = dg.Pool2D(pool_size=3, pool_stride=2, pool_type='max')
     self.conv_3x3 = ConvBN(in_channels, 384, 3, 2, 0)
     self.conv_3x3_2_reduce = ConvBN(in_channels, 192, 1, 1, 0)
     self.conv_3x3_2 = ConvBN(192, 224, 3, 1, 1)
     self.conv_3x3_3 = ConvBN(224, 256, 3, 2, 0)
Esempio n. 4
0
def maxpool(kernel_size, stride, padding):
    return dg.Pool2D(
        pool_size=kernel_size,
        pool_type='max',
        pool_stride=stride,
        pool_padding=padding,
    )
Esempio n. 5
0
    def __init__(self, in_channels=3, out_channels=64, norm="AN"):

        super().__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.conv1 = conv7x7(in_channels, out_channels, stride=2, bias=False)
        self.conv1_norm = get_norm(norm, out_channels)
        self.pool = dg.Pool2D(pool_size=3, pool_type='max', 
                              pool_stride=2, pool_padding=1)
Esempio n. 6
0
    def __init__(self, ):
        super().__init__()
        self.conv1_3x3_s2 = ConvBN(3, 32, 3, 2, 0)
        self.conv2_3x3_s1 = ConvBN(32, 32, 3, 1, 0)
        self.conv3_3x3_s1 = ConvBN(32, 64, 3, 1, 1)

        self.pool1 = dg.Pool2D(pool_size=3, pool_stride=2, pool_type='max')
        self.stem1_3x3_s2 = ConvBN(64, 96, 3, 2, 0)

        self.stem2_3x3_reduce = ConvBN(96 + 64, 64, 1, 1, 0)
        self.stem2_3x3 = ConvBN(64, 96, 3, 1, 0)
        self.stem2_1x7_reduce = ConvBN(64 + 96, 64, 1, 1, 0)
        self.stem2_1x7 = ConvBN(64, 64, (7, 1), 1, (3, 0))
        self.stem2_7x1 = ConvBN(64, 64, (1, 7), 1, (0, 3))
        self.stem2_3x3_2 = ConvBN(64, 96, 3, 1, 0)

        self.stem3_3x3_s2 = ConvBN(192, 192, 3, 2, 0)
        self.pool2 = dg.Pool2D(pool_size=3, pool_stride=2, pool_type='max')
Esempio n. 7
0
    def __init__(self, in_channels=1024):
        super().__init__()
        self.pool = dg.Pool2D(pool_size=3, pool_stride=2, pool_type='max')
        self.conv_3x3_reduce = ConvBN(in_channels, 192, 1, 1, 0)
        self.conv_3x3 = ConvBN(192, 192, 3, 2, 0)

        self.conv_1x7_reduce = ConvBN(in_channels, 256, 1, 1, 0)
        self.conv_1x7 = ConvBN(256, 256, (1, 7), 1, (0, 3))
        self.conv_7x1 = ConvBN(256, 320, (7, 1), 1, (3, 0))
        self.conv_3x3_2 = ConvBN(320, 320, 3, 2, 0)
Esempio n. 8
0
    def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
                 groups=1, width_per_group=64, replace_stride_with_dilation=None,
                 norm_layer=None):
        super(ResNet, self).__init__()
        if norm_layer is None:
            norm_layer = dg.BatchNorm
        self._norm_layer = norm_layer

        self.inplanes = 64
        self.dilation = 1
        if replace_stride_with_dilation is None:
            # each element in the tuple indicates if we should replace
            # the 2x2 stride with a dilated convolution instead
            replace_stride_with_dilation = [False, False, False]
        if len(replace_stride_with_dilation) != 3:
            raise ValueError("replace_stride_with_dilation should be None "
                             "or a 3-element tuple, got {}".format(replace_stride_with_dilation))
        
        self.groups = groups
        self.base_width = width_per_group
        self.conv1 = dg.Conv2D(3, self.inplanes, filter_size=7, stride=2, 
                               padding=3, bias_attr=False)
        self.bn1 = norm_layer(self.inplanes)
        self.relu = ReLU()
        self.maxpool = dg.Pool2D(pool_size=3, pool_type='max', pool_stride=2, pool_padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
                                       dilate=replace_stride_with_dilation[0])
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
                                       dilate=replace_stride_with_dilation[1])
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
                                       dilate=replace_stride_with_dilation[2])
        self.avgpool = lambda x: L.adaptive_pool2d(x, (1, 1), pool_type='avg')
        self.fc = dg.Linear(512 * block.expansion, num_classes)

        for m in self.sublayers():
            if isinstance(m, dg.Conv2D):
                m.param_attr = F.ParamAttr(initializer=F.initializer.MSRAInitializer())
            elif isinstance(m, (dg.BatchNorm, dg.GroupNorm)):
                m.param_attr = F.ParamAttr(initializer=F.initializer.ConstantInitializer(value=0.0))
                m.bias_attr = F.ParamAttr(initializer=F.initializer.ConstantInitializer(value=0.0))
        
        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros, and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        if zero_init_residual:
            for m in self.sublayers():
                if isinstance(m, Bottleneck):
                    m.bn3.param_attr = F.ParamAttr(initializer=F.initializer.ConstantInitializer(value=0.0))
                elif isinstance(m, BasicBlock):
                    m.bn2.param_attr = F.ParamAttr(initializer=F.initializer.ConstantInitializer(value=0.0))
Esempio n. 9
0
 def __init__(self,
              in_features,
              out_features,
              kernel_size=3,
              padding=1,
              groups=1):
     super(DownBlock2d, self).__init__()
     self.conv = dygraph.Conv2D(num_channels=in_features,
                                num_filters=out_features,
                                filter_size=kernel_size,
                                padding=padding,
                                groups=groups)
     self.norm = dygraph.BatchNorm(num_channels=out_features, momentum=0.1)
     self.pool = dygraph.Pool2D(pool_size=(2, 2),
                                pool_type='avg',
                                pool_stride=2)
Esempio n. 10
0
    def __init__(self, in_channels=1024):
        super().__init__()
        self.pool = dg.Pool2D(pool_size=3, pool_padding=1, pool_type='avg')
        self.conv_1x1 = ConvBN(in_channels, 128, 1, 1, 0)

        self.conv_1x1_2 = ConvBN(in_channels, 384, 1, 1, 0)

        self.conv_1x7_reduce = ConvBN(in_channels, 192, 1, 1, 0)
        self.conv_1x7 = ConvBN(192, 224, (1, 7), 1, (0, 3))
        self.conv_7x1 = ConvBN(224, 256, (7, 1), 1, (3, 0))

        self.conv_7x1_2_reduce = ConvBN(in_channels, 192, 1, 1, 0)
        self.conv_1x7_2 = ConvBN(192, 192, (1, 7), 1, (0, 3))
        self.conv_7x1_2 = ConvBN(192, 224, (7, 1), 1, (3, 0))
        self.conv_1x7_3 = ConvBN(224, 224, (1, 7), 1, (0, 3))
        self.conv_7x1_3 = ConvBN(224, 256, (7, 1), 1, (3, 0))
Esempio n. 11
0
    def __init__(
        self,
        in_channels,
    ):
        super().__init__()
        self.pool = dg.Pool2D(pool_size=3, pool_padding=1, pool_type='avg')
        self.conv_1x1 = ConvBN(in_channels, 96, 1, 1, 0)

        self.conv_1x1_2 = ConvBN(in_channels, 96, 1, 1, 0)

        self.conv_3x3_reduce = ConvBN(in_channels, 64, 1, 1, 0)
        self.conv_3x3 = ConvBN(64, 96, 3, 1, 1)

        self.conv_3x3_2_reduce = ConvBN(in_channels, 64, 1, 1, 0)
        self.conv_3x3_2 = ConvBN(64, 96, 3, 1, 1)
        self.conv_3x3_3 = ConvBN(96, 96, 3, 1, 1)
Esempio n. 12
0
    def __init__(self, in_channels=1536):
        super().__init__()
        self.pool = dg.Pool2D(pool_size=3, pool_padding=1, pool_type='avg')
        self.conv_1x1 = ConvBN(in_channels, 256, 1, 1, 0)

        self.conv_1x1_2 = ConvBN(in_channels, 256, 1, 1, 0)

        self.conv_1x1_3 = ConvBN(in_channels, 384, 1, 1, 0)
        self.conv_1x3 = ConvBN(384, 256, (1, 3), 1, (0, 1))
        self.conv_3x1 = ConvBN(384, 256, (3, 1), 1, (1, 0))

        self.conv_1x1_4 = ConvBN(in_channels, 384, 1, 1, 0)
        self.conv_1x3_2 = ConvBN(384, 448, (1, 3), 1, (0, 1))
        self.conv_3x1_2 = ConvBN(448, 512, (3, 1), 1, (1, 0))
        self.conv_1x3_3 = ConvBN(512, 256, (1, 3), 1, (0, 1))
        self.conv_3x1_3 = ConvBN(512, 256, (3, 1), 1, (1, 0))
Esempio n. 13
0
def convpool(in_channel,
             out_channel,
             padding=None,
             pooling=2,
             kernel=3,
             act='relu'):
    if padding == None:
        padding = int((kernel - 1) / 2)
    layers = [
        dygraph.Conv2D(in_channel,
                       out_channel,
                       kernel,
                       padding=padding,
                       act=act),
        dygraph.BatchNorm(out_channel)
    ]
    if pooling > 1:
        layers.append(dygraph.Pool2D(pooling, pool_stride=pooling))

    return fluid.dygraph.Sequential(*layers)
Esempio n. 14
0
    def __init__(self, in_dim, activation=layers.relu):
        super().__init__()
        self.chanel_in = in_dim
        self.activation = activation

        self.theta = SpectralNorm(
            dg.Conv2D(in_dim, in_dim // 8, 1, bias_attr=False))
        self.phi = SpectralNorm(
            dg.Conv2D(in_dim, in_dim // 8, 1, bias_attr=False))
        self.pool = dg.Pool2D(2, 'max', 2)
        self.g = SpectralNorm(
            dg.Conv2D(in_dim, in_dim // 2, 1, bias_attr=False))
        self.o_conv = SpectralNorm(
            dg.Conv2D(in_dim // 2, in_dim, 1, bias_attr=False))
        self.gamma = self.create_parameter([
            1,
        ],
                                           default_initializer=Constant(0.0))

        self.softmax = SoftMax(axis=-1)
Esempio n. 15
0
    def __init__(self):
        super().__init__()
        self.stem = Stem()

        inception_a = []
        for i in range(4):
            inception_a.append(InceptionA(384))
        self.inception_a = dg.LayerList(inception_a)
        self.reduction_a = ReductionA(384)

        inception_b = []
        for i in range(7):
            inception_b.append(InceptionB(1024))
        self.inception_b = dg.LayerList(inception_b)
        self.reduction_b = ReductionB(1024)

        inception_c = []
        for i in range(3):
            inception_c.append(InceptionC(1536))
        self.inception_c = dg.LayerList(inception_c)

        self.pool = dg.Pool2D(pool_type='avg', global_pooling=True)
Esempio n. 16
0
 def __init__(self):
     super().__init__()
     self.emb = D.Embedding([30002, 128], padding_idx=0)
     self.cnn = D.Conv2D(128, 128, (1, 3), padding=(0, 1), act='relu')
     self.pool = D.Pool2D((1, 3), pool_padding=(0, 1))
     self.fc = D.Linear(128, 2)
Esempio n. 17
0
    def __init__(
        self,
        resolution=1024,
        fmap_base=8192,
        num_channels=3,
        structure='fixed',  # 'fixed' = no progressive growing, 'linear' = human-readable, 'recursive' = efficient, only support 'fixed' mode now.
        fmap_max=512,
        fmap_decay=1.0,
        # f=[1, 2, 1]       # (Huge overload, if you dont have enough resouces, please pass it as `f = None`)Low-pass filter to apply when resampling activations. None = no filtering.
        f=None  # (Huge overload, if you dont have enough resouces, please pass it as `f = None`)Low-pass filter to apply when resampling activations. None = no filtering.
    ):
        """

            if H or W >= 128, we use avgpooling2d to do feature map shrinkage.
            else: we use ordinary conv2d.
        """
        super().__init__()
        self.resolution_log2 = int(np.log2(resolution))
        assert resolution == 2**self.resolution_log2 and resolution >= 4
        self.nf = lambda stage: min(
            int(fmap_base / (2.0**(stage * fmap_decay))), fmap_max)
        # fromrgb: fixed mode
        self.fromrgb = dygraph.Conv2D(num_channels,
                                      self.nf(self.resolution_log2 - 1), 1)
        self.structure = structure

        # blur2d
        self.blur2d = Blur2d(f)

        # down_sample
        self.down1 = dygraph.Pool2D(pool_size=2,
                                    pool_stride=2,
                                    pool_type='avg')
        self.down21 = dygraph.Conv2D(self.nf(self.resolution_log2 - 5),
                                     self.nf(self.resolution_log2 - 5),
                                     2,
                                     stride=2)
        self.down22 = dygraph.Conv2D(self.nf(self.resolution_log2 - 6),
                                     self.nf(self.resolution_log2 - 6),
                                     2,
                                     stride=2)
        self.down23 = dygraph.Conv2D(self.nf(self.resolution_log2 - 7),
                                     self.nf(self.resolution_log2 - 7),
                                     2,
                                     stride=2)
        self.down24 = dygraph.Conv2D(self.nf(self.resolution_log2 - 8),
                                     self.nf(self.resolution_log2 - 8),
                                     2,
                                     stride=2)

        # conv1: padding=same
        self.conv1 = Conv2d(self.nf(self.resolution_log2 - 1),
                            self.nf(self.resolution_log2 - 1), 3)
        self.conv2 = Conv2d(self.nf(self.resolution_log2 - 1),
                            self.nf(self.resolution_log2 - 2), 3)
        self.conv3 = Conv2d(self.nf(self.resolution_log2 - 2),
                            self.nf(self.resolution_log2 - 3), 3)
        self.conv4 = Conv2d(self.nf(self.resolution_log2 - 3),
                            self.nf(self.resolution_log2 - 4), 3)
        self.conv5 = Conv2d(self.nf(self.resolution_log2 - 4),
                            self.nf(self.resolution_log2 - 5), 3)
        self.conv6 = Conv2d(self.nf(self.resolution_log2 - 5),
                            self.nf(self.resolution_log2 - 6), 3)
        self.conv7 = Conv2d(self.nf(self.resolution_log2 - 6),
                            self.nf(self.resolution_log2 - 7), 3)
        self.conv8 = Conv2d(self.nf(self.resolution_log2 - 7),
                            self.nf(self.resolution_log2 - 8), 3)

        # calculate point:
        self.conv_last = Conv2d(self.nf(self.resolution_log2 - 8), self.nf(1),
                                3)
        self.dense0 = dygraph.Linear(fmap_base, self.nf(0))
        self.dense1 = dygraph.Linear(self.nf(0), 1)