def upsample(self, x, out_c, name=None):
        fan_in = x.shape[1] * 3 * 3
        stdv = 1. / math.sqrt(fan_in)
        if self.dcn_upsample:
            conv = DeformConv(x,
                              out_c,
                              3,
                              initializer=Uniform(-stdv, stdv),
                              bias_attr=True,
                              name=name + '.0')
        else:
            conv = fluid.layers.conv2d(
                x,
                out_c,
                3,
                padding=1,
                param_attr=ParamAttr(initializer=Uniform(-stdv, stdv)),
                bias_attr=ParamAttr(learning_rate=2., regularizer=L2Decay(0.)))

        norm_name = name + '.1'
        pattr = ParamAttr(name=norm_name + '.weight', initializer=Constant(1.))
        battr = ParamAttr(name=norm_name + '.bias', initializer=Constant(0.))
        bn = fluid.layers.batch_norm(
            input=conv,
            act='relu',
            param_attr=pattr,
            bias_attr=battr,
            name=norm_name + '.output.1',
            moving_mean_name=norm_name + '.running_mean',
            moving_variance_name=norm_name + '.running_var')
        up = fluid.layers.resize_bilinear(bn,
                                          scale=2,
                                          name=name + '.2.upsample')
        return up
Esempio n. 2
0
 def _get_default_param_initializer():
     filter_elem_num = filter_size[0] * filter_size[
         1] * self._num_channels
     negative_slope = math.sqrt(5)  
     gain = math.sqrt(2.0 / (1 + negative_slope ** 2))
     bound = gain * math.sqrt(3.0 / filter_elem_num)
     return Uniform(-bound, bound, 0)
Esempio n. 3
0
def get_ctcn_conv_initializer(x, filter_size):
    c_in = x.shape[1]
    if isinstance(filter_size, int):
        fan_in = c_in * filter_size * filter_size
    else:
        fan_in = c_in * filter_size[0] * filter_size[1]
    std = np.sqrt(1.0 / fan_in)
    return Uniform(0. - std, std)
Esempio n. 4
0
    def net(self, input, class_dim=1000):
        """
        构建网络结构
        :param input: 输入图片
        :param class_dim: 分类类别
        :return:
        """
        layers = self.layers
        supported_layers = [50, 101, 152]
        assert layers in supported_layers, \
            "supported layers are {} but input layer is {}".format(supported_layers, layers)

        if layers == 50:
            depth = [3, 4, 6, 3]
        elif layers == 101:
            depth = [3, 4, 23, 3]
        elif layers == 152:
            depth = [3, 8, 36, 3]
        num_filters = [64, 128, 256, 512]

        conv = self.conv_bn_layer(input=input,
                                  num_filters=64,
                                  filter_size=7,
                                  stride=2,
                                  act='relu',
                                  name="conv1")
        conv = fluid.layers.pool2d(input=conv,
                                   pool_size=3,
                                   pool_stride=2,
                                   pool_padding=1,
                                   pool_type='max')

        for block in range(len(depth)):
            for i in range(depth[block]):
                if 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)
                conv = self.bottleneck_block(
                    input=conv,
                    num_filters=num_filters[block],
                    stride=2 if i == 0 and block != 0 else 1,
                    name=conv_name)

        pool = fluid.layers.pool2d(input=conv,
                                   pool_size=7,
                                   pool_type='avg',
                                   global_pooling=True)
        stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)
        out = fluid.layers.fc(input=pool,
                              size=class_dim,
                              act='softmax',
                              param_attr=fluid.param_attr.ParamAttr(
                                  initializer=Uniform(-stdv, stdv)))
        return out
    def __init__(self,
                 n_class=1000,
                 chn=96,
                 blocks_with_attention="B2",
                 resolution=256):
        super().__init__()

        def DBlock(in_channel,
                   out_channel,
                   downsample=True,
                   use_attention=False,
                   skip_proj=None):
            return ResBlock(in_channel,
                            out_channel,
                            conditional=False,
                            upsample=False,
                            downsample=downsample,
                            use_attention=use_attention,
                            skip_proj=skip_proj)

        self.chn = chn
        self.colors = 3
        self.resolution = resolution
        self.blocks_with_attention = set(blocks_with_attention.split(","))
        self.blocks_with_attention.discard('')

        dblock = []
        in_channels, out_channels = self.get_in_out_channels()

        self.sa_ids = [
            int(s.split('B')[-1]) for s in self.blocks_with_attention
        ]

        for i, (nc_in,
                nc_out) in enumerate(zip(in_channels[:-1], out_channels[:-1])):
            dblock.append(
                DBlock(nc_in,
                       nc_out,
                       downsample=True,
                       use_attention=(i + 1) in self.sa_ids,
                       skip_proj=nc_in == nc_out))
        dblock.append(
            DBlock(in_channels[-1],
                   out_channels[-1],
                   downsample=False,
                   use_attention=len(out_channels) in self.sa_ids,
                   skip_proj=in_channels[-1] == out_channels[-1]))
        self.blocks = dg.LayerList(dblock)

        self.final_fc = SpectralNorm(dg.Linear(16 * chn, 1))

        self.embed_y = dg.Embedding(size=[n_class, 16 * chn],
                                    is_sparse=False,
                                    param_attr=Uniform(-0.1, 0.1))
        self.embed_y = SpectralNorm(self.embed_y)
Esempio n. 6
0
def kaiming_init(input, filter_size):
    fan_in = input.shape[1]
    std = (1.0 / (fan_in * filter_size * filter_size))**0.5
    return Uniform(0. - std, std)
Esempio n. 7
0
 def _get_default_bias_initializer():
     fan_in = kernel_size * kernel_size * in_channels
     bound = 1.0 / math.sqrt(fan_in)
     return Uniform(-bound, bound, 0)
Esempio n. 8
0
 def _get_default_param_initializer():
     filter_elem_num = input_dim
     negative_slope = math.sqrt(5)  
     gain = math.sqrt(2.0 / (1 + negative_slope ** 2))
     bound = gain * math.sqrt(3.0 / filter_elem_num)
     return Uniform(-bound, bound, 0)
Esempio n. 9
0
 def _get_default_bias_initializer():
     fan_in = filter_size[0] * filter_size[
         1] * self._num_channels
     bound = 1.0 / math.sqrt(fan_in) 
     return Uniform(-bound, bound, 0)
Esempio n. 10
0
 def _get_default_bias_initializer():
     fan_in = input_dim
     bound = 1.0 / math.sqrt(fan_in) 
     return Uniform(-bound, bound, 0)