Here is a python function I have for a neural network I have implemented. I feel it become a bit cumbersome. The intention is to weight positive labels in channels 1:end higher than background pixels. Hence the distinction between foreground and background.
def loss_function(
self,
x: torch.tensor,
groundtruth: torch.tensor,
weight: float
) -> torch.tensor:
delta = 0.00001
groundtruth_pos = groundtruth[:, 1:, :, :] == 1
groundtruth_neg = groundtruth[:, 1:, :, :] == 0
foreground_gt = groundtruth[:, 1:, :, :]
background_gt = groundtruth[:, 0, :, :]
foreground_x = x[:, 1:, :, :]
background_x = x[:, 0, :, :]
loss_foreground_pos = -(torch.sum(foreground_gt[groundtruth_pos] * torch.log(foreground_x[groundtruth_pos] + delta))
+ torch.sum((1 - foreground_gt[groundtruth_pos]) * torch.log(1 - foreground_x[groundtruth_pos] + delta)))
loss_foreground_neg = -(torch.sum(foreground_gt[groundtruth_neg] * torch.log(foreground_x[groundtruth_neg] + delta))
+ torch.sum((1 - foreground_gt[groundtruth_neg]) * torch.log(1 - foreground_x[groundtruth_neg] + delta)))
loss_background = -(torch.sum(background_gt * torch.log(background_x + delta))
+ torch.sum((1 - background_gt) * torch.log(1 - background_x + delta)))
return weight * loss_foreground_pos + loss_foreground_neg + loss_background
importstatements. Here at Code Review we prefer self-contained compilable code snippets that allow us reviewers to run the code with some example data. (This is just a remark for your future questions. Since this question already has an answer, leave the question as it is now.) \$\endgroup\$