pythonnumpypytorch

Pytorch tensor to numpy array


I have a pytorch Tensor of shape [4, 3, 966, 1296]. I want to convert it to numpy array using the following code:

imgs = imgs.numpy()[:, ::-1, :, :]

How does that code work?


Solution

  • There are 4 dimensions of the tensor you want to convert.

    [:, ::-1, :, :] 
    

    : means that the first dimension should be copied as it is and converted, same goes for the third and fourth dimension.

    ::-1 means that for the second axes it reverses the the axes