Skip to content

neural_tracing: add opt-in displacement scaling across voxel sizes - #1284

Open
Nieuwlaar wants to merge 2 commits into
ScrollPrize:mainfrom
Nieuwlaar:1149-displacement-scale
Open

neural_tracing: add opt-in displacement scaling across voxel sizes#1284
Nieuwlaar wants to merge 2 commits into
ScrollPrize:mainfrom
Nieuwlaar:1149-displacement-scale

Conversation

@Nieuwlaar

Copy link
Copy Markdown
Contributor

neural_tracing: add opt-in displacement scaling across voxel sizes

Refs #1149.

This implements the three changes @LimeGS proposed in #1149. The analysis, the benchmark, and the interface are theirs; I wrote the patch because the issue has had no maintainer reply in sixteen days and I wanted the option available. Happy to close this in favour of their own PR, or to co-author, whichever they prefer.

Background

Per #1149, copy_displacement_latest was trained at 4.8 µm, and infer_rowcol_triplet_wraps.py applies predicted displacement vectors directly (world + slot_*_disp) with no resolution conversion. On one 7.91 µm Scroll 3 window, a post-hoc 4.8 / 7.91 = 0.6068 scale changed wrong-hop rates from 63.17% to 5.90% (front, n=4,697) and 56.28% to 7.73% (back, n=8,697).

@LimeGS is explicit that this is a local geometric benchmark rather than ground truth: it shows a magnitude-calibration problem in that window, and does not establish that automatic 4.8 / input_pitch scaling is universally correct. The underlying question in the issue, what the intended output-unit contract is for non-4.8 µm inputs, is still unanswered, so nothing here changes behaviour unless you ask it to.

What this adds

--displacement-scale, accepting either a positive number or auto:

# explicit, the value from the issue
--displacement-scale 0.6068
# derive training_voxel_size_um / inference voxel size from the checkpoint
--displacement-scale auto

It is also exposed as a trace-service copy arg through _COPY_ARG_TO_CLI.

Default behaviour is unchanged. With no flag, displacements are applied unscaled exactly as before. This deliberately follows the issue's request to keep scaling opt-in and not enable automatic scaling before the semantics are confirmed.

A warning, but only for a genuine mismatch. When the checkpoint config carries training_voxel_size_um and it differs from the inference voxel size, a warning names both values and suggests the flag. Checkpoints without that key stay completely silent, so nobody gets new noise for an unchanged setup. No published checkpoint carries the key today, so in practice this is inert until someone adds it.

Provenance. displacement_scale and displacement_scale_source (cli, auto, or default) are recorded in the run and tifxyz metadata, so a rendered surface says which scale produced it.

Implementation notes

The scale multiplies the six displacement channels once, immediately after prediction and before accumulation, in _run_triplet_inference. Side assignment uses the sign of displacement dotted with the normal, which is invariant under positive scaling, so it is unaffected.

The inference voxel size is the volume's meta.json voxelsize (or --tifxyz-voxel-size-um) multiplied by 2**volume_scale, reusing the same retarget_factor already used to retarget the surface grid. The unrelated 8.24 µm fallback in resolve_tifxyz_params is deliberately not used for scaling: guessing a voxel size and silently rescaling geometry from it would be worse than doing nothing.

Tests

vesuvius/tests/neural_tracing/test_displacement_scale.py covers the unscaled default (silent, and with the mismatch warning), auto derivation and each of its failure modes, explicit overrides including disagreement with checkpoint metadata, invalid inputs, and the auto predicate itself.

python -m pytest tests/neural_tracing/ -q
119 passed

Open question for maintainers

The issue's actual question still stands: should displacements on non-4.8 µm volumes be interpreted in training-grid or inference-grid voxels? If the answer is training-grid, then auto is arguably the right default and I am happy to flip it. This PR does not presume that answer.

…crollPrize#1149)

@LimeGS reports in ScrollPrize#1149 that copy_displacement_latest (trained at
4.8 um) predicts offsets in training-resolution voxels, and that
inference applies them directly regardless of the target volume's voxel
size. On one 7.91 um Scroll 3 window a post-hoc 4.8/7.91 scale reduced
wrong-hop rates from 63.17% to 5.90% (front, n=4,697) and 56.28% to
7.73% (back, n=8,697). He notes that this is a local geometric
benchmark, not ground truth, and that it does not establish that
automatic scaling is universally correct.

The issue asks for an opt-in flag defaulting to 1.0, checkpoint
metadata, and a mismatch warning, and explicitly asks that automatic
scaling not be enabled by default before the unit contract is settled.
This implements that shape:

- --displacement-scale accepts a positive number, or "auto" to derive
  training_voxel_size_um / inference voxel size from the checkpoint
  config. Also exposed as a trace-service copy arg via _COPY_ARG_TO_CLI.
- Without the flag, displacements are applied unscaled exactly as
  before. A warning fires only when the checkpoint advertises a
  training voxel size that differs from the inference one, so a real
  mismatch is not silent and no geometry changes without an explicit
  request.
- The inference voxel size is the volume meta.json voxelsize (or
  --tifxyz-voxel-size-um) times 2**volume_scale.
- displacement_scale and its source are recorded in run and tifxyz
  metadata.
- Unit tests cover the unscaled default, the mismatch warning, "auto"
  derivation and its failure modes, explicit overrides, and invalid
  input.

Refs ScrollPrize#1149.
@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

@nieuwlaar42 is attempting to deploy a commit to the scroll Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants