PyTorch数据和NumPy数据之间的互操作

说明,由于Python的缓存协议,只要PyTorch的数据是在cpu上,不是在GPU上,那么torch.Tensor类型的数据和numpy.ndarray的数据是共享内存的,相互之间的改变相互影响.This zero-copy interoperability with NumPy arrays is due to the storage system working with the Python buffer protocol (https://docs.python.org/3/c-api/buffer.html).

numpy转为torch:

(ssd4pytorch1_2_0) C:\Users\chenxuqi>
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import torch
>>> points_np = np.ones(12).reshape(3,4)
>>> points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> points = torch.from_numpy(points_np)
>>> points_cuda = torch.from_numpy(points_np).cuda()
>>> ##########################################################
>>> points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> points
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], dtype=torch.float64)
>>> points_cuda
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0', dtype=torch.float64)
>>> points_np[0][0]=999
>>> points[0][1]=888
>>> points_cuda[0][2]=777
>>> points_np
array([[999., 888.,   1.,   1.],
       [  1.,   1.,   1.,   1.],
       [  1.,   1.,   1.,   1.]])
>>> points
tensor([[999., 888.,   1.,   1.],
        [  1.,   1.,   1.,   1.],
        [  1.,   1.,   1.,   1.]], dtype=torch.float64)
>>> points_cuda
tensor([[  1.,   1., 777.,   1.],
        [  1.,   1.,   1.,   1.],
        [  1.,   1.,   1.,   1.]], device='cuda:0', dtype=torch.float64)
>>> id(points_np)
1999751313328
>>> id(points)
1999751939640
>>> id(points_cuda)
1999804358072
>>>
>>> ^Z

torchGPU转为numpy:

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> points_cuda = torch.ones(3, 4).cuda()
>>> points_cpu = points_cuda.cpu()
>>> points_np = points_cuda.numpy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
>>> points_np = points_cuda.cpu().numpy()
>>> id(points_np)
1990030518992
>>> id(points_cpu)
1989698386344
>>> id(points_cuda)
1990030519736
>>> points_cuda
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0')
>>> points_cpu
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
>>> points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)
>>> points_cuda[0][0] = 99
>>> points_cpu[0][1]=88
>>> points_np[0][2]=77
>>> points_cuda
tensor([[99.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.]], device='cuda:0')
>>> points_cpu
tensor([[ 1., 88.,  1.,  1.],
        [ 1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.]])
>>> points_np
array([[ 1.,  1., 77.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]], dtype=float32)
>>>

torchCPU转为numpy:

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> points_cpu = torch.ones(3, 4)
>>> points_np = points_cpu.numpy()
>>> points_cpu
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
>>> points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)
>>> id(points_cpu)==id(points_np)
False
>>> points_cpu[0][0]=999
>>> points_np[0][1]=888
>>> points_np
array([[999., 888.,   1.,   1.],
       [  1.,   1.,   1.,   1.],
       [  1.,   1.,   1.,   1.]], dtype=float32)
>>> points_cpu
tensor([[999., 888.,   1.,   1.],
        [  1.,   1.,   1.,   1.],
        [  1.,   1.,   1.,   1.]])
>>>
>>> type(points_cpu)
<class 'torch.Tensor'>
>>> id(points_np)
1291906427600
>>> type(points_np)
<class 'numpy.ndarray'>
>>>
>>>

承接Matlab、Python和C++的编程,机器学习、计算机视觉的理论实现及辅导,本科和硕士的均可,咸鱼交易,专业回答请走知乎,详谈请联系QQ号757160542,非诚勿扰。

 

猜你喜欢

转载自blog.csdn.net/weixin_36670529/article/details/115253984