File size: 9,324 Bytes
f71ac1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""Pointwise transformations."""

from __future__ import annotations

from typing import TypedDict

import numpy as np

from vis4d.common.typing import NDArrayFloat
from vis4d.data.const import CommonKeys as K

from .base import Transform


@Transform(in_keys=K.points3d, out_keys="transforms.pc_bounds")
class GenPcBounds:
    """Extracts the max and min values of the loaded points."""

    def __call__(
        self, coordinates_list: list[NDArrayFloat]
    ) -> list[NDArrayFloat]:
        """Extracts the max and min values of the pointcloud."""
        coordinates = coordinates_list[0]

        pc_bounds = [np.stack([coordinates.min(0), coordinates.max(0)])] * len(
            coordinates_list
        )

        return pc_bounds


@Transform(in_keys=(K.points3d, "trasforms.pc_bounds"), out_keys=K.points3d)
class NormalizeByMaxBounds:
    """Normalizes the pointcloud by the max bounds."""

    def __init__(self, axes: tuple[int, int, int] = (0, 1, 2)) -> None:
        """Creates an instance of the class.

        Args:
            axes (tuple[int, int, int]): Over which axes to apply
                normalization.
        """
        self.axes = axes

    def __call__(
        self,
        coords_list: list[NDArrayFloat],
        pc_bounds_list: list[NDArrayFloat],
    ) -> list[NDArrayFloat]:
        """Applies the normalization."""
        for i, (coords, pc_bounds) in enumerate(
            zip(coords_list, pc_bounds_list)
        ):
            max_bound = np.max(np.abs(pc_bounds), axis=0)
            for ax in self.axes:
                coords[:, ax] = coords[:, ax] / max_bound[ax]
            coords_list[i] = coords
        return coords_list


@Transform(in_keys=K.points3d, out_keys=K.points3d)
class CenterAndNormalize:
    """Centers and normalizes the pointcloud."""

    def __init__(self, centering: bool = True, normalize: bool = True) -> None:
        """Creates an instance of the class.

        Args:
            centering (bool): Whether to center the pointcloud
            normalize (bool): Whether to normalize the pointcloud
        """
        self.centering = centering
        self.normalize = normalize

    def __call__(self, coords_list: list[NDArrayFloat]) -> list[NDArrayFloat]:
        """Applies the Center and Normalization operations."""
        for i, coords in enumerate(coords_list):
            if self.centering:
                coords = coords - np.mean(coords, axis=0)
            if self.normalize:
                coords = coords / np.max(np.sqrt(np.sum(coords**2, axis=-1)))
            coords_list[i] = coords
        return coords_list


@Transform(in_keys=K.points3d, out_keys=K.points3d)
class AddGaussianNoise:
    """Adds random normal distributed noise with given std to the data.

    Args:
        std (float): Standard Deviation of the noise
    """

    def __init__(self, noise_level: float = 0.01):
        """Creates an instance of the class.

        Args:
            noise_level (float): The noise level. Standard deviation for
                the gaussian noise.
        """
        self.noise_level = noise_level

    def __call__(
        self, coordinates_list: list[NDArrayFloat]
    ) -> list[NDArrayFloat]:
        """Adds gaussian noise to the coordiantes."""
        for i, coordinates in enumerate(coordinates_list):
            coordinates[i] = (
                coordinates
                + np.random.randn(*coordinates.shape) * self.noise_level
            )
        return coordinates_list


@Transform(in_keys=K.points3d, out_keys=K.points3d)
class AddUniformNoise:
    """Adds random normal distributed noise with given std to the data.

    Args:
        std (float): Standard Deviation of the noise
    """

    def __init__(self, noise_level: float = 0.01):
        """Creates an instance of the class.

        Args:
            noise_level (float): The noise level. Half the range of the
                uniform noise. The noise is sampled from
                [-noise_level, noise_level].
        """
        self.noise_level = noise_level

    def __call__(
        self, coordinates_list: list[NDArrayFloat]
    ) -> list[NDArrayFloat]:
        """Adds uniform noise to the coordinates."""
        for i, coordinates in enumerate(coordinates_list):
            coordinates_list[i] = coordinates + np.random.uniform(
                -self.noise_level, self.noise_level, coordinates.shape
            )
        return coordinates_list


class SE3Transform(TypedDict):
    """Parameters for Resize."""

    translation: NDArrayFloat
    rotation: NDArrayFloat


def _gen_random_se3_transform(
    translation_min: NDArrayFloat,
    translation_max: NDArrayFloat,
    rotation_min: NDArrayFloat,
    rotation_max: NDArrayFloat,
) -> SE3Transform:
    """Creates a random SE3 Transforms.

    The transform is generated by sampling a random translation and
    rotation from a uniform distribution.
    """
    angle = np.random.uniform(rotation_min, rotation_max)
    translation = np.random.uniform(translation_min, translation_max)
    cos_x, sin_x = np.cos(angle[0]), np.sin(angle[0])
    cos_y, sin_y = np.cos(angle[1]), np.sin(angle[1])
    cos_z, sin_z = np.cos(angle[2]), np.sin(angle[2])
    rotx = np.array([[1, 0, 0], [0, cos_x, -sin_x], [0, sin_x, cos_x]])
    roty = np.array([[cos_y, 0, sin_y], [0, 1, 0], [-sin_y, 0, cos_y]])
    rotz = np.array([[cos_z, -sin_z, 0], [sin_z, cos_z, 0], [0, 0, 1]])
    rot = np.dot(rotz, np.dot(roty, rotx))
    return SE3Transform(translation=translation, rotation=rot)


@Transform(in_keys=K.points3d, out_keys=K.points3d)
class ApplySE3Transform:
    """Applies a given SE3 Transform to the data."""

    def __init__(
        self,
        translation_min: tuple[float, float, float] = (0.0, 0.0, 0.0),
        translation_max: tuple[float, float, float] = (0.0, 0.0, 0.0),
        rotation_min: tuple[float, float, float] = (0.0, 0.0, 0.0),
        rotation_max: tuple[float, float, float] = (0.0, 0.0, 0.0),
    ) -> None:
        """Creates an instance of the class.

        Args:
            translation_min (tuple[float, float, float]): Minimum translation.
            translation_max (tuple[float, float, float]): Maximum translation.
            rotation_min (tuple[float, float, float]):  Minimum euler rotation
                angles [rad]. Applied in the order rot_x -> rot_y -> rot_z.
            rotation_max (tuple[float, float, float]): Maximum euler rotation
                angles [rad]. Applied in the order rot_x -> rot_y -> rot_z.
        """
        self.translation_min = np.asarray(translation_min)
        self.translation_max = np.asarray(translation_max)
        self.rotation_min = np.asarray(rotation_min)
        self.rotation_max = np.asarray(rotation_max)

    def __call__(
        self, coordinates_list: list[NDArrayFloat]
    ) -> list[NDArrayFloat]:
        """Applies a SE3 Transform."""
        for i, coordinates in enumerate(coordinates_list):
            transform = _gen_random_se3_transform(
                self.translation_min,
                self.translation_max,
                self.rotation_min,
                self.rotation_max,
            )
            if coordinates.shape[-1] == 3:
                coordinates_list[i] = (
                    transform["rotation"] @ coordinates.T
                ).T + transform["translation"]
            elif coordinates.shape[-2] == 3:
                coordinates_list[i] = (
                    transform["rotation"] @ coordinates
                ).T + transform["translation"]
            else:
                raise ValueError(
                    f"Invalid shape for coordinates: {coordinates.shape}"
                )
        return coordinates_list


class ApplySO3Transform(ApplySE3Transform):
    """Applies a given SO3 Transform to the data."""

    def __call__(
        self, coordinates_list: list[NDArrayFloat]
    ) -> list[NDArrayFloat]:
        """Applies a given SO3 Transform to the data."""
        for i, coordinates in enumerate(coordinates_list):
            transform = _gen_random_se3_transform(
                self.translation_min,
                self.translation_max,
                self.rotation_min,
                self.rotation_max,
            )["rotation"]
            if coordinates.shape[-1] == 3:
                coordinates_list[i] = (transform @ coordinates.T).T
            elif coordinates.shape[-2] == 3:
                coordinates_list[i] = (transform @ coordinates).T
            else:
                raise ValueError(
                    f"Invalid shape for coordinates: {coordinates.shape}"
                )
        return coordinates_list


@Transform(in_keys=K.points3d, out_keys=K.points3d)
class TransposeChannels:
    """Transposes some predifined channels."""

    def __init__(self, channels: tuple[int, int] = (-1, -2)):
        """Creates an instance of the class.

        Args:
            channels (tuple[int, int]): Tuple of channels to transpose
        """
        self.channels = channels

    def __call__(
        self, coordinates_list: list[NDArrayFloat]
    ) -> list[NDArrayFloat]:
        """Transposes some predifined channels."""
        for i, coordinates in enumerate(coordinates_list):
            coordinates_list[i] = coordinates.transpose(*self.channels)
        return coordinates_list