Windows Measured in Millimeters: Spacing-Invariant 3D Window Attention for Anisotropic Scans

Transformers for 3D medical scans keep their cost manageable by letting each small block of voxels attend only to other voxels inside a fixed cube, called a window. That cube is sized in voxels, not in physical units. In a typical clinical CT, in-plane voxels are about 0.5 mm wide while slices can be 3 to 5 mm apart. A "cube" of 7×7×7 voxels is therefore really a flat slab several times thicker in depth than in width. Its geometry also changes every time the scanner protocol changes. The current fix is to resample every scan onto an isotropic grid. That either invents slices by interpolation, which wastes compute on fabricated voxels, or blurs away in-plane detail.

We propose to define attention windows and positional biases in millimeters rather than voxels. Concretely, a 3D window transformer computes each scan's window shape from that scan's voxel spacing, so every window covers the same physical volume. It also replaces the usual table of learned position offsets, which is indexed by integer voxel distance, with a small network that maps physical distance to an attention bias. The claim is that one model trained this way can run on scans at their native resolution and handle mixed slice thicknesses without resampling. It should also degrade much less when tested on slice thicknesses it never saw in training, and it should spend fewer tokens on thick-slice scans.

Core Claim and What Is New

The bridge between window attention and volumetric context already exists. Swin UNETR runs a hierarchical 3D shifted-window transformer encoder with a convolutional decoder on BraTS. Its windows are isotropic voxel cubes, and its relative position bias is a lookup table over integer voxel offsets. That table is the hidden problem. A learned entry for "two voxels apart in depth" means 2 mm in one scan and 10 mm in another, so the bias cannot transfer across spacings.

Anisotropy has been handled explicitly only in convolutional networks. AH-Net splits 3D kernels into 3×3×1 in-plane and 1×1×3 through-plane parts, and that split is hard-coded. DwinFormer factorizes window attention along depth, then horizontal, then vertical axes. Its ablation shows that the order matters, but the factorization does not depend on spacing either.

Our proposal differs from all three in one respect: the window geometry and the positional term become functions of the physical spacing of each input. For a scan with voxel spacing $\Delta_a$ along axis $a$ and a target physical window extent $L$, the window size along that axis is

$$M_a = \max\left(1, \operatorname{round}(L/\Delta_a)\right),$$

and the attention logit between tokens $i$ and $j$ gains a bias

$$b_{ij} = g_\theta\big((p_i - p_j)\odot \Delta\big),$$

where $p_i$ is the integer grid position of token $i$, $\odot$ is elementwise multiplication by the spacing vector $\Delta$, and $g_\theta$ is a two-layer network shared across scans. The shifted-window step shifts by half of $M_a$ per axis. Patch merging between stages also becomes spacing-aware: early stages merge only in-plane until the effective spacing is roughly isotropic, so through-plane resolution is never halved prematurely.

Research Program

We will build the model on the Swin UNETR encoder-decoder so that the only changes are the window rule, the continuous bias, and the spacing-aware merging schedule. Variable window shapes inside a batch will be handled by bucketing scans by spacing, which keeps window counts regular inside each bucket.

The analysis has three parts. First, a controlled spacing-shift study. Starting from thin-slice CT, we will synthesize thick-slice versions by averaging adjacent slices to 2.5 mm and 5 mm. We train at one spacing and test at others, so the physical anatomy is held fixed and only acquisition spacing varies. Second, a mixed-spacing study on real data, training once on pooled abdominal CT and reporting accuracy stratified by native slice thickness. Third, a compute study measuring tokens, FLOPs, and memory at native resolution versus after isotropic resampling. BraTS, which is already near-isotropic, serves as a negative control: on it, our model should match Swin UNETR. If it does not, the continuous bias is costing accuracy rather than buying invariance.

Baselines

The naive reference point is Swin UNETR exactly as published, with fixed voxel-cube windows and a lookup-table bias, trained and tested at native spacing with no resampling. It shows how badly voxel-indexed attention fares when spacing varies. On isotropic BraTS 2021 it reports 5-fold Dice of 0.891 for enhancing tumor, 0.933 for whole tumor and 0.917 for tumor core (Swin UNETR). Matching this within noise is the control target.

The strongest practical competitor is the same Swin UNETR with every scan resampled to a common isotropic spacing before training and inference, with predictions resampled back. This is the standard practice that SegVol also follows. It is strongest because it already removes spacing variation, at the cost of interpolated voxels. We will run it with the same data, epochs, augmentation, and hyperparameter search budget as our model, and report both accuracy and compute. Beating it means one of two outcomes. Either we reach equal Dice at clearly lower FLOPs on thick-slice scans, or we reach higher Dice and lower 95% Hausdorff distance on held-out slice thicknesses at equal compute. Accuracy parity at higher cost would count as a loss.

The convolutional competitor for anisotropy is AH-Net's decomposed-kernel design, which reports a LiTS lesion Dice of 0.834 and a liver Dice of 0.970 (AH-Net). We will retrain it under our protocol rather than compare against the published figure, because our splits differ. The factorized-attention competitor is DwinFormer, which reports 87.38% Dice and 8.68 mm HD95 on Synapse, against nnFormer's 86.57% and 10.63 (DwinFormer). We take it as the strongest existing window-attention number on abdominal CT in the file and rerun it on Synapse under matched budget. On TotalSegmentator, the state-space model UlikeMamba_3d with multi-scale blocks reports 88.01 average Dice (Mamba analysis). We include it as the linear-cost non-attention reference. No reported result found on Lacuna for Swin UNETR on LiTS or Synapse.

Three ablations isolate the key ingredients. The first uses millimeter windows with the original voxel-indexed bias table, which tests whether window shape alone suffices. The second uses voxel windows with the continuous millimeter bias. The third is a hand-picked per-dataset anisotropic window, such as 7×7×2 for 5 mm data, with no continuous bias.

Why not just…?

Why not just resample everything to isotropic voxels? This is the real threat, and the proposal is built to be settled by it. Resampling does remove spacing variation. If it matches our accuracy on held-out thicknesses at similar cost, the contribution shrinks to efficiency alone. Two things argue it may fall short. Upsampling 5 mm slices to sub-millimeter spacing multiplies the token count roughly fivefold and fills it with interpolated content that carries no new evidence. Downsampling in-plane instead discards the fine detail that SegVol's zoom-out stage is already known to risk losing (SegVol). The compute-matched comparison above decides the question directly.

Why not just hand-set an anisotropic window per dataset? For a single-protocol dataset, this may well work as well as our method. That is why it is an explicit ablation rather than a straw man. It fails by construction when one model must handle mixed thicknesses, which is common in pooled abdominal collections. It also leaves the voxel-indexed bias tied to one spacing. The mixed-spacing study and the train-thin, test-thick shift will show whether the continuous bias adds anything beyond the window shape.

Why not just use decomposed axis-wise operators, as AH-Net and DwinFormer do? Both treat the through-plane axis separately, but with fixed kernels or a fixed axis order that do not read the spacing. DwinFormer's own ablation shows sensitivity to axis order, which suggests the decomposition encodes a dataset-specific bias rather than a physical one. Retraining both under the spacing-shift protocol will show whether separating the depth axis is already enough to be robust to spacing changes.

Why not just use a 3D state-space model? The Mamba analysis shows linear-cost scanning can beat a transformer baseline on BraTS (87.45 versus 85.97 Dice), but only with added 3D depthwise convolutions and three-direction scanning (Mamba analysis). Its scan order is also voxel-indexed, so it inherits the same spacing problem. We include it in the spacing-shift test. If it proves robust anyway, that would weaken the case that physical positional encoding is the bottleneck.

Decisive Experiments

The first decisive check is the synthetic thickening experiment on LiTS and TotalSegmentator. We train at native thin spacing and test at simulated 2.5 mm and 5 mm, and in reverse. The prediction is that our model's Dice drop is at most half that of voxel-window Swin UNETR, with the gap concentrated in HD95 on structures that span many slices. The second check is mixed-spacing training on pooled AMOS, BTCV, and KiTS23 CT, reporting Dice stratified by slice-thickness bin against the isotropic-resampling baseline at matched FLOPs. The third check is BraTS 2021 as the isotropic control, where all variants should coincide. If the resampling baseline matches us on the first two checks at equal compute, the core claim is false and we will report it as such. If the hand-set window matches us on mixed spacing, the continuous bias is unnecessary and the method reduces to a simple spacing rule.