_video.py 9.02 KB
Newer Older
1
2
from __future__ import annotations

Philip Meier's avatar
Philip Meier committed
3
from typing import Any, List, Optional, Union
4
5
6
7

import torch
from torchvision.transforms.functional import InterpolationMode

Philip Meier's avatar
Philip Meier committed
8
from ._datapoint import _FillTypeJIT, Datapoint
9
10


11
class Video(Datapoint):
Philip Meier's avatar
Philip Meier committed
12
13
14
15
16
17
18
19
20
21
22
23
    """[BETA] :class:`torch.Tensor` subclass for videos.

    Args:
        data (tensor-like): Any data that can be turned into a tensor with :func:`torch.as_tensor`.
        dtype (torch.dtype, optional): Desired data type of the bounding box. If omitted, will be inferred from
            ``data``.
        device (torch.device, optional): Desired device of the bounding box. If omitted and ``data`` is a
            :class:`torch.Tensor`, the device is taken from it. Otherwise, the bounding box is constructed on the CPU.
        requires_grad (bool, optional): Whether autograd should record operations on the bounding box. If omitted and
            ``data`` is a :class:`torch.Tensor`, the value is taken from it. Otherwise, defaults to ``False``.
    """

24
    @classmethod
25
    def _wrap(cls, tensor: torch.Tensor) -> Video:
26
27
        video = tensor.as_subclass(cls)
        return video
28

29
30
31
32
33
34
    def __new__(
        cls,
        data: Any,
        *,
        dtype: Optional[torch.dtype] = None,
        device: Optional[Union[torch.device, str, int]] = None,
35
        requires_grad: Optional[bool] = None,
36
    ) -> Video:
37
        tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
38
39
        if data.ndim < 4:
            raise ValueError
40
        return cls._wrap(tensor)
41
42

    @classmethod
43
44
    def wrap_like(cls, other: Video, tensor: torch.Tensor) -> Video:
        return cls._wrap(tensor)
45

46
    def __repr__(self, *, tensor_contents: Any = None) -> str:  # type: ignore[override]
47
        return self._make_repr()
48

49
    def horizontal_flip(self) -> Video:
50
        output = self._F.horizontal_flip_video(self.as_subclass(torch.Tensor))
51
        return Video.wrap_like(self, output)
52
53

    def vertical_flip(self) -> Video:
54
        output = self._F.vertical_flip_video(self.as_subclass(torch.Tensor))
55
        return Video.wrap_like(self, output)
56
57
58
59

    def resize(  # type: ignore[override]
        self,
        size: List[int],
60
        interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
61
        max_size: Optional[int] = None,
62
        antialias: Optional[Union[str, bool]] = "warn",
63
    ) -> Video:
64
65
66
67
68
69
70
        output = self._F.resize_video(
            self.as_subclass(torch.Tensor),
            size,
            interpolation=interpolation,
            max_size=max_size,
            antialias=antialias,
        )
71
        return Video.wrap_like(self, output)
72
73

    def crop(self, top: int, left: int, height: int, width: int) -> Video:
74
        output = self._F.crop_video(self.as_subclass(torch.Tensor), top, left, height, width)
75
        return Video.wrap_like(self, output)
76
77

    def center_crop(self, output_size: List[int]) -> Video:
78
        output = self._F.center_crop_video(self.as_subclass(torch.Tensor), output_size=output_size)
79
        return Video.wrap_like(self, output)
80
81
82
83
84
85
86
87

    def resized_crop(
        self,
        top: int,
        left: int,
        height: int,
        width: int,
        size: List[int],
88
        interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
89
        antialias: Optional[Union[str, bool]] = "warn",
90
91
    ) -> Video:
        output = self._F.resized_crop_video(
92
93
94
95
96
97
98
99
            self.as_subclass(torch.Tensor),
            top,
            left,
            height,
            width,
            size=list(size),
            interpolation=interpolation,
            antialias=antialias,
100
        )
101
        return Video.wrap_like(self, output)
102
103
104

    def pad(
        self,
105
106
        padding: List[int],
        fill: Optional[Union[int, float, List[float]]] = None,
107
108
        padding_mode: str = "constant",
    ) -> Video:
109
        output = self._F.pad_video(self.as_subclass(torch.Tensor), padding, fill=fill, padding_mode=padding_mode)
110
        return Video.wrap_like(self, output)
111
112
113
114

    def rotate(
        self,
        angle: float,
115
        interpolation: Union[InterpolationMode, int] = InterpolationMode.NEAREST,
116
117
        expand: bool = False,
        center: Optional[List[float]] = None,
Philip Meier's avatar
Philip Meier committed
118
        fill: _FillTypeJIT = None,
119
    ) -> Video:
120
121
        output = self._F.rotate_video(
            self.as_subclass(torch.Tensor), angle, interpolation=interpolation, expand=expand, fill=fill, center=center
122
        )
123
        return Video.wrap_like(self, output)
124
125
126
127
128
129
130

    def affine(
        self,
        angle: Union[int, float],
        translate: List[float],
        scale: float,
        shear: List[float],
131
        interpolation: Union[InterpolationMode, int] = InterpolationMode.NEAREST,
Philip Meier's avatar
Philip Meier committed
132
        fill: _FillTypeJIT = None,
133
134
        center: Optional[List[float]] = None,
    ) -> Video:
135
136
        output = self._F.affine_video(
            self.as_subclass(torch.Tensor),
137
138
139
140
141
142
143
144
            angle,
            translate=translate,
            scale=scale,
            shear=shear,
            interpolation=interpolation,
            fill=fill,
            center=center,
        )
145
        return Video.wrap_like(self, output)
146
147
148

    def perspective(
        self,
149
150
        startpoints: Optional[List[List[int]]],
        endpoints: Optional[List[List[int]]],
151
        interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
Philip Meier's avatar
Philip Meier committed
152
        fill: _FillTypeJIT = None,
153
        coefficients: Optional[List[float]] = None,
154
    ) -> Video:
155
        output = self._F.perspective_video(
156
157
158
159
160
161
            self.as_subclass(torch.Tensor),
            startpoints,
            endpoints,
            interpolation=interpolation,
            fill=fill,
            coefficients=coefficients,
162
        )
163
        return Video.wrap_like(self, output)
164
165
166
167

    def elastic(
        self,
        displacement: torch.Tensor,
168
        interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
Philip Meier's avatar
Philip Meier committed
169
        fill: _FillTypeJIT = None,
170
    ) -> Video:
171
172
173
        output = self._F.elastic_video(
            self.as_subclass(torch.Tensor), displacement, interpolation=interpolation, fill=fill
        )
174
        return Video.wrap_like(self, output)
175

176
    def rgb_to_grayscale(self, num_output_channels: int = 1) -> Video:
177
178
179
180
181
        output = self._F.rgb_to_grayscale_image_tensor(
            self.as_subclass(torch.Tensor), num_output_channels=num_output_channels
        )
        return Video.wrap_like(self, output)

182
    def adjust_brightness(self, brightness_factor: float) -> Video:
183
        output = self._F.adjust_brightness_video(self.as_subclass(torch.Tensor), brightness_factor=brightness_factor)
184
        return Video.wrap_like(self, output)
185
186

    def adjust_saturation(self, saturation_factor: float) -> Video:
187
        output = self._F.adjust_saturation_video(self.as_subclass(torch.Tensor), saturation_factor=saturation_factor)
188
        return Video.wrap_like(self, output)
189
190

    def adjust_contrast(self, contrast_factor: float) -> Video:
191
        output = self._F.adjust_contrast_video(self.as_subclass(torch.Tensor), contrast_factor=contrast_factor)
192
        return Video.wrap_like(self, output)
193
194

    def adjust_sharpness(self, sharpness_factor: float) -> Video:
195
        output = self._F.adjust_sharpness_video(self.as_subclass(torch.Tensor), sharpness_factor=sharpness_factor)
196
        return Video.wrap_like(self, output)
197
198

    def adjust_hue(self, hue_factor: float) -> Video:
199
        output = self._F.adjust_hue_video(self.as_subclass(torch.Tensor), hue_factor=hue_factor)
200
        return Video.wrap_like(self, output)
201
202

    def adjust_gamma(self, gamma: float, gain: float = 1) -> Video:
203
        output = self._F.adjust_gamma_video(self.as_subclass(torch.Tensor), gamma=gamma, gain=gain)
204
        return Video.wrap_like(self, output)
205
206

    def posterize(self, bits: int) -> Video:
207
        output = self._F.posterize_video(self.as_subclass(torch.Tensor), bits=bits)
208
        return Video.wrap_like(self, output)
209
210

    def solarize(self, threshold: float) -> Video:
211
        output = self._F.solarize_video(self.as_subclass(torch.Tensor), threshold=threshold)
212
        return Video.wrap_like(self, output)
213
214

    def autocontrast(self) -> Video:
215
        output = self._F.autocontrast_video(self.as_subclass(torch.Tensor))
216
        return Video.wrap_like(self, output)
217
218

    def equalize(self) -> Video:
219
        output = self._F.equalize_video(self.as_subclass(torch.Tensor))
220
        return Video.wrap_like(self, output)
221
222

    def invert(self) -> Video:
223
        output = self._F.invert_video(self.as_subclass(torch.Tensor))
224
        return Video.wrap_like(self, output)
225
226

    def gaussian_blur(self, kernel_size: List[int], sigma: Optional[List[float]] = None) -> Video:
227
        output = self._F.gaussian_blur_video(self.as_subclass(torch.Tensor), kernel_size=kernel_size, sigma=sigma)
228
        return Video.wrap_like(self, output)
229

230
231
232
233
    def normalize(self, mean: List[float], std: List[float], inplace: bool = False) -> Video:
        output = self._F.normalize_video(self.as_subclass(torch.Tensor), mean=mean, std=std, inplace=inplace)
        return Video.wrap_like(self, output)

234

Philip Meier's avatar
Philip Meier committed
235
236
237
238
_VideoType = Union[torch.Tensor, Video]
_VideoTypeJIT = torch.Tensor
_TensorVideoType = Union[torch.Tensor, Video]
_TensorVideoTypeJIT = torch.Tensor