Direction of rotation of torch.rot90

Viewed 439

In the documentation for torch.rot90 it is stated that

Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0.

However say that we are rotating from axis 0 to axis 1, is axis 0 rotating to axis 1 in the clockwise or anti-clockwise direction? (since they are both 90 degree rotations as per the image below)

enter image description here

2 Answers

axis=0 is the dimension that points downwards, while axis=1 points to the right. Visualize the axes like this:

---------> axis=1
|
|
|
\/
axis=0

Now, k>0 means counter-clockwise direction, k<0 is clockwise.

Thus,

>>> x = torch.arange(6).view(3, 2)
>>> x
tensor([[0, 1],
        [2, 3],
        [4, 5]])

>>> torch.rot90(x, 1, [0,1])
tensor([[1, 3, 5],
        [0, 2, 4]])

>>> torch.rot90(x, 1, [1,0])
tensor([[4, 2, 0],
        [5, 3, 1]])

The torch.rot90() is similar to numpy.rot90()

e.g.

numpy.rot90(m, k=1, axes=(0, 1))

mean

enter image description here

To me those rotations make more sense when reasonning with heights and widths. If you consider axis=0 as the height dimension and axis=1 as the width dimension. Then:

Take a simple input matrix x:

>>> x
tensor([[0, 1],
        [2, 3]])
  • k > 0: axis=0 towards axis=1, it corresponds to "height towards width", which means anti-clockwise.

    >>> x.rot90(k=1)
    tensor([[2, 0],
            [3, 1]])
    
  • k < 0: axis=1 towards axis=0, this time it's "width towards height, i.e. clockwise rotation.

    >>> x.rot90(k=-1)
    tensor([[1, 3],
            [0, 2]])
    
Related