I’d like to generate batches of randomly rotated matrices based on an initial starting matrix (which has a shape of, for example, (4096, 3)
), where the rotation applied to each matrix in the batch is randomly chosen from a group of rotation matrices (in my code in the original post, I only want to randomly select from 8 possible rotation angles). Therefore, what I end up with is a tensor of shape (batch_size, 4096, 3)
.
My current approach is that I pre-make the possible rotated matrices (since I’m only dealing with 8 possible random rotations), and then use a for loop to generate the batch by randomly picking one of the eight pre-made rotated matrices for each item in the batch. This isn’t super efficient, so I was hoping to vectorize the whole process somehow.
Right now, this is how I loop over a batch to generate a batch of rotated matrices one by one:
for view_i in range(batch_size):
# Get rotated view grid points randomly
idx = torch.randint(0, 8, (1,))
pointsf = rotated_points[idx]
In the code below, I generate a pre-made set of random rotation matrices that get randomly selected from in a for-loop over the batch.
The make_3d_grid function generates a (grid_dim * grid_dim * grid_dim, 3)
shaped matrix (basically a 2D array of x, y, z coordinate points). The get_rotation_matrix
function returns a (3, 3)
rotation matrix, where theta is used for rotation around the x-axis.
rotated_points = []
grid_dim = 16
pointsf = make_3d_grid((-1,)*3, (1,)*3, (grid_dim,)*3)
view_angles = torch.tensor([0, np.pi / 4.0, np.pi / 2.0, 3 * np.pi / 4.0, np.pi, 5 * np.pi / 4.0, 3 * np.pi / 2.0, 7 * np.pi / 4.0])
for i in range(len(view_angles)):
theta = view_angles[i]
rot = get_rotation_matrix(theta, torch.tensor(0.0), torch.tensor(0.0))
pointsf_rot = torch.mm(pointsf, rot)
rotated_points.append(pointsf_rot)
Any help in vectorizing this would be greatly appreciated! If code for this can be done in Numpy that works fine too, since I can convert it to PyTorch myself.