Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

_presets.py 8.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. """
  2. This file is part of the private API. Please do not use directly these classes as they will be modified on
  3. future versions without warning. The classes should be accessed only via the transforms argument of Weights.
  4. """
  5. from typing import Optional, Tuple, Union
  6. import torch
  7. from torch import nn, Tensor
  8. from . import functional as F, InterpolationMode
  9. __all__ = [
  10. "ObjectDetection",
  11. "ImageClassification",
  12. "VideoClassification",
  13. "SemanticSegmentation",
  14. "OpticalFlow",
  15. ]
  16. class ObjectDetection(nn.Module):
  17. def forward(self, img: Tensor) -> Tensor:
  18. if not isinstance(img, Tensor):
  19. img = F.pil_to_tensor(img)
  20. return F.convert_image_dtype(img, torch.float)
  21. def __repr__(self) -> str:
  22. return self.__class__.__name__ + "()"
  23. def describe(self) -> str:
  24. return (
  25. "Accepts ``PIL.Image``, batched ``(B, C, H, W)`` and single ``(C, H, W)`` image ``torch.Tensor`` objects. "
  26. "The images are rescaled to ``[0.0, 1.0]``."
  27. )
  28. class ImageClassification(nn.Module):
  29. def __init__(
  30. self,
  31. *,
  32. crop_size: int,
  33. resize_size: int = 256,
  34. mean: Tuple[float, ...] = (0.485, 0.456, 0.406),
  35. std: Tuple[float, ...] = (0.229, 0.224, 0.225),
  36. interpolation: InterpolationMode = InterpolationMode.BILINEAR,
  37. antialias: Optional[bool] = True,
  38. ) -> None:
  39. super().__init__()
  40. self.crop_size = [crop_size]
  41. self.resize_size = [resize_size]
  42. self.mean = list(mean)
  43. self.std = list(std)
  44. self.interpolation = interpolation
  45. self.antialias = antialias
  46. def forward(self, img: Tensor) -> Tensor:
  47. img = F.resize(img, self.resize_size, interpolation=self.interpolation, antialias=self.antialias)
  48. img = F.center_crop(img, self.crop_size)
  49. if not isinstance(img, Tensor):
  50. img = F.pil_to_tensor(img)
  51. img = F.convert_image_dtype(img, torch.float)
  52. img = F.normalize(img, mean=self.mean, std=self.std)
  53. return img
  54. def __repr__(self) -> str:
  55. format_string = self.__class__.__name__ + "("
  56. format_string += f"\n crop_size={self.crop_size}"
  57. format_string += f"\n resize_size={self.resize_size}"
  58. format_string += f"\n mean={self.mean}"
  59. format_string += f"\n std={self.std}"
  60. format_string += f"\n interpolation={self.interpolation}"
  61. format_string += "\n)"
  62. return format_string
  63. def describe(self) -> str:
  64. return (
  65. "Accepts ``PIL.Image``, batched ``(B, C, H, W)`` and single ``(C, H, W)`` image ``torch.Tensor`` objects. "
  66. f"The images are resized to ``resize_size={self.resize_size}`` using ``interpolation={self.interpolation}``, "
  67. f"followed by a central crop of ``crop_size={self.crop_size}``. Finally the values are first rescaled to "
  68. f"``[0.0, 1.0]`` and then normalized using ``mean={self.mean}`` and ``std={self.std}``."
  69. )
  70. class VideoClassification(nn.Module):
  71. def __init__(
  72. self,
  73. *,
  74. crop_size: Tuple[int, int],
  75. resize_size: Union[Tuple[int], Tuple[int, int]],
  76. mean: Tuple[float, ...] = (0.43216, 0.394666, 0.37645),
  77. std: Tuple[float, ...] = (0.22803, 0.22145, 0.216989),
  78. interpolation: InterpolationMode = InterpolationMode.BILINEAR,
  79. ) -> None:
  80. super().__init__()
  81. self.crop_size = list(crop_size)
  82. self.resize_size = list(resize_size)
  83. self.mean = list(mean)
  84. self.std = list(std)
  85. self.interpolation = interpolation
  86. def forward(self, vid: Tensor) -> Tensor:
  87. need_squeeze = False
  88. if vid.ndim < 5:
  89. vid = vid.unsqueeze(dim=0)
  90. need_squeeze = True
  91. N, T, C, H, W = vid.shape
  92. vid = vid.view(-1, C, H, W)
  93. # We hard-code antialias=False to preserve results after we changed
  94. # its default from None to True (see
  95. # https://github.com/pytorch/vision/pull/7160)
  96. # TODO: we could re-train the video models with antialias=True?
  97. vid = F.resize(vid, self.resize_size, interpolation=self.interpolation, antialias=False)
  98. vid = F.center_crop(vid, self.crop_size)
  99. vid = F.convert_image_dtype(vid, torch.float)
  100. vid = F.normalize(vid, mean=self.mean, std=self.std)
  101. H, W = self.crop_size
  102. vid = vid.view(N, T, C, H, W)
  103. vid = vid.permute(0, 2, 1, 3, 4) # (N, T, C, H, W) => (N, C, T, H, W)
  104. if need_squeeze:
  105. vid = vid.squeeze(dim=0)
  106. return vid
  107. def __repr__(self) -> str:
  108. format_string = self.__class__.__name__ + "("
  109. format_string += f"\n crop_size={self.crop_size}"
  110. format_string += f"\n resize_size={self.resize_size}"
  111. format_string += f"\n mean={self.mean}"
  112. format_string += f"\n std={self.std}"
  113. format_string += f"\n interpolation={self.interpolation}"
  114. format_string += "\n)"
  115. return format_string
  116. def describe(self) -> str:
  117. return (
  118. "Accepts batched ``(B, T, C, H, W)`` and single ``(T, C, H, W)`` video frame ``torch.Tensor`` objects. "
  119. f"The frames are resized to ``resize_size={self.resize_size}`` using ``interpolation={self.interpolation}``, "
  120. f"followed by a central crop of ``crop_size={self.crop_size}``. Finally the values are first rescaled to "
  121. f"``[0.0, 1.0]`` and then normalized using ``mean={self.mean}`` and ``std={self.std}``. Finally the output "
  122. "dimensions are permuted to ``(..., C, T, H, W)`` tensors."
  123. )
  124. class SemanticSegmentation(nn.Module):
  125. def __init__(
  126. self,
  127. *,
  128. resize_size: Optional[int],
  129. mean: Tuple[float, ...] = (0.485, 0.456, 0.406),
  130. std: Tuple[float, ...] = (0.229, 0.224, 0.225),
  131. interpolation: InterpolationMode = InterpolationMode.BILINEAR,
  132. antialias: Optional[bool] = True,
  133. ) -> None:
  134. super().__init__()
  135. self.resize_size = [resize_size] if resize_size is not None else None
  136. self.mean = list(mean)
  137. self.std = list(std)
  138. self.interpolation = interpolation
  139. self.antialias = antialias
  140. def forward(self, img: Tensor) -> Tensor:
  141. if isinstance(self.resize_size, list):
  142. img = F.resize(img, self.resize_size, interpolation=self.interpolation, antialias=self.antialias)
  143. if not isinstance(img, Tensor):
  144. img = F.pil_to_tensor(img)
  145. img = F.convert_image_dtype(img, torch.float)
  146. img = F.normalize(img, mean=self.mean, std=self.std)
  147. return img
  148. def __repr__(self) -> str:
  149. format_string = self.__class__.__name__ + "("
  150. format_string += f"\n resize_size={self.resize_size}"
  151. format_string += f"\n mean={self.mean}"
  152. format_string += f"\n std={self.std}"
  153. format_string += f"\n interpolation={self.interpolation}"
  154. format_string += "\n)"
  155. return format_string
  156. def describe(self) -> str:
  157. return (
  158. "Accepts ``PIL.Image``, batched ``(B, C, H, W)`` and single ``(C, H, W)`` image ``torch.Tensor`` objects. "
  159. f"The images are resized to ``resize_size={self.resize_size}`` using ``interpolation={self.interpolation}``. "
  160. f"Finally the values are first rescaled to ``[0.0, 1.0]`` and then normalized using ``mean={self.mean}`` and "
  161. f"``std={self.std}``."
  162. )
  163. class OpticalFlow(nn.Module):
  164. def forward(self, img1: Tensor, img2: Tensor) -> Tuple[Tensor, Tensor]:
  165. if not isinstance(img1, Tensor):
  166. img1 = F.pil_to_tensor(img1)
  167. if not isinstance(img2, Tensor):
  168. img2 = F.pil_to_tensor(img2)
  169. img1 = F.convert_image_dtype(img1, torch.float)
  170. img2 = F.convert_image_dtype(img2, torch.float)
  171. # map [0, 1] into [-1, 1]
  172. img1 = F.normalize(img1, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
  173. img2 = F.normalize(img2, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
  174. img1 = img1.contiguous()
  175. img2 = img2.contiguous()
  176. return img1, img2
  177. def __repr__(self) -> str:
  178. return self.__class__.__name__ + "()"
  179. def describe(self) -> str:
  180. return (
  181. "Accepts ``PIL.Image``, batched ``(B, C, H, W)`` and single ``(C, H, W)`` image ``torch.Tensor`` objects. "
  182. "The images are rescaled to ``[-1.0, 1.0]``."
  183. )
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...