drtk.pixel_grid_mesh#

pixel_grid_mesh

Build a displaced pixel-cell mesh with optional cuts and feathered borders.

bernoulli_topology_samples

Sample two correlated hard topologies for the ARM estimator.

bernoulli_render_loss

Estimate expected rendered loss and its Bernoulli-logit gradient with ARM.

pixel_grid_mesh_ref

Tensor reference for pixel_grid_mesh(), supporting CPU and CUDA.

Build a mesh from an image-aligned grid, deform its cells, and learn which cells and connections to keep. The mesh builder uses hard topology; the Bernoulli helpers estimate gradients of the expected loss over sampled meshes.

See Pixel-grid topology and gradients for camera conventions, cuts, feathering, and the gradient model, and Learning pixel-grid geometry and topology for a rendered optimization example.

For repeated fitting steps, Avoiding CPU synchronization explains how to choose output capacities without reading counts back to the CPU.

drtk.pixel_grid_mesh(depth_z, displacement_xyz, intrinsics, alive, join_right, join_down, diagonal=None, feather_width=None, *, return_metadata=False, output_capacity=None)[source]#

Build a displaced pixel-cell mesh with optional cuts and feathered borders.

Supports float32/float64 CUDA tensors and first-order gradients.

Parameters:
  • depth_z – Positive camera Z depth [N, H, W] (float32 or float64). A batch contains N independent meshes on grids with the same H and W.

  • displacement_xyz – Camera-space translation proposals [N, H, W, 3]. Shared corners are least-squares averages of incident proposals; a cell’s final center need not equal its proposed center exactly.

  • intrinsics – Nonsingular pinhole matrix with last row 0,0,1. Shape [N, 3, 3], or [1, 3, 3] for a shared camera. Gradients to a shared matrix sum over the batch. Pixel centers have integer coordinates, corners half-integers.

  • alive – Boolean cell participation, with the same shape as depth_z.

  • join_right – Boolean horizontal adjacency [N, H, W-1].

  • join_down – Boolean vertical adjacency [N, H-1, W]. Isolated interior one-edge cuts are healed; cuts touching a live/dead or grid border survive. Adjacent cut edges are evaluated before any healing.

  • diagonal – Optional boolean tensor with the same shape as depth_z. False selects TL–BR, True TR–BL. None selects the diagonal whose midpoint best matches the proposed cell center; ties select TL–BR.

  • feather_width – Optional nonnegative widths in source pixels, with the same shape as depth_z. Generates boundary strips and fixed vertex opacity, not a learned opacity head. Widths are averaged within each boundary fan. Large widths can cause overlap; there is no global collision resolution.

  • return_metadata – Return source-cell ownership and stable face-slot IDs.

  • output_capacity – Optional (max_vertices, max_faces) per mesh. Fixes the padded output sizes and keeps vertex/face counts on the GPU, allowing CUDA graph capture. Capacities must accommodate every mesh in the batch; insufficient capacity raises an asynchronous CUDA error. None sizes outputs to the current batch.

Returns:

A fixed seven-element tuple (v, vt0, vt1, vi, vertex_opacity, face_cell_ids, face_ids). Vertices already include displacement and optional strips, in source-camera 3D, shape [N, Vmax, 3]. UVs have shape [N, Vmax, 2] and faces [N, Fmax, 3] int32. vt0 is the source anchor; vt1 is the displaced source projection. Vertex opacity is [N, Vmax] or None without feathering. The two metadata tensors are [N, Fmax] int64 or None.

Vmax and Fmax are the supplied capacities, or the largest vertex and face counts in the batch when output_capacity is None. Each mesh occupies a compact prefix; face indices and metadata stay local to that mesh. Unused faces and metadata are -1. Unused positions are (0,0,1), and unused UVs and opacity are zero. Padding contributes no gradients. vi[..., 0] >= 0 identifies valid faces. Empty meshes and empty batches are supported.

Gradients flow through v and vt1. The source anchors vt0, vertex opacity, face indices, and metadata describe the fixed topology and are nondifferentiable outputs.

All tensors must share device; floating inputs must share dtype. Inputs must be finite, depths/widths valid, and displaced vertices in front of the source camera. CUDA graph capture requires output_capacity.

See pixel_grid_mesh_ref() for the tensor reference implementation. Vertex/face storage order may differ; use stable face IDs when comparing geometry.

drtk.bernoulli_topology_samples(logits, *, generator=None)[source]#

Sample two correlated hard topologies for the ARM estimator.

Parameters:
  • logits – Floating-point Bernoulli logits of any shape. Each element describes one independent proposal decision.

  • generator – Optional random generator matching the logits’ device.

Returns:

A three-element tuple (plus, minus, uniform) with the same shape and device as logits. For p = sigmoid(logits) and shared u = uniform, plus = (u > 1 - p) and minus = (u < p) are boolean tensors. uniform has the logits’ floating dtype.

Both hard samples have Bernoulli marginals with probability p; they are correlated, not independent samples or elementwise complements. The returned samples and random numbers have no gradient to logits. Use bernoulli_render_loss() to obtain an ARM loss gradient, or use the pair and shared noise to implement a custom estimator.

drtk.bernoulli_render_loss(logits, evaluate, *, pairs=1, generator=None)[source]#

Estimate expected rendered loss and its Bernoulli-logit gradient with ARM.

Parameters:
  • logits – Floating-point Bernoulli logits of any shape, typically a learnable tensor or the output of a prediction network.

  • evaluate – Callable receiving boolean decisions of shape [2 * pairs, *logits.shape] and returning one scalar loss per sample as a [2 * pairs] tensor. The first half contains the plus samples and the second half their corresponding minus samples. The callable builds meshes, renders them, and compares each image with its target. Continuous parameters captured by the callable retain ordinary autograd.

  • pairs – Positive number of correlated sample pairs. Each pair uses two meshes; more pairs reduce sampling variance at the cost of more evaluations.

  • generator – Optional random generator matching the logits’ device.

Returns:

A scalar whose forward value is the mean of the 2 * pairs loss evaluations. Backward adds the ARM estimator for logits and preserves ordinary continuous-input gradients from those evaluations. Calling backward() accumulates these gradients normally.

Each sample’s loss must depend on its own topology. Cross-sample coupling, such as training-mode batch normalization, changes the objective. The callback may process the samples in smaller groups to limit memory use, then concatenate their losses.

The score term uses detached loss differences. Any topology cleanup must depend on sampled bits, not detached logits. Use deterministic evaluation, common auxiliary randomness within each pair, or auxiliary randomness independent of the sampled topology.

This helper estimates the expectation of the full loss: average losses, not rendered images. bernoulli_topology_samples() exposes the underlying correlated samples for custom estimators.

drtk.pixel_grid_mesh_ref(depth_z, displacement_xyz, intrinsics, alive, join_right, join_down, diagonal=None, feather_width=None, *, return_metadata=False)[source]#

Tensor reference for pixel_grid_mesh(), supporting CPU and CUDA.

See pixel_grid_mesh() for the complete argument and return-value contract.