MCPcopy Create free account
hub / github.com/CVCUDA/CV-CUDA / numpy_to_cvcuda_via_torch

Function numpy_to_cvcuda_via_torch

samples/interoperability/numpy_interop.py:61–81  ·  view source on GitHub ↗

Transfer NumPy to GPU using PyTorch, then create CV-CUDA tensor. For more details on PyTorch interop, see pytorch_interop.py

()

Source from the content-addressed store, hash-verified

59
60
61def 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
84def numpy_to_cvcuda_via_cupy():

Callers 1

mainFunction · 0.85

Calls 2

cpuMethod · 0.80
cudaMethod · 0.45

Tested by

no test coverage detected