"router/vscode:/vscode.git/clone" did not exist on "e11f5f1c383831a226e26bf3560d6cdade0ee914"
_presets.py 4.48 KB
Newer Older
1
2
3
4
5
"""
This file is part of the private API. Please do not use directly these classes as they will be modified on
future versions without warning. The classes should be accessed only via the transforms argument of Weights.
"""
from typing import Optional, Tuple
6
7
8
9

import torch
from torch import Tensor, nn

10
from . import functional as F, InterpolationMode
11
12


13
__all__ = [
14
15
16
17
18
    "ObjectDetection",
    "ImageClassification",
    "VideoClassification",
    "SemanticSegmentation",
    "OpticalFlow",
19
]
20
21


22
23
class ObjectDetection(nn.Module):
    def forward(self, img: Tensor) -> Tensor:
24
25
        if not isinstance(img, Tensor):
            img = F.pil_to_tensor(img)
26
        return F.convert_image_dtype(img, torch.float)
27
28


29
class ImageClassification(nn.Module):
30
31
    def __init__(
        self,
32
        *,
33
34
35
36
        crop_size: int,
        resize_size: int = 256,
        mean: Tuple[float, ...] = (0.485, 0.456, 0.406),
        std: Tuple[float, ...] = (0.229, 0.224, 0.225),
37
        interpolation: InterpolationMode = InterpolationMode.BILINEAR,
38
    ) -> None:
39
        super().__init__()
40
41
42
43
44
        self._crop_size = [crop_size]
        self._size = [resize_size]
        self._mean = list(mean)
        self._std = list(std)
        self._interpolation = interpolation
45
46

    def forward(self, img: Tensor) -> Tensor:
47
48
        img = F.resize(img, self._size, interpolation=self._interpolation)
        img = F.center_crop(img, self._crop_size)
49
50
51
        if not isinstance(img, Tensor):
            img = F.pil_to_tensor(img)
        img = F.convert_image_dtype(img, torch.float)
52
53
        img = F.normalize(img, mean=self._mean, std=self._std)
        return img
54
55


56
class VideoClassification(nn.Module):
57
58
    def __init__(
        self,
59
        *,
60
        crop_size: Tuple[int, int],
61
        resize_size: Tuple[int, int],
62
63
        mean: Tuple[float, ...] = (0.43216, 0.394666, 0.37645),
        std: Tuple[float, ...] = (0.22803, 0.22145, 0.216989),
64
        interpolation: InterpolationMode = InterpolationMode.BILINEAR,
65
66
    ) -> None:
        super().__init__()
67
68
69
70
71
        self._crop_size = list(crop_size)
        self._size = list(resize_size)
        self._mean = list(mean)
        self._std = list(std)
        self._interpolation = interpolation
72
73

    def forward(self, vid: Tensor) -> Tensor:
74
75
76
77
78
79
80
81
        need_squeeze = False
        if vid.ndim < 5:
            vid = vid.unsqueeze(dim=0)
            need_squeeze = True

        vid = vid.permute(0, 1, 4, 2, 3)  # (N, T, H, W, C) => (N, T, C, H, W)
        N, T, C, H, W = vid.shape
        vid = vid.view(-1, C, H, W)
82
83
84
85
        vid = F.resize(vid, self._size, interpolation=self._interpolation)
        vid = F.center_crop(vid, self._crop_size)
        vid = F.convert_image_dtype(vid, torch.float)
        vid = F.normalize(vid, mean=self._mean, std=self._std)
86
87
88
89
90
91
92
        H, W = self._crop_size
        vid = vid.view(N, T, C, H, W)
        vid = vid.permute(0, 2, 1, 3, 4)  # (N, T, C, H, W) => (N, C, T, H, W)

        if need_squeeze:
            vid = vid.squeeze(dim=0)
        return vid
93
94


95
class SemanticSegmentation(nn.Module):
96
97
    def __init__(
        self,
98
99
        *,
        resize_size: Optional[int],
100
101
        mean: Tuple[float, ...] = (0.485, 0.456, 0.406),
        std: Tuple[float, ...] = (0.229, 0.224, 0.225),
102
        interpolation: InterpolationMode = InterpolationMode.BILINEAR,
103
104
    ) -> None:
        super().__init__()
105
        self._size = [resize_size] if resize_size is not None else None
106
107
108
109
        self._mean = list(mean)
        self._std = list(std)
        self._interpolation = interpolation

110
111
112
    def forward(self, img: Tensor) -> Tensor:
        if isinstance(self._size, list):
            img = F.resize(img, self._size, interpolation=self._interpolation)
113
114
115
116
        if not isinstance(img, Tensor):
            img = F.pil_to_tensor(img)
        img = F.convert_image_dtype(img, torch.float)
        img = F.normalize(img, mean=self._mean, std=self._std)
117
        return img
118
119


120
121
122
123
124
125
class OpticalFlow(nn.Module):
    def forward(self, img1: Tensor, img2: Tensor) -> Tuple[Tensor, Tensor]:
        if not isinstance(img1, Tensor):
            img1 = F.pil_to_tensor(img1)
        if not isinstance(img2, Tensor):
            img2 = F.pil_to_tensor(img2)
126

127
128
        img1 = F.convert_image_dtype(img1, torch.float)
        img2 = F.convert_image_dtype(img2, torch.float)
129
130
131
132
133
134
135
136

        # map [0, 1] into [-1, 1]
        img1 = F.normalize(img1, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
        img2 = F.normalize(img2, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])

        img1 = img1.contiguous()
        img2 = img2.contiguous()

137
        return img1, img2