Introduce new ReconstructionLoss class

This commit is contained in:
henryruhs
2025-03-11 14:43:09 +01:00
parent 085c493e18
commit 086d9eed87
3 changed files with 46 additions and 17 deletions
+25 -3
View File
@@ -141,6 +141,28 @@ class FaceSwapperLoss:
return translation, scale, rotation
class ReconstructionLoss(torch.nn.Module):
def __init__(self) -> None:
super(ReconstructionLoss, self).__init__()
def calc(self, source_tensor : Tensor, target_tensor : Tensor, output_tensor : Tensor) -> Tensor:
batch_size = CONFIG.getint('training.loader', 'batch_size')
loss_tensor = torch.pow(output_tensor - target_tensor, 2).reshape(batch_size, -1)
loss_tensor = torch.mean(loss_tensor, dim = 1) * 0.5
if torch.equal(source_tensor, target_tensor):
loss_tensor = torch.sum(loss_tensor * torch.tensor(0)) / (torch.tensor(0).sum() + 1e-4)
else:
loss_tensor = torch.sum(loss_tensor * torch.tensor(1)) / (torch.tensor(1).sum() + 1e-4)
data_range = float(torch.max(output_tensor) - torch.min(output_tensor))
similarity = 1 - ssim(output_tensor, target_tensor, data_range = data_range).mean()
loss_tensor = (loss_tensor + similarity) * 0.5
return loss_tensor
class IdentityLoss(torch.nn.Module):
def __init__(self) -> None:
super(IdentityLoss, self).__init__()
@@ -148,8 +170,8 @@ class IdentityLoss(torch.nn.Module):
self.embedder = torch.jit.load(embedder_path, map_location = 'cpu') # type:ignore[no-untyped-call]
self.embedder.eval()
def calc_loss(self, source_tensor : Tensor, output_tensor : Tensor) -> Tensor:
def calc(self, source_tensor : Tensor, output_tensor : Tensor) -> Tensor:
output_embedding = calc_embedding(self.embedder, output_tensor, (30, 0, 10, 10))
source_embedding = calc_embedding(self.embedder, source_tensor, (30, 0, 10, 10))
loss = (1 - torch.cosine_similarity(source_embedding, output_embedding)).mean()
return loss
loss_tensor = (1 - torch.cosine_similarity(source_embedding, output_embedding)).mean()
return loss_tensor