Chucks90's picture
download
raw
4.26 kB
"""Spacing-conditioned geometry for anisotropic 3D medical volumes.
Design constant : physical receptive field per axis, in millimetres.
Free variables : dilation and pooling stride, both derived from voxel spacing.
Parameter count is invariant to spacing. Dilation resamples a fixed kernel and
pooling has no weights, so one set of weights serves every acquisition protocol
with no resampling anywhere in the pipeline.
Verified properties (see test_geometry.py):
1. Isotropic input reduces exactly to a plain 3D CNN.
2. Anisotropy of the step receptive field is non-increasing across levels.
3. Dilation and stride depend only on spacing ratios, so a global rescale of
spacing leaves them unchanged and scales cumulative RF linearly.
4. Dilation stays in [1, d_max] under 33:1 anisotropy.
"""
from dataclasses import dataclass, asdict
@dataclass
class Level:
index: int
spacing: tuple # effective mm/voxel entering this level
kernel: tuple # kernel size in voxels
dilation: tuple
stride: tuple # pooling applied after this level
step_rf_mm: tuple # physical extent added by this level, per axis
cum_rf_mm: tuple # cumulative full receptive field, per axis, mm
anisotropy: float # max/min of step_rf_mm; 1.0 is isotropic
def as_dict(self):
return asdict(self)
def geometry(spacing, n_levels=4, k=1, d_max=2, tau=2.0, convs_per_level=2,
pool_kernel=2):
"""Build the level ladder for one scan.
Args:
spacing: (sx, sy, sz) native mm/voxel, straight off the DICOM header.
n_levels: encoder depth.
k: kernel half-width. k=1 -> 3-tap.
d_max: dilation cap. Bounds gridding; the residual anisotropy is
handed to pooling instead.
tau: pool axis a only if s_a < tau * min(s). Coarse axes are held
until the fine axes catch up to them.
convs_per_level: convolutions per encoder block.
pool_kernel: max-pool kernel size (stride is always 2 where pooled).
Returns:
list[Level], length n_levels. The final level never pools.
Receptive field uses the standard native-voxel recursion,
r_l = r_{l-1} + (k_eff - 1) * j_{l-1}, k_eff = d * (ksize - 1) + 1
j_l = j_{l-1} * stride
then converts to millimetres by multiplying by native spacing.
"""
if len(spacing) != 3:
raise ValueError(f"spacing must be 3 values, got {spacing}")
if any(v <= 0 for v in spacing):
raise ValueError(f"spacing must be strictly positive, got {spacing}")
native = [float(v) for v in spacing]
s = list(native)
ksize = 2 * k + 1
j = [1.0, 1.0, 1.0]
r = [1.0, 1.0, 1.0]
out = []
for l in range(n_levels):
s_max, s_min = max(s), min(s)
d = tuple(max(1, min(d_max, round(s_max / v))) for v in s)
step = tuple(k * d[a] * s[a] for a in range(3))
last = l == n_levels - 1
stride = (1, 1, 1) if last else tuple(
2 if s[a] < tau * s_min else 1 for a in range(3)
)
for _ in range(convs_per_level):
k_eff = [d[a] * (ksize - 1) + 1 for a in range(3)]
r = [r[a] + (k_eff[a] - 1) * j[a] for a in range(3)]
if not last:
r = [r[a] + (pool_kernel - 1) * j[a] if stride[a] > 1 else r[a]
for a in range(3)]
j = [j[a] * stride[a] for a in range(3)]
out.append(Level(
index=l,
spacing=tuple(s),
kernel=(ksize,) * 3,
dilation=d,
stride=stride,
step_rf_mm=step,
cum_rf_mm=tuple(r[a] * native[a] for a in range(3)),
anisotropy=max(step) / min(step),
))
s = [s[a] * stride[a] for a in range(3)]
return out
def final_rf_mm(spacing, **kw):
"""Cumulative full RF at the deepest level. Log this per scan as a
covariate; see the RF-spread confound in the spec."""
return geometry(spacing, **kw)[-1].cum_rf_mm
def fixed_geometry(spacing, n_levels=4, k=1, **kw):
"""Baseline arm A: spacing-blind. Dilation 1, symmetric pooling."""
return geometry(spacing, n_levels=n_levels, k=k, d_max=1,
tau=float("inf"), **kw)

Xet Storage Details

Size:
4.26 kB
·
Xet hash:
3e8a5662bbffa2f2cd3d34b6e3107f84035985b6c01206e6c4f78b077de34857

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.