-
Notifications
You must be signed in to change notification settings - Fork 1
/
loss.py
34 lines (24 loc) · 1019 Bytes
/
loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import torch
import torch.nn as nn
from torch.nn import functional as F
from pytorch_ssim import msssim
class GeneratorLoss(nn.Module):
def __init__(self):
super(GeneratorLoss, self).__init__()
self.perceptual_cri = CharbonnierLoss(loss_weight=1.0)
self.gamma = 1.0
def forward(self, prediction, target, is_ds=False):
if not is_ds:
loss = self.perceptual_cri(prediction, target) + \
self.gamma * (1 - msssim(prediction, target, val_range=2., normalize='relu'))
else:
loss = self.perceptual_cri(prediction, target)
return loss
class CharbonnierLoss(nn.Module):
def __init__(self, loss_weight=1.0, reduction='mean'):
super(CharbonnierLoss, self).__init__()
self.loss_weight = loss_weight
self.reduction = reduction
self.eps = 1e-3
def forward(self, pred, target):
return self.loss_weight * torch.mean(torch.sqrt(torch.square(target - pred) + self.eps * self.eps))