File size: 5,546 Bytes
1ea89dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import torch
import torch.nn as nn

from .utils import FNS, REGRESSION_DICT, masked_mean, masked_quantile


class Regression(nn.Module):
    def __init__(
        self,
        weight: float,
        gamma: float,
        fn: str,
        input_fn: str,
        output_fn: str,
        alpha: float = 1.0,
        dims: tuple[int] = (-1,),
        quantile: float = 0.0,
        **kwargs,
    ):
        super().__init__()
        self.name = self.__class__.__name__
        self.output_fn = FNS[output_fn]
        self.input_fn = FNS[input_fn]
        self.fn = REGRESSION_DICT[fn]
        self.weight = weight
        self.gamma = gamma
        self.alpha = alpha
        self.dims = dims
        self.quantile = quantile

    @torch.autocast(device_type="cuda", enabled=False, dtype=torch.float32)
    def forward(
        self,
        input: torch.Tensor,
        target: torch.Tensor,
        mask: torch.Tensor | None = None,
        **kwargs,
    ) -> torch.Tensor:
        if mask is not None:  # usually it is just repeated
            mask = mask[:, 0]

        input = self.input_fn(input.float())
        target = self.input_fn(target.float())
        error = self.fn(input - target, gamma=self.gamma, alpha=self.alpha).mean(dim=1)
        if self.quantile > 0.0:
            mask_quantile = error < masked_quantile(
                error, mask, dims=self.dims, q=1 - self.quantile
            ).view(-1, *((1,) * len(self.dims)))
            mask = mask & mask_quantile if mask is not None else mask_quantile
        mean_error = masked_mean(data=error, mask=mask, dim=self.dims).squeeze(
            self.dims
        )
        mean_error = self.output_fn(mean_error)
        return mean_error

    @classmethod
    def build(cls, config):
        obj = cls(
            weight=config["weight"],
            fn=config["fn"],
            gamma=config["gamma"],
            alpha=config.get("alpha", 1.0),
            output_fn=config["output_fn"],
            input_fn=config["input_fn"],
            dims=config.get("dims", (-1,)),
            quantile=config.get("quantile", 0.0),
        )
        return obj


class PolarRegression(nn.Module):
    def __init__(
        self,
        weight: float,
        gamma: float,
        fn: str,
        input_fn: str,
        output_fn: str,
        alpha: float = 1.0,
        dims: list[int] = [-1, -2],
        polar_weight: float = 1.0,
        polar_asym: float = 0.5,
        **kwargs,
    ):
        super().__init__()
        self.name = self.__class__.__name__
        self.output_fn = FNS[output_fn]
        self.input_fn = FNS[input_fn]
        self.fn = REGRESSION_DICT[fn]
        self.weight = weight
        self.gamma = gamma
        self.alpha = alpha
        self.dims = dims
        self.polar_weight = polar_weight
        self.polar_asym = polar_asym

    @torch.autocast(device_type="cuda", enabled=False, dtype=torch.float32)
    def forward(
        self,
        input: torch.Tensor,
        target: torch.Tensor,
        mask: torch.Tensor | None = None,
        **kwargs,
    ) -> torch.Tensor:
        if mask is not None:  # usually it is just repeated
            mask = mask.squeeze(1)

        input = self.input_fn(input.float())
        target = self.input_fn(target.float())
        input = input / torch.norm(input, dim=1, keepdim=True).clamp(min=1e-5)
        target = target / torch.norm(target, dim=1, keepdim=True).clamp(min=1e-5)

        x_target, y_target, z_target = target.unbind(dim=1)
        z_clipped = z_target.clip(min=-0.99999, max=0.99999)
        x_clipped = x_target.abs().clip(min=1e-5) * (2 * (x_target > 0).float() - 1)
        polar_target = torch.arccos(z_clipped)
        azimuth_target = torch.atan2(y_target, x_clipped)

        x_input, y_input, z_input = input.unbind(dim=1)
        z_clipped = z_input.clip(min=-0.99999, max=0.99999)
        x_clipped = x_input.abs().clip(min=1e-5) * (2 * (x_input > 0).float() - 1)
        polar_input = torch.arccos(z_clipped)
        azimuth_input = torch.atan2(y_input, x_clipped)

        polar_error = self.fn(
            polar_input - polar_target, gamma=self.gamma, alpha=self.alpha
        )
        azimuth_error = self.fn(
            azimuth_input - azimuth_target, gamma=self.gamma, alpha=self.alpha
        )

        quantile_weight = torch.ones_like(polar_input)
        quantile_weight[
            (polar_target > polar_input) & (polar_target > torch.pi / 2)
        ] = (2 * self.polar_asym)
        quantile_weight[
            (polar_target <= polar_input) & (polar_target > torch.pi / 2)
        ] = 2 * (1 - self.polar_asym)

        mean_polar_error = masked_mean(
            data=polar_error * quantile_weight, mask=mask, dim=self.dims
        ).squeeze(self.dims)
        mean_azimuth_error = masked_mean(
            data=azimuth_error, mask=mask, dim=self.dims
        ).squeeze(self.dims)
        mean_error = (self.polar_weight * mean_polar_error + mean_azimuth_error) / (
            1 + self.polar_weight
        )

        mean_error = self.output_fn(mean_error)
        return mean_error

    @classmethod
    def build(cls, config):
        obj = cls(
            weight=config["weight"],
            fn=config["fn"],
            gamma=config["gamma"],
            alpha=config.get("alpha", 1.0),
            output_fn=config["output_fn"],
            input_fn=config["input_fn"],
            dims=config.get("dims", (-1,)),
            polar_weight=config["polar_weight"],
            polar_asym=config["polar_asym"],
        )
        return obj