helpers.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. from functools import wraps
  18. from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  19. import torch
  20. from torch.distributions import Beta, Uniform
  21. from kornia.core import Tensor, as_tensor
  22. from kornia.geometry.boxes import Boxes
  23. from kornia.geometry.keypoints import Keypoints
  24. from kornia.utils import _extract_device_dtype
  25. def _validate_input(f: Callable[..., Any]) -> Callable[..., Any]:
  26. r"""Validate the 2D input of the wrapped function.
  27. Args:
  28. f: a function that takes the first argument as tensor.
  29. Returns:
  30. the wrapped function after input is validated.
  31. """
  32. @wraps(f)
  33. def wrapper(input: Tensor, *args: Any, **kwargs: Any) -> Any:
  34. if not torch.is_tensor(input):
  35. raise TypeError(f"Input type is not a Tensor. Got {type(input)}")
  36. _validate_shape(input.shape, required_shapes=("BCHW",))
  37. _validate_input_dtype(input, accepted_dtypes=[torch.float16, torch.float32, torch.float64])
  38. return f(input, *args, **kwargs)
  39. return wrapper
  40. def _validate_input3d(f: Callable[..., Any]) -> Callable[..., Any]:
  41. r"""Validate the 3D input of the wrapped function.
  42. Args:
  43. f: a function that takes the first argument as tensor.
  44. Returns:
  45. the wrapped function after input is validated.
  46. """
  47. @wraps(f)
  48. def wrapper(input: Tensor, *args: Any, **kwargs: Any) -> Any:
  49. if not torch.is_tensor(input):
  50. raise TypeError(f"Input type is not a Tensor. Got {type(input)}")
  51. input_shape = len(input.shape)
  52. if input_shape != 5:
  53. raise AssertionError(f"Expect input of 5 dimensions, got {input_shape} instead")
  54. _validate_input_dtype(input, accepted_dtypes=[torch.float16, torch.float32, torch.float64])
  55. return f(input, *args, **kwargs)
  56. return wrapper
  57. def _infer_batch_shape(input: Union[Tensor, Tuple[Tensor, Tensor]]) -> torch.Size:
  58. r"""Infer input shape.
  59. Input may be either (tensor,) or (tensor, transform_matrix)
  60. """
  61. if isinstance(input, tuple):
  62. tensor = _transform_input(input[0])
  63. else:
  64. tensor = _transform_input(input)
  65. return tensor.shape
  66. def _infer_batch_shape3d(input: Union[Tensor, Tuple[Tensor, Tensor]]) -> torch.Size:
  67. r"""Infer input shape.
  68. Input may be either (tensor,) or (tensor, transform_matrix)
  69. """
  70. if isinstance(input, tuple):
  71. tensor = _transform_input3d(input[0])
  72. else:
  73. tensor = _transform_input3d(input)
  74. return tensor.shape
  75. def _transform_input_by_shape(input: Tensor, reference_shape: Tensor, match_channel: bool = True) -> Tensor:
  76. """Reshape an input tensor to have the same dimensions as the reference_shape.
  77. Arguments:
  78. input: tensor to be transformed
  79. reference_shape: shape used as reference
  80. match_channel: if True, C_{src} == C_{ref}. otherwise, no constrain. C =1 by default
  81. """
  82. B = reference_shape[-4] if len(reference_shape) >= 4 else None
  83. C = reference_shape[-3] if len(reference_shape) >= 3 else None
  84. if len(input.shape) == 2:
  85. input = input.unsqueeze(0)
  86. if len(input.shape) == 3:
  87. # If the first dim matches within the batch_size, add a `C` dim
  88. # Useful to handler Masks without `C` dimensions
  89. input = input.unsqueeze(1) if B == input.shape[-3] else input.unsqueeze(0)
  90. if match_channel and C:
  91. if not input.shape[-3] == C:
  92. raise ValueError("The C dimension of tensor did not match with the reference tensor.")
  93. elif match_channel and C is None:
  94. raise ValueError("The reference tensor do not have a C dimension!")
  95. return input
  96. def _transform_input3d_by_shape(input: Tensor, reference_shape: Tensor, match_channel: bool = True) -> Tensor:
  97. """Reshape an input tensor to have the same dimensions as the reference_shape.
  98. Arguments:
  99. input: tensor to be transformed
  100. reference_shape: shape used as reference
  101. match_channel: if True, C_{src} == C_{ref}. otherwise, no constrain. C =1 by default
  102. """
  103. B = reference_shape[-5] if len(reference_shape) >= 5 else None
  104. C = reference_shape[-4] if len(reference_shape) >= 4 else None
  105. if len(input.shape) == 3:
  106. input = input.unsqueeze(0)
  107. if len(input.shape) == 4 and B == input.shape[-4]:
  108. # If the first dim matches within the batch_size, add a `C` dim
  109. # Useful to handler Masks without `C` dimensions
  110. input = input.unsqueeze(2)
  111. if match_channel and C:
  112. if not input.shape[-4] == C:
  113. raise ValueError("The C dimension of tensor did not match with the reference tensor.")
  114. elif match_channel and C is None:
  115. raise ValueError("The reference tensor do not have a C dimension!")
  116. return input
  117. def _transform_input(input: Tensor) -> Tensor:
  118. r"""Reshape an input tensor to be (*, C, H, W). Accept either (H, W), (C, H, W) or (*, C, H, W).
  119. Args:
  120. input: Tensor
  121. Returns:
  122. Tensor
  123. """
  124. if not torch.is_tensor(input):
  125. raise TypeError(f"Input type is not a Tensor. Got {type(input)}")
  126. if len(input.shape) not in [2, 3, 4]:
  127. raise ValueError(f"Input size must have a shape of either (H, W), (C, H, W) or (*, C, H, W). Got {input.shape}")
  128. if len(input.shape) == 2:
  129. input = input.unsqueeze(0)
  130. if len(input.shape) == 3:
  131. input = input.unsqueeze(0)
  132. return input
  133. def _transform_input3d(input: Tensor) -> Tensor:
  134. r"""Reshape an input tensor to be (*, C, D, H, W). Accept either (D, H, W), (C, D, H, W) or (*, C, D, H, W).
  135. Args:
  136. input: Tensor
  137. Returns:
  138. Tensor
  139. """
  140. if not torch.is_tensor(input):
  141. raise TypeError(f"Input type is not a Tensor. Got {type(input)}")
  142. if len(input.shape) not in [3, 4, 5]:
  143. raise ValueError(
  144. f"Input size must have a shape of either (D, H, W), (C, D, H, W) or (*, C, D, H, W). Got {input.shape}"
  145. )
  146. if len(input.shape) == 3:
  147. input = input.unsqueeze(0)
  148. if len(input.shape) == 4:
  149. input = input.unsqueeze(0)
  150. return input
  151. def _validate_input_dtype(input: Tensor, accepted_dtypes: List[torch.dtype]) -> None:
  152. r"""Check if the dtype of the input tensor is in the range of accepted_dtypes.
  153. Args:
  154. input: Tensor
  155. accepted_dtypes: List. e.g. [torch.float32, torch.float64]
  156. """
  157. if input.dtype not in accepted_dtypes:
  158. raise TypeError(f"Expected input of {accepted_dtypes}. Got {input.dtype}")
  159. def _transform_output_shape(
  160. output: Tensor, shape: Tuple[int, ...], *, reference_shape: Optional[Tensor] = None
  161. ) -> Tensor:
  162. r"""Collapse the broadcasted batch dimensions an input tensor to be the specified shape.
  163. Args:
  164. output: Tensor
  165. shape: List/tuple of int
  166. reference_shape: Tensor representation of shape to control which dimensions are collapsed.
  167. Returns:
  168. Tensor
  169. """
  170. out_tensor = output.clone()
  171. for dim in range(len(out_tensor.shape) - len(shape)):
  172. idx = 0
  173. if reference_shape is not None and out_tensor.shape[0] == reference_shape[0] != 1 and len(shape) > 2:
  174. idx = 1
  175. if out_tensor.shape[idx] != 1:
  176. raise AssertionError(f"Dimension {dim} of input is expected to be 1, got {out_tensor.shape[idx]}")
  177. out_tensor = out_tensor.squeeze(idx)
  178. return out_tensor
  179. def _validate_shape(shape: Union[Tuple[int, ...], torch.Size], required_shapes: Tuple[str, ...] = ("BCHW",)) -> None:
  180. r"""Check if the dtype of the input tensor is in the range of accepted_dtypes.
  181. Args:
  182. shape: tensor shape
  183. required_shapes: List. e.g. ["BCHW", "BCDHW"]
  184. """
  185. passed = False
  186. for required_shape in required_shapes:
  187. if len(shape) == len(required_shape):
  188. passed = True
  189. break
  190. if not passed:
  191. raise TypeError(f"Expected input shape in {required_shape}. Got {shape}.")
  192. def _validate_input_shape(input: Tensor, channel_index: int, number: int) -> bool:
  193. r"""Validate if an input has the right shape.
  194. e.g. to check if an input is channel first.
  195. If channel first, the second channel of an RGB input shall be fixed to 3. To verify using:
  196. _validate_input_shape(input, 1, 3)
  197. Args:
  198. input: Tensor
  199. channel_index: int
  200. number: int
  201. Returns:
  202. bool
  203. """
  204. return input.shape[channel_index] == number
  205. def _adapted_rsampling(
  206. shape: Union[Tuple[int, ...], torch.Size],
  207. dist: torch.distributions.Distribution,
  208. same_on_batch: Optional[bool] = False,
  209. ) -> Tensor:
  210. r"""Sample from a uniform reparameterized sampling function that accepts 'same_on_batch'.
  211. If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]). By default,
  212. same_on_batch is set to False.
  213. """
  214. if isinstance(shape, tuple):
  215. shape = torch.Size(shape)
  216. if same_on_batch:
  217. rsample_size = torch.Size((1, *shape[1:]))
  218. rsample = dist.rsample(rsample_size)
  219. return rsample.repeat(shape[0], *[1] * (len(rsample.shape) - 1))
  220. return dist.rsample(shape)
  221. def _adapted_sampling(
  222. shape: Union[Tuple[int, ...], torch.Size],
  223. dist: torch.distributions.Distribution,
  224. same_on_batch: Optional[bool] = False,
  225. ) -> Tensor:
  226. r"""Sample from a uniform sampling function that accepts 'same_on_batch'.
  227. If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]). By default,
  228. same_on_batch is set to False.
  229. """
  230. if isinstance(shape, tuple):
  231. shape = torch.Size(shape)
  232. if same_on_batch:
  233. return dist.sample(torch.Size((1, *shape[1:]))).repeat(shape[0], *[1] * (len(shape) - 1))
  234. return dist.sample(shape)
  235. def _adapted_uniform(
  236. shape: Union[Tuple[int, ...], torch.Size],
  237. low: Union[float, Tensor],
  238. high: Union[float, Tensor],
  239. same_on_batch: bool = False,
  240. ) -> Tensor:
  241. r"""Sample from a uniform sampling function that accepts 'same_on_batch'.
  242. If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]). By default,
  243. same_on_batch is set to False.
  244. By default, sampling happens on the default device and dtype. If low/high is a tensor, sampling will happen in the
  245. same device/dtype as low/high tensor.
  246. """
  247. device, dtype = _extract_device_dtype(
  248. [low if isinstance(low, Tensor) else None, high if isinstance(high, Tensor) else None]
  249. )
  250. low = as_tensor(low, device=device, dtype=dtype)
  251. high = as_tensor(high, device=device, dtype=dtype)
  252. # validate_args=False to fix pytorch 1.7.1 error:
  253. # ValueError: Uniform is not defined when low>= high.
  254. dist = Uniform(low, high, validate_args=False)
  255. return _adapted_rsampling(shape, dist, same_on_batch)
  256. def _adapted_beta(
  257. shape: Union[Tuple[int, ...], torch.Size],
  258. a: Union[float, Tensor],
  259. b: Union[float, Tensor],
  260. same_on_batch: bool = False,
  261. ) -> Tensor:
  262. r"""Sample from a beta sampling function that accepts 'same_on_batch'.
  263. If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]). By default,
  264. same_on_batch is set to False.
  265. By default, sampling happens on the default device and dtype. If a/b is a tensor, sampling will happen in the same
  266. device/dtype as a/b tensor.
  267. """
  268. device, dtype = _extract_device_dtype([a if isinstance(a, Tensor) else None, b if isinstance(b, Tensor) else None])
  269. a = as_tensor(a, device=device, dtype=dtype)
  270. b = as_tensor(b, device=device, dtype=dtype)
  271. dist = Beta(a, b, validate_args=False)
  272. return _adapted_rsampling(shape, dist, same_on_batch)
  273. def _shape_validation(param: Tensor, shape: Union[Tuple[int, ...], List[int]], name: str) -> None:
  274. if param.shape != torch.Size(shape):
  275. raise AssertionError(f"Invalid shape for {name}. Expected {shape}. Got {param.shape}")
  276. def deepcopy_dict(params: Dict[str, Any]) -> Dict[str, Any]:
  277. """Perform deep copy on any dict.
  278. Support tensor copying here.
  279. """
  280. out = {}
  281. for k, v in params.items():
  282. # NOTE: Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol
  283. if isinstance(v, Tensor):
  284. out.update({k: v.clone()})
  285. else:
  286. out.update({k: v})
  287. return out
  288. def override_parameters(
  289. params: Dict[str, Any],
  290. params_override: Optional[Dict[str, Any]] = None,
  291. if_none_exist: str = "ignore",
  292. in_place: bool = False,
  293. ) -> Dict[str, Any]:
  294. """Override params dict w.r.t params_override.
  295. Args:
  296. params: source parameters.
  297. params_override: key-values to override the source parameters.
  298. if_none_exist: behaviour if the key in `params_override` does not exist in `params`.
  299. 'raise' | 'ignore'.
  300. in_place: if to override in-place or not.
  301. """
  302. if params_override is None:
  303. return params
  304. out = params if in_place else deepcopy_dict(params)
  305. for k, v in params_override.items():
  306. if k in params_override:
  307. out[k] = v
  308. elif if_none_exist == "ignore":
  309. pass
  310. elif if_none_exist == "raise":
  311. raise RuntimeError(f"Param `{k}` not existed in `{params_override}`.")
  312. else:
  313. raise ValueError(f"`{if_none_exist}` is not a valid option.")
  314. return out
  315. def preprocess_boxes(input: Union[Tensor, Boxes], mode: str = "vertices_plus") -> Boxes:
  316. r"""Preprocess input boxes.
  317. Args:
  318. input: 2D boxes, shape of :math:`(N, 4, 2)`, :math:`(B, N, 4, 2)` or a list of :math:`(N, 4, 2)`.
  319. See below for more details.
  320. mode: The format in which the boxes are provided.
  321. * 'xyxy': boxes are assumed to be in the format ``xmin, ymin, xmax, ymax`` where ``width = xmax - xmin``
  322. and ``height = ymax - ymin``. With shape :math:`(N, 4)`, :math:`(B, N, 4)`.
  323. * 'xyxy_plus': similar to 'xyxy' mode but where box width and length are defined as
  324. ``width = xmax - xmin + 1`` and ``height = ymax - ymin + 1``.
  325. With shape :math:`(N, 4)`, :math:`(B, N, 4)`.
  326. * 'xywh': boxes are assumed to be in the format ``xmin, ymin, width, height`` where
  327. ``width = xmax - xmin`` and ``height = ymax - ymin``. With shape :math:`(N, 4)`, :math:`(B, N, 4)`.
  328. * 'vertices': boxes are defined by their vertices points in the following ``clockwise`` order:
  329. *top-left, top-right, bottom-right, bottom-left*. Vertices coordinates are in (x,y) order. Finally,
  330. box width and height are defined as ``width = xmax - xmin`` and ``height = ymax - ymin``.
  331. With shape :math:`(N, 4, 2)` or :math:`(B, N, 4, 2)`.
  332. * 'vertices_plus': similar to 'vertices' mode but where box width and length are defined as
  333. ``width = xmax - xmin + 1`` and ``height = ymax - ymin + 1``. ymin + 1``.
  334. With shape :math:`(N, 4, 2)` or :math:`(B, N, 4, 2)`.
  335. Note:
  336. **2D boxes format** is defined as a floating data type tensor of shape ``Nx4x2`` or ``BxNx4x2``
  337. where each box is a `quadrilateral <https://en.wikipedia.org/wiki/Quadrilateral>`_ defined by it's 4 vertices
  338. coordinates (A, B, C, D). Coordinates must be in ``x, y`` order. The height and width of a box is defined as
  339. ``width = xmax - xmin + 1`` and ``height = ymax - ymin + 1``. Examples of
  340. `quadrilaterals <https://en.wikipedia.org/wiki/Quadrilateral>`_ are rectangles, rhombus and trapezoids.
  341. """
  342. # TODO: We may allow list here.
  343. # input is BxNx4x2 or Boxes.
  344. if isinstance(input, Tensor):
  345. if not (len(input.shape) == 4 and input.shape[2:] == torch.Size([4, 2])):
  346. raise RuntimeError(f"Only BxNx4x2 tensor is supported. Got {input.shape}.")
  347. input = Boxes.from_tensor(input, mode=mode)
  348. if not isinstance(input, Boxes):
  349. raise RuntimeError(f"Expect `Boxes` type. Got {type(input)}.")
  350. return input
  351. def preprocess_keypoints(input: Union[Tensor, Keypoints]) -> Keypoints:
  352. """Preprocess input keypoints."""
  353. # TODO: We may allow list here.
  354. if isinstance(input, Tensor):
  355. if not (len(input.shape) == 3 and input.shape[1:] == torch.Size([2])):
  356. raise RuntimeError(f"Only BxNx2 tensor is supported. Got {input.shape}.")
  357. input = Keypoints(input, False)
  358. if isinstance(input, Keypoints):
  359. raise RuntimeError(f"Expect `Keypoints` type. Got {type(input)}.")
  360. return input
  361. def preprocess_classes(input: Tensor) -> Tensor:
  362. """Preprocess input class tags."""
  363. # TODO: We may allow list here.
  364. return input
  365. class MultiprocessWrapper:
  366. """When used as a base class, makes the class work with the 'spawn' multiprocessing context."""
  367. def __init__(self, *args: Any, **kwargs: Any) -> None:
  368. args = tuple(arg.clone() if isinstance(arg, torch.Tensor) else arg for arg in args)
  369. kwargs = {key: val.clone() if isinstance(val, torch.Tensor) else val for key, val in kwargs.items()}
  370. super().__init__(*args, **kwargs)