nerfbaselines.utils¶
- class nerfbaselines.utils.CancellationToken[source]¶
Bases:
object
CancellationToken
is a context manager that can be used to cancel a long running operation.CancellationToken.current
is a thread-local variable that can be used to access the current token.Example
# Create the token token = CancellationToken() # Now, you would pass the token to another # thread to allow it to cancel the operation # Make the token the current token for the thread with token: # Do something token.cancel_if_requested() # Do something else token.cancel_if_requested() # From the different thread, run. # It will stop the main thread at the nearest `cancel_if_requested` token.cancel()
- exception nerfbaselines.utils.CancelledException[source]¶
Bases:
Exception
Exception raised when an operation is cancelled using the
CancellationToken
.
- class nerfbaselines.utils.Indices(steps)[source]¶
Bases:
object
A class that represents a set of indices or slices. This is useful for specifying subsets of data training iterations or evaluation steps.
- classmethod every_iters(iters: int, zero: bool = False)[source]¶
Create an
Indices
object that represents everyiters
iterations. For zero=False, this is equivalent toIndices(range(iters, total, iters))
.- Parameters:
iters – The number of iterations.
zero – Whether to include 0 in the indices.
- Returns:
The created
Indices
- nerfbaselines.utils.apply_colormap(array: TTensor, *, pallete: str = 'viridis', invert: bool = False) TTensor [source]¶
Apply a colormap to an array.
- Parameters:
array – The input array.
pallete – The matplotlib colormap to use.
invert – Whether to invert the colormap.
- Returns:
The array with the colormap applied.
- nerfbaselines.utils.apply_transform(transform, poses)[source]¶
Applies a transform-scale matrix to a set of poses. The scale is applied after the transform.
- Parameters:
transform – A 4x4 or 3x4 matrix.
poses – A set of poses.
- Returns:
The transformed poses.
- nerfbaselines.utils.convert_image_dtype(image: ndarray, dtype) ndarray [source]¶
Convert an image to a given dtype.
- Parameters:
image – The input image.
dtype – The output dtype.
- Returns:
The converted image.
- nerfbaselines.utils.get_transform_and_scale(transform)[source]¶
Decomposes a transform-scale matrix into a scale and a transform matrix. The scale is applied after the transform.
- Parameters:
transform – A 4x4 or 3x4 matrix.
- Returns:
A tuple of the transform matrix and the scale.
- nerfbaselines.utils.image_to_srgb(tensor, dtype, color_space: str | None = None, allow_alpha: bool = False, background_color: ndarray | None = None)[source]¶
Convert an image to sRGB color space (if it is not in sRGB color space already). If the image has an alpha channel, it will be blended with a specified background color, or black if no background color is specified. The image will be converted to the specified dtype. In case the linear->sRGB conversion happens, the following formula is used:
sRGB = 1.055 * linear^1/2.4 - 0.055, if linear > 0.0031308 sRGB = 12.92 * linear, if linear <= 0.0031308
- Parameters:
tensor – The input image tensor.
dtype – The output dtype.
color_space – The input color space. If None, it is assumed to be sRGB.
allow_alpha – Whether to allow an alpha channel. If False, the alpha channel will be removed by blending with a black background.
background_color – The background color to blend with if the image has an alpha channel. If None, it will be black.
- Returns:
The converted image tensor.
- nerfbaselines.utils.invert_transform(transform, has_scale=False)[source]¶
Inverts a transform or a transform-scale matrix. By default, it assumes the transform has no scale.
- Parameters:
transform – A 4x4 or 3x4 matrix.
has_scale – Whether the transform has scale.
- Returns:
The inverted transform.
- nerfbaselines.utils.pad_poses(p)[source]¶
Pad […, 3, 4] pose matrices with a homogeneous bottom row [0,0,0,1].
- nerfbaselines.utils.padded_stack(tensors: ndarray | Tuple[ndarray, ...] | List[ndarray]) ndarray [source]¶
Stack a list of tensors, padding them to the maximum shape.
- Parameters:
tensors – A list of tensors to stack.
- Returns:
The stacked tensor.
- nerfbaselines.utils.quaternion_conjugate(q)[source]¶
Return quaternion-conjugate of quaternion q̄
- Parameters:
q – A quaternion.
- Returns:
The quaternion conjugate.
- nerfbaselines.utils.quaternion_multiply(q1, q2)[source]¶
Multiply two sets of quaternions.
- Parameters:
q1 – A quaternion.
q2 – A quaternion.
- Returns:
The multiplied quaternions.
- nerfbaselines.utils.quaternion_to_rotation_matrix(r)[source]¶
Convert input quaternion to a rotation matrix.
- Parameters:
r – A quaternion.
- Returns:
The rotation matrix.
- nerfbaselines.utils.rotate_spherical_harmonics(R, y)[source]¶
Rotate spherical harmonics coefficients by a rotation matrix R.
- Parameters:
R – A 3x3 rotation matrix.
y – The spherical harmonics coefficients.
- Returns:
The rotated spherical harmonics coefficients.
- nerfbaselines.utils.rotation_matrix_to_quaternion(R)[source]¶
Convert input 3x3 rotation matrix to unit quaternion.
Assuming an orthogonal 3x3 matrix ℛ rotates a vector v such that
v’ = ℛ * v,
we can also express this rotation in terms of a unit quaternion R such that
v’ = R * v * R⁻¹,
where v and v’ are now considered pure-vector quaternions. This function returns that quaternion. If rot is not orthogonal, the “closest” orthogonal matrix is used; see Notes below.
- Parameters:
R ((...Nx3x3) float array) – Each 3x3 matrix represents a rotation by multiplying (from the left) a column vector to produce a rotated column vector. Note that this input may actually have ndims>3; it is just assumed that the last two dimensions have size 3, representing the matrix.
- Returns:
q – Unit quaternions resulting in rotations corresponding to input rotations. Output shape is rot.shape[:-2].
- Return type:
array of quaternions
- Raises:
LinAlgError – If any of the eigenvalue solutions does not converge
Notes
This function uses Bar-Itzhack’s algorithm to allow for non-orthogonal matrices. [J. Guidance, Vol. 23, No. 6, p. 1085 <http://dx.doi.org/10.2514/2.4654>] This will almost certainly be quite a bit slower than simpler versions, though it will be more robust to numerical errors in the rotation matrix. Also note that the Bar-Itzhack paper uses some pretty weird conventions. The last component of the quaternion appears to represent the scalar, and the quaternion itself is conjugated relative to the convention used throughout the quaternionic module.