def test_make_objects_axis_aligned(self): rotation1 = rotation_matrix.from_rotation_around_x(-math.pi / 6.0) rotation2 = tf.constant( [[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], dtype=tf.float32) inputs = { standard_fields.InputDataFields.objects_length: tf.constant([[1.0], [2.0]], dtype=tf.float32), standard_fields.InputDataFields.objects_width: tf.constant([[3.0], [3.0]], dtype=tf.float32), standard_fields.InputDataFields.objects_height: tf.constant([[2.0], [1.0]], dtype=tf.float32), standard_fields.InputDataFields.objects_center: tf.constant([[1.0, 1.0, 1.0], [-1.0, 2.0, 1.0]]), standard_fields.InputDataFields.objects_rotation_matrix: tf.stack([rotation1, rotation2], axis=0), } expected_inputs = { standard_fields.InputDataFields.objects_length: tf.constant([[1.0], [1.0]], dtype=tf.float32), standard_fields.InputDataFields.objects_width: tf.constant([[3.5980759], [2.0]], dtype=tf.float32), standard_fields.InputDataFields.objects_height: tf.constant([[3.232051], [3.0]], dtype=tf.float32), standard_fields.InputDataFields.objects_center: tf.constant([[1.0, 1.0, 1.0], [-1.0, 2.0, 1.0]]), standard_fields.InputDataFields.objects_rotation_matrix: tf.tile(tf.expand_dims(tf.eye(3, dtype=tf.float32), axis=0), [2, 1, 1]), } preprocessor_utils.make_objects_axis_aligned(inputs) for key in expected_inputs: self.assertAllClose(inputs[key].numpy(), expected_inputs[key].numpy())
def test_rotation_around_axis_with_vector_input(self): angle = tf.constant([0.0, 0.5 * np.pi, -0.5 * np.pi, np.pi, -np.pi]) x = rotation_matrix.from_rotation_around_x(angle) y = rotation_matrix.from_rotation_around_y(angle) z = rotation_matrix.from_rotation_around_z(angle) self.assertEqual(x.shape, (5, 3, 3)) self.assertEqual(y.shape, (5, 3, 3)) self.assertEqual(z.shape, (5, 3, 3)) x = tf.matmul(x, rotation_matrix.from_rotation_around_x(-angle)) y = tf.matmul(y, rotation_matrix.from_rotation_around_y(-angle)) z = tf.matmul(z, rotation_matrix.from_rotation_around_z(-angle)) expected = np.repeat(np.expand_dims(np.eye(3), axis=0), 5, axis=0) self.assertAllClose(x.numpy(), expected) self.assertAllClose(y.numpy(), expected) self.assertAllClose(z.numpy(), expected)
def test_rotation_around_axis_with_tensor_input(self): angle = tf.random.uniform(shape=(5, 1)) x = rotation_matrix.from_rotation_around_x(angle) y = rotation_matrix.from_rotation_around_y(angle) z = rotation_matrix.from_rotation_around_z(angle) self.assertEqual(x.shape, (5, 3, 3)) self.assertEqual(y.shape, (5, 3, 3)) self.assertEqual(z.shape, (5, 3, 3)) x = tf.matmul(x, rotation_matrix.from_rotation_around_x(-angle)) y = tf.matmul(y, rotation_matrix.from_rotation_around_y(-angle)) z = tf.matmul(z, rotation_matrix.from_rotation_around_z(-angle)) expected = np.repeat(np.expand_dims(np.eye(3), axis=0), 5, axis=0) self.assertAllClose(x.numpy(), expected) self.assertAllClose(y.numpy(), expected) self.assertAllClose(z.numpy(), expected)
def test_rotation_around_axis_with_scalar_input(self): for rad in [0.0, 0.5 * np.pi, -0.5 * np.pi, np.pi, -np.pi]: angle = tf.constant(rad) x = rotation_matrix.from_rotation_around_x(angle) y = rotation_matrix.from_rotation_around_y(angle) z = rotation_matrix.from_rotation_around_z(angle) self.assertEqual(x.shape, (3, 3)) self.assertEqual(y.shape, (3, 3)) self.assertEqual(z.shape, (3, 3)) x = tf.matmul(x, rotation_matrix.from_rotation_around_x(-angle)) y = tf.matmul(y, rotation_matrix.from_rotation_around_y(-angle)) z = tf.matmul(z, rotation_matrix.from_rotation_around_z(-angle)) self.assertAllClose(x.numpy(), np.eye(3)) self.assertAllClose(y.numpy(), np.eye(3)) self.assertAllClose(z.numpy(), np.eye(3))
def test_from_euler(self): angles = tf.constant([[np.pi, 0.0, 0.0], [0.0, np.pi, 0.0], [0.0, 0.0, np.pi], [np.pi, np.pi, np.pi], [0.0, 0.0, 0.0]]) rotations = rotation_matrix.from_euler(angles) x = rotation_matrix.from_rotation_around_x(tf.constant(np.pi)) y = rotation_matrix.from_rotation_around_y(tf.constant(np.pi)) z = rotation_matrix.from_rotation_around_z(tf.constant(np.pi)) self.assertEqual(rotations.shape, (5, 3, 3)) np_rotations = rotations.numpy() self.assertAllClose(np_rotations[0, Ellipsis], x.numpy()) self.assertAllClose(np_rotations[1, Ellipsis], y.numpy()) self.assertAllClose(np_rotations[2, Ellipsis], z.numpy()) self.assertAllClose(np_rotations[3, Ellipsis], z.numpy().dot(y.numpy()).dot(x.numpy())) self.assertAllClose(np_rotations[4, Ellipsis], np.eye(3))