-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathregression.py
75 lines (56 loc) · 2.61 KB
/
regression.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
from typing import Optional
import torch
import torch.nn as nn
from torch import Tensor
from src import register
from src.tools import ops
from src.typing import LossData
__all__ = ['RegressionLoss']
def l1_loss(pred: Tensor, target: Tensor) -> Tensor:
"""Dense L1 loss."""
loss = (pred - target).abs()
return loss
def log_l1_loss(pred: Tensor, target: Tensor) -> Tensor:
"""Dense Log L1 loss."""
loss = (1 + l1_loss(pred, target)).log()
return loss
def berhu_loss(pred: Tensor, target: Tensor, delta: float = 0.2, dynamic: bool = True) -> Tensor:
"""Dense berHu loss.
:param pred: (Tensor) Network prediction.
:param target: (Tensor) Ground-truth target.
:param delta: (float) Threshold above which the loss switches from L1.
:param dynamic: (bool) If `True`, set threshold dynamically, using `delta` as the max error percentage.
:return: (Tensor) The computed `berhu` loss.
"""
diff = l1_loss(pred, target)
delta = delta if not dynamic else delta*diff.max()
diff_delta = (diff.pow(2) + delta.pow(2)) / (2*delta + ops.eps(pred))
loss = torch.where(diff <= delta, diff, diff_delta)
return loss
@register(('depth_regr', 'stereo_const'))
class RegressionLoss(nn.Module):
"""Class implementing a supervised regression loss.
NOTE: The DepthHints automask is not computed here. Instead, we rely on the `MonoDepthModule` to compute it.
Probably not the best way of doing it, but it keeps this loss clean...
Contributions:
- Virtual stereo consistency: From Monodepth (https://arxiv.org/abs/1609.03677)
- Proxy berHu regression: From Kuznietsov (https://arxiv.org/abs/1702.02706)
- Proxy LogL1 regression: From Depth Hints (https://arxiv.org/abs/1909.09051)
- Proxy loss automasking: From Depth Hints/Monodepth2 (https://arxiv.org/abs/1909.09051)
:param loss_name: (str) Loss type to use. {l1, log_l1, berhu}
:param use_automask: (bool) If `True`, use DepthHints automask based on the pred/hints errors.
"""
def __init__(self, loss_name: str = 'berhu', use_automask: bool = False):
super().__init__()
self.loss_name = loss_name
self.use_automask = use_automask
self.criterion = {
'l1': l1_loss,
'log_l1': log_l1_loss,
'berhu': berhu_loss,
}[self.loss_name]
def forward(self, pred: Tensor, target: Tensor, mask: Optional[Tensor] = None) -> LossData:
if mask is None: mask = torch.ones_like(target)
err = mask * self.criterion(pred, target)
loss = err.sum()/mask.sum()
return loss, {'err_regr': err, 'mask_regr': mask}