PIL转PytorchTensor:不进行归一化

1.PIL转Tensor可以使用torchvision.transforms.toTensor(),但是该方法会进行归一化,将原本的Int转成float或其他浮点数,若果不想归一化,参考2.

2.

from PIL import Image
import numpy as np

img = Image.open(...)
img = np.array(img)
img = torch.tensor(img)
print(img.size(),type(img))
print(img)
print(img.max(),img.min())

结果:

torch.Size([324, 500]) <class 'torch.Tensor'>

tensor([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,

.........

          10,  10,  10,  10,  10,  10,  10,  10,  10,  10]], dtype=torch.uint8)
tensor(255, dtype=torch.uint8) tensor(0, dtype=torch.uint8)

发布了51 篇原创文章 · 获赞 29 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qwer7512090/article/details/103438916