_transforms_video.py 4.83 KB
Newer Older
Zhicheng Yan's avatar
Zhicheng Yan committed
1
2
3
4
#!/usr/bin/env python3

import numbers
import random
5
import warnings
Zhicheng Yan's avatar
Zhicheng Yan committed
6

7
from torchvision.transforms import RandomCrop, RandomResizedCrop
Zhicheng Yan's avatar
Zhicheng Yan committed
8

9
from . import _functional_video as F
Zhicheng Yan's avatar
Zhicheng Yan committed
10
11
12
13
14
15
16
17
18
19
20
21


__all__ = [
    "RandomCropVideo",
    "RandomResizedCropVideo",
    "CenterCropVideo",
    "NormalizeVideo",
    "ToTensorVideo",
    "RandomHorizontalFlipVideo",
]


22
warnings.warn(
23
    "The 'torchvision.transforms._transforms_video' module is deprecated since 0.12 and will be removed in the future. "
24
25
    "Please use the 'torchvision.transforms' module instead."
)
26
27


Zhicheng Yan's avatar
Zhicheng Yan committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class RandomCropVideo(RandomCrop):
    def __init__(self, size):
        if isinstance(size, numbers.Number):
            self.size = (int(size), int(size))
        else:
            self.size = size

    def __call__(self, clip):
        """
        Args:
            clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
        Returns:
            torch.tensor: randomly cropped/resized video clip.
                size is (C, T, OH, OW)
        """
        i, j, h, w = self.get_params(clip, self.size)
        return F.crop(clip, i, j, h, w)

Joao Gomes's avatar
Joao Gomes committed
46
47
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(size={self.size})"
Zhicheng Yan's avatar
Zhicheng Yan committed
48
49
50
51
52
53
54
55
56
57
58


class RandomResizedCropVideo(RandomResizedCrop):
    def __init__(
        self,
        size,
        scale=(0.08, 1.0),
        ratio=(3.0 / 4.0, 4.0 / 3.0),
        interpolation_mode="bilinear",
    ):
        if isinstance(size, tuple):
59
60
            if len(size) != 2:
                raise ValueError(f"size should be tuple (height, width), instead got {size}")
Zhicheng Yan's avatar
Zhicheng Yan committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
            self.size = size
        else:
            self.size = (size, size)

        self.interpolation_mode = interpolation_mode
        self.scale = scale
        self.ratio = ratio

    def __call__(self, clip):
        """
        Args:
            clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
        Returns:
            torch.tensor: randomly cropped/resized video clip.
                size is (C, T, H, W)
        """
        i, j, h, w = self.get_params(clip, self.scale, self.ratio)
        return F.resized_crop(clip, i, j, h, w, self.size, self.interpolation_mode)

Joao Gomes's avatar
Joao Gomes committed
80
81
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(size={self.size}, interpolation_mode={self.interpolation_mode}, scale={self.scale}, ratio={self.ratio})"
Zhicheng Yan's avatar
Zhicheng Yan committed
82
83


84
class CenterCropVideo:
Zhicheng Yan's avatar
Zhicheng Yan committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    def __init__(self, crop_size):
        if isinstance(crop_size, numbers.Number):
            self.crop_size = (int(crop_size), int(crop_size))
        else:
            self.crop_size = crop_size

    def __call__(self, clip):
        """
        Args:
            clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
        Returns:
            torch.tensor: central cropping of video clip. Size is
            (C, T, crop_size, crop_size)
        """
        return F.center_crop(clip, self.crop_size)

Joao Gomes's avatar
Joao Gomes committed
101
102
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(crop_size={self.crop_size})"
Zhicheng Yan's avatar
Zhicheng Yan committed
103
104


105
class NormalizeVideo:
Zhicheng Yan's avatar
Zhicheng Yan committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    """
    Normalize the video clip by mean subtraction and division by standard deviation
    Args:
        mean (3-tuple): pixel RGB mean
        std (3-tuple): pixel RGB standard deviation
        inplace (boolean): whether do in-place normalization
    """

    def __init__(self, mean, std, inplace=False):
        self.mean = mean
        self.std = std
        self.inplace = inplace

    def __call__(self, clip):
        """
        Args:
            clip (torch.tensor): video clip to be normalized. Size is (C, T, H, W)
        """
        return F.normalize(clip, self.mean, self.std, self.inplace)

Joao Gomes's avatar
Joao Gomes committed
126
127
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(mean={self.mean}, std={self.std}, inplace={self.inplace})"
Zhicheng Yan's avatar
Zhicheng Yan committed
128
129


130
class ToTensorVideo:
Zhicheng Yan's avatar
Zhicheng Yan committed
131
132
    """
    Convert tensor data type from uint8 to float, divide value by 255.0 and
133
    permute the dimensions of clip tensor
Zhicheng Yan's avatar
Zhicheng Yan committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    """

    def __init__(self):
        pass

    def __call__(self, clip):
        """
        Args:
            clip (torch.tensor, dtype=torch.uint8): Size is (T, H, W, C)
        Return:
            clip (torch.tensor, dtype=torch.float): Size is (C, T, H, W)
        """
        return F.to_tensor(clip)

Joao Gomes's avatar
Joao Gomes committed
148
    def __repr__(self) -> str:
Zhicheng Yan's avatar
Zhicheng Yan committed
149
150
151
        return self.__class__.__name__


152
class RandomHorizontalFlipVideo:
Zhicheng Yan's avatar
Zhicheng Yan committed
153
    """
154
    Flip the video clip along the horizontal direction with a given probability
Zhicheng Yan's avatar
Zhicheng Yan committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    Args:
        p (float): probability of the clip being flipped. Default value is 0.5
    """

    def __init__(self, p=0.5):
        self.p = p

    def __call__(self, clip):
        """
        Args:
            clip (torch.tensor): Size is (C, T, H, W)
        Return:
            clip (torch.tensor): Size is (C, T, H, W)
        """
        if random.random() < self.p:
            clip = F.hflip(clip)
        return clip

Joao Gomes's avatar
Joao Gomes committed
173
174
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(p={self.p})"