test_functional_tensor.py 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. import colorsys
  2. import itertools
  3. import math
  4. import os
  5. import warnings
  6. from functools import partial
  7. from typing import Sequence
  8. import numpy as np
  9. import PIL.Image
  10. import pytest
  11. import torch
  12. import torchvision.transforms as T
  13. import torchvision.transforms._functional_pil as F_pil
  14. import torchvision.transforms._functional_tensor as F_t
  15. import torchvision.transforms.functional as F
  16. from common_utils import (
  17. _assert_approx_equal_tensor_to_pil,
  18. _assert_equal_tensor_to_pil,
  19. _create_data,
  20. _create_data_batch,
  21. _test_fn_on_batch,
  22. assert_equal,
  23. cpu_and_cuda,
  24. needs_cuda,
  25. )
  26. from torchvision.transforms import InterpolationMode
  27. NEAREST, NEAREST_EXACT, BILINEAR, BICUBIC = (
  28. InterpolationMode.NEAREST,
  29. InterpolationMode.NEAREST_EXACT,
  30. InterpolationMode.BILINEAR,
  31. InterpolationMode.BICUBIC,
  32. )
  33. @pytest.mark.parametrize("device", cpu_and_cuda())
  34. @pytest.mark.parametrize("fn", [F.get_image_size, F.get_image_num_channels, F.get_dimensions])
  35. def test_image_sizes(device, fn):
  36. script_F = torch.jit.script(fn)
  37. img_tensor, pil_img = _create_data(16, 18, 3, device=device)
  38. value_img = fn(img_tensor)
  39. value_pil_img = fn(pil_img)
  40. assert value_img == value_pil_img
  41. value_img_script = script_F(img_tensor)
  42. assert value_img == value_img_script
  43. batch_tensors = _create_data_batch(16, 18, 3, num_samples=4, device=device)
  44. value_img_batch = fn(batch_tensors)
  45. assert value_img == value_img_batch
  46. @needs_cuda
  47. def test_scale_channel():
  48. """Make sure that _scale_channel gives the same results on CPU and GPU as
  49. histc or bincount are used depending on the device.
  50. """
  51. # TODO: when # https://github.com/pytorch/pytorch/issues/53194 is fixed,
  52. # only use bincount and remove that test.
  53. size = (1_000,)
  54. img_chan = torch.randint(0, 256, size=size).to("cpu")
  55. scaled_cpu = F_t._scale_channel(img_chan)
  56. scaled_cuda = F_t._scale_channel(img_chan.to("cuda"))
  57. assert_equal(scaled_cpu, scaled_cuda.to("cpu"))
  58. class TestRotate:
  59. ALL_DTYPES = [None, torch.float32, torch.float64, torch.float16]
  60. scripted_rotate = torch.jit.script(F.rotate)
  61. IMG_W = 26
  62. @pytest.mark.parametrize("device", cpu_and_cuda())
  63. @pytest.mark.parametrize("height, width", [(7, 33), (26, IMG_W), (32, IMG_W)])
  64. @pytest.mark.parametrize(
  65. "center",
  66. [
  67. None,
  68. (int(IMG_W * 0.3), int(IMG_W * 0.4)),
  69. [int(IMG_W * 0.5), int(IMG_W * 0.6)],
  70. ],
  71. )
  72. @pytest.mark.parametrize("dt", ALL_DTYPES)
  73. @pytest.mark.parametrize("angle", range(-180, 180, 34))
  74. @pytest.mark.parametrize("expand", [True, False])
  75. @pytest.mark.parametrize(
  76. "fill",
  77. [
  78. None,
  79. [0, 0, 0],
  80. (1, 2, 3),
  81. [255, 255, 255],
  82. [
  83. 1,
  84. ],
  85. (2.0,),
  86. ],
  87. )
  88. @pytest.mark.parametrize("fn", [F.rotate, scripted_rotate])
  89. def test_rotate(self, device, height, width, center, dt, angle, expand, fill, fn):
  90. tensor, pil_img = _create_data(height, width, device=device)
  91. if dt == torch.float16 and torch.device(device).type == "cpu":
  92. # skip float16 on CPU case
  93. return
  94. if dt is not None:
  95. tensor = tensor.to(dtype=dt)
  96. f_pil = int(fill[0]) if fill is not None and len(fill) == 1 else fill
  97. out_pil_img = F.rotate(pil_img, angle=angle, interpolation=NEAREST, expand=expand, center=center, fill=f_pil)
  98. out_pil_tensor = torch.from_numpy(np.array(out_pil_img).transpose((2, 0, 1)))
  99. out_tensor = fn(tensor, angle=angle, interpolation=NEAREST, expand=expand, center=center, fill=fill).cpu()
  100. if out_tensor.dtype != torch.uint8:
  101. out_tensor = out_tensor.to(torch.uint8)
  102. assert (
  103. out_tensor.shape == out_pil_tensor.shape
  104. ), f"{(height, width, NEAREST, dt, angle, expand, center)}: {out_tensor.shape} vs {out_pil_tensor.shape}"
  105. num_diff_pixels = (out_tensor != out_pil_tensor).sum().item() / 3.0
  106. ratio_diff_pixels = num_diff_pixels / out_tensor.shape[-1] / out_tensor.shape[-2]
  107. # Tolerance : less than 3% of different pixels
  108. assert ratio_diff_pixels < 0.03, (
  109. f"{(height, width, NEAREST, dt, angle, expand, center, fill)}: "
  110. f"{ratio_diff_pixels}\n{out_tensor[0, :7, :7]} vs \n"
  111. f"{out_pil_tensor[0, :7, :7]}"
  112. )
  113. @pytest.mark.parametrize("device", cpu_and_cuda())
  114. @pytest.mark.parametrize("dt", ALL_DTYPES)
  115. def test_rotate_batch(self, device, dt):
  116. if dt == torch.float16 and device == "cpu":
  117. # skip float16 on CPU case
  118. return
  119. batch_tensors = _create_data_batch(26, 36, num_samples=4, device=device)
  120. if dt is not None:
  121. batch_tensors = batch_tensors.to(dtype=dt)
  122. center = (20, 22)
  123. _test_fn_on_batch(batch_tensors, F.rotate, angle=32, interpolation=NEAREST, expand=True, center=center)
  124. def test_rotate_interpolation_type(self):
  125. tensor, _ = _create_data(26, 26)
  126. res1 = F.rotate(tensor, 45, interpolation=PIL.Image.BILINEAR)
  127. res2 = F.rotate(tensor, 45, interpolation=BILINEAR)
  128. assert_equal(res1, res2)
  129. class TestAffine:
  130. ALL_DTYPES = [None, torch.float32, torch.float64, torch.float16]
  131. scripted_affine = torch.jit.script(F.affine)
  132. @pytest.mark.parametrize("device", cpu_and_cuda())
  133. @pytest.mark.parametrize("height, width", [(26, 26), (32, 26)])
  134. @pytest.mark.parametrize("dt", ALL_DTYPES)
  135. def test_identity_map(self, device, height, width, dt):
  136. # Tests on square and rectangular images
  137. tensor, pil_img = _create_data(height, width, device=device)
  138. if dt == torch.float16 and device == "cpu":
  139. # skip float16 on CPU case
  140. return
  141. if dt is not None:
  142. tensor = tensor.to(dtype=dt)
  143. # 1) identity map
  144. out_tensor = F.affine(tensor, angle=0, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=NEAREST)
  145. assert_equal(tensor, out_tensor, msg=f"{out_tensor[0, :5, :5]} vs {tensor[0, :5, :5]}")
  146. out_tensor = self.scripted_affine(
  147. tensor, angle=0, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=NEAREST
  148. )
  149. assert_equal(tensor, out_tensor, msg=f"{out_tensor[0, :5, :5]} vs {tensor[0, :5, :5]}")
  150. @pytest.mark.parametrize("device", cpu_and_cuda())
  151. @pytest.mark.parametrize("height, width", [(26, 26)])
  152. @pytest.mark.parametrize("dt", ALL_DTYPES)
  153. @pytest.mark.parametrize(
  154. "angle, config",
  155. [
  156. (90, {"k": 1, "dims": (-1, -2)}),
  157. (45, None),
  158. (30, None),
  159. (-30, None),
  160. (-45, None),
  161. (-90, {"k": -1, "dims": (-1, -2)}),
  162. (180, {"k": 2, "dims": (-1, -2)}),
  163. ],
  164. )
  165. @pytest.mark.parametrize("fn", [F.affine, scripted_affine])
  166. def test_square_rotations(self, device, height, width, dt, angle, config, fn):
  167. # 2) Test rotation
  168. tensor, pil_img = _create_data(height, width, device=device)
  169. if dt == torch.float16 and device == "cpu":
  170. # skip float16 on CPU case
  171. return
  172. if dt is not None:
  173. tensor = tensor.to(dtype=dt)
  174. out_pil_img = F.affine(
  175. pil_img, angle=angle, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=NEAREST
  176. )
  177. out_pil_tensor = torch.from_numpy(np.array(out_pil_img).transpose((2, 0, 1))).to(device)
  178. out_tensor = fn(tensor, angle=angle, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=NEAREST)
  179. if config is not None:
  180. assert_equal(torch.rot90(tensor, **config), out_tensor)
  181. if out_tensor.dtype != torch.uint8:
  182. out_tensor = out_tensor.to(torch.uint8)
  183. num_diff_pixels = (out_tensor != out_pil_tensor).sum().item() / 3.0
  184. ratio_diff_pixels = num_diff_pixels / out_tensor.shape[-1] / out_tensor.shape[-2]
  185. # Tolerance : less than 6% of different pixels
  186. assert ratio_diff_pixels < 0.06
  187. @pytest.mark.parametrize("device", cpu_and_cuda())
  188. @pytest.mark.parametrize("height, width", [(32, 26)])
  189. @pytest.mark.parametrize("dt", ALL_DTYPES)
  190. @pytest.mark.parametrize("angle", [90, 45, 15, -30, -60, -120])
  191. @pytest.mark.parametrize("fn", [F.affine, scripted_affine])
  192. @pytest.mark.parametrize("center", [None, [0, 0]])
  193. def test_rect_rotations(self, device, height, width, dt, angle, fn, center):
  194. # Tests on rectangular images
  195. tensor, pil_img = _create_data(height, width, device=device)
  196. if dt == torch.float16 and device == "cpu":
  197. # skip float16 on CPU case
  198. return
  199. if dt is not None:
  200. tensor = tensor.to(dtype=dt)
  201. out_pil_img = F.affine(
  202. pil_img, angle=angle, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=NEAREST, center=center
  203. )
  204. out_pil_tensor = torch.from_numpy(np.array(out_pil_img).transpose((2, 0, 1)))
  205. out_tensor = fn(
  206. tensor, angle=angle, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=NEAREST, center=center
  207. ).cpu()
  208. if out_tensor.dtype != torch.uint8:
  209. out_tensor = out_tensor.to(torch.uint8)
  210. num_diff_pixels = (out_tensor != out_pil_tensor).sum().item() / 3.0
  211. ratio_diff_pixels = num_diff_pixels / out_tensor.shape[-1] / out_tensor.shape[-2]
  212. # Tolerance : less than 3% of different pixels
  213. assert ratio_diff_pixels < 0.03
  214. @pytest.mark.parametrize("device", cpu_and_cuda())
  215. @pytest.mark.parametrize("height, width", [(26, 26), (32, 26)])
  216. @pytest.mark.parametrize("dt", ALL_DTYPES)
  217. @pytest.mark.parametrize("t", [[10, 12], (-12, -13)])
  218. @pytest.mark.parametrize("fn", [F.affine, scripted_affine])
  219. def test_translations(self, device, height, width, dt, t, fn):
  220. # 3) Test translation
  221. tensor, pil_img = _create_data(height, width, device=device)
  222. if dt == torch.float16 and device == "cpu":
  223. # skip float16 on CPU case
  224. return
  225. if dt is not None:
  226. tensor = tensor.to(dtype=dt)
  227. out_pil_img = F.affine(pil_img, angle=0, translate=t, scale=1.0, shear=[0.0, 0.0], interpolation=NEAREST)
  228. out_tensor = fn(tensor, angle=0, translate=t, scale=1.0, shear=[0.0, 0.0], interpolation=NEAREST)
  229. if out_tensor.dtype != torch.uint8:
  230. out_tensor = out_tensor.to(torch.uint8)
  231. _assert_equal_tensor_to_pil(out_tensor, out_pil_img)
  232. @pytest.mark.parametrize("device", cpu_and_cuda())
  233. @pytest.mark.parametrize("height, width", [(26, 26), (32, 26)])
  234. @pytest.mark.parametrize("dt", ALL_DTYPES)
  235. @pytest.mark.parametrize(
  236. "a, t, s, sh, f",
  237. [
  238. (45.5, [5, 6], 1.0, [0.0, 0.0], None),
  239. (33, (5, -4), 1.0, [0.0, 0.0], [0, 0, 0]),
  240. (45, [-5, 4], 1.2, [0.0, 0.0], (1, 2, 3)),
  241. (33, (-4, -8), 2.0, [0.0, 0.0], [255, 255, 255]),
  242. (85, (10, -10), 0.7, [0.0, 0.0], [1]),
  243. (0, [0, 0], 1.0, [35.0], (2.0,)),
  244. (-25, [0, 0], 1.2, [0.0, 15.0], None),
  245. (-45, [-10, 0], 0.7, [2.0, 5.0], None),
  246. (-45, [-10, -10], 1.2, [4.0, 5.0], None),
  247. (-90, [0, 0], 1.0, [0.0, 0.0], None),
  248. ],
  249. )
  250. @pytest.mark.parametrize("fn", [F.affine, scripted_affine])
  251. def test_all_ops(self, device, height, width, dt, a, t, s, sh, f, fn):
  252. # 4) Test rotation + translation + scale + shear
  253. tensor, pil_img = _create_data(height, width, device=device)
  254. if dt == torch.float16 and device == "cpu":
  255. # skip float16 on CPU case
  256. return
  257. if dt is not None:
  258. tensor = tensor.to(dtype=dt)
  259. f_pil = int(f[0]) if f is not None and len(f) == 1 else f
  260. out_pil_img = F.affine(pil_img, angle=a, translate=t, scale=s, shear=sh, interpolation=NEAREST, fill=f_pil)
  261. out_pil_tensor = torch.from_numpy(np.array(out_pil_img).transpose((2, 0, 1)))
  262. out_tensor = fn(tensor, angle=a, translate=t, scale=s, shear=sh, interpolation=NEAREST, fill=f).cpu()
  263. if out_tensor.dtype != torch.uint8:
  264. out_tensor = out_tensor.to(torch.uint8)
  265. num_diff_pixels = (out_tensor != out_pil_tensor).sum().item() / 3.0
  266. ratio_diff_pixels = num_diff_pixels / out_tensor.shape[-1] / out_tensor.shape[-2]
  267. # Tolerance : less than 5% (cpu), 6% (cuda) of different pixels
  268. tol = 0.06 if device == "cuda" else 0.05
  269. assert ratio_diff_pixels < tol
  270. @pytest.mark.parametrize("device", cpu_and_cuda())
  271. @pytest.mark.parametrize("dt", ALL_DTYPES)
  272. def test_batches(self, device, dt):
  273. if dt == torch.float16 and device == "cpu":
  274. # skip float16 on CPU case
  275. return
  276. batch_tensors = _create_data_batch(26, 36, num_samples=4, device=device)
  277. if dt is not None:
  278. batch_tensors = batch_tensors.to(dtype=dt)
  279. _test_fn_on_batch(batch_tensors, F.affine, angle=-43, translate=[-3, 4], scale=1.2, shear=[4.0, 5.0])
  280. @pytest.mark.parametrize("device", cpu_and_cuda())
  281. def test_interpolation_type(self, device):
  282. tensor, pil_img = _create_data(26, 26, device=device)
  283. res1 = F.affine(tensor, 45, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=PIL.Image.BILINEAR)
  284. res2 = F.affine(tensor, 45, translate=[0, 0], scale=1.0, shear=[0.0, 0.0], interpolation=BILINEAR)
  285. assert_equal(res1, res2)
  286. def _get_data_dims_and_points_for_perspective():
  287. # Ideally we would parametrize independently over data dims and points, but
  288. # we want to tests on some points that also depend on the data dims.
  289. # Pytest doesn't support covariant parametrization, so we do it somewhat manually here.
  290. data_dims = [(26, 34), (26, 26)]
  291. points = [
  292. [[[0, 0], [33, 0], [33, 25], [0, 25]], [[3, 2], [32, 3], [30, 24], [2, 25]]],
  293. [[[3, 2], [32, 3], [30, 24], [2, 25]], [[0, 0], [33, 0], [33, 25], [0, 25]]],
  294. [[[3, 2], [32, 3], [30, 24], [2, 25]], [[5, 5], [30, 3], [33, 19], [4, 25]]],
  295. ]
  296. dims_and_points = list(itertools.product(data_dims, points))
  297. # up to here, we could just have used 2 @parametrized.
  298. # Down below is the covarariant part as the points depend on the data dims.
  299. n = 10
  300. for dim in data_dims:
  301. points += [(dim, T.RandomPerspective.get_params(dim[1], dim[0], i / n)) for i in range(n)]
  302. return dims_and_points
  303. @pytest.mark.parametrize("device", cpu_and_cuda())
  304. @pytest.mark.parametrize("dims_and_points", _get_data_dims_and_points_for_perspective())
  305. @pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
  306. @pytest.mark.parametrize("fill", (None, [0, 0, 0], [1, 2, 3], [255, 255, 255], [1], (2.0,)))
  307. @pytest.mark.parametrize("fn", [F.perspective, torch.jit.script(F.perspective)])
  308. def test_perspective_pil_vs_tensor(device, dims_and_points, dt, fill, fn):
  309. if dt == torch.float16 and device == "cpu":
  310. # skip float16 on CPU case
  311. return
  312. data_dims, (spoints, epoints) = dims_and_points
  313. tensor, pil_img = _create_data(*data_dims, device=device)
  314. if dt is not None:
  315. tensor = tensor.to(dtype=dt)
  316. interpolation = NEAREST
  317. fill_pil = int(fill[0]) if fill is not None and len(fill) == 1 else fill
  318. out_pil_img = F.perspective(
  319. pil_img, startpoints=spoints, endpoints=epoints, interpolation=interpolation, fill=fill_pil
  320. )
  321. out_pil_tensor = torch.from_numpy(np.array(out_pil_img).transpose((2, 0, 1)))
  322. out_tensor = fn(tensor, startpoints=spoints, endpoints=epoints, interpolation=interpolation, fill=fill).cpu()
  323. if out_tensor.dtype != torch.uint8:
  324. out_tensor = out_tensor.to(torch.uint8)
  325. num_diff_pixels = (out_tensor != out_pil_tensor).sum().item() / 3.0
  326. ratio_diff_pixels = num_diff_pixels / out_tensor.shape[-1] / out_tensor.shape[-2]
  327. # Tolerance : less than 5% of different pixels
  328. assert ratio_diff_pixels < 0.05
  329. @pytest.mark.parametrize("device", cpu_and_cuda())
  330. @pytest.mark.parametrize("dims_and_points", _get_data_dims_and_points_for_perspective())
  331. @pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
  332. def test_perspective_batch(device, dims_and_points, dt):
  333. if dt == torch.float16 and device == "cpu":
  334. # skip float16 on CPU case
  335. return
  336. data_dims, (spoints, epoints) = dims_and_points
  337. batch_tensors = _create_data_batch(*data_dims, num_samples=4, device=device)
  338. if dt is not None:
  339. batch_tensors = batch_tensors.to(dtype=dt)
  340. # Ignore the equivalence between scripted and regular function on float16 cuda. The pixels at
  341. # the border may be entirely different due to small rounding errors.
  342. scripted_fn_atol = -1 if (dt == torch.float16 and device == "cuda") else 1e-8
  343. _test_fn_on_batch(
  344. batch_tensors,
  345. F.perspective,
  346. scripted_fn_atol=scripted_fn_atol,
  347. startpoints=spoints,
  348. endpoints=epoints,
  349. interpolation=NEAREST,
  350. )
  351. def test_perspective_interpolation_type():
  352. spoints = [[0, 0], [33, 0], [33, 25], [0, 25]]
  353. epoints = [[3, 2], [32, 3], [30, 24], [2, 25]]
  354. tensor = torch.randint(0, 256, (3, 26, 26))
  355. res1 = F.perspective(tensor, startpoints=spoints, endpoints=epoints, interpolation=PIL.Image.BILINEAR)
  356. res2 = F.perspective(tensor, startpoints=spoints, endpoints=epoints, interpolation=BILINEAR)
  357. assert_equal(res1, res2)
  358. @pytest.mark.parametrize("device", cpu_and_cuda())
  359. @pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
  360. @pytest.mark.parametrize("size", [32, 26, [32], [32, 32], (32, 32), [26, 35]])
  361. @pytest.mark.parametrize("max_size", [None, 34, 40, 1000])
  362. @pytest.mark.parametrize("interpolation", [BILINEAR, BICUBIC, NEAREST, NEAREST_EXACT])
  363. def test_resize(device, dt, size, max_size, interpolation):
  364. if dt == torch.float16 and device == "cpu":
  365. # skip float16 on CPU case
  366. return
  367. if max_size is not None and isinstance(size, Sequence) and len(size) != 1:
  368. return # unsupported
  369. torch.manual_seed(12)
  370. script_fn = torch.jit.script(F.resize)
  371. tensor, pil_img = _create_data(26, 36, device=device)
  372. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  373. if dt is not None:
  374. # This is a trivial cast to float of uint8 data to test all cases
  375. tensor = tensor.to(dt)
  376. batch_tensors = batch_tensors.to(dt)
  377. resized_tensor = F.resize(tensor, size=size, interpolation=interpolation, max_size=max_size, antialias=True)
  378. resized_pil_img = F.resize(pil_img, size=size, interpolation=interpolation, max_size=max_size, antialias=True)
  379. assert resized_tensor.size()[1:] == resized_pil_img.size[::-1]
  380. if interpolation != NEAREST:
  381. # We can not check values if mode = NEAREST, as results are different
  382. # E.g. resized_tensor = [[a, a, b, c, d, d, e, ...]]
  383. # E.g. resized_pil_img = [[a, b, c, c, d, e, f, ...]]
  384. resized_tensor_f = resized_tensor
  385. # we need to cast to uint8 to compare with PIL image
  386. if resized_tensor_f.dtype == torch.uint8:
  387. resized_tensor_f = resized_tensor_f.to(torch.float)
  388. # Pay attention to high tolerance for MAE
  389. _assert_approx_equal_tensor_to_pil(resized_tensor_f, resized_pil_img, tol=3.0)
  390. if isinstance(size, int):
  391. script_size = [size]
  392. else:
  393. script_size = size
  394. resize_result = script_fn(tensor, size=script_size, interpolation=interpolation, max_size=max_size, antialias=True)
  395. assert_equal(resized_tensor, resize_result)
  396. _test_fn_on_batch(
  397. batch_tensors, F.resize, size=script_size, interpolation=interpolation, max_size=max_size, antialias=True
  398. )
  399. @pytest.mark.parametrize("device", cpu_and_cuda())
  400. def test_resize_asserts(device):
  401. tensor, pil_img = _create_data(26, 36, device=device)
  402. res1 = F.resize(tensor, size=32, interpolation=PIL.Image.BILINEAR)
  403. res2 = F.resize(tensor, size=32, interpolation=BILINEAR)
  404. assert_equal(res1, res2)
  405. for img in (tensor, pil_img):
  406. exp_msg = "max_size should only be passed if size specifies the length of the smaller edge"
  407. with pytest.raises(ValueError, match=exp_msg):
  408. F.resize(img, size=(32, 34), max_size=35)
  409. with pytest.raises(ValueError, match="max_size = 32 must be strictly greater"):
  410. F.resize(img, size=32, max_size=32)
  411. @pytest.mark.parametrize("device", cpu_and_cuda())
  412. @pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
  413. @pytest.mark.parametrize("size", [[96, 72], [96, 420], [420, 72]])
  414. @pytest.mark.parametrize("interpolation", [BILINEAR, BICUBIC])
  415. def test_resize_antialias(device, dt, size, interpolation):
  416. if dt == torch.float16 and device == "cpu":
  417. # skip float16 on CPU case
  418. return
  419. torch.manual_seed(12)
  420. script_fn = torch.jit.script(F.resize)
  421. tensor, pil_img = _create_data(320, 290, device=device)
  422. if dt is not None:
  423. # This is a trivial cast to float of uint8 data to test all cases
  424. tensor = tensor.to(dt)
  425. resized_tensor = F.resize(tensor, size=size, interpolation=interpolation, antialias=True)
  426. resized_pil_img = F.resize(pil_img, size=size, interpolation=interpolation, antialias=True)
  427. assert resized_tensor.size()[1:] == resized_pil_img.size[::-1]
  428. resized_tensor_f = resized_tensor
  429. # we need to cast to uint8 to compare with PIL image
  430. if resized_tensor_f.dtype == torch.uint8:
  431. resized_tensor_f = resized_tensor_f.to(torch.float)
  432. _assert_approx_equal_tensor_to_pil(resized_tensor_f, resized_pil_img, tol=0.5, msg=f"{size}, {interpolation}, {dt}")
  433. accepted_tol = 1.0 + 1e-5
  434. if interpolation == BICUBIC:
  435. # this overall mean value to make the tests pass
  436. # High value is mostly required for test cases with
  437. # downsampling and upsampling where we can not exactly
  438. # match PIL implementation.
  439. accepted_tol = 15.0
  440. _assert_approx_equal_tensor_to_pil(
  441. resized_tensor_f, resized_pil_img, tol=accepted_tol, agg_method="max", msg=f"{size}, {interpolation}, {dt}"
  442. )
  443. if isinstance(size, int):
  444. script_size = [
  445. size,
  446. ]
  447. else:
  448. script_size = size
  449. resize_result = script_fn(tensor, size=script_size, interpolation=interpolation, antialias=True)
  450. assert_equal(resized_tensor, resize_result)
  451. def test_resize_antialias_default_warning():
  452. img = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8)
  453. match = "The default value of the antialias"
  454. with pytest.warns(UserWarning, match=match):
  455. F.resize(img, size=(20, 20))
  456. with pytest.warns(UserWarning, match=match):
  457. F.resized_crop(img, 0, 0, 10, 10, size=(20, 20))
  458. # For modes that aren't bicubic or bilinear, don't throw a warning
  459. with warnings.catch_warnings():
  460. warnings.simplefilter("error")
  461. F.resize(img, size=(20, 20), interpolation=NEAREST)
  462. F.resized_crop(img, 0, 0, 10, 10, size=(20, 20), interpolation=NEAREST)
  463. def check_functional_vs_PIL_vs_scripted(
  464. fn, fn_pil, fn_t, config, device, dtype, channels=3, tol=2.0 + 1e-10, agg_method="max"
  465. ):
  466. script_fn = torch.jit.script(fn)
  467. torch.manual_seed(15)
  468. tensor, pil_img = _create_data(26, 34, channels=channels, device=device)
  469. batch_tensors = _create_data_batch(16, 18, num_samples=4, channels=channels, device=device)
  470. if dtype is not None:
  471. tensor = F.convert_image_dtype(tensor, dtype)
  472. batch_tensors = F.convert_image_dtype(batch_tensors, dtype)
  473. out_fn_t = fn_t(tensor, **config)
  474. out_pil = fn_pil(pil_img, **config)
  475. out_scripted = script_fn(tensor, **config)
  476. assert out_fn_t.dtype == out_scripted.dtype
  477. assert out_fn_t.size()[1:] == out_pil.size[::-1]
  478. rbg_tensor = out_fn_t
  479. if out_fn_t.dtype != torch.uint8:
  480. rbg_tensor = F.convert_image_dtype(out_fn_t, torch.uint8)
  481. # Check that max difference does not exceed 2 in [0, 255] range
  482. # Exact matching is not possible due to incompatibility convert_image_dtype and PIL results
  483. _assert_approx_equal_tensor_to_pil(rbg_tensor.float(), out_pil, tol=tol, agg_method=agg_method)
  484. atol = 1e-6
  485. if out_fn_t.dtype == torch.uint8 and "cuda" in torch.device(device).type:
  486. atol = 1.0
  487. assert out_fn_t.allclose(out_scripted, atol=atol)
  488. # FIXME: fn will be scripted again in _test_fn_on_batch. We could avoid that.
  489. _test_fn_on_batch(batch_tensors, fn, scripted_fn_atol=atol, **config)
  490. @pytest.mark.parametrize("device", cpu_and_cuda())
  491. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  492. @pytest.mark.parametrize("config", [{"brightness_factor": f} for f in (0.1, 0.5, 1.0, 1.34, 2.5)])
  493. @pytest.mark.parametrize("channels", [1, 3])
  494. def test_adjust_brightness(device, dtype, config, channels):
  495. check_functional_vs_PIL_vs_scripted(
  496. F.adjust_brightness,
  497. F_pil.adjust_brightness,
  498. F_t.adjust_brightness,
  499. config,
  500. device,
  501. dtype,
  502. channels,
  503. )
  504. @pytest.mark.parametrize("device", cpu_and_cuda())
  505. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  506. @pytest.mark.parametrize("channels", [1, 3])
  507. def test_invert(device, dtype, channels):
  508. check_functional_vs_PIL_vs_scripted(
  509. F.invert, F_pil.invert, F_t.invert, {}, device, dtype, channels, tol=1.0, agg_method="max"
  510. )
  511. @pytest.mark.parametrize("device", cpu_and_cuda())
  512. @pytest.mark.parametrize("config", [{"bits": bits} for bits in range(0, 8)])
  513. @pytest.mark.parametrize("channels", [1, 3])
  514. def test_posterize(device, config, channels):
  515. check_functional_vs_PIL_vs_scripted(
  516. F.posterize,
  517. F_pil.posterize,
  518. F_t.posterize,
  519. config,
  520. device,
  521. dtype=None,
  522. channels=channels,
  523. tol=1.0,
  524. agg_method="max",
  525. )
  526. @pytest.mark.parametrize("device", cpu_and_cuda())
  527. @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0, 64, 128, 192, 255]])
  528. @pytest.mark.parametrize("channels", [1, 3])
  529. def test_solarize1(device, config, channels):
  530. check_functional_vs_PIL_vs_scripted(
  531. F.solarize,
  532. F_pil.solarize,
  533. F_t.solarize,
  534. config,
  535. device,
  536. dtype=None,
  537. channels=channels,
  538. tol=1.0,
  539. agg_method="max",
  540. )
  541. @pytest.mark.parametrize("device", cpu_and_cuda())
  542. @pytest.mark.parametrize("dtype", (torch.float32, torch.float64))
  543. @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0.0, 0.25, 0.5, 0.75, 1.0]])
  544. @pytest.mark.parametrize("channels", [1, 3])
  545. def test_solarize2(device, dtype, config, channels):
  546. check_functional_vs_PIL_vs_scripted(
  547. F.solarize,
  548. lambda img, threshold: F_pil.solarize(img, 255 * threshold),
  549. F_t.solarize,
  550. config,
  551. device,
  552. dtype,
  553. channels,
  554. tol=1.0,
  555. agg_method="max",
  556. )
  557. @pytest.mark.parametrize(
  558. ("dtype", "threshold"),
  559. [
  560. *[
  561. (dtype, threshold)
  562. for dtype, threshold in itertools.product(
  563. [torch.float32, torch.float16],
  564. [0.0, 0.25, 0.5, 0.75, 1.0],
  565. )
  566. ],
  567. *[(torch.uint8, threshold) for threshold in [0, 64, 128, 192, 255]],
  568. *[(torch.int64, threshold) for threshold in [0, 2**32, 2**63 - 1]],
  569. ],
  570. )
  571. @pytest.mark.parametrize("device", cpu_and_cuda())
  572. def test_solarize_threshold_within_bound(threshold, dtype, device):
  573. make_img = torch.rand if dtype.is_floating_point else partial(torch.randint, 0, torch.iinfo(dtype).max)
  574. img = make_img((3, 12, 23), dtype=dtype, device=device)
  575. F_t.solarize(img, threshold)
  576. @pytest.mark.parametrize(
  577. ("dtype", "threshold"),
  578. [
  579. (torch.float32, 1.5),
  580. (torch.float16, 1.5),
  581. (torch.uint8, 260),
  582. (torch.int64, 2**64),
  583. ],
  584. )
  585. @pytest.mark.parametrize("device", cpu_and_cuda())
  586. def test_solarize_threshold_above_bound(threshold, dtype, device):
  587. make_img = torch.rand if dtype.is_floating_point else partial(torch.randint, 0, torch.iinfo(dtype).max)
  588. img = make_img((3, 12, 23), dtype=dtype, device=device)
  589. with pytest.raises(TypeError, match="Threshold should be less than bound of img."):
  590. F_t.solarize(img, threshold)
  591. @pytest.mark.parametrize("device", cpu_and_cuda())
  592. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  593. @pytest.mark.parametrize("config", [{"sharpness_factor": f} for f in [0.2, 0.5, 1.0, 1.5, 2.0]])
  594. @pytest.mark.parametrize("channels", [1, 3])
  595. def test_adjust_sharpness(device, dtype, config, channels):
  596. check_functional_vs_PIL_vs_scripted(
  597. F.adjust_sharpness,
  598. F_pil.adjust_sharpness,
  599. F_t.adjust_sharpness,
  600. config,
  601. device,
  602. dtype,
  603. channels,
  604. )
  605. @pytest.mark.parametrize("device", cpu_and_cuda())
  606. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  607. @pytest.mark.parametrize("channels", [1, 3])
  608. def test_autocontrast(device, dtype, channels):
  609. check_functional_vs_PIL_vs_scripted(
  610. F.autocontrast, F_pil.autocontrast, F_t.autocontrast, {}, device, dtype, channels, tol=1.0, agg_method="max"
  611. )
  612. @pytest.mark.parametrize("device", cpu_and_cuda())
  613. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  614. @pytest.mark.parametrize("channels", [1, 3])
  615. def test_autocontrast_equal_minmax(device, dtype, channels):
  616. a = _create_data_batch(32, 32, num_samples=1, channels=channels, device=device)
  617. a = a / 2.0 + 0.3
  618. assert (F.autocontrast(a)[0] == F.autocontrast(a[0])).all()
  619. a[0, 0] = 0.7
  620. assert (F.autocontrast(a)[0] == F.autocontrast(a[0])).all()
  621. @pytest.mark.parametrize("device", cpu_and_cuda())
  622. @pytest.mark.parametrize("channels", [1, 3])
  623. def test_equalize(device, channels):
  624. torch.use_deterministic_algorithms(False)
  625. check_functional_vs_PIL_vs_scripted(
  626. F.equalize,
  627. F_pil.equalize,
  628. F_t.equalize,
  629. {},
  630. device,
  631. dtype=None,
  632. channels=channels,
  633. tol=1.0,
  634. agg_method="max",
  635. )
  636. @pytest.mark.parametrize("device", cpu_and_cuda())
  637. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  638. @pytest.mark.parametrize("config", [{"contrast_factor": f} for f in [0.2, 0.5, 1.0, 1.5, 2.0]])
  639. @pytest.mark.parametrize("channels", [1, 3])
  640. def test_adjust_contrast(device, dtype, config, channels):
  641. check_functional_vs_PIL_vs_scripted(
  642. F.adjust_contrast, F_pil.adjust_contrast, F_t.adjust_contrast, config, device, dtype, channels
  643. )
  644. @pytest.mark.parametrize("device", cpu_and_cuda())
  645. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  646. @pytest.mark.parametrize("config", [{"saturation_factor": f} for f in [0.5, 0.75, 1.0, 1.5, 2.0]])
  647. @pytest.mark.parametrize("channels", [1, 3])
  648. def test_adjust_saturation(device, dtype, config, channels):
  649. check_functional_vs_PIL_vs_scripted(
  650. F.adjust_saturation, F_pil.adjust_saturation, F_t.adjust_saturation, config, device, dtype, channels
  651. )
  652. @pytest.mark.parametrize("device", cpu_and_cuda())
  653. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  654. @pytest.mark.parametrize("config", [{"hue_factor": f} for f in [-0.45, -0.25, 0.0, 0.25, 0.45]])
  655. @pytest.mark.parametrize("channels", [1, 3])
  656. def test_adjust_hue(device, dtype, config, channels):
  657. check_functional_vs_PIL_vs_scripted(
  658. F.adjust_hue, F_pil.adjust_hue, F_t.adjust_hue, config, device, dtype, channels, tol=16.1, agg_method="max"
  659. )
  660. @pytest.mark.parametrize("device", cpu_and_cuda())
  661. @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64))
  662. @pytest.mark.parametrize("config", [{"gamma": g1, "gain": g2} for g1, g2 in zip([0.8, 1.0, 1.2], [0.7, 1.0, 1.3])])
  663. @pytest.mark.parametrize("channels", [1, 3])
  664. def test_adjust_gamma(device, dtype, config, channels):
  665. check_functional_vs_PIL_vs_scripted(
  666. F.adjust_gamma,
  667. F_pil.adjust_gamma,
  668. F_t.adjust_gamma,
  669. config,
  670. device,
  671. dtype,
  672. channels,
  673. )
  674. @pytest.mark.parametrize("device", cpu_and_cuda())
  675. @pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
  676. @pytest.mark.parametrize("pad", [2, [3], [0, 3], (3, 3), [4, 2, 4, 3]])
  677. @pytest.mark.parametrize(
  678. "config",
  679. [
  680. {"padding_mode": "constant", "fill": 0},
  681. {"padding_mode": "constant", "fill": 10},
  682. {"padding_mode": "constant", "fill": 20.2},
  683. {"padding_mode": "edge"},
  684. {"padding_mode": "reflect"},
  685. {"padding_mode": "symmetric"},
  686. ],
  687. )
  688. def test_pad(device, dt, pad, config):
  689. script_fn = torch.jit.script(F.pad)
  690. tensor, pil_img = _create_data(7, 8, device=device)
  691. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  692. if dt == torch.float16 and device == "cpu":
  693. # skip float16 on CPU case
  694. return
  695. if dt is not None:
  696. # This is a trivial cast to float of uint8 data to test all cases
  697. tensor = tensor.to(dt)
  698. batch_tensors = batch_tensors.to(dt)
  699. pad_tensor = F_t.pad(tensor, pad, **config)
  700. pad_pil_img = F_pil.pad(pil_img, pad, **config)
  701. pad_tensor_8b = pad_tensor
  702. # we need to cast to uint8 to compare with PIL image
  703. if pad_tensor_8b.dtype != torch.uint8:
  704. pad_tensor_8b = pad_tensor_8b.to(torch.uint8)
  705. _assert_equal_tensor_to_pil(pad_tensor_8b, pad_pil_img, msg=f"{pad}, {config}")
  706. if isinstance(pad, int):
  707. script_pad = [
  708. pad,
  709. ]
  710. else:
  711. script_pad = pad
  712. pad_tensor_script = script_fn(tensor, script_pad, **config)
  713. assert_equal(pad_tensor, pad_tensor_script, msg=f"{pad}, {config}")
  714. _test_fn_on_batch(batch_tensors, F.pad, padding=script_pad, **config)
  715. @pytest.mark.parametrize("device", cpu_and_cuda())
  716. @pytest.mark.parametrize("mode", [NEAREST, NEAREST_EXACT, BILINEAR, BICUBIC])
  717. def test_resized_crop(device, mode):
  718. # test values of F.resized_crop in several cases:
  719. # 1) resize to the same size, crop to the same size => should be identity
  720. tensor, _ = _create_data(26, 36, device=device)
  721. out_tensor = F.resized_crop(
  722. tensor, top=0, left=0, height=26, width=36, size=[26, 36], interpolation=mode, antialias=True
  723. )
  724. assert_equal(tensor, out_tensor, msg=f"{out_tensor[0, :5, :5]} vs {tensor[0, :5, :5]}")
  725. # 2) resize by half and crop a TL corner
  726. tensor, _ = _create_data(26, 36, device=device)
  727. out_tensor = F.resized_crop(tensor, top=0, left=0, height=20, width=30, size=[10, 15], interpolation=NEAREST)
  728. expected_out_tensor = tensor[:, :20:2, :30:2]
  729. assert_equal(
  730. expected_out_tensor,
  731. out_tensor,
  732. msg=f"{expected_out_tensor[0, :10, :10]} vs {out_tensor[0, :10, :10]}",
  733. )
  734. batch_tensors = _create_data_batch(26, 36, num_samples=4, device=device)
  735. _test_fn_on_batch(
  736. batch_tensors,
  737. F.resized_crop,
  738. top=1,
  739. left=2,
  740. height=20,
  741. width=30,
  742. size=[10, 15],
  743. interpolation=NEAREST,
  744. )
  745. @pytest.mark.parametrize("device", cpu_and_cuda())
  746. @pytest.mark.parametrize(
  747. "func, args",
  748. [
  749. (F_t.get_dimensions, ()),
  750. (F_t.get_image_size, ()),
  751. (F_t.get_image_num_channels, ()),
  752. (F_t.vflip, ()),
  753. (F_t.hflip, ()),
  754. (F_t.crop, (1, 2, 4, 5)),
  755. (F_t.adjust_brightness, (0.0,)),
  756. (F_t.adjust_contrast, (1.0,)),
  757. (F_t.adjust_hue, (-0.5,)),
  758. (F_t.adjust_saturation, (2.0,)),
  759. (F_t.pad, ([2], 2, "constant")),
  760. (F_t.resize, ([10, 11],)),
  761. (F_t.perspective, ([0.2])),
  762. (F_t.gaussian_blur, ((2, 2), (0.7, 0.5))),
  763. (F_t.invert, ()),
  764. (F_t.posterize, (0,)),
  765. (F_t.solarize, (0.3,)),
  766. (F_t.adjust_sharpness, (0.3,)),
  767. (F_t.autocontrast, ()),
  768. (F_t.equalize, ()),
  769. ],
  770. )
  771. def test_assert_image_tensor(device, func, args):
  772. shape = (100,)
  773. tensor = torch.rand(*shape, dtype=torch.float, device=device)
  774. with pytest.raises(Exception, match=r"Tensor is not a torch image."):
  775. func(tensor, *args)
  776. @pytest.mark.parametrize("device", cpu_and_cuda())
  777. def test_vflip(device):
  778. script_vflip = torch.jit.script(F.vflip)
  779. img_tensor, pil_img = _create_data(16, 18, device=device)
  780. vflipped_img = F.vflip(img_tensor)
  781. vflipped_pil_img = F.vflip(pil_img)
  782. _assert_equal_tensor_to_pil(vflipped_img, vflipped_pil_img)
  783. # scriptable function test
  784. vflipped_img_script = script_vflip(img_tensor)
  785. assert_equal(vflipped_img, vflipped_img_script)
  786. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  787. _test_fn_on_batch(batch_tensors, F.vflip)
  788. @pytest.mark.parametrize("device", cpu_and_cuda())
  789. def test_hflip(device):
  790. script_hflip = torch.jit.script(F.hflip)
  791. img_tensor, pil_img = _create_data(16, 18, device=device)
  792. hflipped_img = F.hflip(img_tensor)
  793. hflipped_pil_img = F.hflip(pil_img)
  794. _assert_equal_tensor_to_pil(hflipped_img, hflipped_pil_img)
  795. # scriptable function test
  796. hflipped_img_script = script_hflip(img_tensor)
  797. assert_equal(hflipped_img, hflipped_img_script)
  798. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  799. _test_fn_on_batch(batch_tensors, F.hflip)
  800. @pytest.mark.parametrize("device", cpu_and_cuda())
  801. @pytest.mark.parametrize(
  802. "top, left, height, width",
  803. [
  804. (1, 2, 4, 5), # crop inside top-left corner
  805. (2, 12, 3, 4), # crop inside top-right corner
  806. (8, 3, 5, 6), # crop inside bottom-left corner
  807. (8, 11, 4, 3), # crop inside bottom-right corner
  808. (50, 50, 10, 10), # crop outside the image
  809. (-50, -50, 10, 10), # crop outside the image
  810. ],
  811. )
  812. def test_crop(device, top, left, height, width):
  813. script_crop = torch.jit.script(F.crop)
  814. img_tensor, pil_img = _create_data(16, 18, device=device)
  815. pil_img_cropped = F.crop(pil_img, top, left, height, width)
  816. img_tensor_cropped = F.crop(img_tensor, top, left, height, width)
  817. _assert_equal_tensor_to_pil(img_tensor_cropped, pil_img_cropped)
  818. img_tensor_cropped = script_crop(img_tensor, top, left, height, width)
  819. _assert_equal_tensor_to_pil(img_tensor_cropped, pil_img_cropped)
  820. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  821. _test_fn_on_batch(batch_tensors, F.crop, top=top, left=left, height=height, width=width)
  822. @pytest.mark.parametrize("device", cpu_and_cuda())
  823. @pytest.mark.parametrize("image_size", ("small", "large"))
  824. @pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
  825. @pytest.mark.parametrize("ksize", [(3, 3), [3, 5], (23, 23)])
  826. @pytest.mark.parametrize("sigma", [[0.5, 0.5], (0.5, 0.5), (0.8, 0.8), (1.7, 1.7)])
  827. @pytest.mark.parametrize("fn", [F.gaussian_blur, torch.jit.script(F.gaussian_blur)])
  828. def test_gaussian_blur(device, image_size, dt, ksize, sigma, fn):
  829. # true_cv2_results = {
  830. # # np_img = np.arange(3 * 10 * 12, dtype="uint8").reshape((10, 12, 3))
  831. # # cv2.GaussianBlur(np_img, ksize=(3, 3), sigmaX=0.8)
  832. # "3_3_0.8": ...
  833. # # cv2.GaussianBlur(np_img, ksize=(3, 3), sigmaX=0.5)
  834. # "3_3_0.5": ...
  835. # # cv2.GaussianBlur(np_img, ksize=(3, 5), sigmaX=0.8)
  836. # "3_5_0.8": ...
  837. # # cv2.GaussianBlur(np_img, ksize=(3, 5), sigmaX=0.5)
  838. # "3_5_0.5": ...
  839. # # np_img2 = np.arange(26 * 28, dtype="uint8").reshape((26, 28))
  840. # # cv2.GaussianBlur(np_img2, ksize=(23, 23), sigmaX=1.7)
  841. # "23_23_1.7": ...
  842. # }
  843. p = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "gaussian_blur_opencv_results.pt")
  844. true_cv2_results = torch.load(p)
  845. if image_size == "small":
  846. tensor = (
  847. torch.from_numpy(np.arange(3 * 10 * 12, dtype="uint8").reshape((10, 12, 3))).permute(2, 0, 1).to(device)
  848. )
  849. else:
  850. tensor = torch.from_numpy(np.arange(26 * 28, dtype="uint8").reshape((1, 26, 28))).to(device)
  851. if dt == torch.float16 and device == "cpu":
  852. # skip float16 on CPU case
  853. return
  854. if dt is not None:
  855. tensor = tensor.to(dtype=dt)
  856. _ksize = (ksize, ksize) if isinstance(ksize, int) else ksize
  857. _sigma = sigma[0] if sigma is not None else None
  858. shape = tensor.shape
  859. gt_key = f"{shape[-2]}_{shape[-1]}_{shape[-3]}__{_ksize[0]}_{_ksize[1]}_{_sigma}"
  860. if gt_key not in true_cv2_results:
  861. return
  862. true_out = (
  863. torch.tensor(true_cv2_results[gt_key]).reshape(shape[-2], shape[-1], shape[-3]).permute(2, 0, 1).to(tensor)
  864. )
  865. out = fn(tensor, kernel_size=ksize, sigma=sigma)
  866. torch.testing.assert_close(out, true_out, rtol=0.0, atol=1.0, msg=f"{ksize}, {sigma}")
  867. @pytest.mark.parametrize("device", cpu_and_cuda())
  868. def test_hsv2rgb(device):
  869. scripted_fn = torch.jit.script(F_t._hsv2rgb)
  870. shape = (3, 100, 150)
  871. for _ in range(10):
  872. hsv_img = torch.rand(*shape, dtype=torch.float, device=device)
  873. rgb_img = F_t._hsv2rgb(hsv_img)
  874. ft_img = rgb_img.permute(1, 2, 0).flatten(0, 1)
  875. (
  876. h,
  877. s,
  878. v,
  879. ) = hsv_img.unbind(0)
  880. h = h.flatten().cpu().numpy()
  881. s = s.flatten().cpu().numpy()
  882. v = v.flatten().cpu().numpy()
  883. rgb = []
  884. for h1, s1, v1 in zip(h, s, v):
  885. rgb.append(colorsys.hsv_to_rgb(h1, s1, v1))
  886. colorsys_img = torch.tensor(rgb, dtype=torch.float32, device=device)
  887. torch.testing.assert_close(ft_img, colorsys_img, rtol=0.0, atol=1e-5)
  888. s_rgb_img = scripted_fn(hsv_img)
  889. torch.testing.assert_close(rgb_img, s_rgb_img)
  890. batch_tensors = _create_data_batch(120, 100, num_samples=4, device=device).float()
  891. _test_fn_on_batch(batch_tensors, F_t._hsv2rgb)
  892. @pytest.mark.parametrize("device", cpu_and_cuda())
  893. def test_rgb2hsv(device):
  894. scripted_fn = torch.jit.script(F_t._rgb2hsv)
  895. shape = (3, 150, 100)
  896. for _ in range(10):
  897. rgb_img = torch.rand(*shape, dtype=torch.float, device=device)
  898. hsv_img = F_t._rgb2hsv(rgb_img)
  899. ft_hsv_img = hsv_img.permute(1, 2, 0).flatten(0, 1)
  900. (
  901. r,
  902. g,
  903. b,
  904. ) = rgb_img.unbind(dim=-3)
  905. r = r.flatten().cpu().numpy()
  906. g = g.flatten().cpu().numpy()
  907. b = b.flatten().cpu().numpy()
  908. hsv = []
  909. for r1, g1, b1 in zip(r, g, b):
  910. hsv.append(colorsys.rgb_to_hsv(r1, g1, b1))
  911. colorsys_img = torch.tensor(hsv, dtype=torch.float32, device=device)
  912. ft_hsv_img_h, ft_hsv_img_sv = torch.split(ft_hsv_img, [1, 2], dim=1)
  913. colorsys_img_h, colorsys_img_sv = torch.split(colorsys_img, [1, 2], dim=1)
  914. max_diff_h = ((colorsys_img_h * 2 * math.pi).sin() - (ft_hsv_img_h * 2 * math.pi).sin()).abs().max()
  915. max_diff_sv = (colorsys_img_sv - ft_hsv_img_sv).abs().max()
  916. max_diff = max(max_diff_h, max_diff_sv)
  917. assert max_diff < 1e-5
  918. s_hsv_img = scripted_fn(rgb_img)
  919. torch.testing.assert_close(hsv_img, s_hsv_img, rtol=1e-5, atol=1e-7)
  920. batch_tensors = _create_data_batch(120, 100, num_samples=4, device=device).float()
  921. _test_fn_on_batch(batch_tensors, F_t._rgb2hsv)
  922. @pytest.mark.parametrize("device", cpu_and_cuda())
  923. @pytest.mark.parametrize("num_output_channels", (3, 1))
  924. def test_rgb_to_grayscale(device, num_output_channels):
  925. script_rgb_to_grayscale = torch.jit.script(F.rgb_to_grayscale)
  926. img_tensor, pil_img = _create_data(32, 34, device=device)
  927. gray_pil_image = F.rgb_to_grayscale(pil_img, num_output_channels=num_output_channels)
  928. gray_tensor = F.rgb_to_grayscale(img_tensor, num_output_channels=num_output_channels)
  929. _assert_approx_equal_tensor_to_pil(gray_tensor.float(), gray_pil_image, tol=1.0 + 1e-10, agg_method="max")
  930. s_gray_tensor = script_rgb_to_grayscale(img_tensor, num_output_channels=num_output_channels)
  931. assert_equal(s_gray_tensor, gray_tensor)
  932. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  933. _test_fn_on_batch(batch_tensors, F.rgb_to_grayscale, num_output_channels=num_output_channels)
  934. @pytest.mark.parametrize("device", cpu_and_cuda())
  935. def test_center_crop(device):
  936. script_center_crop = torch.jit.script(F.center_crop)
  937. img_tensor, pil_img = _create_data(32, 34, device=device)
  938. cropped_pil_image = F.center_crop(pil_img, [10, 11])
  939. cropped_tensor = F.center_crop(img_tensor, [10, 11])
  940. _assert_equal_tensor_to_pil(cropped_tensor, cropped_pil_image)
  941. cropped_tensor = script_center_crop(img_tensor, [10, 11])
  942. _assert_equal_tensor_to_pil(cropped_tensor, cropped_pil_image)
  943. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  944. _test_fn_on_batch(batch_tensors, F.center_crop, output_size=[10, 11])
  945. @pytest.mark.parametrize("device", cpu_and_cuda())
  946. def test_five_crop(device):
  947. script_five_crop = torch.jit.script(F.five_crop)
  948. img_tensor, pil_img = _create_data(32, 34, device=device)
  949. cropped_pil_images = F.five_crop(pil_img, [10, 11])
  950. cropped_tensors = F.five_crop(img_tensor, [10, 11])
  951. for i in range(5):
  952. _assert_equal_tensor_to_pil(cropped_tensors[i], cropped_pil_images[i])
  953. cropped_tensors = script_five_crop(img_tensor, [10, 11])
  954. for i in range(5):
  955. _assert_equal_tensor_to_pil(cropped_tensors[i], cropped_pil_images[i])
  956. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  957. tuple_transformed_batches = F.five_crop(batch_tensors, [10, 11])
  958. for i in range(len(batch_tensors)):
  959. img_tensor = batch_tensors[i, ...]
  960. tuple_transformed_imgs = F.five_crop(img_tensor, [10, 11])
  961. assert len(tuple_transformed_imgs) == len(tuple_transformed_batches)
  962. for j in range(len(tuple_transformed_imgs)):
  963. true_transformed_img = tuple_transformed_imgs[j]
  964. transformed_img = tuple_transformed_batches[j][i, ...]
  965. assert_equal(true_transformed_img, transformed_img)
  966. # scriptable function test
  967. s_tuple_transformed_batches = script_five_crop(batch_tensors, [10, 11])
  968. for transformed_batch, s_transformed_batch in zip(tuple_transformed_batches, s_tuple_transformed_batches):
  969. assert_equal(transformed_batch, s_transformed_batch)
  970. @pytest.mark.parametrize("device", cpu_and_cuda())
  971. def test_ten_crop(device):
  972. script_ten_crop = torch.jit.script(F.ten_crop)
  973. img_tensor, pil_img = _create_data(32, 34, device=device)
  974. cropped_pil_images = F.ten_crop(pil_img, [10, 11])
  975. cropped_tensors = F.ten_crop(img_tensor, [10, 11])
  976. for i in range(10):
  977. _assert_equal_tensor_to_pil(cropped_tensors[i], cropped_pil_images[i])
  978. cropped_tensors = script_ten_crop(img_tensor, [10, 11])
  979. for i in range(10):
  980. _assert_equal_tensor_to_pil(cropped_tensors[i], cropped_pil_images[i])
  981. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  982. tuple_transformed_batches = F.ten_crop(batch_tensors, [10, 11])
  983. for i in range(len(batch_tensors)):
  984. img_tensor = batch_tensors[i, ...]
  985. tuple_transformed_imgs = F.ten_crop(img_tensor, [10, 11])
  986. assert len(tuple_transformed_imgs) == len(tuple_transformed_batches)
  987. for j in range(len(tuple_transformed_imgs)):
  988. true_transformed_img = tuple_transformed_imgs[j]
  989. transformed_img = tuple_transformed_batches[j][i, ...]
  990. assert_equal(true_transformed_img, transformed_img)
  991. # scriptable function test
  992. s_tuple_transformed_batches = script_ten_crop(batch_tensors, [10, 11])
  993. for transformed_batch, s_transformed_batch in zip(tuple_transformed_batches, s_tuple_transformed_batches):
  994. assert_equal(transformed_batch, s_transformed_batch)
  995. def test_elastic_transform_asserts():
  996. with pytest.raises(TypeError, match="Argument displacement should be a Tensor"):
  997. _ = F.elastic_transform("abc", displacement=None)
  998. with pytest.raises(TypeError, match="img should be PIL Image or Tensor"):
  999. _ = F.elastic_transform("abc", displacement=torch.rand(1))
  1000. img_tensor = torch.rand(1, 3, 32, 24)
  1001. with pytest.raises(ValueError, match="Argument displacement shape should"):
  1002. _ = F.elastic_transform(img_tensor, displacement=torch.rand(1, 2))
  1003. @pytest.mark.parametrize("device", cpu_and_cuda())
  1004. @pytest.mark.parametrize("interpolation", [NEAREST, BILINEAR, BICUBIC])
  1005. @pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
  1006. @pytest.mark.parametrize(
  1007. "fill",
  1008. [None, [255, 255, 255], (2.0,)],
  1009. )
  1010. def test_elastic_transform_consistency(device, interpolation, dt, fill):
  1011. script_elastic_transform = torch.jit.script(F.elastic_transform)
  1012. img_tensor, _ = _create_data(32, 34, device=device)
  1013. # As there is no PIL implementation for elastic_transform,
  1014. # thus we do not run tests tensor vs pillow
  1015. if dt is not None:
  1016. img_tensor = img_tensor.to(dt)
  1017. displacement = T.ElasticTransform.get_params([1.5, 1.5], [2.0, 2.0], [32, 34])
  1018. kwargs = dict(
  1019. displacement=displacement,
  1020. interpolation=interpolation,
  1021. fill=fill,
  1022. )
  1023. out_tensor1 = F.elastic_transform(img_tensor, **kwargs)
  1024. out_tensor2 = script_elastic_transform(img_tensor, **kwargs)
  1025. assert_equal(out_tensor1, out_tensor2)
  1026. batch_tensors = _create_data_batch(16, 18, num_samples=4, device=device)
  1027. displacement = T.ElasticTransform.get_params([1.5, 1.5], [2.0, 2.0], [16, 18])
  1028. kwargs["displacement"] = displacement
  1029. if dt is not None:
  1030. batch_tensors = batch_tensors.to(dt)
  1031. _test_fn_on_batch(batch_tensors, F.elastic_transform, **kwargs)
  1032. if __name__ == "__main__":
  1033. pytest.main([__file__])