Rotate feature vector representing directions

waltherwj: <p>Hello,</p>
<p>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.</p>
<p>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:</p>
<ul>
<li>
<p>Is there a pytorch or numpy function or module that would help me apply the rotation to the entire displacement field at once?</p>
</li>
<li>
<p>If there isn’t, would it be better to build a matrix to do this rotation on the entire field or apply a <code>for</code> 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?</p>
</li>
</ul>

ptrblck: <p><a href="https://en.wikipedia.org/wiki/Rotation_matrix">This Wikipedia article</a> gives you a good overview for rotation matrices and <a href="https://discuss.pytorch.org/t/how-to-rotate-90-and-270-degrees-of-5d-tensor/22476/13">this post</a> gives you a simple example in PyTorch, which could be a good starter. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

waltherwj: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a>, 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.</p>
<p>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.</p>

ptrblck: <p>My example compares the elementwise rotation with a rotation matrix using a matmul or wouldn’t it work for you?</p>

waltherwj: <p>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:</p>
<pre><code class="lang-auto">| [x1,y1]  [x3,y3] |    - - - - - - - - - - -  - - &gt;     | [x2,y2]  [x1,y1] | 
| [x2,y2]  [x4,y4] |   90 degree clockwise rotation      | [x4,y4]  [x3,y3] | 
</code></pre>
<p>However mine works this way</p>
<pre><code class="lang-auto">| [x1,y1]  [x3,y3] |    - - - - - - - - - - -  - - &gt;     | [x2*,y2*]  [x1*,y1*] | 
| [x2,y2]  [x4,y4] |   90 degree clockwise rotation      | [x4*,y4*]  [x3*,y3*] | 
</code></pre>
<p>The asterisk <code>*</code> 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.</p>
<p>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.</p>
<p>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.<br>
<a href="https://math.stackexchange.com/questions/912070/rotating-vector-functions#:~:text=To%20rotate%20the%20E%20vectors,position%20of%20every%20vector%20E" rel="nofollow noopener">https://math.stackexchange.com/questions/912070/rotating-vector-functions#:~:text=To%20rotate%20the%20E%20vectors,position%20of%20every%20vector%20E</a>.</p>

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