【问题标题】:Generating pytorch's theta from affine transform matrix从仿射变换矩阵生成pytorch的theta
【发布时间】:2020-05-22 22:36:44
【问题描述】:

我正在尝试使用 pytorch 对 3D 体积执行刚性 + 比例转换,但我似乎无法理解 torch.nn.functional.affine_grid 所需的 theta 是如何工作的。

我有一个大小为 (1,4,4) 的变换矩阵,通过矩阵平移 * 比例 * 旋转相乘生成。例如,如果我在 scipy.ndimage.affine_transform 中使用此矩阵,则它可以正常工作。但是,相同的矩阵(裁剪为 (1,3,4) 的大小)在 torch.nn.functional.affine_grid 中完全失败。

我已经设法理解了转换的工作原理(范围 -1 到 1),并且我已经确认转换矩阵可以通过简单地将值标准化到该范围来工作。至于另外两个,我迷路了。

我尝试单独使用基本的缩放矩阵(如下)作为最基本的比较,但 pytorch 中的结果与 scipy 中的结果不同

Scaling = 
[[0.75, 0, 0, 0],
[[0, 0.75, 0, 0],
[[0, 0, 0.75, 0],
[[0, 0, 0, 1]]

如何将 (1,4,4) 仿射矩阵转换为与 torch.nn.functional.affine_grid 相同的工作?或者,有没有办法根据变换参数(移位、欧拉角、缩放)生成正确的矩阵?

【问题讨论】:

  • 您如何将结果与 scipy.ndimage.affine_transform 进行比较?

标签: python pytorch affinetransform


【解决方案1】:

对于将来遇到类似问题的任何人,scipy 与 pytorch 仿射变换的问题是 scipy 在 (0, 0, 0) 周围应用变换,而 pytorch 在图像/体积的中间应用它。

例如,我们取参数:

euler_angles = [ea0, ea1, ea2]
translation = [tr0, tr1, tr2]
scale = [sc0, sc1, sc2]

并创建以下变换矩阵:

# Rotation matrix
R_x(ea0, ea1, ea2) = np.array([[1, 0, 0, 0],
                              [0, math.cos(ea0), -math.sin(ea0), 0],
                              [0, math.sin(ea0), math.cos(ea0), 0],
                              [0, 0, 0, 1]])

R_y(ea0, ea1, ea2) = np.array([[math.cos(ea1), 0, math.sin(ea1), 0],
                               [0, 1, 0, 0],
                               [-math.sin(ea1), 0, math.cos(ea1)], 0],
                               [0, 0, 0, 1]])

R_z(ea0, ea1, ea2) = np.array([[math.cos(ea2), -math.sin(ea2), 0, 0],
                               [math.sin(ea2), math.cos(ea2), 0, 0],
                               [0, 0, 1, 0],
                               [0, 0, 0, 1]])

R = R_x.dot(R_y).dot(R_z)

# Translation matrix
T(tr0, tr1, tr2) = np.array([[1, 0, 0, -tr0],
                             [0, 1, 0, -tr1],
                             [0, 0, 1, -tr2],
                             [0, 0, 0, 1]])
# Scaling matrix
S(sc0, sc1, sc2) = np.array([[1/sc0, 0, 0, 0],
                             [0, 1/sc1, 0, 0],
                             [0, 0, 1/sc2, 0],
                             [0, 0, 0, 1]])

如果你有一个大小为 (100, 100, 100) 的体积,围绕体积中心的 scipy 变换需要先将体积中心移动到 (0, 0, 0),然后再将其移回(50, 50, 50) 在应用 S、T 和 R 之后。定义:

T_zero = np.array([[1, 0, 0, 50],
                   [0, 1, 0, 50],
                   [0, 0, 1, 50],
                   [0, 0, 0, 1]])

T_centre = np.array([[1, 0, 0, -50],
                     [0, 1, 0, -50],
                     [0, 0, 1, -50],
                     [0, 0, 0, 1]])

那么围绕中心的 scipy 变换是:

transform_scipy_centre = T_zero.dot(T).dot(S).dot(R).T_centre

在pytorch中,参数有一些细微的差别。

翻译定义在-1和1之间。它们的顺序也不同。以相同的(100,100,100)体积为例,pytorch中的平移参数由下式给出:

# Note the order difference
translation_pytorch = =[tr0_p, tr1_p, tr2_p] = [tr0/50, tr2/50, tr1/50] 
T_p = T(tr0_p, tr1_p, tr2_p)

比例参数的顺序不同: scale_pytorch = [sc0_p, sc1_p, sc2_p] = [sc2, sc0, sc1]
S_p = S(sc0_p, sc1_p, sc2_p)

欧拉角是最大的区别。为了得到等价的变换,首先参数是负的并且顺序不同:

# Note the order difference
euler_angles_pytorch = [ea0_p, ea1_p, ea2_p] = [-ea0, -ea2, -ea1]
R_x_p = R_x(ea0_p, ea1_p, ea2_p)
R_y_p = R_y(ea0_p, ea1_p, ea2_p)
R_z_p = R_z(ea0_p, ea1_p, ea2_p)

旋转矩阵的计算顺序也不同: # 注意顺序差异 R_p = R_x_p.dot(R_z_p).dot(R_y_p)

考虑到所有这些因素,scipy 转换为:

transform_scipy_centre = T_zero.dot(T).dot(S).dot(R).T_centre

相当于pytorch变换:

transform_pytorch = T_p.dot(S_p).dot(R_p)

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2020-07-06
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多