示例#1
0
    def __init__(self, cfg):
        super(Encoder, self).__init__()
        self.cfg = cfg

        # Layer Definition
        vgg16_bn = vgg16(pretrained=False, batch_norm=True)
        self.vgg = paddle.nn.Sequential(
            *list(vgg16_bn.features.children())[:27])
        self.layer1 = paddle.nn.Sequential(
            paddle.nn.Conv2D(
                512,
                512,
                kernel_size=1,
                weight_attr=paddle.nn.initializer.KaimingNormal(),
                bias_attr=paddle.nn.initializer.Constant(value=0.0)),
            paddle.nn.BatchNorm2D(512),
            paddle.nn.ELU(),
        )
        self.layer2 = paddle.nn.Sequential(
            paddle.nn.Conv2D(
                512,
                256,
                kernel_size=3,
                weight_attr=paddle.nn.initializer.KaimingNormal(),
                bias_attr=paddle.nn.initializer.Constant(value=0.0)),
            paddle.nn.BatchNorm2D(256), paddle.nn.ELU(),
            paddle.nn.MaxPool2D(kernel_size=4))
        self.layer3 = paddle.nn.Sequential(
            paddle.nn.Conv2D(
                256,
                128,
                kernel_size=3,
                weight_attr=paddle.nn.initializer.KaimingNormal(),
                bias_attr=paddle.nn.initializer.Constant(value=0.0)),
            paddle.nn.BatchNorm2D(128), paddle.nn.ELU())
示例#2
0
    def __init__(self,
                 generator,
                 discriminator=None,
                 cycle_criterion=None,
                 idt_criterion=None,
                 gan_criterion=None,
                 l1_criterion=None,
                 l2_criterion=None,
                 pool_size=50,
                 direction='a2b',
                 lambda_a=10.,
                 lambda_b=10.,
                 is_train=True):
        """Initialize the PSGAN class.

        Parameters:
            cfg (dict)-- config of model.
        """
        super(MakeupModel, self).__init__()
        self.lambda_a = lambda_a
        self.lambda_b = lambda_b
        self.is_train = is_train
        # define networks (both Generators and discriminators)
        # The naming is different from those used in the paper.
        # Code (vs. paper): G_A (G), G_B (F), D_A (D_Y), D_B (D_X)
        self.nets['netG'] = build_generator(generator)
        init_weights(self.nets['netG'], init_type='xavier', init_gain=1.0)

        if self.is_train:  # define discriminators
            vgg = vgg16(pretrained=False)
            self.vgg = vgg.features
            cur_path = os.path.abspath(os.path.dirname(__file__))
            vgg_weight_path = get_path_from_url(VGGFACE_WEIGHT_URL, cur_path)
            param = paddle.load(vgg_weight_path)
            vgg.load_dict(param)

            self.nets['netD_A'] = build_discriminator(discriminator)
            self.nets['netD_B'] = build_discriminator(discriminator)
            init_weights(self.nets['netD_A'],
                         init_type='xavier',
                         init_gain=1.0)
            init_weights(self.nets['netD_B'],
                         init_type='xavier',
                         init_gain=1.0)

            # create image buffer to store previously generated images
            self.fake_A_pool = ImagePool(pool_size)
            self.fake_B_pool = ImagePool(pool_size)

            # define loss functions
            if gan_criterion:
                self.gan_criterion = build_criterion(gan_criterion)
            if cycle_criterion:
                self.cycle_criterion = build_criterion(cycle_criterion)
            if idt_criterion:
                self.idt_criterion = build_criterion(idt_criterion)
            if l1_criterion:
                self.l1_criterion = build_criterion(l1_criterion)
            if l2_criterion:
                self.l2_criterion = build_criterion(l2_criterion)
示例#3
0
    def __init__(self, cfg):
        """Initialize the PSGAN class.

        Parameters:
            cfg (dict)-- config of model.
        """
        super(MakeupModel, self).__init__(cfg)

        # define networks (both Generators and discriminators)
        # The naming is different from those used in the paper.
        # Code (vs. paper): G_A (G), G_B (F), D_A (D_Y), D_B (D_X)
        self.nets['netG'] = build_generator(cfg.model.generator)
        init_weights(self.nets['netG'], init_type='xavier', init_gain=1.0)

        if self.is_train:  # define discriminators
            vgg = vgg16(pretrained=False)
            self.vgg = vgg.features
            cur_path = os.path.abspath(os.path.dirname(__file__))
            vgg_weight_path = get_path_from_url(VGGFACE_WEIGHT_URL, cur_path)
            param = paddle.load(vgg_weight_path)
            vgg.load_dict(param)

            self.nets['netD_A'] = build_discriminator(cfg.model.discriminator)
            self.nets['netD_B'] = build_discriminator(cfg.model.discriminator)
            init_weights(self.nets['netD_A'],
                         init_type='xavier',
                         init_gain=1.0)
            init_weights(self.nets['netD_B'],
                         init_type='xavier',
                         init_gain=1.0)

            self.fake_A_pool = ImagePool(
                cfg.dataset.train.pool_size
            )  # create image buffer to store previously generated images
            self.fake_B_pool = ImagePool(
                cfg.dataset.train.pool_size
            )  # create image buffer to store previously generated images
            # define loss functions
            self.criterionGAN = GANLoss(
                cfg.model.gan_mode)  #.to(self.device)  # define GAN loss.
            self.criterionCycle = paddle.nn.L1Loss()
            self.criterionIdt = paddle.nn.L1Loss()
            self.criterionL1 = paddle.nn.L1Loss()
            self.criterionL2 = paddle.nn.MSELoss()

            self.build_lr_scheduler()
            self.optimizers['optimizer_G'] = build_optimizer(
                cfg.optimizer,
                self.lr_scheduler,
                parameter_list=self.nets['netG'].parameters())
            self.optimizers['optimizer_DA'] = build_optimizer(
                cfg.optimizer,
                self.lr_scheduler,
                parameter_list=self.nets['netD_A'].parameters())
            self.optimizers['optimizer_DB'] = build_optimizer(
                cfg.optimizer,
                self.lr_scheduler,
                parameter_list=self.nets['netD_B'].parameters())
示例#4
0
文件: dsifn.py 项目: geoyee/PdRSCD
 def __init__(self, in_channels=3):
     super(Vgg16Base, self).__init__()
     features = vgg16(pretrained=True).sublayers()[0].sublayers()
     if in_channels != 3:
         features[0] = nn.Conv2D(in_channels,
                                 64,
                                 kernel_size=[3, 3],
                                 padding=1,
                                 data_format='NCHW')
     self.features = nn.LayerList(features)
     self.features.eval()
示例#5
0
    def __init__(self):
        super(StyleTransferModel, self).__init__()
        # pretrained设置为true,会自动下载imagenet上的预训练权重并加载
        vgg = vgg16(pretrained=True)
        self.base_model = vgg.features

        for p in self.base_model.parameters():
            p.stop_gradient = True
        self.layers = {
            '0': 'conv1_1',
            '5': 'conv2_1',
            '10': 'conv3_1',
            '17': 'conv4_1',
            '19': 'conv4_2',  ## content representation
            '24': 'conv5_1'
        }