2121import torchvision .transforms .v2 as transforms
2222
2323from common_utils import (
24+ assert_close ,
2425 assert_equal ,
2526 cache ,
2627 cpu_and_cuda ,
4243)
4344
4445from torch import nn
45- from torch .testing import assert_close
4646from torch .utils ._pytree import tree_flatten , tree_map
4747from torch .utils .data import DataLoader , default_collate
4848from torchvision import tv_tensors
@@ -3499,11 +3499,8 @@ def test_functional_image_correctness(self, kwargs, make_input):
34993499
35003500 actual = F .crop (image , ** kwargs )
35013501
3502- if make_input == make_image_cvcuda :
3503- actual = F .cvcuda_to_tensor (actual ).to (device = "cpu" )
3504- actual = actual .squeeze (0 )
3505- image = F .cvcuda_to_tensor (image ).to (device = "cpu" )
3506- image = image .squeeze (0 )
3502+ if make_input is make_image_cvcuda :
3503+ image = cvcuda_to_pil_compatible_tensor (image )
35073504
35083505 expected = F .to_image (F .crop (F .to_pil_image (image ), ** kwargs ))
35093506
@@ -3624,15 +3621,15 @@ def test_transform_image_correctness(self, param, value, seed, make_input):
36243621
36253622 torch .manual_seed (seed )
36263623
3627- if make_input == make_image_cvcuda :
3624+ if make_input is make_image_cvcuda :
36283625 image = cvcuda_to_pil_compatible_tensor (image )
36293626
36303627 expected = F .to_image (transform (F .to_pil_image (image )))
36313628
36323629 if make_input == make_image_cvcuda and will_pad :
36333630 # when padding is applied, CV-CUDA will always fill with zeros
36343631 # cannot use assert_equal since it will fail unless random is all zeros
3635- torch . testing . assert_close (actual , expected , rtol = 0 , atol = get_max_value (image .dtype ))
3632+ assert_close (actual , expected , rtol = 0 , atol = get_max_value (image .dtype ))
36363633 else :
36373634 assert_equal (actual , expected )
36383635
@@ -4458,6 +4455,9 @@ def test_kernel(self, kernel, make_input):
44584455 make_segmentation_mask ,
44594456 make_video ,
44604457 make_keypoints ,
4458+ pytest .param (
4459+ make_image_cvcuda , marks = pytest .mark .skipif (not CVCUDA_AVAILABLE , reason = "test requires CVCUDA" )
4460+ ),
44614461 ],
44624462 )
44634463 def test_functional (self , make_input ):
@@ -4474,9 +4474,16 @@ def test_functional(self, make_input):
44744474 (F .resized_crop_mask , tv_tensors .Mask ),
44754475 (F .resized_crop_video , tv_tensors .Video ),
44764476 (F .resized_crop_keypoints , tv_tensors .KeyPoints ),
4477+ pytest .param (
4478+ F .resized_crop_image ,
4479+ "cvcuda.Tensor" ,
4480+ marks = pytest .mark .skipif (not CVCUDA_AVAILABLE , reason = "test requires CVCUDA" ),
4481+ ),
44774482 ],
44784483 )
44794484 def test_functional_signature (self , kernel , input_type ):
4485+ if input_type == "cvcuda.Tensor" :
4486+ input_type = _import_cvcuda ().Tensor
44804487 check_functional_kernel_signature_match (F .resized_crop , kernel = kernel , input_type = input_type )
44814488
44824489 @param_value_parametrization (
@@ -4493,6 +4500,9 @@ def test_functional_signature(self, kernel, input_type):
44934500 make_segmentation_mask ,
44944501 make_video ,
44954502 make_keypoints ,
4503+ pytest .param (
4504+ make_image_cvcuda , marks = pytest .mark .skipif (not CVCUDA_AVAILABLE , reason = "test requires CVCUDA" )
4505+ ),
44964506 ],
44974507 )
44984508 def test_transform (self , param , value , make_input ):
@@ -4504,20 +4514,37 @@ def test_transform(self, param, value, make_input):
45044514
45054515 # `InterpolationMode.NEAREST` is modeled after the buggy `INTER_NEAREST` interpolation of CV2.
45064516 # The PIL equivalent of `InterpolationMode.NEAREST` is `InterpolationMode.NEAREST_EXACT`
4517+ @pytest .mark .parametrize (
4518+ "make_input" ,
4519+ [
4520+ make_image ,
4521+ pytest .param (
4522+ make_image_cvcuda , marks = pytest .mark .skipif (not CVCUDA_AVAILABLE , reason = "test requires CVCUDA" )
4523+ ),
4524+ ],
4525+ )
45074526 @pytest .mark .parametrize ("interpolation" , set (INTERPOLATION_MODES ) - {transforms .InterpolationMode .NEAREST })
4508- def test_functional_image_correctness (self , interpolation ):
4509- image = make_image (self .INPUT_SIZE , dtype = torch .uint8 )
4527+ def test_functional_image_correctness (self , make_input , interpolation ):
4528+ image = make_input (self .INPUT_SIZE , dtype = torch .uint8 )
45104529
45114530 actual = F .resized_crop (
45124531 image , ** self .CROP_KWARGS , size = self .OUTPUT_SIZE , interpolation = interpolation , antialias = True
45134532 )
4533+
4534+ if make_input is make_image_cvcuda :
4535+ image = cvcuda_to_pil_compatible_tensor (image )
4536+
45144537 expected = F .to_image (
45154538 F .resized_crop (
45164539 F .to_pil_image (image ), ** self .CROP_KWARGS , size = self .OUTPUT_SIZE , interpolation = interpolation
45174540 )
45184541 )
45194542
4520- torch .testing .assert_close (actual , expected , atol = 1 , rtol = 0 )
4543+ atol = 1
4544+ if make_input is make_image_cvcuda and interpolation == transforms .InterpolationMode .BICUBIC :
4545+ # CV-CUDA BICUBIC differs from PIL ground truth BICUBIC
4546+ atol = 10
4547+ assert_close (actual , expected , atol = atol , rtol = 0 )
45214548
45224549 def _reference_resized_crop_bounding_boxes (self , bounding_boxes , * , top , left , height , width , size ):
45234550 new_height , new_width = size
@@ -4992,7 +5019,7 @@ def test_image_correctness(self, output_size, make_input, fn):
49925019
49935020 actual = fn (image , output_size )
49945021
4995- if make_input == make_image_cvcuda :
5022+ if make_input is make_image_cvcuda :
49965023 image = cvcuda_to_pil_compatible_tensor (image )
49975024
49985025 expected = F .to_image (F .center_crop (F .to_pil_image (image ), output_size = output_size ))
0 commit comments