学术
模块更新于 2026-11-21· 浏览 1· 下载 0

HerKry HG 厄米高斯泼溅

在高斯包络上叠加厄米多项式,用系数线性组合表达局部方向性结构。固定几何后可共享渲染算子,为系数空间求解提供表示基础。

三维视觉医学影像CT重建连续表示高斯泼溅逆问题

结构图

HerKry HG 厄米高斯泼溅结构图
HerKry HG 厄米高斯泼溅结构图

在高斯包络上叠加厄米多项式,用系数线性组合表达局部方向性结构。固定几何后可共享渲染算子,为系数空间求解提供表示基础。

论文信息

  • 论文:HerKry: A Hermite–Gaussian Hierarchy with Recurrent Krylov Trajectory Modeling for Robust Sparse-View CT Reconstruction

核心痛点

稀疏视图CT的粗尺度重建需要限制自由度,但图像下采样与扇束投影不交换,简单金字塔会带来几何失配。

模块亮点

  • 在高斯包络上叠加厄米多项式,用系数线性组合表达局部方向性结构。固定几何后可共享渲染算子,为系数空间求解提供表示基础。

适用场景

稀疏视图CT的连续图像或特征表示;需要真实投影几何与匹配求解器,其他任务需单独验证。

插入位置建议

  • 放在基元参数预测后、图像解码或物理投影前。检查模式数、坐标尺度、协方差因子和显存分块,源码不是完整重建系统。

适用任务

  • 稀疏视图CT的连续图像或特征表示
  • 需要真实投影几何与匹配求解器
  • 其他任务需单独验证

完整代码

hg.py
"""HerKry(A Hermite–Gaussian Hierarchy with Recurrent Krylov Trajectory Modeling)模块:
HermiteGaussianSplatting。

问题:稀疏视图 CT 多尺度重建中,图像下采样与扇形束投影不可交换,粗尺度一致性引入几何
失配;全分辨率更新则过拟合弱约束方向。
方法:以各向异性 Hermite–Gaussian 基元表示图像,按阶数–密度调度从稀疏高阶到稠密零阶。
该模块作用:实现式 (2)(5)(7) 多通道 HG 泼溅算子 B_l(Θ_l),冻结几何下将基元系数渲染为连续
图像/特征场,供神经先验与 HC-CGLS 共用,使表示分辨率与测量尺度解耦。
"""

import math

import torch
import torch.nn as nn


class HermiteGaussianSplatting(nn.Module):
    """多通道 2D 各向异性 Hermite–Gaussian 泼溅算子 B(Θ)。

    Δx(u) = Σ_i Σ_(p,q) a_{i,pq} · H̄p(v_i,x) H̄q(v_i,y) · exp(-‖v_i(u)‖²/2) / (2π),
    其中 v_i(u) = Σ_i^{-1/2} (u - µ_i),坐标 u 归一化到 [-1, 1],故输出与像素分辨率无关。

    参数
    ----
    order : int
        Hermite 最高阶数 d,基函数个数 M = (d+1)**2,按 p*(d+1)+q 排列。
    chunk : int
        每批参与泼溅的基元数,控制显存 O(chunk·H·W)。
    """

    def __init__(self, order: int = 3, chunk: int = 1024, eps: float = 1e-8) -> None:
        super().__init__()
        self.order = int(order)
        self.chunk = int(chunk)
        self.eps = float(eps)

    def extra_repr(self) -> str:
        return f"order={self.order}, modes={(self.order + 1) ** 2}, chunk={self.chunk}"

    def _hermite(self, t: torch.Tensor) -> torch.Tensor:
        """归一化 Hermite 多项式 H̄n(t) = Hn(t)/sqrt(2^n·n!),返回 (..., order+1)。"""
        polys = [torch.ones_like(t)]
        if self.order >= 1:
            polys.append(t * math.sqrt(2.0))
        for n in range(1, self.order):
            a = math.sqrt(2.0 / (n + 1))
            b = math.sqrt(n / (n + 1))
            polys.append(a * t * polys[n] - b * polys[n - 1])
        return torch.stack(polys, dim=-1)

    def forward(
        self,
        coeff: torch.Tensor,
        mean: torch.Tensor,
        cov_factor: torch.Tensor,
        height: int,
        width: int,
    ) -> torch.Tensor:
        """渲染 HG 系数场。

        参数
        ----
        coeff      : (B, N, C, M) 每基元、每通道的 HG 线性系数(M = (d+1)**2)
        mean       : (B, N, 2)    基元中心 µ_i,归一化坐标 [-1, 1]
        cov_factor : (B, N, 2, 2) 协方差因子 Σ_i^{1/2}(下三角),2x2
        height     : 输出高度;width: 输出宽度

        返回
        ----
        (B, C, height, width) 连续图像 / 多通道特征场
        """
        if coeff.dim() != 4:
            raise ValueError("coeff 需为 (B, N, C, M) 四维张量")
        bsz, num, chan, modes = coeff.shape
        d = self.order
        if modes != (d + 1) ** 2:
            raise ValueError(f"coeff 的模式数 {modes} 与 order={d} 不匹配(应为 {(d + 1) ** 2})")
        device, dtype = coeff.device, coeff.dtype

        # 归一化坐标网格 (P, 2),P = H*W;与像素尺度无关
        ys = torch.linspace(-1.0, 1.0, height, device=device, dtype=dtype)
        xs = torch.linspace(-1.0, 1.0, width, device=device, dtype=dtype)
        gy, gx = torch.meshgrid(ys, xs, indexing="ij")
        grid = torch.stack((gx.reshape(-1), gy.reshape(-1)), dim=-1)  # (P, 2)
        pix = grid.shape[0]

        # 下三角因子求逆,得到 Σ^{-1/2}:v = Σ^{-1/2} (u - µ)
        l11 = cov_factor[..., 0, 0].clamp_min(self.eps)
        l21 = cov_factor[..., 1, 0]
        l22 = cov_factor[..., 1, 1].clamp_min(self.eps)
        inv = torch.zeros_like(cov_factor)
        inv[..., 0, 0] = 1.0 / l11
        inv[..., 1, 0] = -l21 / (l11 * l22)
        inv[..., 1, 1] = 1.0 / l22

        out = coeff.new_zeros(bsz, chan, pix)
        for s in range(0, num, self.chunk):
            e = min(s + self.chunk, num)
            diff = grid.view(1, 1, pix, 2) - mean[:, s:e, None, :]        # (B, n, P, 2)
            v = torch.einsum("bnij,bnpj->bnpi", inv[:, s:e], diff)         # (B, n, P, 2)
            env = torch.exp(-0.5 * v.pow(2).sum(-1)) / (2.0 * math.pi)     # (B, n, P)
            hx = self._hermite(v[..., 0])                                 # (B, n, P, d+1)
            hy = self._hermite(v[..., 1])                                 # (B, n, P, d+1)
            basis = (hx.unsqueeze(-1) * hy.unsqueeze(-2)).flatten(-2)     # (B, n, P, M)
            out = out + torch.einsum("bncm,bnpm->bcp", coeff[:, s:e], basis * env.unsqueeze(-1))
        return out.view(bsz, chan, height, width)


if __name__ == "__main__":
    import sys

    sys.stdout.reconfigure(encoding="utf-8")
    torch.manual_seed(0)

    B, N, C, d, H, W = 2, 64, 3, 2, 48, 40
    m = (d + 1) ** 2
    coeff = torch.randn(B, N, C, m, requires_grad=True)
    mean = torch.rand(B, N, 2) * 1.6 - 0.8
    cov_factor = torch.tril(torch.rand(B, N, 2, 2) * 0.4 + 0.05)
    print(f"输入 coeff={tuple(coeff.shape)} mean={tuple(mean.shape)} "
          f"cov_factor={tuple(cov_factor.shape)}")

    splat = HermiteGaussianSplatting(order=d)
    out = splat(coeff, mean, cov_factor, H, W)
    print(f"输出 shape={tuple(out.shape)}  dtype={out.dtype}  "
          f"finite={bool(torch.isfinite(out).all())}  mean={out.mean().item():.4f}")

    out.sum().backward()
    print(f"梯度 shape={tuple(coeff.grad.shape)}  finite={bool(torch.isfinite(coeff.grad).all())}")

    # 单基元、零阶:应还原归一化高斯包络,峰值在基元中心
    one = torch.zeros(1, 1, 1, 1)
    one[..., 0] = 2 * math.pi
    peak = HermiteGaussianSplatting(order=0)(
        one, torch.tensor([[[0.0, 0.0]]]), torch.eye(2).view(1, 1, 2, 2), 33, 33)
    print(f"零阶单基元峰值={peak.max().item():.4f}(理论值 1.0),"
          f"峰值位置={tuple(torch.nonzero(peak == peak.max())[0, 2:].tolist())}")

环境与依赖

Python 3.9+

继续浏览

下一篇HGEGC MixerCSeg 裂缝分割原理创新:核心TransMixer基于Mamba隐式注意力特性,将token解耦为全局/局部两路,分别用Transformer建模长依赖、CNN提取局部纹理,三者协同工作而非简单堆叠,特征提取能力显著提升

相关资源