_geometry.py 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. import math
  2. import numbers
  3. import warnings
  4. from typing import Any, Callable, Dict, List, Literal, Optional, Sequence, Tuple, Type, Union
  5. import PIL.Image
  6. import torch
  7. from torchvision import transforms as _transforms, tv_tensors
  8. from torchvision.ops.boxes import box_iou
  9. from torchvision.transforms.functional import _get_perspective_coeffs
  10. from torchvision.transforms.v2 import functional as F, InterpolationMode, Transform
  11. from torchvision.transforms.v2.functional._utils import _FillType
  12. from ._transform import _RandomApplyTransform
  13. from ._utils import (
  14. _check_padding_arg,
  15. _check_padding_mode_arg,
  16. _check_sequence_input,
  17. _get_fill,
  18. _setup_angle,
  19. _setup_fill_arg,
  20. _setup_number_or_seq,
  21. _setup_size,
  22. get_bounding_boxes,
  23. has_all,
  24. has_any,
  25. is_pure_tensor,
  26. query_size,
  27. )
  28. class RandomHorizontalFlip(_RandomApplyTransform):
  29. """Horizontally flip the input with a given probability.
  30. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  31. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  32. it can have arbitrary number of leading batch dimensions. For example,
  33. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  34. Args:
  35. p (float, optional): probability of the input being flipped. Default value is 0.5
  36. """
  37. _v1_transform_cls = _transforms.RandomHorizontalFlip
  38. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  39. return self._call_kernel(F.horizontal_flip, inpt)
  40. class RandomVerticalFlip(_RandomApplyTransform):
  41. """Vertically flip the input with a given probability.
  42. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  43. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  44. it can have arbitrary number of leading batch dimensions. For example,
  45. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  46. Args:
  47. p (float, optional): probability of the input being flipped. Default value is 0.5
  48. """
  49. _v1_transform_cls = _transforms.RandomVerticalFlip
  50. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  51. return self._call_kernel(F.vertical_flip, inpt)
  52. class Resize(Transform):
  53. """Resize the input to the given size.
  54. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  55. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  56. it can have arbitrary number of leading batch dimensions. For example,
  57. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  58. Args:
  59. size (sequence or int): Desired output size. If size is a sequence like
  60. (h, w), output size will be matched to this. If size is an int,
  61. smaller edge of the image will be matched to this number.
  62. i.e, if height > width, then image will be rescaled to
  63. (size * height / width, size).
  64. .. note::
  65. In torchscript mode size as single int is not supported, use a sequence of length 1: ``[size, ]``.
  66. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  67. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
  68. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.NEAREST_EXACT``,
  69. ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported.
  70. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  71. max_size (int, optional): The maximum allowed for the longer edge of
  72. the resized image. If the longer edge of the image is greater
  73. than ``max_size`` after being resized according to ``size``,
  74. ``size`` will be overruled so that the longer edge is equal to
  75. ``max_size``.
  76. As a result, the smaller edge may be shorter than ``size``. This
  77. is only supported if ``size`` is an int (or a sequence of length
  78. 1 in torchscript mode).
  79. antialias (bool, optional): Whether to apply antialiasing.
  80. It only affects **tensors** with bilinear or bicubic modes and it is
  81. ignored otherwise: on PIL images, antialiasing is always applied on
  82. bilinear or bicubic modes; on other modes (for PIL images and
  83. tensors), antialiasing makes no sense and this parameter is ignored.
  84. Possible values are:
  85. - ``True`` (default): will apply antialiasing for bilinear or bicubic modes.
  86. Other mode aren't affected. This is probably what you want to use.
  87. - ``False``: will not apply antialiasing for tensors on any mode. PIL
  88. images are still antialiased on bilinear or bicubic modes, because
  89. PIL doesn't support no antialias.
  90. - ``None``: equivalent to ``False`` for tensors and ``True`` for
  91. PIL images. This value exists for legacy reasons and you probably
  92. don't want to use it unless you really know what you are doing.
  93. The default value changed from ``None`` to ``True`` in
  94. v0.17, for the PIL and Tensor backends to be consistent.
  95. """
  96. _v1_transform_cls = _transforms.Resize
  97. def __init__(
  98. self,
  99. size: Union[int, Sequence[int]],
  100. interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
  101. max_size: Optional[int] = None,
  102. antialias: Optional[bool] = True,
  103. ) -> None:
  104. super().__init__()
  105. if isinstance(size, int):
  106. size = [size]
  107. elif isinstance(size, Sequence) and len(size) in {1, 2}:
  108. size = list(size)
  109. else:
  110. raise ValueError(
  111. f"size can either be an integer or a sequence of one or two integers, but got {size} instead."
  112. )
  113. self.size = size
  114. self.interpolation = interpolation
  115. self.max_size = max_size
  116. self.antialias = antialias
  117. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  118. return self._call_kernel(
  119. F.resize,
  120. inpt,
  121. self.size,
  122. interpolation=self.interpolation,
  123. max_size=self.max_size,
  124. antialias=self.antialias,
  125. )
  126. class CenterCrop(Transform):
  127. """Crop the input at the center.
  128. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  129. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  130. it can have arbitrary number of leading batch dimensions. For example,
  131. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  132. If image size is smaller than output size along any edge, image is padded with 0 and then center cropped.
  133. Args:
  134. size (sequence or int): Desired output size of the crop. If size is an
  135. int instead of sequence like (h, w), a square crop (size, size) is
  136. made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
  137. """
  138. _v1_transform_cls = _transforms.CenterCrop
  139. def __init__(self, size: Union[int, Sequence[int]]):
  140. super().__init__()
  141. self.size = _setup_size(size, error_msg="Please provide only two dimensions (h, w) for size.")
  142. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  143. return self._call_kernel(F.center_crop, inpt, output_size=self.size)
  144. class RandomResizedCrop(Transform):
  145. """Crop a random portion of the input and resize it to a given size.
  146. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  147. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  148. it can have arbitrary number of leading batch dimensions. For example,
  149. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  150. A crop of the original input is made: the crop has a random area (H * W)
  151. and a random aspect ratio. This crop is finally resized to the given
  152. size. This is popularly used to train the Inception networks.
  153. Args:
  154. size (int or sequence): expected output size of the crop, for each edge. If size is an
  155. int instead of sequence like (h, w), a square output size ``(size, size)`` is
  156. made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
  157. .. note::
  158. In torchscript mode size as single int is not supported, use a sequence of length 1: ``[size, ]``.
  159. scale (tuple of float, optional): Specifies the lower and upper bounds for the random area of the crop,
  160. before resizing. The scale is defined with respect to the area of the original image.
  161. ratio (tuple of float, optional): lower and upper bounds for the random aspect ratio of the crop, before
  162. resizing.
  163. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  164. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
  165. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.NEAREST_EXACT``,
  166. ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported.
  167. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  168. antialias (bool, optional): Whether to apply antialiasing.
  169. It only affects **tensors** with bilinear or bicubic modes and it is
  170. ignored otherwise: on PIL images, antialiasing is always applied on
  171. bilinear or bicubic modes; on other modes (for PIL images and
  172. tensors), antialiasing makes no sense and this parameter is ignored.
  173. Possible values are:
  174. - ``True`` (default): will apply antialiasing for bilinear or bicubic modes.
  175. Other mode aren't affected. This is probably what you want to use.
  176. - ``False``: will not apply antialiasing for tensors on any mode. PIL
  177. images are still antialiased on bilinear or bicubic modes, because
  178. PIL doesn't support no antialias.
  179. - ``None``: equivalent to ``False`` for tensors and ``True`` for
  180. PIL images. This value exists for legacy reasons and you probably
  181. don't want to use it unless you really know what you are doing.
  182. The default value changed from ``None`` to ``True`` in
  183. v0.17, for the PIL and Tensor backends to be consistent.
  184. """
  185. _v1_transform_cls = _transforms.RandomResizedCrop
  186. def __init__(
  187. self,
  188. size: Union[int, Sequence[int]],
  189. scale: Tuple[float, float] = (0.08, 1.0),
  190. ratio: Tuple[float, float] = (3.0 / 4.0, 4.0 / 3.0),
  191. interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
  192. antialias: Optional[bool] = True,
  193. ) -> None:
  194. super().__init__()
  195. self.size = _setup_size(size, error_msg="Please provide only two dimensions (h, w) for size.")
  196. if not isinstance(scale, Sequence):
  197. raise TypeError("Scale should be a sequence")
  198. if not isinstance(ratio, Sequence):
  199. raise TypeError("Ratio should be a sequence")
  200. if (scale[0] > scale[1]) or (ratio[0] > ratio[1]):
  201. warnings.warn("Scale and ratio should be of kind (min, max)")
  202. self.scale = scale
  203. self.ratio = ratio
  204. self.interpolation = interpolation
  205. self.antialias = antialias
  206. self._log_ratio = torch.log(torch.tensor(self.ratio))
  207. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  208. height, width = query_size(flat_inputs)
  209. area = height * width
  210. log_ratio = self._log_ratio
  211. for _ in range(10):
  212. target_area = area * torch.empty(1).uniform_(self.scale[0], self.scale[1]).item()
  213. aspect_ratio = torch.exp(
  214. torch.empty(1).uniform_(
  215. log_ratio[0], # type: ignore[arg-type]
  216. log_ratio[1], # type: ignore[arg-type]
  217. )
  218. ).item()
  219. w = int(round(math.sqrt(target_area * aspect_ratio)))
  220. h = int(round(math.sqrt(target_area / aspect_ratio)))
  221. if 0 < w <= width and 0 < h <= height:
  222. i = torch.randint(0, height - h + 1, size=(1,)).item()
  223. j = torch.randint(0, width - w + 1, size=(1,)).item()
  224. break
  225. else:
  226. # Fallback to central crop
  227. in_ratio = float(width) / float(height)
  228. if in_ratio < min(self.ratio):
  229. w = width
  230. h = int(round(w / min(self.ratio)))
  231. elif in_ratio > max(self.ratio):
  232. h = height
  233. w = int(round(h * max(self.ratio)))
  234. else: # whole image
  235. w = width
  236. h = height
  237. i = (height - h) // 2
  238. j = (width - w) // 2
  239. return dict(top=i, left=j, height=h, width=w)
  240. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  241. return self._call_kernel(
  242. F.resized_crop, inpt, **params, size=self.size, interpolation=self.interpolation, antialias=self.antialias
  243. )
  244. class FiveCrop(Transform):
  245. """Crop the image or video into four corners and the central crop.
  246. If the input is a :class:`torch.Tensor` or a :class:`~torchvision.tv_tensors.Image` or a
  247. :class:`~torchvision.tv_tensors.Video` it can have arbitrary number of leading batch dimensions.
  248. For example, the image can have ``[..., C, H, W]`` shape.
  249. .. Note::
  250. This transform returns a tuple of images and there may be a mismatch in the number of
  251. inputs and targets your Dataset returns. See below for an example of how to deal with
  252. this.
  253. Args:
  254. size (sequence or int): Desired output size of the crop. If size is an ``int``
  255. instead of sequence like (h, w), a square crop of size (size, size) is made.
  256. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
  257. Example:
  258. >>> class BatchMultiCrop(transforms.Transform):
  259. ... def forward(self, sample: Tuple[Tuple[Union[tv_tensors.Image, tv_tensors.Video], ...], int]):
  260. ... images_or_videos, labels = sample
  261. ... batch_size = len(images_or_videos)
  262. ... image_or_video = images_or_videos[0]
  263. ... images_or_videos = tv_tensors.wrap(torch.stack(images_or_videos), like=image_or_video)
  264. ... labels = torch.full((batch_size,), label, device=images_or_videos.device)
  265. ... return images_or_videos, labels
  266. ...
  267. >>> image = tv_tensors.Image(torch.rand(3, 256, 256))
  268. >>> label = 3
  269. >>> transform = transforms.Compose([transforms.FiveCrop(224), BatchMultiCrop()])
  270. >>> images, labels = transform(image, label)
  271. >>> images.shape
  272. torch.Size([5, 3, 224, 224])
  273. >>> labels
  274. tensor([3, 3, 3, 3, 3])
  275. """
  276. _v1_transform_cls = _transforms.FiveCrop
  277. def __init__(self, size: Union[int, Sequence[int]]) -> None:
  278. super().__init__()
  279. self.size = _setup_size(size, error_msg="Please provide only two dimensions (h, w) for size.")
  280. def _call_kernel(self, functional: Callable, inpt: Any, *args: Any, **kwargs: Any) -> Any:
  281. if isinstance(inpt, (tv_tensors.BoundingBoxes, tv_tensors.Mask)):
  282. warnings.warn(
  283. f"{type(self).__name__}() is currently passing through inputs of type "
  284. f"tv_tensors.{type(inpt).__name__}. This will likely change in the future."
  285. )
  286. return super()._call_kernel(functional, inpt, *args, **kwargs)
  287. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  288. return self._call_kernel(F.five_crop, inpt, self.size)
  289. def _check_inputs(self, flat_inputs: List[Any]) -> None:
  290. if has_any(flat_inputs, tv_tensors.BoundingBoxes, tv_tensors.Mask):
  291. raise TypeError(f"BoundingBoxes'es and Mask's are not supported by {type(self).__name__}()")
  292. class TenCrop(Transform):
  293. """Crop the image or video into four corners and the central crop plus the flipped version of
  294. these (horizontal flipping is used by default).
  295. If the input is a :class:`torch.Tensor` or a :class:`~torchvision.tv_tensors.Image` or a
  296. :class:`~torchvision.tv_tensors.Video` it can have arbitrary number of leading batch dimensions.
  297. For example, the image can have ``[..., C, H, W]`` shape.
  298. See :class:`~torchvision.transforms.v2.FiveCrop` for an example.
  299. .. Note::
  300. This transform returns a tuple of images and there may be a mismatch in the number of
  301. inputs and targets your Dataset returns. See below for an example of how to deal with
  302. this.
  303. Args:
  304. size (sequence or int): Desired output size of the crop. If size is an
  305. int instead of sequence like (h, w), a square crop (size, size) is
  306. made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
  307. vertical_flip (bool, optional): Use vertical flipping instead of horizontal
  308. """
  309. _v1_transform_cls = _transforms.TenCrop
  310. def __init__(self, size: Union[int, Sequence[int]], vertical_flip: bool = False) -> None:
  311. super().__init__()
  312. self.size = _setup_size(size, error_msg="Please provide only two dimensions (h, w) for size.")
  313. self.vertical_flip = vertical_flip
  314. def _call_kernel(self, functional: Callable, inpt: Any, *args: Any, **kwargs: Any) -> Any:
  315. if isinstance(inpt, (tv_tensors.BoundingBoxes, tv_tensors.Mask)):
  316. warnings.warn(
  317. f"{type(self).__name__}() is currently passing through inputs of type "
  318. f"tv_tensors.{type(inpt).__name__}. This will likely change in the future."
  319. )
  320. return super()._call_kernel(functional, inpt, *args, **kwargs)
  321. def _check_inputs(self, flat_inputs: List[Any]) -> None:
  322. if has_any(flat_inputs, tv_tensors.BoundingBoxes, tv_tensors.Mask):
  323. raise TypeError(f"BoundingBoxes'es and Mask's are not supported by {type(self).__name__}()")
  324. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  325. return self._call_kernel(F.ten_crop, inpt, self.size, vertical_flip=self.vertical_flip)
  326. class Pad(Transform):
  327. """Pad the input on all sides with the given "pad" value.
  328. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  329. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  330. it can have arbitrary number of leading batch dimensions. For example,
  331. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  332. Args:
  333. padding (int or sequence): Padding on each border. If a single int is provided this
  334. is used to pad all borders. If sequence of length 2 is provided this is the padding
  335. on left/right and top/bottom respectively. If a sequence of length 4 is provided
  336. this is the padding for the left, top, right and bottom borders respectively.
  337. .. note::
  338. In torchscript mode padding as single int is not supported, use a sequence of
  339. length 1: ``[padding, ]``.
  340. fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant.
  341. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  342. Fill value can be also a dictionary mapping data type to the fill value, e.g.
  343. ``fill={tv_tensors.Image: 127, tv_tensors.Mask: 0}`` where ``Image`` will be filled with 127 and
  344. ``Mask`` will be filled with 0.
  345. padding_mode (str, optional): Type of padding. Should be: constant, edge, reflect or symmetric.
  346. Default is "constant".
  347. - constant: pads with a constant value, this value is specified with fill
  348. - edge: pads with the last value at the edge of the image.
  349. - reflect: pads with reflection of image without repeating the last value on the edge.
  350. For example, padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
  351. will result in [3, 2, 1, 2, 3, 4, 3, 2]
  352. - symmetric: pads with reflection of image repeating the last value on the edge.
  353. For example, padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
  354. will result in [2, 1, 1, 2, 3, 4, 4, 3]
  355. """
  356. _v1_transform_cls = _transforms.Pad
  357. def _extract_params_for_v1_transform(self) -> Dict[str, Any]:
  358. params = super()._extract_params_for_v1_transform()
  359. if not (params["fill"] is None or isinstance(params["fill"], (int, float))):
  360. raise ValueError(f"{type(self).__name__}() can only be scripted for a scalar `fill`, but got {self.fill}.")
  361. return params
  362. def __init__(
  363. self,
  364. padding: Union[int, Sequence[int]],
  365. fill: Union[_FillType, Dict[Union[Type, str], _FillType]] = 0,
  366. padding_mode: Literal["constant", "edge", "reflect", "symmetric"] = "constant",
  367. ) -> None:
  368. super().__init__()
  369. _check_padding_arg(padding)
  370. _check_padding_mode_arg(padding_mode)
  371. # This cast does Sequence[int] -> List[int] and is required to make mypy happy
  372. if not isinstance(padding, int):
  373. padding = list(padding)
  374. self.padding = padding
  375. self.fill = fill
  376. self._fill = _setup_fill_arg(fill)
  377. self.padding_mode = padding_mode
  378. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  379. fill = _get_fill(self._fill, type(inpt))
  380. return self._call_kernel(F.pad, inpt, padding=self.padding, fill=fill, padding_mode=self.padding_mode) # type: ignore[arg-type]
  381. class RandomZoomOut(_RandomApplyTransform):
  382. """ "Zoom out" transformation from
  383. `"SSD: Single Shot MultiBox Detector" <https://arxiv.org/abs/1512.02325>`_.
  384. This transformation randomly pads images, videos, bounding boxes and masks creating a zoom out effect.
  385. Output spatial size is randomly sampled from original size up to a maximum size configured
  386. with ``side_range`` parameter:
  387. .. code-block:: python
  388. r = uniform_sample(side_range[0], side_range[1])
  389. output_width = input_width * r
  390. output_height = input_height * r
  391. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  392. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  393. it can have arbitrary number of leading batch dimensions. For example,
  394. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  395. Args:
  396. fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant.
  397. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  398. Fill value can be also a dictionary mapping data type to the fill value, e.g.
  399. ``fill={tv_tensors.Image: 127, tv_tensors.Mask: 0}`` where ``Image`` will be filled with 127 and
  400. ``Mask`` will be filled with 0.
  401. side_range (sequence of floats, optional): tuple of two floats defines minimum and maximum factors to
  402. scale the input size.
  403. p (float, optional): probability that the zoom operation will be performed.
  404. """
  405. def __init__(
  406. self,
  407. fill: Union[_FillType, Dict[Union[Type, str], _FillType]] = 0,
  408. side_range: Sequence[float] = (1.0, 4.0),
  409. p: float = 0.5,
  410. ) -> None:
  411. super().__init__(p=p)
  412. self.fill = fill
  413. self._fill = _setup_fill_arg(fill)
  414. _check_sequence_input(side_range, "side_range", req_sizes=(2,))
  415. self.side_range = side_range
  416. if side_range[0] < 1.0 or side_range[0] > side_range[1]:
  417. raise ValueError(f"Invalid side range provided {side_range}.")
  418. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  419. orig_h, orig_w = query_size(flat_inputs)
  420. r = self.side_range[0] + torch.rand(1) * (self.side_range[1] - self.side_range[0])
  421. canvas_width = int(orig_w * r)
  422. canvas_height = int(orig_h * r)
  423. r = torch.rand(2)
  424. left = int((canvas_width - orig_w) * r[0])
  425. top = int((canvas_height - orig_h) * r[1])
  426. right = canvas_width - (left + orig_w)
  427. bottom = canvas_height - (top + orig_h)
  428. padding = [left, top, right, bottom]
  429. return dict(padding=padding)
  430. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  431. fill = _get_fill(self._fill, type(inpt))
  432. return self._call_kernel(F.pad, inpt, **params, fill=fill)
  433. class RandomRotation(Transform):
  434. """Rotate the input by angle.
  435. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  436. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  437. it can have arbitrary number of leading batch dimensions. For example,
  438. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  439. Args:
  440. degrees (sequence or number): Range of degrees to select from.
  441. If degrees is a number instead of sequence like (min, max), the range of degrees
  442. will be (-degrees, +degrees).
  443. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  444. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
  445. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
  446. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  447. expand (bool, optional): Optional expansion flag.
  448. If true, expands the output to make it large enough to hold the entire rotated image.
  449. If false or omitted, make the output image the same size as the input image.
  450. Note that the expand flag assumes rotation around the center (see note below) and no translation.
  451. center (sequence, optional): Optional center of rotation, (x, y). Origin is the upper left corner.
  452. Default is the center of the image.
  453. .. note::
  454. In theory, setting ``center`` has no effect if ``expand=True``, since the image center will become the
  455. center of rotation. In practice however, due to numerical precision, this can lead to off-by-one
  456. differences of the resulting image size compared to using the image center in the first place. Thus, when
  457. setting ``expand=True``, it's best to leave ``center=None`` (default).
  458. fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant.
  459. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  460. Fill value can be also a dictionary mapping data type to the fill value, e.g.
  461. ``fill={tv_tensors.Image: 127, tv_tensors.Mask: 0}`` where ``Image`` will be filled with 127 and
  462. ``Mask`` will be filled with 0.
  463. .. _filters: https://pillow.readthedocs.io/en/latest/handbook/concepts.html#filters
  464. """
  465. _v1_transform_cls = _transforms.RandomRotation
  466. def __init__(
  467. self,
  468. degrees: Union[numbers.Number, Sequence],
  469. interpolation: Union[InterpolationMode, int] = InterpolationMode.NEAREST,
  470. expand: bool = False,
  471. center: Optional[List[float]] = None,
  472. fill: Union[_FillType, Dict[Union[Type, str], _FillType]] = 0,
  473. ) -> None:
  474. super().__init__()
  475. self.degrees = _setup_angle(degrees, name="degrees", req_sizes=(2,))
  476. self.interpolation = interpolation
  477. self.expand = expand
  478. self.fill = fill
  479. self._fill = _setup_fill_arg(fill)
  480. if center is not None:
  481. _check_sequence_input(center, "center", req_sizes=(2,))
  482. self.center = center
  483. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  484. angle = torch.empty(1).uniform_(self.degrees[0], self.degrees[1]).item()
  485. return dict(angle=angle)
  486. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  487. fill = _get_fill(self._fill, type(inpt))
  488. return self._call_kernel(
  489. F.rotate,
  490. inpt,
  491. **params,
  492. interpolation=self.interpolation,
  493. expand=self.expand,
  494. center=self.center,
  495. fill=fill,
  496. )
  497. class RandomAffine(Transform):
  498. """Random affine transformation the input keeping center invariant.
  499. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  500. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  501. it can have arbitrary number of leading batch dimensions. For example,
  502. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  503. Args:
  504. degrees (sequence or number): Range of degrees to select from.
  505. If degrees is a number instead of sequence like (min, max), the range of degrees
  506. will be (-degrees, +degrees). Set to 0 to deactivate rotations.
  507. translate (tuple, optional): tuple of maximum absolute fraction for horizontal
  508. and vertical translations. For example translate=(a, b), then horizontal shift
  509. is randomly sampled in the range -img_width * a < dx < img_width * a and vertical shift is
  510. randomly sampled in the range -img_height * b < dy < img_height * b. Will not translate by default.
  511. scale (tuple, optional): scaling factor interval, e.g (a, b), then scale is
  512. randomly sampled from the range a <= scale <= b. Will keep original scale by default.
  513. shear (sequence or number, optional): Range of degrees to select from.
  514. If shear is a number, a shear parallel to the x-axis in the range (-shear, +shear)
  515. will be applied. Else if shear is a sequence of 2 values a shear parallel to the x-axis in the
  516. range (shear[0], shear[1]) will be applied. Else if shear is a sequence of 4 values,
  517. an x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied.
  518. Will not apply shear by default.
  519. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  520. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
  521. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
  522. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  523. fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant.
  524. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  525. Fill value can be also a dictionary mapping data type to the fill value, e.g.
  526. ``fill={tv_tensors.Image: 127, tv_tensors.Mask: 0}`` where ``Image`` will be filled with 127 and
  527. ``Mask`` will be filled with 0.
  528. center (sequence, optional): Optional center of rotation, (x, y). Origin is the upper left corner.
  529. Default is the center of the image.
  530. .. _filters: https://pillow.readthedocs.io/en/latest/handbook/concepts.html#filters
  531. """
  532. _v1_transform_cls = _transforms.RandomAffine
  533. def __init__(
  534. self,
  535. degrees: Union[numbers.Number, Sequence],
  536. translate: Optional[Sequence[float]] = None,
  537. scale: Optional[Sequence[float]] = None,
  538. shear: Optional[Union[int, float, Sequence[float]]] = None,
  539. interpolation: Union[InterpolationMode, int] = InterpolationMode.NEAREST,
  540. fill: Union[_FillType, Dict[Union[Type, str], _FillType]] = 0,
  541. center: Optional[List[float]] = None,
  542. ) -> None:
  543. super().__init__()
  544. self.degrees = _setup_angle(degrees, name="degrees", req_sizes=(2,))
  545. if translate is not None:
  546. _check_sequence_input(translate, "translate", req_sizes=(2,))
  547. for t in translate:
  548. if not (0.0 <= t <= 1.0):
  549. raise ValueError("translation values should be between 0 and 1")
  550. self.translate = translate
  551. if scale is not None:
  552. _check_sequence_input(scale, "scale", req_sizes=(2,))
  553. for s in scale:
  554. if s <= 0:
  555. raise ValueError("scale values should be positive")
  556. self.scale = scale
  557. if shear is not None:
  558. self.shear = _setup_angle(shear, name="shear", req_sizes=(2, 4))
  559. else:
  560. self.shear = shear
  561. self.interpolation = interpolation
  562. self.fill = fill
  563. self._fill = _setup_fill_arg(fill)
  564. if center is not None:
  565. _check_sequence_input(center, "center", req_sizes=(2,))
  566. self.center = center
  567. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  568. height, width = query_size(flat_inputs)
  569. angle = torch.empty(1).uniform_(self.degrees[0], self.degrees[1]).item()
  570. if self.translate is not None:
  571. max_dx = float(self.translate[0] * width)
  572. max_dy = float(self.translate[1] * height)
  573. tx = int(round(torch.empty(1).uniform_(-max_dx, max_dx).item()))
  574. ty = int(round(torch.empty(1).uniform_(-max_dy, max_dy).item()))
  575. translate = (tx, ty)
  576. else:
  577. translate = (0, 0)
  578. if self.scale is not None:
  579. scale = torch.empty(1).uniform_(self.scale[0], self.scale[1]).item()
  580. else:
  581. scale = 1.0
  582. shear_x = shear_y = 0.0
  583. if self.shear is not None:
  584. shear_x = torch.empty(1).uniform_(self.shear[0], self.shear[1]).item()
  585. if len(self.shear) == 4:
  586. shear_y = torch.empty(1).uniform_(self.shear[2], self.shear[3]).item()
  587. shear = (shear_x, shear_y)
  588. return dict(angle=angle, translate=translate, scale=scale, shear=shear)
  589. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  590. fill = _get_fill(self._fill, type(inpt))
  591. return self._call_kernel(
  592. F.affine,
  593. inpt,
  594. **params,
  595. interpolation=self.interpolation,
  596. fill=fill,
  597. center=self.center,
  598. )
  599. class RandomCrop(Transform):
  600. """Crop the input at a random location.
  601. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  602. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  603. it can have arbitrary number of leading batch dimensions. For example,
  604. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  605. Args:
  606. size (sequence or int): Desired output size of the crop. If size is an
  607. int instead of sequence like (h, w), a square crop (size, size) is
  608. made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
  609. padding (int or sequence, optional): Optional padding on each border
  610. of the image. Default is None. If a single int is provided this
  611. is used to pad all borders. If sequence of length 2 is provided this is the padding
  612. on left/right and top/bottom respectively. If a sequence of length 4 is provided
  613. this is the padding for the left, top, right and bottom borders respectively.
  614. .. note::
  615. In torchscript mode padding as single int is not supported, use a sequence of
  616. length 1: ``[padding, ]``.
  617. pad_if_needed (boolean, optional): It will pad the image if smaller than the
  618. desired size to avoid raising an exception. Since cropping is done
  619. after padding, the padding seems to be done at a random offset.
  620. fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant.
  621. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  622. Fill value can be also a dictionary mapping data type to the fill value, e.g.
  623. ``fill={tv_tensors.Image: 127, tv_tensors.Mask: 0}`` where ``Image`` will be filled with 127 and
  624. ``Mask`` will be filled with 0.
  625. padding_mode (str, optional): Type of padding. Should be: constant, edge, reflect or symmetric.
  626. Default is constant.
  627. - constant: pads with a constant value, this value is specified with fill
  628. - edge: pads with the last value at the edge of the image.
  629. - reflect: pads with reflection of image without repeating the last value on the edge.
  630. For example, padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
  631. will result in [3, 2, 1, 2, 3, 4, 3, 2]
  632. - symmetric: pads with reflection of image repeating the last value on the edge.
  633. For example, padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
  634. will result in [2, 1, 1, 2, 3, 4, 4, 3]
  635. """
  636. _v1_transform_cls = _transforms.RandomCrop
  637. def _extract_params_for_v1_transform(self) -> Dict[str, Any]:
  638. params = super()._extract_params_for_v1_transform()
  639. if not (params["fill"] is None or isinstance(params["fill"], (int, float))):
  640. raise ValueError(f"{type(self).__name__}() can only be scripted for a scalar `fill`, but got {self.fill}.")
  641. padding = self.padding
  642. if padding is not None:
  643. pad_left, pad_right, pad_top, pad_bottom = padding
  644. padding = [pad_left, pad_top, pad_right, pad_bottom]
  645. params["padding"] = padding
  646. return params
  647. def __init__(
  648. self,
  649. size: Union[int, Sequence[int]],
  650. padding: Optional[Union[int, Sequence[int]]] = None,
  651. pad_if_needed: bool = False,
  652. fill: Union[_FillType, Dict[Union[Type, str], _FillType]] = 0,
  653. padding_mode: Literal["constant", "edge", "reflect", "symmetric"] = "constant",
  654. ) -> None:
  655. super().__init__()
  656. self.size = _setup_size(size, error_msg="Please provide only two dimensions (h, w) for size.")
  657. if pad_if_needed or padding is not None:
  658. if padding is not None:
  659. _check_padding_arg(padding)
  660. _check_padding_mode_arg(padding_mode)
  661. self.padding = F._geometry._parse_pad_padding(padding) if padding else None # type: ignore[arg-type]
  662. self.pad_if_needed = pad_if_needed
  663. self.fill = fill
  664. self._fill = _setup_fill_arg(fill)
  665. self.padding_mode = padding_mode
  666. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  667. padded_height, padded_width = query_size(flat_inputs)
  668. if self.padding is not None:
  669. pad_left, pad_right, pad_top, pad_bottom = self.padding
  670. padded_height += pad_top + pad_bottom
  671. padded_width += pad_left + pad_right
  672. else:
  673. pad_left = pad_right = pad_top = pad_bottom = 0
  674. cropped_height, cropped_width = self.size
  675. if self.pad_if_needed:
  676. if padded_height < cropped_height:
  677. diff = cropped_height - padded_height
  678. pad_top += diff
  679. pad_bottom += diff
  680. padded_height += 2 * diff
  681. if padded_width < cropped_width:
  682. diff = cropped_width - padded_width
  683. pad_left += diff
  684. pad_right += diff
  685. padded_width += 2 * diff
  686. if padded_height < cropped_height or padded_width < cropped_width:
  687. raise ValueError(
  688. f"Required crop size {(cropped_height, cropped_width)} is larger than "
  689. f"{'padded ' if self.padding is not None else ''}input image size {(padded_height, padded_width)}."
  690. )
  691. # We need a different order here than we have in self.padding since this padding will be parsed again in `F.pad`
  692. padding = [pad_left, pad_top, pad_right, pad_bottom]
  693. needs_pad = any(padding)
  694. needs_vert_crop, top = (
  695. (True, int(torch.randint(0, padded_height - cropped_height + 1, size=())))
  696. if padded_height > cropped_height
  697. else (False, 0)
  698. )
  699. needs_horz_crop, left = (
  700. (True, int(torch.randint(0, padded_width - cropped_width + 1, size=())))
  701. if padded_width > cropped_width
  702. else (False, 0)
  703. )
  704. return dict(
  705. needs_crop=needs_vert_crop or needs_horz_crop,
  706. top=top,
  707. left=left,
  708. height=cropped_height,
  709. width=cropped_width,
  710. needs_pad=needs_pad,
  711. padding=padding,
  712. )
  713. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  714. if params["needs_pad"]:
  715. fill = _get_fill(self._fill, type(inpt))
  716. inpt = self._call_kernel(F.pad, inpt, padding=params["padding"], fill=fill, padding_mode=self.padding_mode)
  717. if params["needs_crop"]:
  718. inpt = self._call_kernel(
  719. F.crop, inpt, top=params["top"], left=params["left"], height=params["height"], width=params["width"]
  720. )
  721. return inpt
  722. class RandomPerspective(_RandomApplyTransform):
  723. """Perform a random perspective transformation of the input with a given probability.
  724. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  725. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  726. it can have arbitrary number of leading batch dimensions. For example,
  727. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  728. Args:
  729. distortion_scale (float, optional): argument to control the degree of distortion and ranges from 0 to 1.
  730. Default is 0.5.
  731. p (float, optional): probability of the input being transformed. Default is 0.5.
  732. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  733. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
  734. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
  735. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  736. fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant.
  737. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  738. Fill value can be also a dictionary mapping data type to the fill value, e.g.
  739. ``fill={tv_tensors.Image: 127, tv_tensors.Mask: 0}`` where ``Image`` will be filled with 127 and
  740. ``Mask`` will be filled with 0.
  741. """
  742. _v1_transform_cls = _transforms.RandomPerspective
  743. def __init__(
  744. self,
  745. distortion_scale: float = 0.5,
  746. p: float = 0.5,
  747. interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
  748. fill: Union[_FillType, Dict[Union[Type, str], _FillType]] = 0,
  749. ) -> None:
  750. super().__init__(p=p)
  751. if not (0 <= distortion_scale <= 1):
  752. raise ValueError("Argument distortion_scale value should be between 0 and 1")
  753. self.distortion_scale = distortion_scale
  754. self.interpolation = interpolation
  755. self.fill = fill
  756. self._fill = _setup_fill_arg(fill)
  757. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  758. height, width = query_size(flat_inputs)
  759. distortion_scale = self.distortion_scale
  760. half_height = height // 2
  761. half_width = width // 2
  762. bound_height = int(distortion_scale * half_height) + 1
  763. bound_width = int(distortion_scale * half_width) + 1
  764. topleft = [
  765. int(torch.randint(0, bound_width, size=(1,))),
  766. int(torch.randint(0, bound_height, size=(1,))),
  767. ]
  768. topright = [
  769. int(torch.randint(width - bound_width, width, size=(1,))),
  770. int(torch.randint(0, bound_height, size=(1,))),
  771. ]
  772. botright = [
  773. int(torch.randint(width - bound_width, width, size=(1,))),
  774. int(torch.randint(height - bound_height, height, size=(1,))),
  775. ]
  776. botleft = [
  777. int(torch.randint(0, bound_width, size=(1,))),
  778. int(torch.randint(height - bound_height, height, size=(1,))),
  779. ]
  780. startpoints = [[0, 0], [width - 1, 0], [width - 1, height - 1], [0, height - 1]]
  781. endpoints = [topleft, topright, botright, botleft]
  782. perspective_coeffs = _get_perspective_coeffs(startpoints, endpoints)
  783. return dict(coefficients=perspective_coeffs)
  784. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  785. fill = _get_fill(self._fill, type(inpt))
  786. return self._call_kernel(
  787. F.perspective,
  788. inpt,
  789. startpoints=None,
  790. endpoints=None,
  791. fill=fill,
  792. interpolation=self.interpolation,
  793. **params,
  794. )
  795. class ElasticTransform(Transform):
  796. """Transform the input with elastic transformations.
  797. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  798. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  799. it can have arbitrary number of leading batch dimensions. For example,
  800. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  801. Given alpha and sigma, it will generate displacement
  802. vectors for all pixels based on random offsets. Alpha controls the strength
  803. and sigma controls the smoothness of the displacements.
  804. The displacements are added to an identity grid and the resulting grid is
  805. used to transform the input.
  806. .. note::
  807. Implementation to transform bounding boxes is approximative (not exact).
  808. We construct an approximation of the inverse grid as ``inverse_grid = identity - displacement``.
  809. This is not an exact inverse of the grid used to transform images, i.e. ``grid = identity + displacement``.
  810. Our assumption is that ``displacement * displacement`` is small and can be ignored.
  811. Large displacements would lead to large errors in the approximation.
  812. Applications:
  813. Randomly transforms the morphology of objects in images and produces a
  814. see-through-water-like effect.
  815. Args:
  816. alpha (float or sequence of floats, optional): Magnitude of displacements. Default is 50.0.
  817. sigma (float or sequence of floats, optional): Smoothness of displacements. Default is 5.0.
  818. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  819. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
  820. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
  821. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  822. fill (number or tuple or dict, optional): Pixel fill value used when the ``padding_mode`` is constant.
  823. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively.
  824. Fill value can be also a dictionary mapping data type to the fill value, e.g.
  825. ``fill={tv_tensors.Image: 127, tv_tensors.Mask: 0}`` where ``Image`` will be filled with 127 and
  826. ``Mask`` will be filled with 0.
  827. """
  828. _v1_transform_cls = _transforms.ElasticTransform
  829. def __init__(
  830. self,
  831. alpha: Union[float, Sequence[float]] = 50.0,
  832. sigma: Union[float, Sequence[float]] = 5.0,
  833. interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
  834. fill: Union[_FillType, Dict[Union[Type, str], _FillType]] = 0,
  835. ) -> None:
  836. super().__init__()
  837. self.alpha = _setup_number_or_seq(alpha, "alpha")
  838. self.sigma = _setup_number_or_seq(sigma, "sigma")
  839. self.interpolation = interpolation
  840. self.fill = fill
  841. self._fill = _setup_fill_arg(fill)
  842. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  843. size = list(query_size(flat_inputs))
  844. dx = torch.rand([1, 1] + size) * 2 - 1
  845. if self.sigma[0] > 0.0:
  846. kx = int(8 * self.sigma[0] + 1)
  847. # if kernel size is even we have to make it odd
  848. if kx % 2 == 0:
  849. kx += 1
  850. dx = self._call_kernel(F.gaussian_blur, dx, [kx, kx], list(self.sigma))
  851. dx = dx * self.alpha[0] / size[0]
  852. dy = torch.rand([1, 1] + size) * 2 - 1
  853. if self.sigma[1] > 0.0:
  854. ky = int(8 * self.sigma[1] + 1)
  855. # if kernel size is even we have to make it odd
  856. if ky % 2 == 0:
  857. ky += 1
  858. dy = self._call_kernel(F.gaussian_blur, dy, [ky, ky], list(self.sigma))
  859. dy = dy * self.alpha[1] / size[1]
  860. displacement = torch.concat([dx, dy], 1).permute([0, 2, 3, 1]) # 1 x H x W x 2
  861. return dict(displacement=displacement)
  862. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  863. fill = _get_fill(self._fill, type(inpt))
  864. return self._call_kernel(
  865. F.elastic,
  866. inpt,
  867. **params,
  868. fill=fill,
  869. interpolation=self.interpolation,
  870. )
  871. class RandomIoUCrop(Transform):
  872. """Random IoU crop transformation from
  873. `"SSD: Single Shot MultiBox Detector" <https://arxiv.org/abs/1512.02325>`_.
  874. This transformation requires an image or video data and ``tv_tensors.BoundingBoxes`` in the input.
  875. .. warning::
  876. In order to properly remove the bounding boxes below the IoU threshold, `RandomIoUCrop`
  877. must be followed by :class:`~torchvision.transforms.v2.SanitizeBoundingBoxes`, either immediately
  878. after or later in the transforms pipeline.
  879. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  880. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  881. it can have arbitrary number of leading batch dimensions. For example,
  882. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  883. Args:
  884. min_scale (float, optional): Minimum factors to scale the input size.
  885. max_scale (float, optional): Maximum factors to scale the input size.
  886. min_aspect_ratio (float, optional): Minimum aspect ratio for the cropped image or video.
  887. max_aspect_ratio (float, optional): Maximum aspect ratio for the cropped image or video.
  888. sampler_options (list of float, optional): List of minimal IoU (Jaccard) overlap between all the boxes and
  889. a cropped image or video. Default, ``None`` which corresponds to ``[0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0]``
  890. trials (int, optional): Number of trials to find a crop for a given value of minimal IoU (Jaccard) overlap.
  891. Default, 40.
  892. """
  893. def __init__(
  894. self,
  895. min_scale: float = 0.3,
  896. max_scale: float = 1.0,
  897. min_aspect_ratio: float = 0.5,
  898. max_aspect_ratio: float = 2.0,
  899. sampler_options: Optional[List[float]] = None,
  900. trials: int = 40,
  901. ):
  902. super().__init__()
  903. # Configuration similar to https://github.com/weiliu89/caffe/blob/ssd/examples/ssd/ssd_coco.py#L89-L174
  904. self.min_scale = min_scale
  905. self.max_scale = max_scale
  906. self.min_aspect_ratio = min_aspect_ratio
  907. self.max_aspect_ratio = max_aspect_ratio
  908. if sampler_options is None:
  909. sampler_options = [0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0]
  910. self.options = sampler_options
  911. self.trials = trials
  912. def _check_inputs(self, flat_inputs: List[Any]) -> None:
  913. if not (
  914. has_all(flat_inputs, tv_tensors.BoundingBoxes)
  915. and has_any(flat_inputs, PIL.Image.Image, tv_tensors.Image, is_pure_tensor)
  916. ):
  917. raise TypeError(
  918. f"{type(self).__name__}() requires input sample to contain tensor or PIL images "
  919. "and bounding boxes. Sample can also contain masks."
  920. )
  921. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  922. orig_h, orig_w = query_size(flat_inputs)
  923. bboxes = get_bounding_boxes(flat_inputs)
  924. while True:
  925. # sample an option
  926. idx = int(torch.randint(low=0, high=len(self.options), size=(1,)))
  927. min_jaccard_overlap = self.options[idx]
  928. if min_jaccard_overlap >= 1.0: # a value larger than 1 encodes the leave as-is option
  929. return dict()
  930. for _ in range(self.trials):
  931. # check the aspect ratio limitations
  932. r = self.min_scale + (self.max_scale - self.min_scale) * torch.rand(2)
  933. new_w = int(orig_w * r[0])
  934. new_h = int(orig_h * r[1])
  935. aspect_ratio = new_w / new_h
  936. if not (self.min_aspect_ratio <= aspect_ratio <= self.max_aspect_ratio):
  937. continue
  938. # check for 0 area crops
  939. r = torch.rand(2)
  940. left = int((orig_w - new_w) * r[0])
  941. top = int((orig_h - new_h) * r[1])
  942. right = left + new_w
  943. bottom = top + new_h
  944. if left == right or top == bottom:
  945. continue
  946. # check for any valid boxes with centers within the crop area
  947. xyxy_bboxes = F.convert_bounding_box_format(
  948. bboxes.as_subclass(torch.Tensor),
  949. bboxes.format,
  950. tv_tensors.BoundingBoxFormat.XYXY,
  951. )
  952. cx = 0.5 * (xyxy_bboxes[..., 0] + xyxy_bboxes[..., 2])
  953. cy = 0.5 * (xyxy_bboxes[..., 1] + xyxy_bboxes[..., 3])
  954. is_within_crop_area = (left < cx) & (cx < right) & (top < cy) & (cy < bottom)
  955. if not is_within_crop_area.any():
  956. continue
  957. # check at least 1 box with jaccard limitations
  958. xyxy_bboxes = xyxy_bboxes[is_within_crop_area]
  959. ious = box_iou(
  960. xyxy_bboxes,
  961. torch.tensor([[left, top, right, bottom]], dtype=xyxy_bboxes.dtype, device=xyxy_bboxes.device),
  962. )
  963. if ious.max() < min_jaccard_overlap:
  964. continue
  965. return dict(top=top, left=left, height=new_h, width=new_w, is_within_crop_area=is_within_crop_area)
  966. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  967. if len(params) < 1:
  968. return inpt
  969. output = self._call_kernel(
  970. F.crop, inpt, top=params["top"], left=params["left"], height=params["height"], width=params["width"]
  971. )
  972. if isinstance(output, tv_tensors.BoundingBoxes):
  973. # We "mark" the invalid boxes as degenreate, and they can be
  974. # removed by a later call to SanitizeBoundingBoxes()
  975. output[~params["is_within_crop_area"]] = 0
  976. return output
  977. class ScaleJitter(Transform):
  978. """Perform Large Scale Jitter on the input according to
  979. `"Simple Copy-Paste is a Strong Data Augmentation Method for Instance Segmentation" <https://arxiv.org/abs/2012.07177>`_.
  980. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  981. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  982. it can have arbitrary number of leading batch dimensions. For example,
  983. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  984. Args:
  985. target_size (tuple of int): Target size. This parameter defines base scale for jittering,
  986. e.g. ``min(target_size[0] / width, target_size[1] / height)``.
  987. scale_range (tuple of float, optional): Minimum and maximum of the scale range. Default, ``(0.1, 2.0)``.
  988. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  989. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
  990. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.NEAREST_EXACT``,
  991. ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported.
  992. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  993. antialias (bool, optional): Whether to apply antialiasing.
  994. It only affects **tensors** with bilinear or bicubic modes and it is
  995. ignored otherwise: on PIL images, antialiasing is always applied on
  996. bilinear or bicubic modes; on other modes (for PIL images and
  997. tensors), antialiasing makes no sense and this parameter is ignored.
  998. Possible values are:
  999. - ``True`` (default): will apply antialiasing for bilinear or bicubic modes.
  1000. Other mode aren't affected. This is probably what you want to use.
  1001. - ``False``: will not apply antialiasing for tensors on any mode. PIL
  1002. images are still antialiased on bilinear or bicubic modes, because
  1003. PIL doesn't support no antialias.
  1004. - ``None``: equivalent to ``False`` for tensors and ``True`` for
  1005. PIL images. This value exists for legacy reasons and you probably
  1006. don't want to use it unless you really know what you are doing.
  1007. The default value changed from ``None`` to ``True`` in
  1008. v0.17, for the PIL and Tensor backends to be consistent.
  1009. """
  1010. def __init__(
  1011. self,
  1012. target_size: Tuple[int, int],
  1013. scale_range: Tuple[float, float] = (0.1, 2.0),
  1014. interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
  1015. antialias: Optional[bool] = True,
  1016. ):
  1017. super().__init__()
  1018. self.target_size = target_size
  1019. self.scale_range = scale_range
  1020. self.interpolation = interpolation
  1021. self.antialias = antialias
  1022. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  1023. orig_height, orig_width = query_size(flat_inputs)
  1024. scale = self.scale_range[0] + torch.rand(1) * (self.scale_range[1] - self.scale_range[0])
  1025. r = min(self.target_size[1] / orig_height, self.target_size[0] / orig_width) * scale
  1026. new_width = int(orig_width * r)
  1027. new_height = int(orig_height * r)
  1028. return dict(size=(new_height, new_width))
  1029. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  1030. return self._call_kernel(
  1031. F.resize, inpt, size=params["size"], interpolation=self.interpolation, antialias=self.antialias
  1032. )
  1033. class RandomShortestSize(Transform):
  1034. """Randomly resize the input.
  1035. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  1036. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  1037. it can have arbitrary number of leading batch dimensions. For example,
  1038. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  1039. Args:
  1040. min_size (int or sequence of int): Minimum spatial size. Single integer value or a sequence of integer values.
  1041. max_size (int, optional): Maximum spatial size. Default, None.
  1042. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  1043. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
  1044. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.NEAREST_EXACT``,
  1045. ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported.
  1046. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  1047. antialias (bool, optional): Whether to apply antialiasing.
  1048. It only affects **tensors** with bilinear or bicubic modes and it is
  1049. ignored otherwise: on PIL images, antialiasing is always applied on
  1050. bilinear or bicubic modes; on other modes (for PIL images and
  1051. tensors), antialiasing makes no sense and this parameter is ignored.
  1052. Possible values are:
  1053. - ``True`` (default): will apply antialiasing for bilinear or bicubic modes.
  1054. Other mode aren't affected. This is probably what you want to use.
  1055. - ``False``: will not apply antialiasing for tensors on any mode. PIL
  1056. images are still antialiased on bilinear or bicubic modes, because
  1057. PIL doesn't support no antialias.
  1058. - ``None``: equivalent to ``False`` for tensors and ``True`` for
  1059. PIL images. This value exists for legacy reasons and you probably
  1060. don't want to use it unless you really know what you are doing.
  1061. The default value changed from ``None`` to ``True`` in
  1062. v0.17, for the PIL and Tensor backends to be consistent.
  1063. """
  1064. def __init__(
  1065. self,
  1066. min_size: Union[List[int], Tuple[int], int],
  1067. max_size: Optional[int] = None,
  1068. interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
  1069. antialias: Optional[bool] = True,
  1070. ):
  1071. super().__init__()
  1072. self.min_size = [min_size] if isinstance(min_size, int) else list(min_size)
  1073. self.max_size = max_size
  1074. self.interpolation = interpolation
  1075. self.antialias = antialias
  1076. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  1077. orig_height, orig_width = query_size(flat_inputs)
  1078. min_size = self.min_size[int(torch.randint(len(self.min_size), ()))]
  1079. r = min_size / min(orig_height, orig_width)
  1080. if self.max_size is not None:
  1081. r = min(r, self.max_size / max(orig_height, orig_width))
  1082. new_width = int(orig_width * r)
  1083. new_height = int(orig_height * r)
  1084. return dict(size=(new_height, new_width))
  1085. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  1086. return self._call_kernel(
  1087. F.resize, inpt, size=params["size"], interpolation=self.interpolation, antialias=self.antialias
  1088. )
  1089. class RandomResize(Transform):
  1090. """Randomly resize the input.
  1091. This transformation can be used together with ``RandomCrop`` as data augmentations to train
  1092. models on image segmentation task.
  1093. Output spatial size is randomly sampled from the interval ``[min_size, max_size]``:
  1094. .. code-block:: python
  1095. size = uniform_sample(min_size, max_size)
  1096. output_width = size
  1097. output_height = size
  1098. If the input is a :class:`torch.Tensor` or a ``TVTensor`` (e.g. :class:`~torchvision.tv_tensors.Image`,
  1099. :class:`~torchvision.tv_tensors.Video`, :class:`~torchvision.tv_tensors.BoundingBoxes` etc.)
  1100. it can have arbitrary number of leading batch dimensions. For example,
  1101. the image can have ``[..., C, H, W]`` shape. A bounding box can have ``[..., 4]`` shape.
  1102. Args:
  1103. min_size (int): Minimum output size for random sampling
  1104. max_size (int): Maximum output size for random sampling
  1105. interpolation (InterpolationMode, optional): Desired interpolation enum defined by
  1106. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
  1107. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.NEAREST_EXACT``,
  1108. ``InterpolationMode.BILINEAR`` and ``InterpolationMode.BICUBIC`` are supported.
  1109. The corresponding Pillow integer constants, e.g. ``PIL.Image.BILINEAR`` are accepted as well.
  1110. antialias (bool, optional): Whether to apply antialiasing.
  1111. It only affects **tensors** with bilinear or bicubic modes and it is
  1112. ignored otherwise: on PIL images, antialiasing is always applied on
  1113. bilinear or bicubic modes; on other modes (for PIL images and
  1114. tensors), antialiasing makes no sense and this parameter is ignored.
  1115. Possible values are:
  1116. - ``True`` (default): will apply antialiasing for bilinear or bicubic modes.
  1117. Other mode aren't affected. This is probably what you want to use.
  1118. - ``False``: will not apply antialiasing for tensors on any mode. PIL
  1119. images are still antialiased on bilinear or bicubic modes, because
  1120. PIL doesn't support no antialias.
  1121. - ``None``: equivalent to ``False`` for tensors and ``True`` for
  1122. PIL images. This value exists for legacy reasons and you probably
  1123. don't want to use it unless you really know what you are doing.
  1124. The default value changed from ``None`` to ``True`` in
  1125. v0.17, for the PIL and Tensor backends to be consistent.
  1126. """
  1127. def __init__(
  1128. self,
  1129. min_size: int,
  1130. max_size: int,
  1131. interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
  1132. antialias: Optional[bool] = True,
  1133. ) -> None:
  1134. super().__init__()
  1135. self.min_size = min_size
  1136. self.max_size = max_size
  1137. self.interpolation = interpolation
  1138. self.antialias = antialias
  1139. def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
  1140. size = int(torch.randint(self.min_size, self.max_size, ()))
  1141. return dict(size=[size])
  1142. def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
  1143. return self._call_kernel(
  1144. F.resize, inpt, params["size"], interpolation=self.interpolation, antialias=self.antialias
  1145. )