Loading an unsigned 8bit integer

The second approach is the common one and an uint8 image tensor will be normalized to a float32 tensor in the range [0, 1] using torchvision.transforms.ToTensor() as seen in this example:

# load uint8 image
img = PIL.Image.open(PATH)
# or generate random image
img = transforms.ToPILImage()(torch.randn(3, 224, 224))
print(np.array(img).min(), np.array(img).max())
# 0 255

out = transforms.ToTensor()(img)
print(out.min(), out.max())
# tensor(0.) tensor(1.)

8bit integers, i.e. images using the uint8 data type, can be mapped to float32 without a loss in precision, since float32 can represent all integers up to 2**24 where a rounding to multiple of 2s starts:

torch.tensor(2.**24)
# tensor(16777216.)
torch.tensor(2.**24+1)
# tensor(16777216.)
torch.tensor(2.**24+2)
# tensor(16777218.)

in case you want to keep the original integer values. If not, you can also verify that a “reverse” mapping is possible:

y = out * 255
(y == torch.from_numpy(np.array(img)).permute(2, 0, 1)).all()
# tensor(true)
1 Like