| 22 | |
| 23 | |
| 24 | def main() -> None: |
| 25 | # Basic tensor with explicit layout |
| 26 | tensor1 = cvcuda.Tensor((224, 224, 3), np.uint8, layout="HWC") # noqa: F841 |
| 27 | |
| 28 | # Batch of images |
| 29 | tensor2 = cvcuda.Tensor((10, 224, 224, 3), np.float32, layout="NHWC") # noqa: F841 |
| 30 | |
| 31 | # For image batch (infers NHWC layout from format) |
| 32 | tensor3 = cvcuda.Tensor( # noqa: F841 |
| 33 | nimages=5, imgsize=(640, 480), format=cvcuda.Format.RGB8 |
| 34 | ) |
| 35 | |
| 36 | # With row alignment for optimized memory access |
| 37 | tensor4 = cvcuda.Tensor( # noqa: F841 |
| 38 | (224, 224, 3), np.uint8, layout="HWC", rowalign=32 |
| 39 | ) # Align rows to 32-byte boundaries |
| 40 | |
| 41 | # Generic N-D tensor |
| 42 | tensor5 = cvcuda.Tensor((100, 50, 25), np.float32, layout="DHW") # noqa: F841 |
| 43 | |
| 44 | # Wrap existing torch tensor (zero-copy, NHWC) |
| 45 | torch_tensor = torch.zeros((10, 224, 224, 3), dtype=torch.float32, device="cuda") |
| 46 | cvcuda_tensor = cvcuda.as_tensor(torch_tensor, layout="NHWC") |
| 47 | |
| 48 | # Common ML layout: NCHW |
| 49 | torch_nchw = torch.randn((4, 3, 256, 256), dtype=torch.float32, device="cuda") |
| 50 | cvcuda_nchw = cvcuda.as_tensor(torch_nchw, layout="NCHW") # noqa: F841 |
| 51 | |
| 52 | # Bidirectional: CV-CUDA back to torch (also zero-copy) |
| 53 | torch_output = torch.as_tensor(cvcuda_tensor.cuda(), device="cuda") # noqa: F841 |
| 54 | |
| 55 | # Video tensor with temporal dimension (Batch, Frames, Height, Width, Channels) |
| 56 | video_tensor = cvcuda.Tensor( # noqa: F841 |
| 57 | (2, 30, 720, 1280, 3), np.uint8, layout="NDHWC" |
| 58 | ) |
| 59 | |
| 60 | |
| 61 | # docs-end: main |