@@ -6747,63 +6747,28 @@ def test_functional_error(self):
67476747class TestToCVCUDATensor :
67486748 """Tests for to_cvcuda_tensor function following patterns from TestToPil"""
67496749
6750- def test_1_channel_uint8_tensor_to_cvcuda_tensor (self ):
6750+ @pytest .mark .parametrize ("dtype" , [torch .uint8 , torch .uint16 , torch .float32 , torch .float64 ])
6751+ def test_1_channel_to_cvcuda_tensor (self , dtype ):
67516752 # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6752- img_data = torch .randint (0 , 256 , (1 , 4 , 4 ), dtype = torch .uint8 )
6753- img_data = img_data .cuda ()
6754- cvcuda_img = F .to_cvcuda_tensor (img_data )
6755- # Check that the conversion succeeded and format is correct
6756- assert cvcuda_img is not None
6757-
6758- def test_1_channel_int16_tensor_to_cvcuda_tensor (self ):
6759- # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6760- img_data = torch .randint (0 , 256 , (1 , 4 , 4 ), dtype = torch .int16 )
6761- img_data = img_data .cuda ()
6762- cvcuda_img = F .to_cvcuda_tensor (img_data )
6763- assert cvcuda_img is not None
6764-
6765- def test_1_channel_int32_tensor_to_cvcuda_tensor (self ):
6766- # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6767- img_data = torch .randint (0 , 256 , (1 , 4 , 4 ), dtype = torch .int32 )
6768- img_data = img_data .cuda ()
6769- cvcuda_img = F .to_cvcuda_tensor (img_data )
6770- assert cvcuda_img is not None
6771-
6772- def test_1_channel_float32_tensor_to_cvcuda_tensor (self ):
6773- # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6774- img_data = torch .rand (1 , 4 , 4 )
6775- img_data = img_data .cuda ()
6776- cvcuda_img = F .to_cvcuda_tensor (img_data )
6777- assert cvcuda_img is not None
6778-
6779- def test_3_channel_uint8_tensor_to_cvcuda_tensor (self ):
6780- # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6781- img_data = torch .randint (0 , 256 , (3 , 4 , 4 ), dtype = torch .uint8 )
6753+ if dtype in (torch .uint8 , torch .uint16 ):
6754+ img_data = torch .randint (0 , 256 , (1 , 4 , 4 ), dtype = dtype )
6755+ else :
6756+ img_data = torch .rand (1 , 4 , 4 , dtype = dtype )
67826757 img_data = img_data .cuda ()
67836758 cvcuda_img = F .to_cvcuda_tensor (img_data )
67846759 assert cvcuda_img is not None
67856760
6786- def test_3_channel_float32_tensor_to_cvcuda_tensor (self ):
6761+ @pytest .mark .parametrize ("dtype" , [torch .uint8 , torch .uint16 , torch .float32 , torch .float64 ])
6762+ def test_3_channel_to_cvcuda_tensor (self , dtype ):
67876763 # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6788- img_data = torch .rand (3 , 4 , 4 )
6764+ if dtype in (torch .uint8 , torch .uint16 ):
6765+ img_data = torch .randint (0 , 256 , (3 , 4 , 4 ), dtype = dtype )
6766+ else :
6767+ img_data = torch .rand (3 , 4 , 4 , dtype = dtype )
67896768 img_data = img_data .cuda ()
67906769 cvcuda_img = F .to_cvcuda_tensor (img_data )
67916770 assert cvcuda_img is not None
67926771
6793- def test_unsupported_num_channels (self ):
6794- # Test 2-channel image (not supported)
6795- # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6796- img_data = torch .rand (2 , 5 , 5 )
6797- img_data = img_data .cuda ()
6798- with pytest .raises (ValueError , match = "Only 1 and 3 channel images are supported" ):
6799- F .to_cvcuda_tensor (img_data )
6800-
6801- # Test 4-channel image (not supported)
6802- img_data = torch .randint (0 , 256 , (4 , 5 , 5 ), dtype = torch .uint8 )
6803- img_data = img_data .cuda ()
6804- with pytest .raises (ValueError , match = "Only 1 and 3 channel images are supported" ):
6805- F .to_cvcuda_tensor (img_data )
6806-
68076772 def test_invalid_input_type (self ):
68086773 with pytest .raises (TypeError , match = r"pic should be `torch.Tensor`" ):
68096774 F .to_cvcuda_tensor ("invalid_input" )
@@ -6828,32 +6793,12 @@ def test_invalid_dimensions(self):
68286793 img_data = img_data .cuda ()
68296794 F .to_cvcuda_tensor (img_data )
68306795
6831- def test_float64_tensor_to_cvcuda_tensor (self ):
6832- # Test single channel float64 (F64 format is supported)
6833- # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6834- img_data = torch .rand (1 , 4 , 4 , dtype = torch .float64 )
6835- img_data = img_data .cuda ()
6836- cvcuda_img = F .to_cvcuda_tensor (img_data )
6837- assert cvcuda_img is not None
6838-
6839- def test_float64_rgb_not_supported (self ):
6840- # Test 3-channel float64 is NOT supported (no RGBf64 format in CV-CUDA)
6841- # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6842- img_data = torch .rand (3 , 4 , 4 , dtype = torch .float64 )
6843- img_data = img_data .cuda ()
6844- with pytest .raises (TypeError , match = r"Unsupported dtype" ):
6845- F .to_cvcuda_tensor (img_data )
6846-
68476796 @pytest .mark .parametrize ("num_channels" , [1 , 3 ])
6848- @pytest .mark .parametrize ("dtype" , [torch .uint8 , torch .int16 , torch . int32 , torch .float32 , torch .float64 ])
6797+ @pytest .mark .parametrize ("dtype" , [torch .uint8 , torch .uint16 , torch .float32 , torch .float64 ])
68496798 def test_round_trip (self , num_channels , dtype ):
6850- # Skip int16/int32/float64 for 3-channel (only supported for single-channel)
6851- if num_channels == 3 and dtype in (torch .int16 , torch .int32 , torch .float64 ):
6852- pytest .skip (f"{ dtype } is only supported for single-channel images" )
6853-
68546799 # Setup: Create a tensor in CHW format (PyTorch standard)
68556800 # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6856- if dtype in (torch .uint8 , torch .int16 , torch . int32 ):
6801+ if dtype in (torch .uint8 , torch .uint16 ):
68576802 original_tensor = torch .randint (0 , 256 , (num_channels , 4 , 4 ), dtype = dtype )
68586803 else :
68596804 original_tensor = torch .rand (num_channels , 4 , 4 , dtype = dtype )
@@ -6871,16 +6816,12 @@ def test_round_trip(self, num_channels, dtype):
68716816 torch .testing .assert_close (result_tensor , original_tensor , rtol = 0 , atol = 0 )
68726817
68736818 @pytest .mark .parametrize ("num_channels" , [1 , 3 ])
6874- @pytest .mark .parametrize ("dtype" , [torch .uint8 , torch .int16 , torch . int32 , torch .float32 , torch .float64 ])
6819+ @pytest .mark .parametrize ("dtype" , [torch .uint8 , torch .uint16 , torch .float32 , torch .float64 ])
68756820 @pytest .mark .parametrize ("batch_size" , [1 , 2 , 4 ])
68766821 def test_round_trip_batched (self , num_channels , dtype , batch_size ):
6877- # Skip int16/int32/float64 for 3-channel (only supported for single-channel)
6878- if num_channels == 3 and dtype in (torch .int16 , torch .int32 , torch .float64 ):
6879- pytest .skip (f"{ dtype } is only supported for single-channel images" )
6880-
68816822 # Setup: Create a batched tensor in NCHW format
68826823 # Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6883- if dtype in (torch .uint8 , torch .int16 , torch . int32 ):
6824+ if dtype in (torch .uint8 , torch .uint16 ):
68846825 original_tensor = torch .randint (0 , 256 , (batch_size , num_channels , 4 , 4 ), dtype = dtype )
68856826 else :
68866827 original_tensor = torch .rand (batch_size , num_channels , 4 , 4 , dtype = dtype )
0 commit comments