odak.learn.tools
odak.learn.tools
Provides necessary definitions for general tools used across the library.
blur_gaussian(field, kernel_length=[21, 21], nsigma=[3, 3], padding='same')
¶
A definition to blur a field using a Gaussian kernel.
Parameters:
-
field–MxN field. -
kernel_length(list, default:[21, 21]) –Length of the Gaussian kernel along X and Y axes. -
nsigma–Sigma of the Gaussian kernel along X and Y axes. -
padding–Padding value, see torch.nn.functional.conv2d() for more.
Returns:
-
blurred_field(tensor) –Blurred field.
Source code in odak/learn/tools/matrix.py
center_of_triangle(triangle)
¶
Definition to calculate center of a triangle.
Parameters:
-
triangle–An array that contains three points defining a triangle (Mx3). It can also parallel process many triangles (NxMx3).
Source code in odak/raytracing/primitives.py
circular_binary_mask(px, py, r)
¶
Definition to generate a 2D circular binary mask.
Parameter
px : int Pixel count in x. py : int Pixel count in y. r : int Radius of the circle.
Returns:
-
mask(tensor) –Mask [1 x 1 x m x n].
Source code in odak/learn/tools/mask.py
convolve2d(field, kernel)
¶
Definition to convolve a field with a kernel by multiplying in frequency space.
Parameters:
-
field–Input field with MxN shape. -
kernel–Input kernel with MxN shape.
Returns:
-
new_field(tensor) –Convolved field.
Source code in odak/learn/tools/matrix.py
correlation_2d(first_tensor, second_tensor)
¶
Definition to calculate the correlation between two tensors.
Parameters:
-
first_tensor–First tensor. -
second_tensor(tensor) –Second tensor.
Returns:
-
correlation(tensor) –Correlation between the two tensors.
Source code in odak/learn/tools/matrix.py
crop_center(field, size=None)
¶
Definition to crop the center of a field with 2Mx2N size. The outcome is a MxN array.
Parameters:
-
field–Input field 2M x 2N or K x L x 2M x 2N or K x 2M x 2N x L array. -
size–Dimensions to crop with respect to center of the image (e.g., M x N or 1 x 1 x M x N).
Returns:
-
cropped(ndarray) –Cropped version of the input field.
Source code in odak/learn/tools/matrix.py
cross_product(vector1, vector2)
¶
Definition to cross product two vectors and return the resultant vector. Used method described under: http://en.wikipedia.org/wiki/Cross_product
Parameters:
-
vector1–A vector/ray. -
vector2–A vector/ray.
Returns:
-
ray(tensor) –Array that contains starting points and cosines of a created ray.
Source code in odak/learn/tools/vector.py
distance_between_two_points(point1, point2)
¶
Definition to calculate distance between two given points.
Parameters:
-
point1–First point in X,Y,Z. -
point2–Second point in X,Y,Z.
Returns:
-
distance(Tensor) –Distance in between given two points.
Source code in odak/learn/tools/vector.py
evaluate_3d_gaussians(points, centers=torch.zeros(1, 3), scales=torch.ones(1, 3), angles=torch.zeros(1, 3), opacity=torch.ones(1, 1))
¶
Evaluate 3D Gaussian functions at given points, with optional rotation.
Parameters:
-
points–The 3D points at which to evaluate the Gaussians. -
centers–The centers of the Gaussians. -
scales–The standard deviations (spread) of the Gaussians along each axis. -
angles–The rotation angles (in radians) for each Gaussian, applied to the points. -
opacity–Opacity of the Gaussians.
Returns:
-
intensities((Tensor, shape[n, 3])) –The evaluated Gaussian intensities at each point.
Source code in odak/learn/tools/function.py
expanduser(filename)
¶
Definition to decode filename using namespaces and shortcuts.
Parameters:
-
filename–Filename.
Returns:
-
new_filename(str) –Filename.
Source code in odak/tools/file.py
freeze(model)
¶
A utility function to freeze the parameters of a provided model defined as a Pythonic class.
For instance, odak.learn.models.unet is such a model.
Parameters:
-
model–Model to be frozen, in other terms `requires_grad` to be set to `False`.
Source code in odak/learn/tools/models.py
generate_2d_dirac_delta(kernel_length=[21, 21], a=[3, 3], mu=[0, 0], theta=0, normalize=False)
¶
Generate 2D Dirac delta function by using Gaussian distribution. Inspired from https://en.wikipedia.org/wiki/Dirac_delta_function
Parameters:
-
kernel_length(list, default:[21, 21]) –Length of the Dirac delta function along X and Y axes. -
a–The scale factor in Gaussian distribution to approximate the Dirac delta function. As a approaches zero, the Gaussian distribution becomes infinitely narrow and tall at the center (x=0), approaching the Dirac delta function. -
mu–Mu of the Gaussian kernel along X and Y axes. -
theta–The rotation angle of the 2D Dirac delta function. -
normalize–If set True, normalize the output.
Returns:
-
kernel_2d(tensor) –Generated 2D Dirac delta function.
Source code in odak/learn/tools/matrix.py
generate_2d_gaussian(kernel_length=[21, 21], nsigma=[3, 3], mu=[0, 0], normalize=False)
¶
Generate 2D Gaussian kernel. Inspired from https://stackoverflow.com/questions/29731726/how-to-calculate-a-gaussian-kernel-matrix-efficiently-in-numpy
Parameters:
-
kernel_length(list, default:[21, 21]) –Length of the Gaussian kernel along X and Y axes. -
nsigma–Sigma of the Gaussian kernel along X and Y axes. -
mu–Mu of the Gaussian kernel along X and Y axes. -
normalize–If set True, normalize the output.
Returns:
-
kernel_2d(tensor) –Generated Gaussian kernel.
Source code in odak/learn/tools/matrix.py
get_rotation_matrix(tilt_angles=[0.0, 0.0, 0.0], tilt_order='XYZ')
¶
Function to generate rotation matrix for given tilt angles and tilt order.
Parameters:
-
tilt_angles–Tilt angles in degrees along XYZ axes. -
tilt_order–Rotation order (e.g., XYZ, XZY, ZXY, YXZ, ZYX).
Returns:
-
rotmat(tensor) –Rotation matrix.
Source code in odak/learn/tools/transformation.py
grid_sample(no=[10, 10], size=[100.0, 100.0], center=[0.0, 0.0, 0.0], angles=[0.0, 0.0, 0.0])
¶
Definition to generate samples over a surface.
Parameters:
-
no–Number of samples. -
size–Physical size of the surface. -
center–Center location of the surface. -
angles–Tilt of the surface.
Returns:
-
samples(tensor) –Samples generated.
-
rotx(tensor) –Rotation matrix at X axis.
-
roty(tensor) –Rotation matrix at Y axis.
-
rotz(tensor) –Rotation matrix at Z axis.
Source code in odak/learn/tools/sample.py
histogram_loss(frame, ground_truth, bins=32, limits=[0.0, 1.0])
¶
Function for evaluating a frame against a target using histogram.
Parameters:
-
frame–Input frame [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
ground_truth–Ground truth [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
bins–Number of bins. -
limits–Limits.
Returns:
-
loss(float) –Loss from evaluation.
Source code in odak/learn/tools/loss.py
load_image(fn, normalizeby=0.0, torch_style=False)
¶
Definition to load an image from a given location as a torch tensor.
Parameters:
-
fn–Filename. -
normalizeby–Value to to normalize images with. Default value of zero will lead to no normalization. -
torch_style–If set True, it will load an image mxnx3 as 3xmxn.
Returns:
-
image(ndarray) –Image loaded as a Numpy array.
Source code in odak/learn/tools/file.py
load_voxelized_PLY(ply_filename, voxel_size=[0.05, 0.05, 0.05], device=torch.device('cpu'))
¶
Load a point cloud from a PLY file and convert it into a voxel grid representation.
Parameters:
-
ply_filename(str or Path) –The path to the input PLY file containing triangle data. -
voxel_size–The size of each voxel in the x, y, and z directions. Default is [0.05, 0.05, 0.05]. -
device–The device on which to perform computations. Default is CPU.
Returns:
-
points((Tensor, shape(N, 3))) –A tensor containing the coordinates of the voxel centers.
-
ground_truth((Tensor, shape(Gx * Gy * Gz))) –A binary tensor where each element indicates whether a corresponding voxel contains at least one point.
Notes
- The function reads triangle data from the PLY file and computes the center points of these triangles.
- These points are then processed to create a normalized point cloud, which is converted into a voxel grid.
- Only voxels containing at least one point are marked as 1 in
ground_truth. - All operations are performed on the specified device for efficiency.
Source code in odak/learn/tools/transformation.py
michelson_contrast(image, roi_high, roi_low)
¶
A function to calculate michelson contrast ratio of given region of interests of the image.
Parameters:
-
image–Image to be tested [1 x 3 x m x n] or [3 x m x n] or [m x n]. -
roi_high–Corner locations of the roi for high intensity area [m_start, m_end, n_start, n_end]. -
roi_low–Corner locations of the roi for low intensity area [m_start, m_end, n_start, n_end].
Returns:
-
result(tensor) –Michelson contrast for the given regions. [1] or [3] depending on input image.
Source code in odak/learn/tools/loss.py
multi_scale_total_variation_loss(frame, levels=3)
¶
Function for evaluating a frame against a target using multi scale total variation approach. Here, multi scale refers to image pyramid of an input frame, where at each level image resolution is half of the previous level.
Parameters:
-
frame–Input frame [1 x 3 x m x n] or [3 x m x n] or [m x n]. -
levels–Number of levels to go in the image pyriamid.
Returns:
-
loss(float) –Loss from evaluation.
Source code in odak/learn/tools/loss.py
point_cloud_to_voxel(points, voxel_size=[0.1, 0.1, 0.1])
¶
Convert a point cloud to a voxel grid representation.
Parameters:
-
points–The input point cloud, where each row is a 3D point. -
voxel_size((list or Tensor, shape(3)), default:[0.1, 0.1, 0.1]) –The size of each voxel in the x, y, and z directions. Default is [0.1, 0.1, 0.1].
Returns:
-
locations((Tensor, shape(Gx, Gy, Gz, 3))) –The coordinates of each voxel center in the grid.
-
grid((Tensor, shape(Gx, Gy, Gz))) –A binary voxel grid where 1 indicates the presence of at least one point.
Notes
- The voxel grid is constructed by discretizing the space between the minimum and maximum coordinates of the point cloud.
- Only voxels containing at least one point are marked as 1.
- The output grid is of type float32 and resides on the same device as the input points.
Source code in odak/learn/tools/transformation.py
quantize(image_field, bits=8, limits=[0.0, 1.0])
¶
Definition to quantize a image field (0-255, 8 bit) to a certain bits level.
Parameters:
-
image_field(tensor) –Input image field between any range. -
bits–A value in between one to eight. -
limits–The minimum and maximum of the image_field variable.
Returns:
-
new_field(tensor) –Quantized image field.
Source code in odak/learn/tools/matrix.py
radial_basis_function(value, epsilon=0.5)
¶
Function to pass a value into radial basis function with Gaussian description.
Parameters:
-
value–Value(s) to pass to the radial basis function. -
epsilon–Epsilon used in the Gaussian radial basis function (e.g., y=e^(-(epsilon x value)^2).
Returns:
-
output(tensor) –Output values.
Source code in odak/learn/tools/loss.py
read_PLY(fn, offset=[0, 0, 0], angles=[0.0, 0.0, 0.0], mode='XYZ')
¶
Definition to read a PLY file and extract meshes from a given PLY file. Note that rotation is always with respect to 0,0,0.
Parameters:
-
fn–Filename of a PLY file. -
offset–Offset in X,Y,Z. -
angles–Rotation angles in degrees. -
mode–Rotation mode determines ordering of the rotations at each axis. There are XYZ,YXZ,ZXY and ZYX modes.
Returns:
-
triangles(ndarray) –Triangles from a given PLY file. Note that the triangles coming out of this function isn't always structured in the right order and with the size of (MxN)x3. You can use numpy's reshape to restructure it to mxnx3 if you know what you are doing.
Source code in odak/tools/asset.py
resize(image, multiplier=0.5, mode='nearest')
¶
Definition to resize an image.
Parameters:
-
image–Image with MxNx3 resolution. -
multiplier–Multiplier used in resizing operation (e.g., 0.5 is half size in one axis). -
mode–Mode to be used in scaling, nearest, bilinear, etc.
Returns:
-
new_image(tensor) –Resized image.
Source code in odak/learn/tools/file.py
rotate_points(point, angles=torch.zeros(1, 3), mode='XYZ', origin=torch.zeros(1, 3), offset=torch.zeros(1, 3))
¶
Definition to rotate a given point. Note that rotation is always with respect to 0,0,0.
Parameters:
-
point–A point with size of [3] or [1, 3] or [m, 3]. -
angles–Rotation angles in degrees. -
mode–Rotation mode determines ordering of the rotations at each axis. There are XYZ,YXZ,ZXY and ZYX modes. -
origin–Reference point for a rotation. Expected size is [3] or [1, 3]. -
offset–Shift with the given offset. Expected size is [3] or [1, 3] or [m, 3].
Returns:
-
result(tensor) –Result of the rotation [1 x 3] or [m x 3].
-
rotx(tensor) –Rotation matrix along X axis [3 x 3].
-
roty(tensor) –Rotation matrix along Y axis [3 x 3].
-
rotz(tensor) –Rotation matrix along Z axis [3 x 3].
Source code in odak/learn/tools/transformation.py
rotmatx(angle)
¶
Definition to generate a rotation matrix along X axis.
Parameters:
-
angle–Rotation angles in degrees.
Returns:
-
rotx(tensor) –Rotation matrix along X axis.
Source code in odak/learn/tools/transformation.py
rotmaty(angle)
¶
Definition to generate a rotation matrix along Y axis.
Parameters:
-
angle–Rotation angles in degrees.
Returns:
-
roty(tensor) –Rotation matrix along Y axis.
Source code in odak/learn/tools/transformation.py
rotmatz(angle)
¶
Definition to generate a rotation matrix along Z axis.
Parameters:
-
angle–Rotation angles in degrees.
Returns:
-
rotz(tensor) –Rotation matrix along Z axis.
Source code in odak/learn/tools/transformation.py
same_side(p1, p2, a, b)
¶
Definition to figure which side a point is on with respect to a line and a point. See http://www.blackpawn.com/texts/pointinpoly/ for more. If p1 and p2 are on the sameside, this definition returns True.
Parameters:
-
p1–Point(s) to check. -
p2–This is the point check against. -
a–First point that forms the line. -
b–Second point that forms the line.
Source code in odak/learn/tools/vector.py
save_image(fn, img, cmin=0, cmax=255, color_depth=8)
¶
Definition to save a torch tensor as an image.
Parameters:
-
fn–Filename. -
img–A numpy array with NxMx3 or NxMx1 shapes. -
cmin–Minimum value that will be interpreted as 0 level in the final image. -
cmax–Maximum value that will be interpreted as 255 level in the final image. -
color_depth–Color depth of an image. Default is eight.
Returns:
-
bool(bool) –True if successful.
Source code in odak/learn/tools/file.py
save_torch_tensor(fn, tensor)
¶
Definition to save a torch tensor.
Parameters:
-
fn–Filename. -
tensor–Torch tensor to be saved.
spatial_gradient(frame)
¶
Function to calculate the spatial gradient of a given frame.
Parameters:
-
frame–Input frame [1 x 3 x m x n] or [3 x m x n] or [m x n].
Returns:
-
diff_x(float) –Spatial gradient along X.
-
diff_y(float) –Spatial gradient along Y.
Source code in odak/learn/tools/loss.py
tilt_towards(location, lookat)
¶
Definition to tilt surface normal of a plane towards a point.
Parameters:
-
location–Center of the plane to be tilted. -
lookat–Tilt towards this point.
Returns:
-
angles(list) –Rotation angles in degrees.
Source code in odak/learn/tools/transformation.py
torch_load(fn, weights_only=True)
¶
Definition to load a torch files (*.pt).
Parameters:
-
fn–Filename. -
weights_only(bool, default:True) –See torch.load() for details.
Returns:
-
data(any) –See torch.load() for more.
Source code in odak/learn/tools/file.py
total_variation_loss(frame)
¶
Function for evaluating a frame against a target using total variation approach.
Parameters:
-
frame–Input frame [1 x 3 x m x n] or [3 x m x n] or [m x n].
Returns:
-
loss(float) –Loss from evaluation.
Source code in odak/learn/tools/loss.py
unfreeze(model)
¶
A utility function to unfreeze the parameters of a provided model defined as a Pythonic class.
For instance, odak.learn.models.unet is such a model.
Parameters:
-
model–Model to unfreeze, in other terms `requires_grad` to be set to `True`.
Source code in odak/learn/tools/models.py
weber_contrast(image, roi_high, roi_low)
¶
A function to calculate weber contrast ratio of given region of interests of the image.
Parameters:
-
image–Image to be tested [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
roi_high–Corner locations of the roi for high intensity area [m_start, m_end, n_start, n_end]. -
roi_low–Corner locations of the roi for low intensity area [m_start, m_end, n_start, n_end].
Returns:
-
result(tensor) –Weber contrast for given regions. [1] or [3] depending on input image.
Source code in odak/learn/tools/loss.py
wrapped_mean_squared_error(image, ground_truth, reduction='mean')
¶
A function to calculate the wrapped mean squared error between predicted and target angles.
Parameters:
-
image–Image to be tested [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
ground_truth–Ground truth to be tested [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
reduction–Specifies the reduction to apply to the output: 'mean' (default) or 'sum'.
Returns:
-
wmse(tensor) –The calculated wrapped mean squared error.
Source code in odak/learn/tools/loss.py
zernike_polynomial(n, m, rho, theta)
¶
Compute the 2D Zernike polynomial Z_n^m(rho, theta).
Parameters:
-
n–Radial degree of the polynomial (n >= 0). -
m–Azimuthal frequency of the polynomial. -
rho–Radial distance from the origin (0 to 1). -
theta–Azimuthal angle in radians. Shape (H, W).
Returns:
-
zernike(Tensor) –The computed 2D Zernike polynomial. Values are zero where rho > 1.
Source code in odak/learn/tools/function.py
zero_pad(field, size=None, method='center')
¶
Definition to zero pad a MxN array to 2Mx2N array.
Parameters:
-
field–Input field MxN or KxJxMxN or KxMxNxJ array. -
size–Size to be zeropadded (e.g., [m, n], last two dimensions only). -
method–Zeropad either by placing the content to center or to the left.
Returns:
-
field_zero_padded(ndarray) –Zeropadded version of the input field.
Source code in odak/learn/tools/matrix.py
load_image(fn, normalizeby=0.0, torch_style=False)
¶
Definition to load an image from a given location as a torch tensor.
Parameters:
-
fn–Filename. -
normalizeby–Value to to normalize images with. Default value of zero will lead to no normalization. -
torch_style–If set True, it will load an image mxnx3 as 3xmxn.
Returns:
-
image(ndarray) –Image loaded as a Numpy array.
Source code in odak/learn/tools/file.py
resize(image, multiplier=0.5, mode='nearest')
¶
Definition to resize an image.
Parameters:
-
image–Image with MxNx3 resolution. -
multiplier–Multiplier used in resizing operation (e.g., 0.5 is half size in one axis). -
mode–Mode to be used in scaling, nearest, bilinear, etc.
Returns:
-
new_image(tensor) –Resized image.
Source code in odak/learn/tools/file.py
save_image(fn, img, cmin=0, cmax=255, color_depth=8)
¶
Definition to save a torch tensor as an image.
Parameters:
-
fn–Filename. -
img–A numpy array with NxMx3 or NxMx1 shapes. -
cmin–Minimum value that will be interpreted as 0 level in the final image. -
cmax–Maximum value that will be interpreted as 255 level in the final image. -
color_depth–Color depth of an image. Default is eight.
Returns:
-
bool(bool) –True if successful.
Source code in odak/learn/tools/file.py
save_torch_tensor(fn, tensor)
¶
Definition to save a torch tensor.
Parameters:
-
fn–Filename. -
tensor–Torch tensor to be saved.
torch_load(fn, weights_only=True)
¶
Definition to load a torch files (*.pt).
Parameters:
-
fn–Filename. -
weights_only(bool, default:True) –See torch.load() for details.
Returns:
-
data(any) –See torch.load() for more.
Source code in odak/learn/tools/file.py
histogram_loss(frame, ground_truth, bins=32, limits=[0.0, 1.0])
¶
Function for evaluating a frame against a target using histogram.
Parameters:
-
frame–Input frame [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
ground_truth–Ground truth [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
bins–Number of bins. -
limits–Limits.
Returns:
-
loss(float) –Loss from evaluation.
Source code in odak/learn/tools/loss.py
michelson_contrast(image, roi_high, roi_low)
¶
A function to calculate michelson contrast ratio of given region of interests of the image.
Parameters:
-
image–Image to be tested [1 x 3 x m x n] or [3 x m x n] or [m x n]. -
roi_high–Corner locations of the roi for high intensity area [m_start, m_end, n_start, n_end]. -
roi_low–Corner locations of the roi for low intensity area [m_start, m_end, n_start, n_end].
Returns:
-
result(tensor) –Michelson contrast for the given regions. [1] or [3] depending on input image.
Source code in odak/learn/tools/loss.py
multi_scale_total_variation_loss(frame, levels=3)
¶
Function for evaluating a frame against a target using multi scale total variation approach. Here, multi scale refers to image pyramid of an input frame, where at each level image resolution is half of the previous level.
Parameters:
-
frame–Input frame [1 x 3 x m x n] or [3 x m x n] or [m x n]. -
levels–Number of levels to go in the image pyriamid.
Returns:
-
loss(float) –Loss from evaluation.
Source code in odak/learn/tools/loss.py
radial_basis_function(value, epsilon=0.5)
¶
Function to pass a value into radial basis function with Gaussian description.
Parameters:
-
value–Value(s) to pass to the radial basis function. -
epsilon–Epsilon used in the Gaussian radial basis function (e.g., y=e^(-(epsilon x value)^2).
Returns:
-
output(tensor) –Output values.
Source code in odak/learn/tools/loss.py
spatial_gradient(frame)
¶
Function to calculate the spatial gradient of a given frame.
Parameters:
-
frame–Input frame [1 x 3 x m x n] or [3 x m x n] or [m x n].
Returns:
-
diff_x(float) –Spatial gradient along X.
-
diff_y(float) –Spatial gradient along Y.
Source code in odak/learn/tools/loss.py
total_variation_loss(frame)
¶
Function for evaluating a frame against a target using total variation approach.
Parameters:
-
frame–Input frame [1 x 3 x m x n] or [3 x m x n] or [m x n].
Returns:
-
loss(float) –Loss from evaluation.
Source code in odak/learn/tools/loss.py
weber_contrast(image, roi_high, roi_low)
¶
A function to calculate weber contrast ratio of given region of interests of the image.
Parameters:
-
image–Image to be tested [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
roi_high–Corner locations of the roi for high intensity area [m_start, m_end, n_start, n_end]. -
roi_low–Corner locations of the roi for low intensity area [m_start, m_end, n_start, n_end].
Returns:
-
result(tensor) –Weber contrast for given regions. [1] or [3] depending on input image.
Source code in odak/learn/tools/loss.py
wrapped_mean_squared_error(image, ground_truth, reduction='mean')
¶
A function to calculate the wrapped mean squared error between predicted and target angles.
Parameters:
-
image–Image to be tested [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
ground_truth–Ground truth to be tested [1 x 3 x m x n] or [3 x m x n] or [1 x m x n] or [m x n]. -
reduction–Specifies the reduction to apply to the output: 'mean' (default) or 'sum'.
Returns:
-
wmse(tensor) –The calculated wrapped mean squared error.
Source code in odak/learn/tools/loss.py
blur_gaussian(field, kernel_length=[21, 21], nsigma=[3, 3], padding='same')
¶
A definition to blur a field using a Gaussian kernel.
Parameters:
-
field–MxN field. -
kernel_length(list, default:[21, 21]) –Length of the Gaussian kernel along X and Y axes. -
nsigma–Sigma of the Gaussian kernel along X and Y axes. -
padding–Padding value, see torch.nn.functional.conv2d() for more.
Returns:
-
blurred_field(tensor) –Blurred field.
Source code in odak/learn/tools/matrix.py
convolve2d(field, kernel)
¶
Definition to convolve a field with a kernel by multiplying in frequency space.
Parameters:
-
field–Input field with MxN shape. -
kernel–Input kernel with MxN shape.
Returns:
-
new_field(tensor) –Convolved field.
Source code in odak/learn/tools/matrix.py
correlation_2d(first_tensor, second_tensor)
¶
Definition to calculate the correlation between two tensors.
Parameters:
-
first_tensor–First tensor. -
second_tensor(tensor) –Second tensor.
Returns:
-
correlation(tensor) –Correlation between the two tensors.
Source code in odak/learn/tools/matrix.py
crop_center(field, size=None)
¶
Definition to crop the center of a field with 2Mx2N size. The outcome is a MxN array.
Parameters:
-
field–Input field 2M x 2N or K x L x 2M x 2N or K x 2M x 2N x L array. -
size–Dimensions to crop with respect to center of the image (e.g., M x N or 1 x 1 x M x N).
Returns:
-
cropped(ndarray) –Cropped version of the input field.
Source code in odak/learn/tools/matrix.py
generate_2d_dirac_delta(kernel_length=[21, 21], a=[3, 3], mu=[0, 0], theta=0, normalize=False)
¶
Generate 2D Dirac delta function by using Gaussian distribution. Inspired from https://en.wikipedia.org/wiki/Dirac_delta_function
Parameters:
-
kernel_length(list, default:[21, 21]) –Length of the Dirac delta function along X and Y axes. -
a–The scale factor in Gaussian distribution to approximate the Dirac delta function. As a approaches zero, the Gaussian distribution becomes infinitely narrow and tall at the center (x=0), approaching the Dirac delta function. -
mu–Mu of the Gaussian kernel along X and Y axes. -
theta–The rotation angle of the 2D Dirac delta function. -
normalize–If set True, normalize the output.
Returns:
-
kernel_2d(tensor) –Generated 2D Dirac delta function.
Source code in odak/learn/tools/matrix.py
generate_2d_gaussian(kernel_length=[21, 21], nsigma=[3, 3], mu=[0, 0], normalize=False)
¶
Generate 2D Gaussian kernel. Inspired from https://stackoverflow.com/questions/29731726/how-to-calculate-a-gaussian-kernel-matrix-efficiently-in-numpy
Parameters:
-
kernel_length(list, default:[21, 21]) –Length of the Gaussian kernel along X and Y axes. -
nsigma–Sigma of the Gaussian kernel along X and Y axes. -
mu–Mu of the Gaussian kernel along X and Y axes. -
normalize–If set True, normalize the output.
Returns:
-
kernel_2d(tensor) –Generated Gaussian kernel.
Source code in odak/learn/tools/matrix.py
quantize(image_field, bits=8, limits=[0.0, 1.0])
¶
Definition to quantize a image field (0-255, 8 bit) to a certain bits level.
Parameters:
-
image_field(tensor) –Input image field between any range. -
bits–A value in between one to eight. -
limits–The minimum and maximum of the image_field variable.
Returns:
-
new_field(tensor) –Quantized image field.
Source code in odak/learn/tools/matrix.py
zero_pad(field, size=None, method='center')
¶
Definition to zero pad a MxN array to 2Mx2N array.
Parameters:
-
field–Input field MxN or KxJxMxN or KxMxNxJ array. -
size–Size to be zeropadded (e.g., [m, n], last two dimensions only). -
method–Zeropad either by placing the content to center or to the left.
Returns:
-
field_zero_padded(ndarray) –Zeropadded version of the input field.
Source code in odak/learn/tools/matrix.py
grid_sample(no=[10, 10], size=[100.0, 100.0], center=[0.0, 0.0, 0.0], angles=[0.0, 0.0, 0.0])
¶
Definition to generate samples over a surface.
Parameters:
-
no–Number of samples. -
size–Physical size of the surface. -
center–Center location of the surface. -
angles–Tilt of the surface.
Returns:
-
samples(tensor) –Samples generated.
-
rotx(tensor) –Rotation matrix at X axis.
-
roty(tensor) –Rotation matrix at Y axis.
-
rotz(tensor) –Rotation matrix at Z axis.
Source code in odak/learn/tools/sample.py
get_rotation_matrix(tilt_angles=[0.0, 0.0, 0.0], tilt_order='XYZ')
¶
Function to generate rotation matrix for given tilt angles and tilt order.
Parameters:
-
tilt_angles–Tilt angles in degrees along XYZ axes. -
tilt_order–Rotation order (e.g., XYZ, XZY, ZXY, YXZ, ZYX).
Returns:
-
rotmat(tensor) –Rotation matrix.
Source code in odak/learn/tools/transformation.py
load_voxelized_PLY(ply_filename, voxel_size=[0.05, 0.05, 0.05], device=torch.device('cpu'))
¶
Load a point cloud from a PLY file and convert it into a voxel grid representation.
Parameters:
-
ply_filename(str or Path) –The path to the input PLY file containing triangle data. -
voxel_size–The size of each voxel in the x, y, and z directions. Default is [0.05, 0.05, 0.05]. -
device–The device on which to perform computations. Default is CPU.
Returns:
-
points((Tensor, shape(N, 3))) –A tensor containing the coordinates of the voxel centers.
-
ground_truth((Tensor, shape(Gx * Gy * Gz))) –A binary tensor where each element indicates whether a corresponding voxel contains at least one point.
Notes
- The function reads triangle data from the PLY file and computes the center points of these triangles.
- These points are then processed to create a normalized point cloud, which is converted into a voxel grid.
- Only voxels containing at least one point are marked as 1 in
ground_truth. - All operations are performed on the specified device for efficiency.
Source code in odak/learn/tools/transformation.py
point_cloud_to_voxel(points, voxel_size=[0.1, 0.1, 0.1])
¶
Convert a point cloud to a voxel grid representation.
Parameters:
-
points–The input point cloud, where each row is a 3D point. -
voxel_size((list or Tensor, shape(3)), default:[0.1, 0.1, 0.1]) –The size of each voxel in the x, y, and z directions. Default is [0.1, 0.1, 0.1].
Returns:
-
locations((Tensor, shape(Gx, Gy, Gz, 3))) –The coordinates of each voxel center in the grid.
-
grid((Tensor, shape(Gx, Gy, Gz))) –A binary voxel grid where 1 indicates the presence of at least one point.
Notes
- The voxel grid is constructed by discretizing the space between the minimum and maximum coordinates of the point cloud.
- Only voxels containing at least one point are marked as 1.
- The output grid is of type float32 and resides on the same device as the input points.
Source code in odak/learn/tools/transformation.py
rotate_points(point, angles=torch.zeros(1, 3), mode='XYZ', origin=torch.zeros(1, 3), offset=torch.zeros(1, 3))
¶
Definition to rotate a given point. Note that rotation is always with respect to 0,0,0.
Parameters:
-
point–A point with size of [3] or [1, 3] or [m, 3]. -
angles–Rotation angles in degrees. -
mode–Rotation mode determines ordering of the rotations at each axis. There are XYZ,YXZ,ZXY and ZYX modes. -
origin–Reference point for a rotation. Expected size is [3] or [1, 3]. -
offset–Shift with the given offset. Expected size is [3] or [1, 3] or [m, 3].
Returns:
-
result(tensor) –Result of the rotation [1 x 3] or [m x 3].
-
rotx(tensor) –Rotation matrix along X axis [3 x 3].
-
roty(tensor) –Rotation matrix along Y axis [3 x 3].
-
rotz(tensor) –Rotation matrix along Z axis [3 x 3].
Source code in odak/learn/tools/transformation.py
rotmatx(angle)
¶
Definition to generate a rotation matrix along X axis.
Parameters:
-
angle–Rotation angles in degrees.
Returns:
-
rotx(tensor) –Rotation matrix along X axis.
Source code in odak/learn/tools/transformation.py
rotmaty(angle)
¶
Definition to generate a rotation matrix along Y axis.
Parameters:
-
angle–Rotation angles in degrees.
Returns:
-
roty(tensor) –Rotation matrix along Y axis.
Source code in odak/learn/tools/transformation.py
rotmatz(angle)
¶
Definition to generate a rotation matrix along Z axis.
Parameters:
-
angle–Rotation angles in degrees.
Returns:
-
rotz(tensor) –Rotation matrix along Z axis.
Source code in odak/learn/tools/transformation.py
tilt_towards(location, lookat)
¶
Definition to tilt surface normal of a plane towards a point.
Parameters:
-
location–Center of the plane to be tilted. -
lookat–Tilt towards this point.
Returns:
-
angles(list) –Rotation angles in degrees.
Source code in odak/learn/tools/transformation.py
cross_product(vector1, vector2)
¶
Definition to cross product two vectors and return the resultant vector. Used method described under: http://en.wikipedia.org/wiki/Cross_product
Parameters:
-
vector1–A vector/ray. -
vector2–A vector/ray.
Returns:
-
ray(tensor) –Array that contains starting points and cosines of a created ray.
Source code in odak/learn/tools/vector.py
distance_between_two_points(point1, point2)
¶
Definition to calculate distance between two given points.
Parameters:
-
point1–First point in X,Y,Z. -
point2–Second point in X,Y,Z.
Returns:
-
distance(Tensor) –Distance in between given two points.
Source code in odak/learn/tools/vector.py
same_side(p1, p2, a, b)
¶
Definition to figure which side a point is on with respect to a line and a point. See http://www.blackpawn.com/texts/pointinpoly/ for more. If p1 and p2 are on the sameside, this definition returns True.
Parameters:
-
p1–Point(s) to check. -
p2–This is the point check against. -
a–First point that forms the line. -
b–Second point that forms the line.