|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | +from torchvision import _is_cvcuda_available |
| 4 | +from torchvision.transforms.v2 import functional as F |
| 5 | + |
| 6 | +CVCUDA_AVAILABLE = _is_cvcuda_available() |
| 7 | +CUDA_AVAILABLE = torch.cuda.is_available() |
| 8 | + |
| 9 | + |
| 10 | +if CVCUDA_AVAILABLE: |
| 11 | + import nvcv |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.skipif(CVCUDA_AVAILABLE is False, reason="test requires CVCUDA") |
| 15 | +@pytest.mark.skipif(CUDA_AVAILABLE is False, reason="test requires CUDA") |
| 16 | +class TestToNvcvTensor: |
| 17 | + """Tests for to_nvcv_tensor function following patterns from TestToPil""" |
| 18 | + |
| 19 | + def test_1_channel_uint8_tensor_to_nvcv_tensor(self): |
| 20 | + img_data = torch.ByteTensor(1, 4, 4).random_(0, 255).cuda() |
| 21 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 22 | + # Check that the conversion succeeded and format is correct |
| 23 | + assert nvcv_img is not None |
| 24 | + |
| 25 | + def test_1_channel_int16_tensor_to_nvcv_tensor(self): |
| 26 | + img_data = torch.ShortTensor(1, 4, 4).random_().cuda() |
| 27 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 28 | + assert nvcv_img is not None |
| 29 | + |
| 30 | + def test_1_channel_int32_tensor_to_nvcv_tensor(self): |
| 31 | + img_data = torch.IntTensor(1, 4, 4).random_().cuda() |
| 32 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 33 | + assert nvcv_img is not None |
| 34 | + |
| 35 | + def test_1_channel_float32_tensor_to_nvcv_tensor(self): |
| 36 | + img_data = torch.Tensor(1, 4, 4).uniform_().cuda() |
| 37 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 38 | + assert nvcv_img is not None |
| 39 | + |
| 40 | + def test_2_channel_uint8_tensor_to_nvcv_tensor(self): |
| 41 | + img_data = torch.ByteTensor(2, 4, 4).random_(0, 255).cuda() |
| 42 | + # NVCV doesn't support 2-channel uint8 images |
| 43 | + with pytest.raises(TypeError, match="Unsupported dtype.*for 2-channel image"): |
| 44 | + F.to_nvcv_tensor(img_data) |
| 45 | + |
| 46 | + def test_2_channel_float32_tensor_to_nvcv_tensor(self): |
| 47 | + img_data = torch.Tensor(2, 4, 4).uniform_().cuda() |
| 48 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 49 | + assert nvcv_img is not None |
| 50 | + |
| 51 | + def test_3_channel_uint8_tensor_to_nvcv_tensor(self): |
| 52 | + img_data = torch.ByteTensor(3, 4, 4).random_(0, 255).cuda() |
| 53 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 54 | + assert nvcv_img is not None |
| 55 | + |
| 56 | + def test_3_channel_float32_tensor_to_nvcv_tensor(self): |
| 57 | + img_data = torch.Tensor(3, 4, 4).uniform_().cuda() |
| 58 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 59 | + assert nvcv_img is not None |
| 60 | + |
| 61 | + def test_4_channel_uint8_tensor_to_nvcv_tensor(self): |
| 62 | + img_data = torch.ByteTensor(4, 4, 4).random_(0, 255).cuda() |
| 63 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 64 | + assert nvcv_img is not None |
| 65 | + |
| 66 | + def test_4_channel_float32_tensor_to_nvcv_tensor(self): |
| 67 | + img_data = torch.Tensor(4, 4, 4).uniform_().cuda() |
| 68 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 69 | + assert nvcv_img is not None |
| 70 | + |
| 71 | + def test_2d_uint8_tensor_to_nvcv_tensor(self): |
| 72 | + img_data = torch.ByteTensor(4, 4).random_(0, 255).cuda() |
| 73 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 74 | + assert nvcv_img is not None |
| 75 | + |
| 76 | + def test_2d_float32_tensor_to_nvcv_tensor(self): |
| 77 | + img_data = torch.Tensor(4, 4).uniform_().cuda() |
| 78 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 79 | + assert nvcv_img is not None |
| 80 | + |
| 81 | + def test_1_channel_uint8_ndarray_to_nvcv_tensor(self): |
| 82 | + img_data = torch.ByteTensor(4, 4, 1).random_(0, 255).numpy() |
| 83 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 84 | + assert nvcv_img is not None |
| 85 | + |
| 86 | + def test_3_channel_uint8_ndarray_to_nvcv_tensor(self): |
| 87 | + img_data = torch.ByteTensor(4, 4, 3).random_(0, 255).numpy() |
| 88 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 89 | + assert nvcv_img is not None |
| 90 | + |
| 91 | + def test_4_channel_uint8_ndarray_to_nvcv_tensor(self): |
| 92 | + img_data = torch.ByteTensor(4, 4, 4).random_(0, 255).numpy() |
| 93 | + nvcv_img = F.to_nvcv_tensor(img_data) |
| 94 | + assert nvcv_img is not None |
| 95 | + |
| 96 | + def test_explicit_format_rgb8(self): |
| 97 | + img_data = torch.ByteTensor(3, 4, 4).random_(0, 255).cuda() |
| 98 | + nvcv_img = F.to_nvcv_tensor(img_data, format=nvcv.Format.RGB8) |
| 99 | + assert nvcv_img is not None |
| 100 | + |
| 101 | + def test_explicit_format_bgr8(self): |
| 102 | + img_data = torch.ByteTensor(3, 4, 4).random_(0, 255).cuda() |
| 103 | + nvcv_img = F.to_nvcv_tensor(img_data, format=nvcv.Format.BGR8) |
| 104 | + assert nvcv_img is not None |
| 105 | + |
| 106 | + def test_explicit_format_hsv8(self): |
| 107 | + img_data = torch.ByteTensor(3, 4, 4).random_(0, 255).cuda() |
| 108 | + # HSV8 should work for 3-channel images |
| 109 | + nvcv_img = F.to_nvcv_tensor(img_data, format=nvcv.Format.HSV8) |
| 110 | + assert nvcv_img is not None |
| 111 | + |
| 112 | + def test_explicit_format_rgba8(self): |
| 113 | + img_data = torch.ByteTensor(4, 4, 4).random_(0, 255).cuda() |
| 114 | + nvcv_img = F.to_nvcv_tensor(img_data, format=nvcv.Format.RGBA8) |
| 115 | + assert nvcv_img is not None |
| 116 | + |
| 117 | + def test_explicit_format_bgra8(self): |
| 118 | + img_data = torch.ByteTensor(4, 4, 4).random_(0, 255).cuda() |
| 119 | + # BGRA8 should work for 4-channel images |
| 120 | + nvcv_img = F.to_nvcv_tensor(img_data, format=nvcv.Format.BGRA8) |
| 121 | + assert nvcv_img is not None |
| 122 | + |
| 123 | + def test_invalid_input_type(self): |
| 124 | + with pytest.raises(TypeError, match=r"pic should be Tensor or ndarray"): |
| 125 | + F.to_nvcv_tensor("invalid_input") |
| 126 | + |
| 127 | + def test_invalid_dimensions(self): |
| 128 | + # Test 1D array (too few dimensions) |
| 129 | + with pytest.raises(ValueError, match=r"pic should be 2/3/4 dimensional"): |
| 130 | + F.to_nvcv_tensor(torch.ByteTensor(4).cuda()) |
| 131 | + |
| 132 | + # Test 5D array (too many dimensions) |
| 133 | + with pytest.raises(ValueError, match=r"pic should be 2/3/4 dimensional"): |
| 134 | + F.to_nvcv_tensor(torch.ByteTensor(1, 1, 3, 4, 4).cuda()) |
| 135 | + |
| 136 | + def test_too_many_channels(self): |
| 137 | + with pytest.raises(ValueError, match=r"pic should not have > 4 channels"): |
| 138 | + F.to_nvcv_tensor(torch.ByteTensor(5, 4, 4).random_(0, 255).cuda()) |
| 139 | + |
| 140 | + def test_unsupported_dtype_for_channels(self): |
| 141 | + # Float64 is not supported |
| 142 | + img_data = torch.DoubleTensor(3, 4, 4).uniform_().cuda() |
| 143 | + with pytest.raises(TypeError, match=r"Unsupported dtype"): |
| 144 | + F.to_nvcv_tensor(img_data) |
| 145 | + |
| 146 | + |
| 147 | +def make_nvcv_image(num_channels=3, dtype=torch.uint8): |
| 148 | + """Helper function to create NVCV Tensor for testing""" |
| 149 | + if dtype == torch.uint8: |
| 150 | + img_data = torch.ByteTensor(num_channels, 4, 4).random_(0, 255).cuda() |
| 151 | + else: |
| 152 | + img_data = torch.Tensor(num_channels, 4, 4).uniform_().cuda() |
| 153 | + return F.to_nvcv_tensor(img_data) |
| 154 | + |
| 155 | + |
| 156 | +def transform_cls_to_functional(get_transform_cls): |
| 157 | + def wrapper(inpt): |
| 158 | + transform_cls = get_transform_cls() |
| 159 | + return transform_cls()(inpt) |
| 160 | + |
| 161 | + return wrapper |
| 162 | + |
| 163 | + |
| 164 | +@pytest.mark.skipif(CVCUDA_AVAILABLE is False, reason="test requires CVCUDA") |
| 165 | +@pytest.mark.skipif(CUDA_AVAILABLE is False, reason="test requires CUDA") |
| 166 | +class TestNVCVToTensor: |
| 167 | + @pytest.mark.parametrize("num_channels", [1, 3, 4]) |
| 168 | + @pytest.mark.parametrize("dtype", [torch.uint8, torch.float32]) |
| 169 | + @pytest.mark.parametrize( |
| 170 | + "fn", |
| 171 | + [ |
| 172 | + "functional", |
| 173 | + "transform", |
| 174 | + ], |
| 175 | + ) |
| 176 | + def test_functional_and_transform(self, num_channels, dtype, fn): |
| 177 | + input = make_nvcv_image(num_channels=num_channels, dtype=dtype) |
| 178 | + |
| 179 | + # Delay function reference until test execution time |
| 180 | + if fn == "functional": |
| 181 | + fn_ref = F.nvcv_to_tensor |
| 182 | + else: # fn == "transform" |
| 183 | + fn_ref = transform_cls_to_functional( |
| 184 | + lambda: __import__("torchvision.transforms.v2", fromlist=["NVCVToTensor"]).NVCVToTensor |
| 185 | + ) |
| 186 | + |
| 187 | + output = fn_ref(input) |
| 188 | + |
| 189 | + assert isinstance(output, torch.Tensor) |
| 190 | + # Convert input to tensor to compare sizes |
| 191 | + input_tensor = F.nvcv_to_tensor(input) |
| 192 | + assert F.get_size(output) == F.get_size(input_tensor) |
| 193 | + |
| 194 | + def test_functional_error(self): |
| 195 | + with pytest.raises(TypeError, match="nvcv_img should be NVCV Tensor"): |
| 196 | + F.nvcv_to_tensor(object()) |
0 commit comments