Transfer NumPy to GPU using PyTorch, then create CV-CUDA tensor. For more details on PyTorch interop, see pytorch_interop.py
()
| 59 | |
| 60 | |
| 61 | def numpy_to_cvcuda_via_torch(): |
| 62 | """Transfer NumPy to GPU using PyTorch, then create CV-CUDA tensor. |
| 63 | |
| 64 | For more details on PyTorch interop, see pytorch_interop.py |
| 65 | """ |
| 66 | import torch |
| 67 | |
| 68 | # docs_tag: begin_numpy_torch |
| 69 | numpy_array = np.random.randn(10, 10).astype(np.float32) |
| 70 | |
| 71 | torch_tensor = torch.from_numpy(numpy_array).cuda() |
| 72 | |
| 73 | cvcuda_tensor = cvcuda.as_tensor(torch_tensor) |
| 74 | # docs_tag: end_numpy_torch |
| 75 | |
| 76 | # docs_tag: begin_numpy_torch_verify |
| 77 | result_torch = torch.as_tensor(cvcuda_tensor.cuda()).clone() |
| 78 | result_array = result_torch.cpu().numpy() |
| 79 | |
| 80 | assert np.allclose(numpy_array, result_array) |
| 81 | # docs_tag: end_numpy_torch_verify |
| 82 | |
| 83 | |
| 84 | def numpy_to_cvcuda_via_cupy(): |