mlx3d.losses¶
mlx3d.losses
¶
LPIPS
¶
Bases: Module
Learned Perceptual Image Patch Similarity (VGG-16).
Call lpips(pred, target) with images shaped (H, W, 3) or
(N, H, W, 3) in [0, 1]; returns a scalar perceptual distance
(lower = more similar). Differentiable w.r.t. the images.
Source code in src/mlx3d/losses/lpips.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
load_weights_file(path)
¶
Load converted VGG-16 + lin weights from a .safetensors/.npz file.
The file must contain MLX arrays matching this module's parameter tree (see the conversion recipe in the docs).
Source code in src/mlx3d/losses/lpips.py
92 93 94 95 96 97 98 | |
chamfer_distance(x, y, x_normals=None, y_normals=None, single_directional=False)
¶
Bidirectional (squared) chamfer distance between batches of point clouds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
(N, P1, 3) or (P1, 3). |
required |
y
|
array
|
(N, P2, 3) or (P2, 3). |
required |
x_normals
|
array | None
|
optional (N, P1, 3) normals for |
None
|
y_normals
|
array | None
|
optional (N, P2, 3) normals for |
None
|
single_directional
|
bool
|
only use the x -> y direction. |
False
|
Returns:
| Type | Description |
|---|---|
array
|
|
array | None
|
not provided. |
Source code in src/mlx3d/losses/chamfer.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
ms_ssim(pred, target, max_val=1.0, window_size=11, sigma=1.5, weights=_MSSSIM_WEIGHTS)
¶
Multi-scale SSIM (Wang et al., 2003) — a perceptual image-quality metric.
Evaluates SSIM across len(weights) scales (2x average-pooling between
them), combining the contrast-structure term at coarse scales with the full
SSIM at the finest. More correlated with perceived quality than single-scale
SSIM, and unlike LPIPS needs no pretrained network. Fully differentiable;
use 1 - ms_ssim(...) as a loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
array
|
(H, W, C) or (N, H, W, C) image in [0, max_val]. |
required |
target
|
array
|
image with the same shape as |
required |
weights
|
tuple[float, ...]
|
per-scale weights; the image must be larger than
|
_MSSSIM_WEIGHTS
|
Returns:
| Type | Description |
|---|---|
array
|
Scalar MS-SSIM in [0, 1]. |
Source code in src/mlx3d/losses/image_metrics.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
psnr(pred, target, max_val=1.0)
¶
Peak signal-to-noise ratio in dB. Images in [0, max_val].
Source code in src/mlx3d/losses/image_metrics.py
17 18 19 20 | |
ssim(pred, target, max_val=1.0, window_size=11, sigma=1.5)
¶
Structural similarity (mean SSIM) with a Gaussian window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
array
|
(H, W, C) or (N, H, W, C) image in [0, max_val]. |
required |
target
|
array
|
image with the same shape as |
required |
Returns:
| Type | Description |
|---|---|
array
|
Scalar mean SSIM. Use |
array
|
filtering is fully differentiable). |
Source code in src/mlx3d/losses/image_metrics.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
mesh_edge_loss(meshes, target_length=0.0)
¶
Mean squared deviation of edge lengths from target_length.
Source code in src/mlx3d/losses/mesh_losses.py
11 12 13 14 15 16 17 18 19 20 | |
mesh_laplacian_smoothing(meshes, method='uniform')
¶
Laplacian smoothing loss: mean norm of the uniform graph Laplacian.
For each vertex, measures the distance to the centroid of its neighbors; minimizing it pulls the surface toward locally smooth configurations.
Source code in src/mlx3d/losses/mesh_losses.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
mesh_normal_consistency(meshes)
¶
Penalty on the angle between normals of faces sharing an edge.
Returns the mean of 1 - cos(n_a, n_b) over all interior edges. The
face-adjacency structure is topology-only and computed on CPU once.
Source code in src/mlx3d/losses/mesh_losses.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
closest_point_on_triangle(p, a, b, c)
¶
Closest point on triangle (a, b, c) to each query p.
All inputs broadcast to a common (..., 3) shape. Uses the Voronoi-region
method (Ericson, Real-Time Collision Detection), fully vectorized.
Source code in src/mlx3d/losses/point_mesh.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
point_mesh_face_distance(meshes, points, face_chunk_size=2048)
¶
Mean squared distance from each point to the nearest mesh face.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meshes
|
Meshes
|
a single-mesh :class: |
required |
points
|
array
|
|
required |
face_chunk_size
|
int
|
faces processed per chunk to bound the |
2048
|
Returns:
| Type | Description |
|---|---|
array
|
Scalar mean over points of the squared distance to the closest triangle. |
array
|
Differentiable w.r.t. both points and mesh vertices. |
Source code in src/mlx3d/losses/point_mesh.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |