Diffusers documentation
RePaintScheduler
RePaintScheduler
RePaintScheduler 是一种基于 DDPM 的图像修复调度器,适用于极端掩码条件下的无监督图像修复。它专为配合 RePaintPipeline 使用而设计,其理论基础是 Andreas Lugmayr 等人发表的论文 RePaint: Inpainting using Denoising Diffusion Probabilistic Models。
该论文的摘要如下:
自由形式图像修复(inpainting)是指在由任意二值掩码指定的区域中向图像添加新内容的任务。大多数现有方法针对特定的掩码分布进行训练,这限制了它们对未见过的掩码类型的泛化能力。此外,使用逐像素损失和感知损失进行训练往往导致向缺失区域进行简单的纹理扩展,而非语义上有意义的生成。在本工作中,我们提出了 RePaint:一种基于去噪扩散概率模型(DDPM)的图像修复方法,即使面对极端掩码也适用。我们采用预训练的非条件 DDPM 作为生成先验。为了对生成过程进行条件化,我们仅通过利用给定图像信息对未掩码区域进行采样来修改反向扩散迭代。由于该技术不修改或条件化原始 DDPM 网络本身,该模型能够为任何修复形式生成高质量且多样化的输出图像。我们使用标准掩码和极端掩码,针对人脸和通用图像修复验证了我们的方法。在至少六种掩码分布中的五种上,RePaint 的表现优于最先进的自回归(Autoregressive)和 GAN 方法。GitHub 仓库:this http URL。
原始实现可在 andreas128/RePaint 中找到。
RePaintScheduler
class diffusers.RePaintScheduler
< source >( num_train_timesteps: int = 1000beta_start: float = 0.0001beta_end: float = 0.02beta_schedule: str = 'linear'eta: float = 0.0trained_betas: numpy.ndarray | None = Noneclip_sample: bool = True )
参数
- num_train_timesteps (
int,默认为1000) — 用于训练模型的扩散步数。 - beta_start (
float,默认为0.0001) — 推理的起始beta值。 - beta_end (
float,默认为0.02) — 最终的beta值。 - beta_schedule(
str,默认为"linear") — Beta 调度,将 beta 范围映射为用于模型步进的一系列 beta 值。可选linear、scaled_linear、squaredcos_cap_v2或sigmoid。 - eta (
float,默认为0.0) — 扩散步骤中添加的噪声权重。0.0对应 DDIM,1.0对应 DDPM。 - trained_betas (
np.ndarray, 可选) — 直接传入 betas 数组到构造函数中,以绕过beta_start和beta_end。 - clip_sample (
bool,默认为True) — 将预测样本裁剪到 -1 和 1 之间,以确保数值稳定性。
RePaintScheduler 是用于在给定掩码内进行 DDPM 图像修复的调度器。
该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。
add_noise
< source >( original_samples: Tensornoise: Tensortimesteps: IntTensor )
参数
- original_samples (
torch.Tensor) — 需要添加噪声的原始样本。 - noise (
torch.Tensor) — 将添加到样本中的噪声。 - timesteps (
torch.IntTensor) — 用于确定每个样本噪声水平的时间步。
引发
NotImplementedError
NotImplementedError— RePaint 训练应改用 DDPMScheduler.add_noise()。
表示此调度器不支持在 RePaint 训练中添加噪声。
scale_model_input
< source >( sample: Tensortimestep: int | None = None ) → torch.Tensor
确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器之间的可互换性。
set_timesteps
< source >( num_inference_steps: intjump_length: int = 10jump_n_sample: int = 10device: typing.Union[str, torch.device, NoneType] = None )
设置扩散链所使用的离散时间步(需在推理之前运行)。
步骤
< source >( model_output: Tensortimestep: intsample: Tensororiginal_image: Tensormask: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → RePaintSchedulerOutput or tuple[torch.Tensor, torch.Tensor]
参数
- model_output (
torch.Tensor) — 学习到的扩散模型的直接输出。 - timestep (
int) — 扩散链中的当前离散时间步。 - sample (
torch.Tensor) — 扩散过程生成的样本的当前实例。 - original_image (
torch.Tensor) — 用于执行图像修复的原始图像。 - mask (
torch.Tensor) — 掩码,其中值为 0.0 的位置指示原始图像中需要修复的部分。 - generator (
torch.Generator,可选) — 随机数生成器。 - return_dict (
bool,默认为True) — 是否返回 RePaintSchedulerOutput 而不是元组。
返回值
RePaintSchedulerOutput 或 tuple[torch.Tensor, torch.Tensor]
If return_dict is True, RePaintSchedulerOutput is returned,
otherwise a tuple is returned where the first element is the previous sample and the second is the
predicted original sample.
通过逆向 SDE 预测上一时间步的样本。此函数将扩散过程从学习到的模型输出(通常是预测的噪声)进行传播。
undo_step
< source >( sample: Tensortimestep: intgenerator: typing.Optional[torch.Generator] = None ) → torch.Tensor
向样本添加噪声,使其向前推进一个扩散步骤。
RePaintSchedulerOutput
class diffusers.schedulers.scheduling_repaint.RePaintSchedulerOutput
< source >( prev_sample: Tensorpred_original_sample: Tensor )
调度器 step 函数输出的输出类。