-
Notifications
You must be signed in to change notification settings - Fork 2
/
losses.py
121 lines (86 loc) · 4.28 KB
/
losses.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/env/bin python3.6
import pdb
from operator import add
from functools import reduce
from typing import List, Tuple
import torch
import numpy as np
from torch import Tensor, einsum
from utils import simplex, sset, probs2one_hot, one_hot, map_
class CrossEntropy():
def __init__(self, **kwargs):
# Self.idc is used to filter out some classes of the target mask. Use fancy indexing
self.idc: List[int] = kwargs["idc"]
print(f"Initialized {self.__class__.__name__} with {kwargs}")
def __call__(self, probs: Tensor, target: Tensor, _: Tensor) -> Tensor:
assert simplex(probs) and simplex(target)
log_p: Tensor = (probs[:, self.idc, ...] + 1e-10).log()
mask: Tensor = target[:, self.idc, ...].type(torch.float32)
loss = - einsum("bcwh,bcwh->", mask, log_p)
loss /= mask.sum() + 1e-10
return loss
class NaivePenalty():
"""
Implementation in the same fashion as the log-barrier
"""
def __init__(self, **kwargs):
self.idc: List[int] = kwargs["idc"]
self.C = len(self.idc)
self.__fn__ = getattr(__import__('utils'), kwargs['fn'])
print(f"Initialized {self.__class__.__name__} with {kwargs}")
def __call__(self, probs: Tensor, target: Tensor, bounds: Tensor) -> Tensor:
def penalty(z: Tensor) -> Tensor:
assert z.shape == ()
return torch.max(torch.zeros_like(z), z)**2
assert simplex(probs) # and simplex(target) # Actually, does not care about second part
assert probs.shape == target.shape
b, _, w, h = probs.shape # type: Tuple[int, int, int, int]
_, _, k, two = bounds.shape # scalar or vector
assert two == 2
# assert k == 1 # Keep it simple for now
value: Tensor = self.__fn__(probs[:, self.idc, ...])
lower_b = bounds[:, self.idc, :, 0]
upper_b = bounds[:, self.idc, :, 1]
assert value.shape == (b, self.C, k), value.shape
assert lower_b.shape == upper_b.shape == (b, self.C, k), lower_b.shape
upper_z: Tensor = (value - upper_b).type(torch.float32).flatten()
lower_z: Tensor = (lower_b - value).type(torch.float32).flatten()
upper_penalty: Tensor = reduce(add, (penalty(e) for e in upper_z))
lower_penalty: Tensor = reduce(add, (penalty(e) for e in lower_z))
res: Tensor = upper_penalty + lower_penalty
loss: Tensor = res.sum() / (w * h)
assert loss.requires_grad == probs.requires_grad # Handle the case for validation
return loss
class LogBarrierLoss():
def __init__(self, **kwargs):
self.idc: List[int] = kwargs["idc"]
self.C = len(self.idc)
self.t: float = kwargs["t"]
self.__fn__ = getattr(__import__('utils'), kwargs['fn'])
print(f"Initialized {self.__class__.__name__} with {kwargs}")
def __call__(self, probs: Tensor, target: Tensor, bounds: Tensor) -> Tensor:
def log_barrier(z: Tensor) -> Tensor:
assert z.shape == ()
if z <= - 1 / self.t**2:
return - torch.log(-z) / self.t
else:
return self.t * z + -np.log(1 / (self.t**2)) / self.t + 1 / self.t
assert simplex(probs) # and simplex(target) # Actually, does not care about second part
assert probs.shape == target.shape
b, _, w, h = probs.shape # type: Tuple[int, int, int, int]
_, _, k, two = bounds.shape # scalar or vector
assert two == 2
# assert k == 1 # Keep it simple for now
value: Tensor = self.__fn__(probs[:, self.idc, ...])
lower_b = bounds[:, self.idc, :, 0]
upper_b = bounds[:, self.idc, :, 1]
assert value.shape == (b, self.C, k), value.shape
assert lower_b.shape == upper_b.shape == (b, self.C, k), lower_b.shape
upper_z: Tensor = (value - upper_b).type(torch.float32).flatten()
lower_z: Tensor = (lower_b - value).type(torch.float32).flatten()
upper_barrier: Tensor = reduce(add, (log_barrier(e) for e in upper_z))
lower_barrier: Tensor = reduce(add, (log_barrier(e) for e in lower_z))
res: Tensor = upper_barrier + lower_barrier
loss: Tensor = res.sum() / (w * h)
assert loss.requires_grad == probs.requires_grad # Handle the case for validation
return loss