-
Notifications
You must be signed in to change notification settings - Fork 0
/
region_loss.py
33 lines (23 loc) · 931 Bytes
/
region_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
import torch
from torch.nn.functional import mse_loss
def region_loss(
input: torch.Tensor, output: torch.Tensor, annotations: list[list[dict]], invert: bool
) -> torch.Tensor:
N, _, H, W = input.shape
batch_loss = 0
for tensor_idx, item in enumerate(annotations):
if len(item) == 0:
continue
tensor_loss = 0
for detection in item:
x_tl, y_tl, x_br, y_br = detection["xyxy"]
region_output, region_input = (
output[tensor_idx, :, y_tl:y_br, x_tl:x_br].unsqueeze(0),
input[tensor_idx, :, y_tl:y_br, x_tl:x_br].unsqueeze(0),
)
instance_loss = mse_loss(region_output, region_input)
if invert:
instance_loss = 1 - instance_loss
tensor_loss = tensor_loss + instance_loss
batch_loss = batch_loss + tensor_loss / len(item)
return batch_loss / N