-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
57 lines (42 loc) · 1.69 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Loss function for the MFR Model Implementation."""
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import torch
from torch import Tensor, nn
class GeneratorLoss(nn.Module):
"""Generator loss for the MFR model.
"""
def __init__(self, wadv=1, wrec=50, wlat=1):
super().__init__()
self.loss_lat = nn.SmoothL1Loss()
self.loss_adv = nn.MSELoss()
self.loss_rec = nn.L1Loss()
self.wadv = wadv
self.wrec = wrec
self.wlat = wlat
def forward(
self, latent_i: Tensor, latent_o: Tensor, images: Tensor, fake: Tensor, pred_real: Tensor, pred_fake: Tensor
) -> Tensor:
"""Compute the loss for a batch.
"""
error_lat = self.loss_lat(latent_i, latent_o)
error_rec = self.loss_rec(images, fake)
error_adv = self.loss_adv(pred_real, pred_fake)
loss = error_adv * self.wadv + error_rec * self.wrec + error_lat * self.wlat
return loss
class DiscriminatorLoss(nn.Module):
"""Discriminator loss for the MFR model."""
def __init__(self):
super().__init__()
self.loss_bce = nn.BCELoss()
def forward(self, pred_real, pred_fake):
"""Compute the loss for a predicted batch.
"""
error_discriminator_real = self.loss_bce(
pred_real, torch.ones(size=pred_real.shape, dtype=torch.float32, device=pred_real.device)
)
error_discriminator_fake = self.loss_bce(
pred_fake, torch.zeros(size=pred_fake.shape, dtype=torch.float32, device=pred_fake.device)
)
loss_discriminator = (error_discriminator_fake + error_discriminator_real) * 0.5
return loss_discriminator