import numpy as np import torch import torch.nn as nn from torch import Tensor class L1Loss(nn.Module): """ å·²ç¥åç´ é¨åçL1 loss(å¹³åç»å¯¹è¯¯å·®) """ def __init__(self): super(L1Loss, self).__init__() def forward(self, pred: Tensor, y: Tensor, mask: Tensor) -> Tensor: l_valid = torch.sum(torch.abs(pred*mask-y*mask))/torch.sum(mask) l_hole = torch.sum(torch.abs((1-mask)*(pred-y)))/torch.sum(torch.abs(1-mask)) return l_valid class L2Loss(nn.Module): """ å·²ç¥åç´ é¨åçL2 loss(åæ¹å·®è¯¯å·®æ失) """ def __init__(self): super(L2Loss, self).__init__() def forward(self, pred: Tensor, y: Tensor, mask: Tensor) -> Tensor: pixel_diff = (pred*mask-y*mask)**2 loss = torch.sum(pixel_diff)/torch.sum(mask) return loss