Rotate feature vector representing directions

Hello,

I am working on a project that uses a tensor of dimensions [features x width x height]. The features vector has size 2, representing the x and y physical displacements of a certain mechanical engineering experiment. I wanted to augment my data by rotating my samples, however the same approach as used to rotate images can’t be used, as the channels of a colour image don’t change when you move them in space, but my (x,y) channels have to change to align with my new rotation of the sample.

I know how to do this rotation “element wise”, as in, iterating through every spatial (width x height) coordinate of the tensor and applying a rotation matrix on the (x,y) features to get the new correct orientation for that point in the displacement field based on how much I rotated the sample. I would think, however, that there must be a more efficient way to do this transform. So I have 2 questions:

  • Is there a pytorch or numpy function or module that would help me apply the rotation to the entire displacement field at once?

  • If there isn’t, would it be better to build a matrix to do this rotation on the entire field or apply a for loop on all elements? And if the matrix rotation is more efficient, which I’m pretty sure it is, how would I go about building that matrix for the entire field?

This Wikipedia article gives you a good overview for rotation matrices and this post gives you a simple example in PyTorch, which could be a good starter. :slight_smile:

Hello @ptrblck, thank you for your response. As I said, I understand how to rotate the tensor as a whole and how to rotate individual [x, y] displacement components inside of the tensor.

My question was whether there was a way to do that rotation on all elements of the tensor at once in an efficient way. As of now, what I have are operations that have to be applied to every displacement element of the tensor space-wise, but I suspect there might be a better way to do it on the entire tensor with a single matrix multiplication. I was trying to tap into some brainpower here to help me with figuring out how to implement that, or at least, what that could look like.

My example compares the elementwise rotation with a rotation matrix using a matmul or wouldn’t it work for you?

The solution is great, the thing I’m getting at is that it works only for tensors where the feature vector’s data are non directional, unless I misunderstood something there. So your example works when a rotation works this way, for instance:

| [x1,y1]  [x3,y3] |    - - - - - - - - - - -  - - >     | [x2,y2]  [x1,y1] | 
| [x2,y2]  [x4,y4] |   90 degree clockwise rotation      | [x4,y4]  [x3,y3] | 

However mine works this way

| [x1,y1]  [x3,y3] |    - - - - - - - - - - -  - - >     | [x2*,y2*]  [x1*,y1*] | 
| [x2,y2]  [x4,y4] |   90 degree clockwise rotation      | [x4*,y4*]  [x3*,y3*] | 

The asterisk * symbolizes that the values inside of the features x_n and y_n themselves have changed according to the rotation because they encode directional data and have to point to a new direction if the overall element is rotated.

The solution I currently have first calculates the rotation for the directional features, which doesn’t “move” them “inside” of the tensor, just changes their numbers (essentially a rotation on a vector), and then actually rotates the tensor’s data using a similar approach to what you have there in the post.

This math stackexchange post might be closer to what I’m talking about, but I have to explore it further. I have a gut feeling this thread might bring me closer to the solution I’m looking for, but right now I need to rest. I’ll post a solution if I find it.
https://math.stackexchange.com/questions/912070/rotating-vector-functions#:~:text=To%20rotate%20the%20E%20vectors,position%20of%20every%20vector%20E.

Oh, I missed the “directional features” part (or didn’t understand it properly) so thanks for explaining.
I think your approach of using two transformations makes sense. Let us know, if you came up with a solution or get stuck somewhere.