Example #1
0
    def compute_d_loss(self):
        d_fake = self.netD(self.fake.detach())
        d_real = self.netD(self.ground_truth)

        var_dic = {}
        var_dic["GP"] = gp = gradPenalty(self.netD, self.ground_truth, self.fake, input=None,
                                         use_gpu=self.use_gpu)
        var_dic["WD"] = w_distance = (d_real.mean() - d_fake.mean()).detach()
        var_dic["LOSS_D"] = loss_d = d_fake.mean() - d_real.mean() + gp

        return loss_d, var_dic
Example #2
0
 def compute_d_loss(self):
     d_fake = self.netD(self.fake.detach()).mean()
     d_real = self.netD(self.ground_truth).mean()
     dic = {}
     dic["GP"] = gp = gradPenalty(self.netD,
                                  self.ground_truth,
                                  self.fake,
                                  input=None,
                                  use_gpu=self.use_gpu)
     dic["WD"] = wd = d_real - d_fake
     dic["LOSS_D"] = d_loss = -wd + gp
     return d_loss, dic
Example #3
0
    def compute_valid(self):
        var_dic = {}
        fake = self.netG(self.input).detach()
        d_fake = self.netD(self.fake, self.input).detach()
        d_real = self.netD(self.ground_truth, self.input).detach()

        var_dic["G"] = loss_g = (-d_fake.mean()).detach()
        var_dic["GP"] = gp = (gradPenalty(self.netD,
                                          self.ground_truth,
                                          self.fake,
                                          input=self.input,
                                          use_gpu=self.use_gpu)).detach()
        var_dic["D"] = loss_d = (d_fake.mean() - d_real.mean() + gp).detach()
        var_dic["WD"] = w_distance = (d_real.mean() - d_fake.mean()).detach()
        var_dic["PSNR"] = psnr = getPsnr(fake, input, self.use_gpu)
        return var_dic
Example #4
0
    def compute_valid(self):
        var_dic = {}
        fake = self.netG(self.input).detach()
        d_fake = self.netD(self.fake).mean().detach()
        d_real = self.netD(self.ground_truth).mean().detach()

        var_dic["LOSS_G"] = (-d_fake.mean()).detach()
        var_dic["GP"] = gp = (gradPenalty(self.netD,
                                          self.ground_truth,
                                          self.fake,
                                          input=None,
                                          use_gpu=self.use_gpu)).detach()
        var_dic["LOSS_D"] = (d_fake.mean() - d_real.mean() + gp).detach()
        var_dic["WD"] = (d_real.mean() - d_fake.mean()).detach()
        var_dic["PSNR"] = getPsnr(fake, self.ground_truth, self.use_gpu)
        return var_dic
Example #5
0
    def valid(self):
        avg_loss_g = 0
        avg_loss_d = 0
        avg_w_distance = 0
        # netG = netG._d
        self.netG.eval()
        self.netD.eval()
        input = Variable()
        real = Variable()
        if self.use_gpus:
            input = input.cuda()
            real = real.cuda()
        len_test_data = len(self.cv_loader)
        for iteration, batch in enumerate(self.cv_loader, 1):
            input.data.resize_(batch[0].size()).copy_(batch[0])  # input data
            real.data.resize_(batch[1].size()).copy_(batch[1])  # real data
            ## 计算G的LOSS
            fake = self.netG(input).detach()
            d_fake = self.netD(fake, input).detach()
            loss_g = -d_fake.mean()

            # 计算D的LOSS
            d_real = self.netD(real, input).detach()
            gp = gradPenalty(self.netD, real, fake, input=input)
            loss_d = d_fake.mean() - d_real.mean() + gp
            w_distance = d_real.mean() - d_fake.mean()
            # 求和
            avg_w_distance += w_distance.detach()
            avg_loss_d += loss_d.detach()
            avg_loss_g += loss_g.detach()
        avg_w_distance = avg_w_distance / len_test_data
        avg_loss_d = avg_loss_d / len_test_data
        avg_loss_g = avg_loss_g / len_test_data
        self.valid_losses["WD"].append(avg_w_distance)
        self.valid_losses["D"].append(avg_loss_d)
        self.valid_losses["G"].append(avg_loss_g)
        # print("===> CV_Loss_D: {:.4f} CV_WD:{:.4f} CV_Loss_G: {:.4f}".format(avg_loss_d, avg_w_distance, avg_loss_g))
        self._watchLoss(["D", "G", "WD"],
                        loss_dic=self.valid_losses,
                        type="Valid")
        self._watchImg(input, fake, real, type="Valid")
        self.netG.train()
        self.netD.train()
        self.netG.zero_grad()
        self.netD.zero_grad()

        return avg_w_distance
Example #6
0
    def compute_d_loss(self):
        d_fake = self.netD(self.fake.detach(), self.input)
        d_real = self.netD(self.ground_truth, self.input)

        var_dic = {}
        var_dic["GP"] = gp = gradPenalty(self.netD,
                                         self.ground_truth,
                                         self.fake,
                                         input=self.input,
                                         use_gpu=self.use_gpu)
        var_dic["SGP"] = sgp = spgradPenalty(self.netD, self.ground_truth, self.fake, input=self.input, type="G",
                                             use_gpu=self.use_gpu) * 0.5 + \
                               spgradPenalty(self.netD, self.ground_truth, self.fake, input=self.input, type="X",
                                             use_gpu=self.use_gpu) * 0.5
        var_dic["WD"] = w_distance = (d_real.mean() - d_fake.mean()).detach()
        var_dic["LOSS_D"] = loss_d = d_fake.mean() - d_real.mean() + gp + sgp

        return loss_d, var_dic