Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

您好,HiFaceGAN找不到GANFeatLoss(I cannot use GANFeatLoss when trying to run HiFaceGAN by BasicSR) #690

Open
Lihua20001020 opened this issue Jul 6, 2024 · 1 comment

Comments

@Lihua20001020
Copy link

我在使用您的BasicSR框架运行其中的HiFaceGAN人脸超分辨率模型时,训练前的构建模型阶段报错“KeyError: "No object named 'GANFeatLoss' found in 'loss' registry!"“,请问这个GANFeatLoss在哪个文件中,是否已经实现了呢?

When I was using your BasicSR framework to run the HiFaceGAN face super-resolution model, I encountered an error during the model building stage before training: "KeyError: "No object named 'GANFeatLoss' found in 'loss' registry!" Could you please tell me in which file the GANFeatLoss is located, and if it has already been implemented?

@Lihua20001020
Copy link
Author

Lihua20001020 commented Jul 9, 2024

看起来这几天没人关注,我自己根据HiFaceGAN的论文实现了一下GANFeatLoss。
It seems that no one has paid attention to this question in the past few days, so I implemented the GANFeatLoss based on the HiFaceGAN paper by myself.

import torch
from basicsr.utils.registry import LOSS_REGISTRY
from basicsr.losses.gan_loss import GANLoss

@LOSS_REGISTRY.register()
class GANFeatLoss(GANLoss):

def __init__(self, gan_type, real_label_val=1.0, fake_label_val=0.0, loss_weight=10.0):
    super(GANFeatLoss, self).__init__(gan_type, real_label_val, fake_label_val, loss_weight)
    self.FloatTensor = torch.cuda.FloatTensor
    self.criterionFeat = torch.nn.L1Loss()
    self.loss_weight = loss_weight

def forward(self, pred_fake, pred_real):

    num_D = len(pred_fake)                                                                                                                                    
    GAN_Feat_loss = 0

    for i in range(num_D):  # for each discriminator                                                                
        num_intermediate_outputs = len(pred_fake[i]) - 1                                                            
        for j in range(num_intermediate_outputs):  # for each layer output                                          
            unweighted_loss = self.criterionFeat(pred_fake[i][j], pred_real[i][j].detach())                         
            GAN_Feat_loss += unweighted_loss * self.loss_weight / num_D                                             

    return GAN_Feat_loss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant