Rotation matrix for 4D Tensor

时间:2019-05-16 09:33:39

标签: python math deep-learning pytorch tensor

I have a tensor with shape 8,24,24,24 which represents a 3D-voxelized picture with 8 channels. Now I have to rotate this, to get all 24 permutations. Because the data are very big, I want to rotate on the fly. For this, I think the most efficient way is the multiplication with a rotation matrix.

I have to say, that i absolutely have no idea, who to calculate a tensor, which I can use to rotate the 4D-tensor or better the picture of every channel without a for-loop.

I only found rotation of a vector in 3D-space or using a predefined command. But who I rotate a whole tensor?

x = np.arange(27).reshape(3,3,3)
np.rot90(x)

array([[[ 6,  7,  8],
       [15, 16, 17],
       [24, 25, 26]],

      [[ 3,  4,  5],
       [12, 13, 14],
       [21, 22, 23]],

      [[ 0,  1,  2],
       [ 9, 10, 11],
       [18, 19, 20]]])

I found, that a rotation in 3D is nothing else than a rotation in 2D with specified axis. For this one can use for example this:

r = [[1,0,0],[0,0,-1],[0,1,0]]
np.matmul(x,r)

array([[[  0,   2,  -1],
       [  3,   5,  -4],
       [  6,   8,  -7]],

      [[  9,  11, -10],
       [ 12,  14, -13],
       [ 15,  17, -16]],

      [[ 18,  20, -19],
       [ 21,  23, -22],
       [ 24,  26, -25]]])

But the result is completely different. Did I misunderstood something?

In addition: actually I use the following:

def calc_rot(data):
        t1 = []
        n1 = data
        for i in range(4):
            n1 = np.rot90(n1, axes=(2, 1))
            t1.append(n1)
            n2 = np.copy(n1)
            for j in range(3):
                n2 = np.rot90(n2, axes=(3, 2))
                t1.append(n2)
        for k in [0, 2, 4, 6, 8, 10, 12, 14]:
            t1.append(np.rot90(t1[k], axes=(3, 1)))
        return t1

def calc_rot2(data):
        t1 = []
        n1 = data
        for i in range(4):
            n1 = n1.swapaxes(2,1)[:,:,::-1,:]
            t1.append(n1)
            n2 = np.copy(n1)
            for j in range(3):
                n2 = n2.swapaxes(3,2)[:,:,::-1,:]
                t1.append(n2)
        for k in [0, 2, 4, 6, 8, 10, 12, 14]:
            t1.append(t1[k].swapaxes(3,1)[:,:,::-1,:])
        return t1

Both do the same. Swapaxes seems a little bit faster. But I think in general a tensor/matrix-multiplication should be faster. Right?

0 个答案:

没有答案