示例#1
0
文件: resnet.py 项目: tty33/PaddleGAN
    def build_conv_block(self, dim, padding_type, norm_layer, use_dropout,
                         use_bias):
        """Construct a convolutional block.

        Parameters:
            dim (int)           -- the number of channels in the conv layer.
            padding_type (str)  -- the name of padding layer: reflect | replicate | zero
            norm_layer          -- normalization layer
            use_dropout (bool)  -- if use dropout layers.
            use_bias (bool)     -- if the conv layer uses bias or not

        Returns a conv block (with a conv layer, a normalization layer, and a non-linearity layer (ReLU))
        """
        conv_block = []
        p = 0
        if padding_type == 'reflect':
            conv_block += [nn.ReflectionPad2d([1, 1, 1, 1])]
        elif padding_type == 'replicate':
            conv_block += [nn.ReplicationPad2d([1, 1, 1, 1])]
        elif padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' %
                                      padding_type)

        conv_block += [
            nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias_attr=use_bias),
            norm_layer(dim),
            nn.ReLU()
        ]
        if use_dropout:
            conv_block += [nn.Dropout(0.5)]

        p = 0
        if padding_type == 'reflect':
            conv_block += [nn.ReflectionPad2d([1, 1, 1, 1])]
        elif padding_type == 'replicate':
            conv_block += [nn.ReplicationPad2d([1, 1, 1, 1])]
        elif padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' %
                                      padding_type)
        conv_block += [
            nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias_attr=use_bias),
            norm_layer(dim)
        ]

        return nn.Sequential(*conv_block)
示例#2
0
    def __init__(self, in_c, out_c, padding_type, norm_layer, use_dropout,
                 use_bias):
        super(MobileResnetBlock, self).__init__()
        self.padding_type = padding_type
        self.use_dropout = use_dropout
        self.conv_block = nn.LayerList([])

        p = 0
        if self.padding_type == 'reflect':
            self.conv_block.extend([nn.ReflectionPad2d([1, 1, 1, 1])])
        elif self.padding_type == 'replicate':
            self.conv_block.extend([nn.ReplicationPad2d([1, 1, 1, 1])])
        elif self.padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' %
                                      self.padding_type)

        self.conv_block.extend([
            SeparableConv2D(num_channels=in_c,
                            num_filters=out_c,
                            filter_size=3,
                            padding=p,
                            stride=1),
            norm_layer(out_c),
            nn.ReLU()
        ])

        self.conv_block.extend([nn.Dropout(0.5)])

        if self.padding_type == 'reflect':
            self.conv_block.extend([nn.ReflectionPad2d([1, 1, 1, 1])])
        elif self.padding_type == 'replicate':
            self.conv_block.extend([nn.ReplicationPad2d([1, 1, 1, 1])])
        elif self.padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' %
                                      self.padding_type)

        self.conv_block.extend([
            SeparableConv2D(num_channels=out_c,
                            num_filters=in_c,
                            filter_size=3,
                            padding=p,
                            stride=1),
            norm_layer(in_c)
        ])
示例#3
0
    def test_class(self):
        paddle.disable_static()
        for place in self.places:
            input_shape = (3, 4, 5, 6)
            pad = [1, 2, 2, 1]
            value = 100
            input_data = np.random.rand(*input_shape).astype(np.float32)

            pad_reflection = nn.ReflectionPad2d(padding=pad)
            pad_replication = nn.ReplicationPad2d(padding=pad)
            pad_constant = nn.ConstantPad2d(padding=pad, value=value)
            pad_zero = nn.ZeroPad2d(padding=pad)

            data = paddle.to_tensor(input_data)

            output = pad_reflection(data)
            np_out = self._get_numpy_out(input_data,
                                         pad,
                                         "reflect",
                                         data_format="NCHW")
            self.assertTrue(np.allclose(output.numpy(), np_out))

            output = pad_replication(data)
            np_out = self._get_numpy_out(input_data,
                                         pad,
                                         "replicate",
                                         data_format="NCHW")
            self.assertTrue(np.allclose(output.numpy(), np_out))

            output = pad_constant(data)
            np_out = self._get_numpy_out(input_data,
                                         pad,
                                         "constant",
                                         value=value,
                                         data_format="NCHW")
            self.assertTrue(np.allclose(output.numpy(), np_out))

            output = pad_zero(data)
            np_out = self._get_numpy_out(input_data,
                                         pad,
                                         "constant",
                                         value=0,
                                         data_format="NCHW")
            self.assertTrue(np.allclose(output.numpy(), np_out))
示例#4
0
文件: resnet.py 项目: tty33/PaddleGAN
    def __init__(self,
                 input_nc,
                 output_nc,
                 ngf=64,
                 norm_type='instance',
                 use_dropout=False,
                 n_blocks=6,
                 padding_type='reflect'):
        """Construct a Resnet-based generator

        Args:
            input_nc (int)      -- the number of channels in input images
            output_nc (int)     -- the number of channels in output images
            ngf (int)           -- the number of filters in the last conv layer
            norm_layer          -- normalization layer
            use_dropout (bool)  -- if use dropout layers
            n_blocks (int)      -- the number of ResNet blocks
            padding_type (str)  -- the name of padding layer in conv layers: reflect | replicate | zero
        """
        assert (n_blocks >= 0)
        super(ResnetGenerator, self).__init__()

        norm_layer = build_norm_layer(norm_type)
        if type(norm_layer) == functools.partial:
            use_bias = norm_layer.func == nn.InstanceNorm
        else:
            use_bias = norm_layer == nn.InstanceNorm

        model = [
            nn.ReflectionPad2d([3, 3, 3, 3]),
            nn.Conv2d(input_nc,
                      ngf,
                      kernel_size=7,
                      padding=0,
                      bias_attr=use_bias),
            norm_layer(ngf),
            nn.ReLU()
        ]

        n_downsampling = 2
        for i in range(n_downsampling):  # add downsampling layers
            mult = 2**i
            model += [
                nn.Conv2d(ngf * mult,
                          ngf * mult * 2,
                          kernel_size=3,
                          stride=2,
                          padding=1,
                          bias_attr=use_bias),
                norm_layer(ngf * mult * 2),
                nn.ReLU()
            ]

        mult = 2**n_downsampling
        for i in range(n_blocks):  # add ResNet blocks

            model += [
                ResnetBlock(ngf * mult,
                            padding_type=padding_type,
                            norm_layer=norm_layer,
                            use_dropout=use_dropout,
                            use_bias=use_bias)
            ]

        for i in range(n_downsampling):  # add upsampling layers
            mult = 2**(n_downsampling - i)
            model += [
                nn.ConvTranspose2d(ngf * mult,
                                   int(ngf * mult / 2),
                                   kernel_size=3,
                                   stride=2,
                                   padding=1,
                                   output_padding=1,
                                   bias_attr=use_bias),
                norm_layer(int(ngf * mult / 2)),
                nn.ReLU()
            ]
        model += [nn.ReflectionPad2d([3, 3, 3, 3])]
        model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)]
        model += [nn.Tanh()]

        self.model = nn.Sequential(*model)
示例#5
0
    def __init__(self,
                 input_channel,
                 output_nc,
                 ngf=64,
                 norm_type='instance',
                 use_dropout=False,
                 n_blocks=9,
                 padding_type='reflect'):
        super(MobileResnetGenerator, self).__init__()

        norm_layer = build_norm_layer(norm_type)
        if type(norm_layer) == functools.partial:
            use_bias = norm_layer.func == InstanceNorm
        else:
            use_bias = norm_layer == InstanceNorm

        self.model = nn.LayerList([
            nn.ReflectionPad2d([3, 3, 3, 3]),
            nn.Conv2d(input_channel,
                      int(ngf),
                      kernel_size=7,
                      padding=0,
                      bias_attr=use_bias),
            norm_layer(ngf),
            nn.ReLU()
        ])

        n_downsampling = 2
        for i in range(n_downsampling):
            mult = 2**i
            self.model.extend([
                nn.Conv2d(ngf * mult,
                          ngf * mult * 2,
                          kernel_size=3,
                          stride=2,
                          padding=1,
                          bias_attr=use_bias),
                norm_layer(ngf * mult * 2),
                nn.ReLU()
            ])

        mult = 2**n_downsampling

        for i in range(n_blocks):
            self.model.extend([
                MobileResnetBlock(ngf * mult,
                                  ngf * mult,
                                  padding_type=padding_type,
                                  norm_layer=norm_layer,
                                  use_dropout=use_dropout,
                                  use_bias=use_bias)
            ])

        for i in range(n_downsampling):
            mult = 2**(n_downsampling - i)
            output_size = (i + 1) * 128
            self.model.extend([
                nn.ConvTranspose2d(ngf * mult,
                                   int(ngf * mult / 2),
                                   kernel_size=3,
                                   stride=2,
                                   padding=1,
                                   output_padding=1,
                                   bias_attr=use_bias),
                norm_layer(int(ngf * mult / 2)),
                nn.ReLU()
            ])

        self.model.extend([nn.ReflectionPad2d([3, 3, 3, 3])])
        self.model.extend(
            [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)])
        self.model.extend([nn.Tanh()])