Ejemplo n.º 1
0
    def _init_loss(self, cfg):
        r"""Initialize loss terms.

        Args:
            cfg (obj): Global configuration.
        """
        self.criteria['GAN'] = GANLoss(cfg.trainer.gan_mode)
        self.weights['GAN'] = cfg.trainer.loss_weight.gan
        # Setup the perceptual loss. Note that perceptual loss can run in
        # fp16 mode for additional speed. We find that running on fp16 mode
        # leads to improve training speed while maintaining the same accuracy.
        if hasattr(cfg.trainer, 'perceptual_loss'):
            self.criteria['Perceptual'] = \
                PerceptualLoss(
                    cfg=cfg,
                    network=cfg.trainer.perceptual_loss.mode,
                    layers=cfg.trainer.perceptual_loss.layers,
                    weights=cfg.trainer.perceptual_loss.weights)
            self.weights['Perceptual'] = cfg.trainer.loss_weight.perceptual
        # Setup the feature matching loss.
        self.criteria['FeatureMatching'] = FeatureMatchingLoss()
        self.weights['FeatureMatching'] = \
            cfg.trainer.loss_weight.feature_matching
        # Setup the Gaussian KL divergence loss.
        self.criteria['GaussianKL'] = GaussianKLLoss()
        self.weights['GaussianKL'] = cfg.trainer.loss_weight.kl
Ejemplo n.º 2
0
    def _init_loss(self, cfg):
        r"""Initialize loss terms. In MUNIT, we have several loss terms
        including the GAN loss, the image reconstruction loss, the content
        reconstruction loss, the style reconstruction loss, the cycle
        reconstruction loss. We also have an optional perceptual loss. A user
        can choose to have gradient penalty or consistency regularization too.

        Args:
            cfg (obj): Global configuration.
        """
        self.criteria['gan'] = GANLoss(cfg.trainer.gan_mode)
        self.criteria['kl'] = GaussianKLLoss()
        self.criteria['image_recon'] = torch.nn.L1Loss()
        self.criteria['content_recon'] = torch.nn.L1Loss()
        self.criteria['style_recon'] = torch.nn.L1Loss()

        if getattr(cfg.trainer.loss_weight, 'perceptual', 0) > 0:
            self.criteria['perceptual'] = \
                PerceptualLoss(cfg=cfg,
                               network=cfg.trainer.perceptual_mode,
                               layers=cfg.trainer.perceptual_layers)

        for loss_name, loss_weight in cfg.trainer.loss_weight.__dict__.items():
            if loss_weight > 0:
                self.weights[loss_name] = loss_weight
Ejemplo n.º 3
0
    def _init_loss(self, cfg):
        r"""Initialize loss terms. In FUNIT, we have several loss terms
        including the GAN loss, the image reconstruction loss, the feature
        matching loss, and the gradient penalty loss.

        Args:
            cfg (obj): Global configuration.
        """
        self.criteria['gan'] = GANLoss(cfg.trainer.gan_mode)
        self.criteria['image_recon'] = nn.L1Loss()
        self.criteria['feature_matching'] = nn.L1Loss()

        for loss_name, loss_weight in cfg.trainer.loss_weight.__dict__.items():
            if loss_weight > 0:
                self.weights[loss_name] = loss_weight
Ejemplo n.º 4
0
    def _init_loss(self, cfg):
        r"""Initialize training loss terms. In pix2pixHD, there are three
        loss terms: GAN loss, feature matching loss, and perceptual loss.

        Args:
            cfg (obj): Global configuration.
        """
        self.criteria = dict()
        self.weights = dict()
        trainer_cfg = cfg.trainer
        loss_weight = cfg.trainer.loss_weight
        # GAN loss and feature matching loss.
        self._assign_criteria('GAN', GANLoss(trainer_cfg.gan_mode),
                              loss_weight.gan)
        self._assign_criteria('FeatureMatching', FeatureMatchingLoss(),
                              loss_weight.feature_matching)
        self._assign_criteria(
            'Perceptual',
            PerceptualLoss(cfg=cfg,
                           network=cfg.trainer.perceptual_loss.mode,
                           layers=cfg.trainer.perceptual_loss.layers,
                           weights=cfg.trainer.perceptual_loss.weights),
            loss_weight.perceptual)
Ejemplo n.º 5
0
    def _init_loss(self, cfg):
        r"""Initialize training loss terms. In vid2vid, in addition to
        the GAN loss, feature matching loss, and perceptual loss used in
        pix2pixHD, we also add temporal GAN (and feature matching) loss,
        and flow warping loss. Optionally, we can also add an additional
        face discriminator for the face region.

        Args:
            cfg (obj): Global configuration.
        """
        self.criteria = dict()
        self.weights = dict()
        trainer_cfg = cfg.trainer
        loss_weight = cfg.trainer.loss_weight

        # GAN loss and feature matching loss.
        self._assign_criteria('GAN', GANLoss(trainer_cfg.gan_mode),
                              loss_weight.gan)
        self._assign_criteria('FeatureMatching', FeatureMatchingLoss(),
                              loss_weight.feature_matching)

        # Perceptual loss.
        perceptual_loss = cfg.trainer.perceptual_loss
        self._assign_criteria(
            'Perceptual',
            PerceptualLoss(cfg=cfg,
                           network=perceptual_loss.mode,
                           layers=perceptual_loss.layers,
                           weights=perceptual_loss.weights,
                           num_scales=getattr(perceptual_loss, 'num_scales',
                                              1)), loss_weight.perceptual)

        # L1 Loss.
        if getattr(loss_weight, 'L1', 0) > 0:
            self._assign_criteria('L1', torch.nn.L1Loss(), loss_weight.L1)

        # Whether to add an additional discriminator for specific regions.
        self.add_dis_cfg = getattr(self.cfg.dis, 'additional_discriminators',
                                   None)
        if self.add_dis_cfg is not None:
            for name in self.add_dis_cfg:
                add_dis_cfg = self.add_dis_cfg[name]
                self.weights['GAN_' + name] = add_dis_cfg.loss_weight
                self.weights['FeatureMatching_' + name] = \
                    loss_weight.feature_matching

        # Temporal GAN loss.
        self.num_temporal_scales = get_nested_attr(self.cfg.dis,
                                                   'temporal.num_scales', 0)
        for s in range(self.num_temporal_scales):
            self.weights['GAN_T%d' % s] = loss_weight.temporal_gan
            self.weights['FeatureMatching_T%d' % s] = \
                loss_weight.feature_matching

        # Flow loss. It consists of three parts: L1 loss compared to GT,
        # warping loss when used to warp images, and loss on the occlusion mask.
        self.use_flow = hasattr(cfg.gen, 'flow')
        if self.use_flow:
            self.criteria['Flow'] = FlowLoss(cfg)
            self.weights['Flow'] = self.weights['Flow_L1'] = \
                self.weights['Flow_Warp'] = \
                self.weights['Flow_Mask'] = loss_weight.flow

        # Other custom losses.
        self._define_custom_losses()