common_utils.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. import contextlib
  2. import functools
  3. import itertools
  4. import os
  5. import pathlib
  6. import random
  7. import re
  8. import shutil
  9. import sys
  10. import tempfile
  11. import warnings
  12. from subprocess import CalledProcessError, check_output, STDOUT
  13. import numpy as np
  14. import PIL.Image
  15. import pytest
  16. import torch
  17. import torch.testing
  18. from PIL import Image
  19. from torch.testing._comparison import BooleanPair, NonePair, not_close_error_metas, NumberPair, TensorLikePair
  20. from torchvision import io, tv_tensors
  21. from torchvision.transforms._functional_tensor import _max_value as get_max_value
  22. from torchvision.transforms.v2.functional import to_image, to_pil_image
  23. IN_OSS_CI = any(os.getenv(var) == "true" for var in ["CIRCLECI", "GITHUB_ACTIONS"])
  24. IN_RE_WORKER = os.environ.get("INSIDE_RE_WORKER") is not None
  25. IN_FBCODE = os.environ.get("IN_FBCODE_TORCHVISION") == "1"
  26. CUDA_NOT_AVAILABLE_MSG = "CUDA device not available"
  27. MPS_NOT_AVAILABLE_MSG = "MPS device not available"
  28. OSS_CI_GPU_NO_CUDA_MSG = "We're in an OSS GPU machine, and this test doesn't need cuda."
  29. @contextlib.contextmanager
  30. def get_tmp_dir(src=None, **kwargs):
  31. tmp_dir = tempfile.mkdtemp(**kwargs)
  32. if src is not None:
  33. os.rmdir(tmp_dir)
  34. shutil.copytree(src, tmp_dir)
  35. try:
  36. yield tmp_dir
  37. finally:
  38. shutil.rmtree(tmp_dir)
  39. def set_rng_seed(seed):
  40. torch.manual_seed(seed)
  41. random.seed(seed)
  42. class MapNestedTensorObjectImpl:
  43. def __init__(self, tensor_map_fn):
  44. self.tensor_map_fn = tensor_map_fn
  45. def __call__(self, object):
  46. if isinstance(object, torch.Tensor):
  47. return self.tensor_map_fn(object)
  48. elif isinstance(object, dict):
  49. mapped_dict = {}
  50. for key, value in object.items():
  51. mapped_dict[self(key)] = self(value)
  52. return mapped_dict
  53. elif isinstance(object, (list, tuple)):
  54. mapped_iter = []
  55. for iter in object:
  56. mapped_iter.append(self(iter))
  57. return mapped_iter if not isinstance(object, tuple) else tuple(mapped_iter)
  58. else:
  59. return object
  60. def map_nested_tensor_object(object, tensor_map_fn):
  61. impl = MapNestedTensorObjectImpl(tensor_map_fn)
  62. return impl(object)
  63. def is_iterable(obj):
  64. try:
  65. iter(obj)
  66. return True
  67. except TypeError:
  68. return False
  69. @contextlib.contextmanager
  70. def freeze_rng_state():
  71. rng_state = torch.get_rng_state()
  72. if torch.cuda.is_available():
  73. cuda_rng_state = torch.cuda.get_rng_state()
  74. yield
  75. if torch.cuda.is_available():
  76. torch.cuda.set_rng_state(cuda_rng_state)
  77. torch.set_rng_state(rng_state)
  78. def cycle_over(objs):
  79. for idx, obj1 in enumerate(objs):
  80. for obj2 in objs[:idx] + objs[idx + 1 :]:
  81. yield obj1, obj2
  82. def int_dtypes():
  83. return (torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64)
  84. def float_dtypes():
  85. return (torch.float32, torch.float64)
  86. @contextlib.contextmanager
  87. def disable_console_output():
  88. with contextlib.ExitStack() as stack, open(os.devnull, "w") as devnull:
  89. stack.enter_context(contextlib.redirect_stdout(devnull))
  90. stack.enter_context(contextlib.redirect_stderr(devnull))
  91. yield
  92. def cpu_and_cuda():
  93. import pytest # noqa
  94. return ("cpu", pytest.param("cuda", marks=pytest.mark.needs_cuda))
  95. def cpu_and_cuda_and_mps():
  96. return cpu_and_cuda() + (pytest.param("mps", marks=pytest.mark.needs_mps),)
  97. def needs_cuda(test_func):
  98. import pytest # noqa
  99. return pytest.mark.needs_cuda(test_func)
  100. def needs_mps(test_func):
  101. import pytest # noqa
  102. return pytest.mark.needs_mps(test_func)
  103. def _create_data(height=3, width=3, channels=3, device="cpu"):
  104. # TODO: When all relevant tests are ported to pytest, turn this into a module-level fixture
  105. tensor = torch.randint(0, 256, (channels, height, width), dtype=torch.uint8, device=device)
  106. data = tensor.permute(1, 2, 0).contiguous().cpu().numpy()
  107. mode = "RGB"
  108. if channels == 1:
  109. mode = "L"
  110. data = data[..., 0]
  111. pil_img = Image.fromarray(data, mode=mode)
  112. return tensor, pil_img
  113. def _create_data_batch(height=3, width=3, channels=3, num_samples=4, device="cpu"):
  114. # TODO: When all relevant tests are ported to pytest, turn this into a module-level fixture
  115. batch_tensor = torch.randint(0, 256, (num_samples, channels, height, width), dtype=torch.uint8, device=device)
  116. return batch_tensor
  117. def get_list_of_videos(tmpdir, num_videos=5, sizes=None, fps=None):
  118. names = []
  119. for i in range(num_videos):
  120. if sizes is None:
  121. size = 5 * (i + 1)
  122. else:
  123. size = sizes[i]
  124. if fps is None:
  125. f = 5
  126. else:
  127. f = fps[i]
  128. data = torch.randint(0, 256, (size, 300, 400, 3), dtype=torch.uint8)
  129. name = os.path.join(tmpdir, f"{i}.mp4")
  130. names.append(name)
  131. io.write_video(name, data, fps=f)
  132. return names
  133. def _assert_equal_tensor_to_pil(tensor, pil_image, msg=None):
  134. # FIXME: this is handled automatically by `assert_equal` below. Let's remove this in favor of it
  135. np_pil_image = np.array(pil_image)
  136. if np_pil_image.ndim == 2:
  137. np_pil_image = np_pil_image[:, :, None]
  138. pil_tensor = torch.as_tensor(np_pil_image.transpose((2, 0, 1)))
  139. if msg is None:
  140. msg = f"tensor:\n{tensor} \ndid not equal PIL tensor:\n{pil_tensor}"
  141. assert_equal(tensor.cpu(), pil_tensor, msg=msg)
  142. def _assert_approx_equal_tensor_to_pil(
  143. tensor, pil_image, tol=1e-5, msg=None, agg_method="mean", allowed_percentage_diff=None
  144. ):
  145. # FIXME: this is handled automatically by `assert_close` below. Let's remove this in favor of it
  146. # TODO: we could just merge this into _assert_equal_tensor_to_pil
  147. np_pil_image = np.array(pil_image)
  148. if np_pil_image.ndim == 2:
  149. np_pil_image = np_pil_image[:, :, None]
  150. pil_tensor = torch.as_tensor(np_pil_image.transpose((2, 0, 1))).to(tensor)
  151. if allowed_percentage_diff is not None:
  152. # Assert that less than a given %age of pixels are different
  153. assert (tensor != pil_tensor).to(torch.float).mean() <= allowed_percentage_diff
  154. # error value can be mean absolute error, max abs error
  155. # Convert to float to avoid underflow when computing absolute difference
  156. tensor = tensor.to(torch.float)
  157. pil_tensor = pil_tensor.to(torch.float)
  158. err = getattr(torch, agg_method)(torch.abs(tensor - pil_tensor)).item()
  159. assert err < tol, f"{err} vs {tol}"
  160. def _test_fn_on_batch(batch_tensors, fn, scripted_fn_atol=1e-8, **fn_kwargs):
  161. transformed_batch = fn(batch_tensors, **fn_kwargs)
  162. for i in range(len(batch_tensors)):
  163. img_tensor = batch_tensors[i, ...]
  164. transformed_img = fn(img_tensor, **fn_kwargs)
  165. torch.testing.assert_close(transformed_img, transformed_batch[i, ...], rtol=0, atol=1e-6)
  166. if scripted_fn_atol >= 0:
  167. scripted_fn = torch.jit.script(fn)
  168. # scriptable function test
  169. s_transformed_batch = scripted_fn(batch_tensors, **fn_kwargs)
  170. torch.testing.assert_close(transformed_batch, s_transformed_batch, rtol=1e-5, atol=scripted_fn_atol)
  171. def cache(fn):
  172. """Similar to :func:`functools.cache` (Python >= 3.8) or :func:`functools.lru_cache` with infinite cache size,
  173. but this also caches exceptions.
  174. """
  175. sentinel = object()
  176. out_cache = {}
  177. exc_tb_cache = {}
  178. @functools.wraps(fn)
  179. def wrapper(*args, **kwargs):
  180. key = args + tuple(kwargs.values())
  181. out = out_cache.get(key, sentinel)
  182. if out is not sentinel:
  183. return out
  184. exc_tb = exc_tb_cache.get(key, sentinel)
  185. if exc_tb is not sentinel:
  186. raise exc_tb[0].with_traceback(exc_tb[1])
  187. try:
  188. out = fn(*args, **kwargs)
  189. except Exception as exc:
  190. # We need to cache the traceback here as well. Otherwise, each re-raise will add the internal pytest
  191. # traceback frames anew, but they will only be removed once. Thus, the traceback will be ginormous hiding
  192. # the actual information in the noise. See https://github.com/pytest-dev/pytest/issues/10363 for details.
  193. exc_tb_cache[key] = exc, exc.__traceback__
  194. raise exc
  195. out_cache[key] = out
  196. return out
  197. return wrapper
  198. def combinations_grid(**kwargs):
  199. """Creates a grid of input combinations.
  200. Each element in the returned sequence is a dictionary containing one possible combination as values.
  201. Example:
  202. >>> combinations_grid(foo=("bar", "baz"), spam=("eggs", "ham"))
  203. [
  204. {'foo': 'bar', 'spam': 'eggs'},
  205. {'foo': 'bar', 'spam': 'ham'},
  206. {'foo': 'baz', 'spam': 'eggs'},
  207. {'foo': 'baz', 'spam': 'ham'}
  208. ]
  209. """
  210. return [dict(zip(kwargs.keys(), values)) for values in itertools.product(*kwargs.values())]
  211. class ImagePair(TensorLikePair):
  212. def __init__(
  213. self,
  214. actual,
  215. expected,
  216. *,
  217. mae=False,
  218. **other_parameters,
  219. ):
  220. if all(isinstance(input, PIL.Image.Image) for input in [actual, expected]):
  221. actual, expected = [to_image(input) for input in [actual, expected]]
  222. super().__init__(actual, expected, **other_parameters)
  223. self.mae = mae
  224. def compare(self) -> None:
  225. actual, expected = self.actual, self.expected
  226. self._compare_attributes(actual, expected)
  227. actual, expected = self._equalize_attributes(actual, expected)
  228. if self.mae:
  229. if actual.dtype is torch.uint8:
  230. actual, expected = actual.to(torch.int), expected.to(torch.int)
  231. mae = float(torch.abs(actual - expected).float().mean())
  232. if mae > self.atol:
  233. self._fail(
  234. AssertionError,
  235. f"The MAE of the images is {mae}, but only {self.atol} is allowed.",
  236. )
  237. else:
  238. super()._compare_values(actual, expected)
  239. def assert_close(
  240. actual,
  241. expected,
  242. *,
  243. allow_subclasses=True,
  244. rtol=None,
  245. atol=None,
  246. equal_nan=False,
  247. check_device=True,
  248. check_dtype=True,
  249. check_layout=True,
  250. check_stride=False,
  251. msg=None,
  252. **kwargs,
  253. ):
  254. """Superset of :func:`torch.testing.assert_close` with support for PIL vs. tensor image comparison"""
  255. __tracebackhide__ = True
  256. error_metas = not_close_error_metas(
  257. actual,
  258. expected,
  259. pair_types=(
  260. NonePair,
  261. BooleanPair,
  262. NumberPair,
  263. ImagePair,
  264. TensorLikePair,
  265. ),
  266. allow_subclasses=allow_subclasses,
  267. rtol=rtol,
  268. atol=atol,
  269. equal_nan=equal_nan,
  270. check_device=check_device,
  271. check_dtype=check_dtype,
  272. check_layout=check_layout,
  273. check_stride=check_stride,
  274. **kwargs,
  275. )
  276. if error_metas:
  277. raise error_metas[0].to_error(msg)
  278. assert_equal = functools.partial(assert_close, rtol=0, atol=0)
  279. DEFAULT_SIZE = (17, 11)
  280. NUM_CHANNELS_MAP = {
  281. "GRAY": 1,
  282. "GRAY_ALPHA": 2,
  283. "RGB": 3,
  284. "RGBA": 4,
  285. }
  286. def make_image(
  287. size=DEFAULT_SIZE,
  288. *,
  289. color_space="RGB",
  290. batch_dims=(),
  291. dtype=None,
  292. device="cpu",
  293. memory_format=torch.contiguous_format,
  294. ):
  295. num_channels = NUM_CHANNELS_MAP[color_space]
  296. dtype = dtype or torch.uint8
  297. max_value = get_max_value(dtype)
  298. data = torch.testing.make_tensor(
  299. (*batch_dims, num_channels, *size),
  300. low=0,
  301. high=max_value,
  302. dtype=dtype,
  303. device=device,
  304. memory_format=memory_format,
  305. )
  306. if color_space in {"GRAY_ALPHA", "RGBA"}:
  307. data[..., -1, :, :] = max_value
  308. return tv_tensors.Image(data)
  309. def make_image_tensor(*args, **kwargs):
  310. return make_image(*args, **kwargs).as_subclass(torch.Tensor)
  311. def make_image_pil(*args, **kwargs):
  312. return to_pil_image(make_image(*args, **kwargs))
  313. def make_bounding_boxes(
  314. canvas_size=DEFAULT_SIZE,
  315. *,
  316. format=tv_tensors.BoundingBoxFormat.XYXY,
  317. dtype=None,
  318. device="cpu",
  319. ):
  320. def sample_position(values, max_value):
  321. # We cannot use torch.randint directly here, because it only allows integer scalars as values for low and high.
  322. # However, if we have batch_dims, we need tensors as limits.
  323. return torch.stack([torch.randint(max_value - v, ()) for v in values.tolist()])
  324. if isinstance(format, str):
  325. format = tv_tensors.BoundingBoxFormat[format]
  326. dtype = dtype or torch.float32
  327. num_objects = 1
  328. h, w = [torch.randint(1, c, (num_objects,)) for c in canvas_size]
  329. y = sample_position(h, canvas_size[0])
  330. x = sample_position(w, canvas_size[1])
  331. if format is tv_tensors.BoundingBoxFormat.XYWH:
  332. parts = (x, y, w, h)
  333. elif format is tv_tensors.BoundingBoxFormat.XYXY:
  334. x1, y1 = x, y
  335. x2 = x1 + w
  336. y2 = y1 + h
  337. parts = (x1, y1, x2, y2)
  338. elif format is tv_tensors.BoundingBoxFormat.CXCYWH:
  339. cx = x + w / 2
  340. cy = y + h / 2
  341. parts = (cx, cy, w, h)
  342. else:
  343. raise ValueError(f"Format {format} is not supported")
  344. return tv_tensors.BoundingBoxes(
  345. torch.stack(parts, dim=-1).to(dtype=dtype, device=device), format=format, canvas_size=canvas_size
  346. )
  347. def make_detection_mask(size=DEFAULT_SIZE, *, dtype=None, device="cpu"):
  348. """Make a "detection" mask, i.e. (*, N, H, W), where each object is encoded as one of N boolean masks"""
  349. num_objects = 1
  350. return tv_tensors.Mask(
  351. torch.testing.make_tensor(
  352. (num_objects, *size),
  353. low=0,
  354. high=2,
  355. dtype=dtype or torch.bool,
  356. device=device,
  357. )
  358. )
  359. def make_segmentation_mask(size=DEFAULT_SIZE, *, num_categories=10, batch_dims=(), dtype=None, device="cpu"):
  360. """Make a "segmentation" mask, i.e. (*, H, W), where the category is encoded as pixel value"""
  361. return tv_tensors.Mask(
  362. torch.testing.make_tensor(
  363. (*batch_dims, *size),
  364. low=0,
  365. high=num_categories,
  366. dtype=dtype or torch.uint8,
  367. device=device,
  368. )
  369. )
  370. def make_video(size=DEFAULT_SIZE, *, num_frames=3, batch_dims=(), **kwargs):
  371. return tv_tensors.Video(make_image(size, batch_dims=(*batch_dims, num_frames), **kwargs))
  372. def make_video_tensor(*args, **kwargs):
  373. return make_video(*args, **kwargs).as_subclass(torch.Tensor)
  374. def assert_run_python_script(source_code):
  375. """Utility to check assertions in an independent Python subprocess.
  376. The script provided in the source code should return 0 and not print
  377. anything on stderr or stdout. Modified from scikit-learn test utils.
  378. Args:
  379. source_code (str): The Python source code to execute.
  380. """
  381. with get_tmp_dir() as root:
  382. path = pathlib.Path(root) / "main.py"
  383. with open(path, "w") as file:
  384. file.write(source_code)
  385. try:
  386. out = check_output([sys.executable, str(path)], stderr=STDOUT)
  387. except CalledProcessError as e:
  388. raise RuntimeError(f"script errored with output:\n{e.output.decode()}")
  389. if out != b"":
  390. raise AssertionError(out.decode())
  391. @contextlib.contextmanager
  392. def assert_no_warnings():
  393. # The name `catch_warnings` is a misnomer as the context manager does **not** catch any warnings, but rather scopes
  394. # the warning filters. All changes that are made to the filters while in this context, will be reset upon exit.
  395. with warnings.catch_warnings():
  396. warnings.simplefilter("error")
  397. yield
  398. @contextlib.contextmanager
  399. def ignore_jit_no_profile_information_warning():
  400. # Calling a scripted object often triggers a warning like
  401. # `UserWarning: operator() profile_node %$INT1 : int[] = prim::profile_ivalue($INT2) does not have profile information`
  402. # with varying `INT1` and `INT2`. Since these are uninteresting for us and only clutter the test summary, we ignore
  403. # them.
  404. with warnings.catch_warnings():
  405. warnings.filterwarnings("ignore", message=re.escape("operator() profile_node %"), category=UserWarning)
  406. yield