CVPR 2025 · Diffusion image editing · 深度论文精读

h-Edit

Effective and Flexible Diffusion-Based Editing via Doob's h-Transform

论文把冻结扩散模型上的图像编辑写成反向时间桥问题,以 Doob \(h\)-transform 分离“重建基底”和“编辑增量”。理论给出理想桥的边际分布;实际算法使用固定 inversion 起点、有限步 Langevin 类更新与近似 reward score,因此必须区分精确定理、作者近似和代码行为。

Toan Nguyen · Kien Do · Duc Kieu · Thin NguyenA2I2, Deakin UniversityarXiv:2503.02187v1 · 2025-03-0433 页合订本arXiv官方固定提交
Research gap

关键贡献是可组合的编辑分解

既有 training-free 方法多从 inversion 或 attention control 改善重建,缺少同时容纳文本条件、外部 reward 和多目标组合的统一表达。h-Edit 把每一步写成基底 \(x^{\mathrm{base}}_{t-1}\) 加 \(h\)-score 编辑项,使 deterministic / random inversion、P2P、MasaCtrl、PnP 以及视觉 reward 进入同一框架。

Paper verified

冻结模型

没有训练扩散网络。文本实验用 Stable Diffusion 1.4;人脸实验用 CelebA-HQ 256 无条件扩散模型与 ArcFace;风格实验用 CLIP 第三层特征 Gram reward。

Paper verified

显式与隐式

显式形式在 \(x_t\) 评估 score;隐式形式先得到 \(x^{\mathrm{base}}_{t-1}\),再在 \(t-1\) 状态执行一个或多个 inner-loop。

Critical boundary

近似桥

定理要求 harmonic \(h\) 和倾斜初分布。实际固定 \(x_T^{\mathrm{edit}}=x_T^{\mathrm{orig}}\),reward 又通过 Tweedie 均值近似,输出没有一般性的精确目标分布保证。

h-Edit pipeline overview
Figure 2 对应概览。重建路径先给出基底,编辑 score 在 implicit loop 中更新状态。
Problem formulation

Doob \(h\)-transform 的成立条件

基础反向链为 \(p(x_{t-1}\mid x_t)\)。\(h(x_t,t)\) 应为非负、在相关支持域上严格为正且可积。论文令 \(h(x_0,0)=p_Y(x_0)\);严谨语义是目标属性的未归一化权重或 likelihood,额外归一化后才成为概率密度。

Doob transition 与 harmonic recursion
\[ p_h(x_{t-1}\mid x_t)=p(x_{t-1}\mid x_t)\frac{h(x_{t-1},t-1)}{h(x_t,t)},\qquad h(x_t,t)=\int p(x_{t-1}\mid x_t)h(x_{t-1},t-1)\,dx_{t-1}. \]

第二式负责归一化,并由 Chapman-Kolmogorov 得到 \(h(x_t,t)=\mathbb E_{p(x_0\mid x_t)}[h(x_0,0)]\)。Appendix A.1 末尾积分变量存在排版误写,按 \(x_{t-1}\) 才与推导一致。

Proposition 1 边际与归一化常数
\[ Z=\mathbb E_{p(x_0)}[h(x_0,0)],\qquad p_h(x_T)=\frac{p(x_T)h(x_T,T)}{Z}\Longrightarrow p_h(x_t)=\frac{p(x_t)h(x_t,t)}{Z}. \]
论文真正证明:harmonic recursion、合法支持域和倾斜初分布同时满足时,\(p_h(x_0)=p(x_0)p_Y(x_0)/Z\)。
实际算法边界:单个 inversion 点通常不服从 \(p(x_T)h(x_T,T)/Z\)。任意正 reward 经 \(h(\mathbb E[x_0\mid x_t],0)\) 近似后也不会自动满足 harmonic recursion。定理只能解释更新方向。

Langevin 分解

\[ x\leftarrow x+\eta\nabla_x\log p(x)+\eta\nabla_x\log h(x,t)+\sqrt{2\eta}\,z,\qquad z\sim\mathcal N(0,I). \]

论文把固定时刻 LMC 更新改写成跨 \(t\rightarrow t-1\) 的扩散更新,并把基础 score 合入 \(x^{\mathrm{base}}_{t-1}\)。VP drift、跨时间系数和原图残差使它只构成近似类比。

显式

\[ x^{\mathrm{edit}}_{t-1}=x^{\mathrm{base}}_{t-1}+\gamma_t\nabla_{x_t}\log h(x_t,t),\quad\gamma_t\gt0. \]

score 在当前状态评估;Eq. 13 把固定时刻更新用作跨时间转换。

隐式

\[ x^{(0)}_{t-1}=x^{\mathrm{base}}_{t-1},\quad x^{(k+1)}_{t-1}=x^{(k)}_{t-1}+\gamma_t\nabla_x\log h(x,t-1)|_{x=x^{(k)}_{t-1}}. \]

“隐式”表示在下一状态计算并允许重复更新,没有精确求解一般 implicit SDE。正 \(\gamma\) 不改变全局 argmax;有限步、步长、初始化和重建正则形成实际权衡。

Algorithm 1–2

完整编辑流程

原图轨迹。确定性版本用 DDIM inversion;随机版本可对各时刻并行采样。保存 \(u_t^{\mathrm{orig}}=x_{t-1}^{\mathrm{orig}}-\tilde\mu(x_t^{\mathrm{orig}},c_{\mathrm{orig}})\)。
起点。\(x_T^{\mathrm{edit}}=x_T^{\mathrm{orig}}\),共享噪声起点,同时形成理论与实践的边界。
重建基底。确定性版本执行 original prompt reverse step;随机版本加入 \(u_t^{\mathrm{orig}}\)。残差沿原图轨迹复用,不是新独立 Gaussian LMC noise。
文本增量。SD 的 CFG 为 \(\tilde\epsilon=w\epsilon_c+(1-w)\epsilon_{\varnothing}\)。h-Edit 用 target、original、unconditional 三个预测;基底 \(w_{\mathrm{orig}}\) 与编辑项 \(\hat w_{\mathrm{orig}}\) 可以不同。
Reward。以 Tweedie 估计干净图,再经 VAE decoder 与 reward encoder 回传;官方实现 stop-gradient noise prediction,不计算 UNet Jacobian。
Inner-loop。隐式形式做 \(K\) 次更新,每次重算冻结 noise;P2P local blend 在循环后执行。无 P2P 文本实验跳过前 15 steps。
\[ x_t=a_tx_0+\sigma_t\epsilon,\qquad\tilde\epsilon(x_t,t,c)=w\epsilon_\theta(x_t,t,c)+(1-w)\epsilon_\theta(x_t,t,\varnothing). \]

\(\eta=0\) 对应 deterministic DDIM,\(\eta=1\) 对应 DDPM 型随机采样。

可直接复现的离散系数

采用论文统一记号 \(\bar\alpha_t=\prod_{s=1}^{t}\alpha_s\),并定义

\[ a_t=\sqrt{\bar\alpha_t},\qquad \sigma_t=\sqrt{1-\bar\alpha_t},\qquad \sigma_{\eta,t,t-1}=\eta\,\frac{\sigma_{t-1}}{\sigma_t}\sqrt{1-\frac{a_t^2}{a_{t-1}^2}}. \]

广义 DDIM/DDPM reverse mean 和 noise coefficient 写为

\[ b_t=\sqrt{\sigma_{t-1}^2-\sigma_{\eta,t,t-1}^2}-\frac{\sigma_ta_{t-1}}{a_t}, \qquad \widetilde\mu_{\theta,\eta,t,t-1}(x_t,c) =\frac{a_{t-1}}{a_t}x_t+b_t\widetilde\epsilon_\theta(x_t,t,c). \]

确定性路径取 \(\eta=0\),所以 \(\sigma_{\eta,t,t-1}=0\);随机路径取 \(\eta\gt0\),并在均值外保留 \(\sigma_{\eta,t,t-1}z_t\)。两类 reconstruction base 分别为

\[ x_{t-1}^{\mathrm{base,D}} =\widetilde\mu_{\theta,0,t,t-1}(x_t^{\mathrm{edit}},c_{\mathrm{orig}}), \qquad x_{t-1}^{\mathrm{base,R}} =\widetilde\mu_{\theta,\eta,t,t-1}(x_t^{\mathrm{edit}},c_{\mathrm{orig}})+u_t^{\mathrm{orig}}, \]
\[ u_t^{\mathrm{orig}} =x_{t-1}^{\mathrm{orig}} -\widetilde\mu_{\theta,\eta,t,t-1}(x_t^{\mathrm{orig}},c_{\mathrm{orig}}). \]

文本编辑函数必须保留 original、target 与 unconditional 三个 prediction 的权重差:

\[ f_\theta(x,t) =w_{\mathrm{edit}}\epsilon_\theta(x,t,c_{\mathrm{edit}}) -\widehat w_{\mathrm{orig}}\epsilon_\theta(x,t,c_{\mathrm{orig}}) +\left(\widehat w_{\mathrm{orig}}-w_{\mathrm{edit}}\right)\epsilon_\theta(x,t,\varnothing). \]

显式关系使用当前时刻的 \(\sigma_t\):

\[ \nabla_{x_t}\log h(x_t,t)=-\frac{1}{\sigma_t}f_\theta(x_t,t),\qquad \gamma_t^{\mathrm{E}}=-b_t\sigma_t\gt0, \]
\[ x_{t-1}^{\mathrm{edit}} =x_{t-1}^{\mathrm{base}}+\gamma_t^{\mathrm{E}}\nabla_{x_t}\log h(x_t,t) =x_{t-1}^{\mathrm{base}}+b_tf_\theta(x_t,t). \]

隐式关系使用下一状态和 \(\sigma_{t-1}\):

\[ \nabla_x\log h(x,t-1)|_{x=x_{t-1}^{(k)}} =-\frac{1}{\sigma_{t-1}}f_\theta(x_{t-1}^{(k)},t-1),\qquad \gamma_t^{\mathrm{I}}=-b_t\sigma_{t-1}\gt0, \]
\[ x_{t-1}^{(k+1)} =x_{t-1}^{(k)}+\gamma_t^{\mathrm{I}}\nabla_x\log h(x,t-1)|_{x=x_{t-1}^{(k)}} =x_{t-1}^{(k)}+b_tf_\theta(x_{t-1}^{(k)},t-1). \]

严格正号只适用于对应 \(\sigma\gt0\) 的更新时刻。最后 \(t-1=0\) 时 \(\sigma_0=0\),官方 face 实现将 inner-loop 数设为零。\(w_{\mathrm{orig}}\) 只控制 base CFG,\(\widehat w_{\mathrm{orig}}\) 控制编辑增量,两者不能混同。

Designing h

文本、reward 与重建约束

条件扩散

\[ \nabla_x\log h(x,t)=\nabla_x\log p(x\mid y)-\nabla_x\log p(x). \]

target 与 original prompt 的 noise 差近似两项;\(\hat w_{\mathrm{orig}}\) 调节减去多少原图语义。

干净图 reward

\[ \widehat x_0(x_t)=\frac{x_t-\sigma_t\,\mathrm{stopgrad}[\epsilon_\theta(x_t,t)]}{a_t},\qquad h(x_t,t)\approx h(\widehat x_0(x_t),0). \]

非线性 reward 满足 \(\mathbb E[h(x_0,0)\mid x_t]\neq h(\mathbb E[x_0\mid x_t],0)\)。梯度通过 VAE 与 ArcFace/CLIP/LPIPS,不通过 UNet。

重建专家

\[ h_{\mathrm{rec}}(x)=\exp\!\left(-\lambda\lVert x-x^{\mathrm{base}}\rVert_2^2\right). \]

论文用于多次更新回拉;代码改用 L1 gradient 与 RMS 归一化,\(K=1\) 时不执行 shrink。

Product of experts

\[ h=\prod_{i=1}^{m}h_i,\qquad\nabla\log h=\sum_{i=1}^{m}\nabla\log h_i. \]

score 可相加;多个 harmonic \(h_i\) 的乘积一般不再 harmonic,组合仍是实用近似。

Reproducibility

三项任务、三套协议

文本引导

SD1.4,50 steps。PIE-Bench 700 图、10 类编辑;mask 只用于背景指标。h-Edit-D 权重为 \((1,10,9)\),h-Edit-R 为 \((1,7.5,5)\)。P2P self/cross fraction=.6/.4;random/EF self=.35。

Face

CelebA-HQ 256,100 steps,5,000 对。ArcFace IR-SE-50 同时作 reward 和主 ID 指标,另加负 LPIPS-VGG reward。裁切 \([35{:}223,32{:}220]\)。\(\rho_t=\rho_{\mathrm{face}}\sqrt{\bar\alpha_t}\):1-step/EF 为 100,3-step 为 50;最后 mask 恢复背景。

文本 + 风格

每图随机配 11 种 style 之一。CLIP 第三层 feature Gram 的负 L2 为 reward,梯度范数与文本增量一致。h-Edit/EF 的 style 权重分别 .6/1.5,来自视觉调参;关闭 local blend,主要提供 qualitative 证据。

没有训练设置:方法 training-free,没有 optimizer、learning rate、epoch 或训练时长。
Quantitative evidence

Table 1–3 与 Figure 3 右表

CLIP、Local CLIP、SSIM、PSNR 越高越好;DINO、LPIPS 越低越好。DINO/LPIPS 已乘 100,SSIM 已乘 10。

Table 1 · PIE-Bench(11 行)
Inv.Attn.MethodCLIPLocalDINO×100↓LPIPS×100↓SSIM×10↑PSNR↑
Deter.P2PNP.246.1401.626.908.3426.21
Deter.P2PNT.248.1301.346.078.4127.03
Deter.P2PStyleD.248.0851.176.618.3426.05
Deter.P2PNMG.249.0871.325.598.4727.05
Deter.P2PPnP Inv.250.0951.175.468.4827.22
Deter.P2Ph-Edit-D.253.1471.174.858.5427.87
RandomNoneEF.254.1221.296.098.3725.87
RandomNoneLEDITS++.254.1132.348.888.1123.36
RandomNoneh-Edit-R.255.1481.285.558.4626.43
RandomP2PEF.255.1261.515.708.4026.30
RandomP2Ph-Edit-R.256.1591.455.088.5026.97

不同 inversion / attention 组分别比较。h-Edit-D 在 deterministic + P2P 组最强;h-Edit-R + P2P 在 random + P2P 组更强。无 P2P 更适合添加/删除,P2P 更利于颜色/纹理。

Text comparison
Figure 1 / Appendix E.1 官方图。PnP Inv 与 NMG 在困难案例中可能复现原图,获得保真分数但没有完成编辑。
Figure 3 右表 · Face(7 行)
MethodID↑Expr↓Pose↓LPIPS↓FID↓
FaceShifter.702.392.81.0810.16
MegaFS.342.88*7.71.1527.07
AFS.472.924.68.1317.55
DiffFace.613.044.35.1011.89
EF.743.104.12.0620.78
h-Edit-R.802.763.78.0417.68
h-Edit-R (3s).843.104.29.0519.12

* MegaFS expression 只在可检测脸上计算。3-step 提升 ID,但 expression、pose、LPIPS、FID 全部劣于 1-step。主 reward 与主 ID 同用 ArcFace,存在适应评价风险。

Face comparison
Figure 3 左图官方图。主表输出经过 mask 后处理;无 mask 只在 Appendix F.5 展示。
Table 2 · 外部身份 encoder
Model / MetricFaceShifterMegaFSAFSDiffFaceEFh-Rh-R 3s
ArcFace Res34 / cos↑.54.33.44.56.50.52.55
VGG-Face / L2↓.991.121.03.961.021.00.97
FaceNet128 / L2↓.831.02.86.77.83.80.77
FaceNet512 / L2↓.811.01.87.77.83.81.77

DiffFace 在四个外部表示中最好或并列最好;h-Edit-R 3s 多为第二或并列,FaceShifter 在 Res34 上 .54 高于 1-step 的 .52。

Table 3 · MasaCtrl / PnP(11 行)
Attn.Inv.MethodCLIPLocalDINO×100↓LPIPS×100↓SSIM×10↑PSNR↑
MasaCtrlDeter.PnP Inv.243.0682.478.798.1322.64
MasaCtrlDeter.h-Edit-D.243.0712.388.628.1622.85
MasaCtrlRandomEF.241.0592.758.578.1522.49
MasaCtrlRandomh-Edit-R.242.0652.468.428.1822.68
PnPDeter.NP.250.1521.848.558.1925.05
PnPDeter.NT.251.1441.587.948.2425.53
PnPDeter.NMG.253.1012.089.968.0223.20
PnPDeter.PnP Inv.253.1091.759.298.1524.18
PnPDeter.h-Edit-D.254.1581.517.288.3325.68
PnPRandomEF.253.1181.486.878.3224.77
PnPRandomh-Edit-R.255.1471.395.978.4325.75
Combined editing
Figure 4 / Appendix E.3 官方图。没有统一 style benchmark;参数按曲线交点与视觉质量选择。

Appendix Figure 5–17 导读占位

Figure 5

[原图占位] h-Edit-D + P2P 与 deterministic inversion + P2P baselines 的额外视觉比较,补充 Table 1 的困难编辑案例。

Figure 6

[原图占位] h-Edit-R、EF 与 LEDITS++ 在 P2P 下的额外结果,显示编辑成功与背景保留差异。

Figure 7

[原图占位] h-Edit-R、EF、LEDITS++ 无 P2P 的额外结果;与 Figure 6 联合说明 attention control 对颜色、纹理与添加/删除的影响。

Figure 8

[原图占位] Face baselines 的额外对比,图下 ID 使用外部 ArcFace ResNet34,呼应 Table 2 的跨 encoder 复核。

Figure 9

[原图占位] style weight sweep:h-Edit-R + P2P 范围 .1–1.0,EF + P2P 范围 1.1–2.0;曲线包含 style loss、Local CLIP、LPIPS,没有精确点表。

Figure 10

[原图占位] EF 的 style weight 1.4–1.8 与 h-Edit 的 .4–.8 视觉序列,展示风格增强与保真下降的权衡。

Figure 11

[原图占位] 文本 + 风格组合的额外结果,图下给 style loss;不同方法用各自视觉调优后的 weight。

Figure 12

[原图占位] 固定 \(w_{\mathrm{edit}}=7.5,w_{\mathrm{orig}}=1\),扫描 \(\hat w_{\mathrm{orig}}=1,3,5,7,9\),过高值会删除过多原图信息。

Figure 13

[原图占位] 联合扫描 \(w_{\mathrm{edit}}\) 与 \(\hat w_{\mathrm{orig}}\)。更大编辑权重能处理困难案例,同时可能损害无关区域。

Figure 14

[原图占位] implicit 50 steps 的 1/2/3 inner-loop 与 explicit 200 steps 定性比较;没有 NFE 或 wall-time 等价实验。

Figure 15

[原图占位] 无 P2P、25 sampling steps 下 explicit 与 implicit 的逐例比较,补充 Table 5 的指标取舍。

Figure 16

[原图占位] P2P、25 sampling steps 下 explicit 与 implicit 的逐例比较,两种形式各有胜负。

Figure 17

[原图占位] h-Edit-R 3-step 的 masked / unmasked face outputs。无 mask 时仍有轻微背景模糊,主表则使用 mask 后处理。

Ablations

Table 4–6:参数、形式与成本

Table 4 · \(\hat w_{\mathrm{orig}}\)
\(\hat w\)CLIPLocalDINO×100↓LPIPS×100↓SSIM×10↑PSNR↑
1.256.1181.646.008.3825.75
3.255.1371.525.498.4426.36
5.256.1591.455.088.5026.97
7.254.1731.605.228.5126.94
9.241.1722.306.448.4026.03

\(\hat w=7\) 的 Local CLIP 更高;推荐 5 是多指标折衷。超过 \(w_{\mathrm{edit}}=7.5\) 的 9 破坏重建。

Table 5 · Explicit / implicit(8 行)
Attn.StepsFormCLIPLocalDINO×100↓LPIPS×100↓SSIM×10↑PSNR↑
None25explicit.252.1391.105.108.4926.79
None25implicit.255.1481.395.988.4125.77
None50explicit.253.1411.105.078.5127.00
None50implicit.255.1481.285.558.4626.43
P2P25explicit.254.1531.385.048.5026.81
P2P25implicit.255.1501.385.038.5026.88
P2P50explicit.256.1581.475.108.5026.85
P2P50implicit.256.1591.455.088.5026.97

无 P2P 时 implicit 编辑性较高,explicit 的 DINO / LPIPS 保真较高;P2P 25 steps 时 explicit Local .153 高于 implicit .150。选择 implicit 的主要理由是 \(K\gt1\)。Figure 14 的 200-step explicit 对比只有 qualitative 证据。

Table 6a · 文本秒/图
Inv.Attn.MethodTime
Deter.P2PNP21.68
Deter.P2PNT186.84
Deter.P2PStyleD467.16
Deter.P2PNMG35.67
Deter.P2PPnP Inv37.65
Deter.P2Ph-Edit-D48.63
RandomNoneEF23.20
RandomNoneLEDITS++18.31
RandomNoneh-Edit-R33.07
RandomP2PEF32.95
RandomP2Ph-Edit-R50.21
Table 6b · Face 秒/图
FaceShifter1.31
MegaFS2.29
AFS1.03
DiffFace46.42
EF26.11
h-Edit-R26.34
h-Edit-R 3s51.36
Table 6c · Text + style
EF + P2P44.32
h-Edit-R + P2P50.68

V100 32GB。h-Edit-D 比 PnP Inv 多 10.98 秒;Face 3-step 为 1-step 的约 1.95 倍。无 P2P 跳过 15 steps,名义 steps 与实际 forward 成本不同。

Paper vs. implementation

固定提交 ed87727 核查

Stop-gradient

文本 inner-loop 的 UNet 全在 torch.no_grad()。三种 CFG noise 构成 correction,不计算 UNet Jacobian。

L1 重建

仅 opt_step > 0 使用 F.l1_loss(current,base);梯度乘 correction RMS / gradient RMS 与 reconstruction weight。它和论文指数 L2 专家不同,\(K=1\) 无 shrink。

Face 梯度顺序

每轮先 ArcFace,再重算 detach noise,然后 LPIPS;tm1==0 将 inner-loop 改为 0。

Mask

调用 h-Edit / EF 时传 soft_face_mask=None,reward gradient 不受 mask 限制;mask 只在最后恢复背景。

Attention

P2P 在最后一个 inner-loop 保存 attention map,再执行 callback;风格组合关闭 local blend。

环境

Python 3.8、PyTorch 1.13.1、Diffusers 0.18.0、Transformers 4.31.0;硬件 V100 32GB。

commit ed87727f579045d2b186944f3a728ad8eb8cc3d7
text-guided/inversion/p2p_h_edit.py · face-swapping/inversion/h_edit_R.py · face-swapping/main_edit.py · text-guided-n-style/inversion/h_edit.py
Reviewer commentary

灵活性证据充分,理论保证需要收紧

Strengths

  • 分离 inversion 与 editing term。
  • 完整比较 deterministic/random 与 attention 组合。
  • 四个外部 face encoder 揭示主指标偏差。
  • 公开运行时间、参数与显隐式消融。

Weaknesses

  • 倾斜初分布没有落实到固定 inversion 点。
  • Tweedie、跨时间 LMC、score 相加、stop-gradient 均引入近似。
  • Style 缺少统一量化 benchmark。
  • CLIP 和全图 DINO 存在指标盲点。
  • 困难案例依赖手动权重或 inner-loop。

Potential improvements

  • 学习满足 harmonic recursion 的 \(h_\phi\)。
  • 自动分离编辑区域。
  • 自适应步长与收敛条件。
  • 多 reward 冲突检测。
  • 按编辑类型与跨 encoder 报告结果。
Research extension:程序化材质编辑可让 material graph renderer 提供文本一致性、材质感知、graph validity 和参数改动稀疏性 score,并用执行反馈验证图有效性;论文没有验证该方向。

附录公式勘误

Appendix A.3 Eq. 64 缺 \(\log\),Eq. 65 的 score 符号与 Eq. 57 / 66 不一致。保持时间索引自洽时,显式系数为 \(\gamma_t^{\mathrm{E}}=-b_t\sigma_t\),隐式系数为 \(\gamma_t^{\mathrm{I}}=-b_t\sigma_{t-1}\);两者分别抵消对应 score 分母并得到 \(+b_tf_\theta\)。

References

一手资料

  1. h-Edit arXiv v1:33 页正文、参考文献和 Appendix A–H。
  2. 官方 GitHub 固定提交:三项任务入口、环境、inversion、reward、attention control 和评价代码。
  3. PIE-Bench / PnP Inversion。
  4. ArcFace。