- numpy和pillow
im = np.array(Image.open(im)) im = Image.fromarray(im)
- list和numpy互转
temp = np.array(list) arr = temp.tolist()
- numpy和tensor互转
注意,第一只有都在CPU上的才能相互转换
第二直接使用from_numpy的是指向了同一个地址
第三如果想创建新的b = torch.tensor(a)
import torch import numpy as np a = np.array([1, 2, 3]) b = torch.from_numpy(a) # c=b.numpy() d = torch.tensor(a) e = torch.as_tensor(a) a[1] = 3 print(b) print(d) print(e) 输出: tensor([1, 3, 3], dtype=torch.int32) tensor([1, 2, 3], dtype=torch.int32) tensor([1, 3, 3], dtype=torch.int32)



