I have a PIL image i want to convert to a tensor, but when i do this it converts the data from [0 -255] to [1.0 - 0.0]. How do i get the ToTensor() function to convert to a tensor of uint8?
You could use
from_numpy to transform the type from a numpy array to a PyTorch tensor without any normalization:
# create or load PIL.Image tmp = np.random.randint(0, 255, (224, 224, 3)).astype(np.uint8) img = PIL.Image.fromarray(tmp) # get numpy array arr = np.array(img) tensor = torch.from_numpy(arr) print(tensor)
You might additionally want to
permute the axes to make the tensor channels-first, which is the expected default format.