Matrix 张量流中的三维旋转矩阵

Matrix 张量流中的三维旋转矩阵,matrix,tensorflow,Matrix,Tensorflow,我想用Tensorflow学习3D中旋转矩阵的参数。因此,我用以下方法定义了旋转矩阵 g = tf.Graph() with g.as_default(): #rotations thetax = tf.Variable(tf.zeros([1])) thetax = tf.Variable(tf.zeros([1])) thetay = tf.Variable(tf.zeros([1])) p = tf.placeholder(tf.float32, [3])

我想用Tensorflow学习3D中旋转矩阵的参数。因此,我用以下方法定义了旋转矩阵

g = tf.Graph()
with g.as_default():
  #rotations

  thetax =  tf.Variable(tf.zeros([1]))
  thetax =  tf.Variable(tf.zeros([1]))
  thetay =  tf.Variable(tf.zeros([1]))
  p =  tf.placeholder(tf.float32, [3])
  rotation_matrix_x = tf.pack([tf.constant(1.0),tf.constant(0.0),tf.constant(0.0),
                               tf.constant(0.0),tf.cos(thetax), -tf.sin(thetax),
                               tf.constant(0.0),tf.sin(thetax), tf.cos(thetax)])
  rotation_matrix_y = tf.pack([
                          tf.cos(thetax),tf.constant(0.0), -tf.sin(thetax),
                          tf.constant(1.0),tf.constant(0.0),tf.constant(0.0),
                          tf.sin(thetax),0, tf.cos(thetax)])


 rotation_matrix_z = tf.pack([
                              tf.cos(thetax), -tf.sin(thetax),tf.constant(0.0),  
                              tf.sin(thetax), tf.cos(thetax),tf.constant(0.0),
                              tf.constant(1.0),tf.constant(0.0),tf.constant(0.0)])
 rotation_matrix_x = tf.reshape(rotation_matrix_x, (3,3))
 rotation_matrix_y = tf.reshape(rotation_matrix_y, (3,3))
 rotation_matrix_z = tf.reshape(rotation_matrix_z, (3,3))
 rotated = tf.mult(tf.mult(rotation_matrix_x,tf.mult(rotation_matrix_y,rotation_matrix_z) ,p)
我现在有两个问题

  • 我收到一条错误消息:
    ValueError:Shapes TensorShape([])和
    张力形状([尺寸(1)]必须具有相同的等级
  • 有没有 定义旋转矩阵的更优雅的方法 引入任何额外的自由度?例如,法向量+角度完全可以接受
  • 提前感谢问题(1)-形状错误-我认为问题是因为您试图将标量(例如
    tf.constant(0.0)
    )与单元素向量(即
    tf.Variable(tf.zeros([1]))
    )组合在一起。您应该能够通过将变量重新定义为标量来解决此问题:

    thetax = tf.Variable(tf.zeros([]))
    thetax = tf.Variable(tf.zeros([]))
    thetay = tf.Variable(tf.zeros([]))
    
    我不知道如何更优雅地重新定义这个问题。。。但希望这能让你摆脱困境

    对于问题(1)-形状错误-我认为问题是由于您试图将标量(例如
    tf.constant(0.0)
    )与单个元素向量(即
    tf.Variable(tf.zero([1]))
    )组合在一起造成的。您应该能够通过将变量重新定义为标量来解决此问题:

    thetax = tf.Variable(tf.zeros([]))
    thetax = tf.Variable(tf.zeros([]))
    thetay = tf.Variable(tf.zeros([]))
    

    我不知道如何更优雅地重新定义这个问题。。。但希望这能让你摆脱困境

    我最近遇到了同样的问题。这是我当前的解决方案:

    one  = tf.ones_like(cos_rot_x,  dtype=tf.float32)    
    zero = tf.zeros_like(cos_rot_x, dtype=tf.float32)
    
    rot_x = tf.stack([tf.concat([one,        zero,      zero],      axis=1),
                      tf.concat([zero,       cos_rot_x, sin_rot_x], axis=1),
                      tf.concat([zero,      -sin_rot_x, cos_rot_x], axis=1)], axis=1)
    
    rot_y = tf.stack([tf.concat([cos_rot_y,  zero,     -sin_rot_y], axis=1),
                      tf.concat([zero,       one,       zero],      axis=1),
                      tf.concat([sin_rot_y,  zero,      cos_rot_y], axis=1)], axis=1)
    
    rot_z = tf.stack([tf.concat([cos_rot_z,  sin_rot_z, zero],      axis=1),
                      tf.concat([-sin_rot_z, cos_rot_z, zero],      axis=1),
                      tf.concat([zero,       zero,      one],       axis=1)], axis=1)
    
    rot_matrix = tf.matmul(rot_z, tf.matmul(rot_y, rot_x))
    

    请注意,在这段代码中,cos_rot_x具有形状(batchsize,1),因此您可以在转换过程中保留批处理维度。

    我最近遇到了相同的问题。这是我当前的解决方案:

    one  = tf.ones_like(cos_rot_x,  dtype=tf.float32)    
    zero = tf.zeros_like(cos_rot_x, dtype=tf.float32)
    
    rot_x = tf.stack([tf.concat([one,        zero,      zero],      axis=1),
                      tf.concat([zero,       cos_rot_x, sin_rot_x], axis=1),
                      tf.concat([zero,      -sin_rot_x, cos_rot_x], axis=1)], axis=1)
    
    rot_y = tf.stack([tf.concat([cos_rot_y,  zero,     -sin_rot_y], axis=1),
                      tf.concat([zero,       one,       zero],      axis=1),
                      tf.concat([sin_rot_y,  zero,      cos_rot_y], axis=1)], axis=1)
    
    rot_z = tf.stack([tf.concat([cos_rot_z,  sin_rot_z, zero],      axis=1),
                      tf.concat([-sin_rot_z, cos_rot_z, zero],      axis=1),
                      tf.concat([zero,       zero,      one],       axis=1)], axis=1)
    
    rot_matrix = tf.matmul(rot_z, tf.matmul(rot_y, rot_x))
    

    请注意,在这段代码中,cos_rot_x具有形状(batchsize,1),因此您可以在转换过程中保留批次维度。

    谢谢,我可以确认这是第一个答案!非常感谢大家,我可以确认答案第一!非常感谢您,您知道如何使用此旋转矩阵在tensorflow中旋转图像([批次、高度、宽度、深度、特征])吗?您知道如何使用此旋转矩阵在tensorflow中旋转图像([批次、高度、宽度、深度、特征])?