Diffusers documentation

UNet1DModel

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

UNet1DModel

UNet 模型最初由 Ronneberger 等人提出,用于生物医学图像分割,但由于其输出的图像尺寸与输入相同,因此在 🤗 Diffusers 中也被广泛使用。它是扩散系统中最关键的组件之一,因为它实现了实际的扩散过程。🤗 Diffusers 中存在多种 UNet 模型变体,具体取决于其维度数量以及是否为条件模型。这是一个 1D UNet 模型。

该论文的摘要如下:

人们普遍认为,成功训练深度网络需要数千个带标注的训练样本。在本文中,我们提出了一种网络和训练策略,该策略通过充分利用数据增强,更高效地利用现有的带标注样本。该架构由一条用于捕获上下文的收缩路径和一条对称的扩展路径组成,后者能够实现精确定位。我们证明,这样的网络可以从极少量的图像中进行端到端训练,并在 ISBI 电子显微镜堆叠图像中神经元结构分割挑战赛中,优于此前最佳方法(滑动窗口卷积网络)。使用在透射光显微镜图像(相差和 DIC)上训练的同一网络,我们在 2015 年 ISBI 细胞追踪挑战赛中以较大优势赢得了这些类别的冠军。此外,该网络速度很快。在最新的 GPU 上,对一张 512x512 图像进行分割所需时间不到一秒。完整的实现(基于 Caffe)和训练好的网络可在 http://lmb.informatik.uni-freiburg.de/people/ronneber/u-net 获取。

UNet1DModel

class diffusers.UNet1DModel

< >

( sample_size: int = 65536sample_rate: int | None = Nonein_channels: int = 2out_channels: int = 2extra_in_channels: int = 0time_embedding_type: str = 'fourier'time_embedding_dim: int | None = Noneflip_sin_to_cos: bool = Trueuse_timestep_embedding: bool = Falsefreq_shift: float = 0.0down_block_types: tuple[str, ...] = ('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D')up_block_types: tuple[str, ...] = ('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip')mid_block_type: str = 'UNetMidBlock1D'out_block_type: str = Noneblock_out_channels: tuple[int, ...] = (32, 32, 64)act_fn: str = Nonenorm_num_groups: int = 8layers_per_block: int = 1downsample_each_block: bool = False )

参数

  • sample_size (int, 可选) — 样本的默认长度。应在运行时可自适应。
  • in_channels (int,可选,默认为 2) — 输入样本的通道数。
  • out_channels (int,可选,默认为 2) — 输出中的通道数。
  • extra_in_channels (int,可选,默认为 0) — 添加到第一个下采样块输入中的额外通道数。适用于输入数据的通道数多于模型最初设计所支持的情况。
  • time_embedding_type(str,可选,默认为 "fourier")— 要使用的时间嵌入类型。
  • freq_shift(float,可选,默认值为 0.0)——用于傅里叶时间嵌入的频率偏移。
  • flip_sin_to_cos (bool,可选,默认为 False) — 是否将傅里叶时间嵌入中的 sin 翻转为 cos。
  • down_block_types (tuple[str],可选,默认为 ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")) — 下采样块类型的元组。
  • up_block_types (tuple[str],可选,默认为 ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")) — 上采样块类型的元组。
  • block_out_channels (tuple[int],可选,默认为 (32, 32, 64)) — 块输出通道的元组。
  • mid_block_type (str,可选,默认为 "UNetMidBlock1D") — UNet 中间部分的块类型。
  • out_block_type(str,可选,默认为 None)— UNet 的可选输出处理块。
  • act_fn (str,可选,默认为 None) — UNet 块中可选的激活函数。
  • norm_num_groups (int,可选,默认为 8) — 归一化的组数。
  • layers_per_block (int,可选,默认为 1) — 每个块中的层数。
  • downsample_each_block (int,可选,默认为 False) — 实验性功能,用于在不使用上采样的情况下使用 UNet。

一个一维 UNet 模型,接收带噪声的样本和时间步,并返回形状相同的样本。

该模型继承自 ModelMixin。请查阅其父类文档,了解为所有模型实现的通用方法(例如下载或保存)。

forward

< >

( sample: Tensortimestep: typing.Union[torch.Tensor, float, int]return_dict: bool = True ) → UNet1DOutput or tuple

参数

  • sample (torch.Tensor) — 具有形状 (batch_size, num_channels, sample_size) 的含噪输入张量。
  • timestep(torch.Tensor 或 float 或 int)— 用于对输入进行去噪的时间步数。
  • return_dict (bool,可选,默认为 True) — 是否返回 UNet1DOutput 而不是普通元组。

返回值

UNet1DOutput 或 tuple

如果 return_dict 为 True,则返回 UNet1DOutput,否则返回一个 tuple,其中第一个元素是样本张量。

UNet1DModel 的前向方法。

UNet1DOutput

class diffusers.models.unets.unet_1d.UNet1DOutput

< >

( sample: Tensor )

参数

  • sample(形状为 (batch_size, num_channels, sample_size) 的 torch.Tensor)— 模型最后一层输出的隐藏状态。

UNet1DModel 的输出。

Update on GitHub