"Convert PIL style `image` array to torch style image tensor."
a = np.asarray(image)
if a.ndim==2 : a = np.expand_dims(a,2)
a = np.transpose(a, (1, 0, 2))
a = np.transpose(a, (2, 1, 0))
return torch.from_numpy(a.astype(dtype, copy=False) )
Does anybody know why there are 2 transposes in this pil2tensor function?
Maybe it’s for clarity?
The first reverses width and height, and the second moves the channels to dimension zero, the way PyTorch expects. Though in the absence of any explanation, using two transposes (that could be one) make the author’s intention even more unclear!
I’d guess that numpy’s transpose does not move any data but only fiddles with indexing. So using an extra transpose does not cost much.
To me it just looks like poor commenting, which results in more confusion for the next person.