ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

RePaintScheduler 详解:基于 DDPM 的极端掩码无监督图像修复调度器

RePaintScheduler 详解:基于 DDPM 的极端掩码无监督图像修复调度器 RePaintScheduler 详解基于 DDPM 的极端掩码无监督图像修复调度器【免费下载链接】diffusers Diffusers: State-of-the-art diffusion models for image, video, and audio generation in PyTorch.项目地址: https://gitcode.com/GitHub_Trending/di/diffusersRePaintScheduler 是 Hugging Face Diffusers 中一个基于 DDPMDenoising Diffusion Probabilistic Models的专用图像修复inpainting调度器其设计目标是应对极端掩码extreme masks下的无监督修复任务——即使被遮挡区域占比极大仍能生成语义连贯、内容合理的填充结果。本文以 repaint.md 为骨架深入源码讲解其设计原理、Resampling 机制、完整使用方法与参数细节帮助你掌握如何在 Diffusers 中直接调用 RePaint 完成高质量图像修复。背景为什么图像修复需要专门的调度器常规的图像修复方法往往针对特定分布的掩码进行训练一旦遇到未见过的掩码类型泛化能力就会显著下降同时基于像素级与感知损失pixel-wise and perceptual losses的训练方式常常导致缺失区域只是简单的纹理延伸而非有语义意义的生成。RePaint 论文RePaint: Inpainting using Denoising Diffusion Probabilistic ModelsAndreas Lugmayr 等人论文编号 2201.09865提出了一种完全不同的思路不训练、不修改网络直接复用预训练的无条件 DDPM 作为生成先验generative prior。在反向扩散迭代过程中仅通过在未掩码区域采样已知图像信息这一操作来完成条件化。由于 DDPM 网络本身从未被修改或条件化模型对任意形式的修复掩码都能输出高质量且多样化的结果论文在面部与通用图像修复上、针对标准与极端掩码均进行了验证并在六种掩码分布中的至少五种上超越了当时的 SOTA 自回归Autoregressive与 GAN 方法。在 Diffusers 中RePaintScheduler需要配合RePaintPipeline使用核心实现位于 src/diffusers/schedulers/scheduling_repaint.py 与 src/diffusers/pipelines/deprecated/repaint/pipeline_repaint.py。核心概念Resampling重采样跳跃机制RePaint 算法的关键思想是在去噪过程中周期性回跳jump back到更早的时间步让已知区域的像素信息有机会重新扩散到未知区域。这一机制由set_timesteps中的两个参数控制对应论文 Figure 9、Figure 10 的示意jump_length默认10单次跳跃中向前时间上回退走的步数即论文中的 jjump_n_sample默认10对于某个选定时间样本执行向前时间跳跃的次数。从源码看set_timesteps首先构造跳跃表每间隔jump_length步记录一次可跳次数然后在生成时间步序列的过程中每走到一个可跳点就回退t t 1并重新追加时间步jumps {} for j in range(0, num_inference_steps - jump_length, jump_length): jumps[j] jump_n_sample - 1 t num_inference_steps while t 1: t t - 1 timesteps.append(t) if jumps.get(t, 0) 0: jumps[t] jumps[t] - 1 for _ in range(jump_length): t t 1 timesteps.append(t)最终时间步会按num_train_timesteps // num_inference_steps缩放映射回训练时的完整时间步空间scheduling_repaint.py。可以推断jump_length越大每次回跳覆盖的时间跨度越长jump_n_sample越大回跳发生的次数越多修复质量通常越高但总迭代步数与推理耗时也随之增加。反向去噪step方法中的已知/未知区域混合step方法是 RePaint 采样的核心其输入比普通调度器多了两个关键张量original_image待修复的原始图像mask修复掩码其中值为0.0的位置表示需要被修复的区域注意与常见约定相反0是被修复区。方法内部遵循 RePaint 论文 Algorithm 1 的流程预测原始样本由模型输出的噪声残差反解出pred_original_sample即预测的 x₀并在clip_sampleTrue时裁剪到[-1, 1]计算未知区域样本使用 DDIM 论文公式 (12) 计算prev_unknown_part——这里源码特意指出RePaint Algorithm 1 Line 5 的原始公式有误应参照论文公式 (8a)用方差的开方而非方差本身缩放高斯噪声因此prev_known_part的实现为prev_known_part (alpha_prod_t_prev**0.5) * original_image ((1 - alpha_prod_t_prev) ** 0.5) * noise按掩码混合最终上一时间步样本为pred_prev_sample mask * prev_known_part (1.0 - mask) * prev_unknown_part即已知区域直接继承带噪的原始图像信息未知区域完全由 DDPM 生成scheduling_repaint.py。step的返回值是RePaintSchedulerOutput含prev_sample与pred_original_sample后者可用于进度预览或 guidance当return_dictFalse时返回二元组。回跳的实现undo_stepundo_step负责将样本从x_{t-1}加噪推回x_t即正向扩散一步对应论文 Algorithm 1 Line 10。它按num_train_timesteps // num_inference_steps的次数循环逐步执行sample (1 - beta) ** 0.5 * sample beta**0.5 * noisescheduling_repaint.py。源码还针对 Apple MPS 设备做了特殊处理以保证随机数生成的可复现性。在 pipeline_repaint.py 的去噪循环中调度器的两个方法被这样组合使用t_last self.scheduler.timesteps[0] 1 for i, t in enumerate(self.progress_bar(self.scheduler.timesteps)): if t t_last: # 正常去噪x_t - x_{t-1} model_output self.unet(image, t).sample image self.scheduler.step(model_output, t, image, original_image, mask_image, generator).prev_sample else: # 回跳x_{t-1} - x_t重新加噪 image self.scheduler.undo_step(image, t_last, generator) t_last t可以看到set_timesteps生成的时间序列并非单调递减——当序列出现增大回跳时pipeline 改用undo_step把图像推回更早的时间步这正是 RePaint 反复利用已知区域信息的精髓。完整使用示例RePaintPipeline 的 docstring 给出了一个可直接运行的示例pipeline_repaint.py基于预训练的google/ddpm-ema-celebahq-256无条件 DDPM 模型from io import BytesIO import torch import PIL import requests from diffusers import RePaintPipeline, RePaintScheduler def download_image(url): response requests.get(url) return PIL.Image.open(BytesIO(response.content)).convert(RGB) img_url https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/celeba_hq_256.png mask_url https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png # 加载原始图像与掩码 original_image download_image(img_url).resize((256, 256)) mask_image download_image(mask_url).resize((256, 256)) # 基于预训练 DDPM 加载 RePaint 调度器与 pipeline scheduler RePaintScheduler.from_pretrained(google/ddpm-ema-celebahq-256) pipe RePaintPipeline.from_pretrained(google/ddpm-ema-celebahq-256, schedulerscheduler) pipe pipe.to(cuda) generator torch.Generator(devicecuda).manual_seed(0) output pipe( imageoriginal_image, mask_imagemask_image, num_inference_steps250, eta0.0, jump_length10, jump_n_sample10, generatorgenerator, ) inpainted_image output.images[0]关键点掩码约定mask_image中0.0表示待修复区域1.0表示保留区域预处理pipeline 内置的_preprocess_mask会将掩码 resize 到 32 的整数倍并二值化 0.5置 0 0.5置 1图像则 resize 到 8 的整数倍并归一化到[-1, 1]pipeline_repaint.py输出默认output_typepil返回ImagePipelineOutput可通过output.images[0]取出图像。参数一览RePaintScheduler构造函数参数均通过register_to_config注册可被from_pretrained读取参数默认值说明num_train_timesteps1000模型训练所用的扩散步数beta_start0.0001beta 序列起始值beta_end0.02beta 序列终止值beta_schedulelinearbeta 调度方式可选linear、scaled_linear、squaredcos_cap_v2、sigmoideta0.0扩散步中添加噪声的权重0.0对应 DDIM1.0对应 DDPMtrained_betasNone直接传入自定义 beta 数组绕过beta_start/beta_endclip_sampleTrue将预测样本裁剪到[-1, 1]以保证数值稳定性RePaintPipeline.__call__的核心运行参数参数默认值说明num_inference_steps250去噪步数越多质量越高但越慢eta0.0噪声权重0.0 DDIM1.0 DDPMjump_length10单次回跳的步数论文中的 jjump_n_sample10每个采样点的回跳次数generatorNone随机数生成器保证可复现output_typepil输出格式可选PIL.Image或np.arrayreturn_dictTrue是否返回ImagePipelineOutput而非 tuple训练与采样分工add_noise 的限制值得注意的一个设计细节是RePaintScheduler.add_noise直接抛出NotImplementedError提示训练 RePaint 采样应改用DDPMScheduler.add_noise()scheduling_repaint.py。这体现了 RePaint 的核心哲学——它不训练新网络只是为已有的无条件 DDPM 增加带掩码条件化采样的能力因此训练侧完全复用标准 DDPM 流程即可这也是它能直接加载google/ddpm-ema-celebahq-256这类现成检查点的原因。与库的集成RePaintScheduler在库中通过 src/diffusers/schedulers/init.py 导出与RePaintPipeline一同注册在 src/diffusers/init.py 的顶层命名空间中即from diffusers import RePaintScheduler, RePaintPipeline即可直接使用。此外社区示例 examples/community/stable_diffusion_repaint.py 还提供了将 RePaint 思想应用于 Stable Diffusion 潜在空间的参考实现可作为进阶阅读材料。相关文档收录于 docs/source/en/api/schedulers/repaint.md并在 docs/source/en/_toctree.yml 的调度器章节中注册。小结RePaintScheduler提供了一种零训练、零网络修改的通用修复方案通过周期性回跳Resampling让已知区域信息反复参与生成配合step中的已知/未知区域掩码混合即可让任意无条件 DDPM 胜任从标准掩码到极端掩码的图像修复任务。理解jump_length、jump_n_sample与eta三个参数就掌握了在 Diffusers 中调优 RePaint 的核心控制面——质量与速度的权衡尽在这组参数之中。【免费下载链接】diffusers Diffusers: State-of-the-art diffusion models for image, video, and audio generation in PyTorch.项目地址: https://gitcode.com/GitHub_Trending/di/diffusers创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表