Diffusers documentation

UNet2DModel

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

UNet2DModel

UNetモデルは、Ronneberger らによって生体医用画像のセグメンテーションのために最初に提案されましたが、入力と同じサイズの画像を出力するため、🤗 Diffusers でも広く使用されています。実際の拡散プロセスを担うため、拡散システムにおいて最も重要なコンポーネントの一つです。🤗 Diffusers には、次元数や条件付きモデルかどうかによって異なる複数の UNet モデルのバリアントがあります。ここでは 2D UNet モデルについて説明します。

論文の抄録は以下の通りです:

深層ネットワークの成功した学習には、数千枚のラベル付き訓練サンプルが必要であるという点について、大きな合意がある。本論文では、データ拡張を強力に活用することで、利用可能なラベル付きサンプルをより効率的に使用するためのネットワークと学習戦略を提案する。このアーキテクチャは、コンテキストを捉えるための縮小パスと、正確な位置特定を可能にする対称的な拡大パスで構成される。このようなネットワークは、非常に少ない画像からエンドツーエンドで学習でき、電子顕微鏡スタックにおける神経構造のセグメンテーションに関するISBIチャレンジにおいて、従来の最良手法(スライディングウィンドウ型畳み込みネットワーク)を上回ることを示す。透過光顕微鏡画像(位相差およびDIC)で学習させた同じネットワークを用いて、これらのカテゴリにおいてISBIセルトラッキングチャレンジ2015を大幅な差で制覇した。さらに、このネットワークは高速である。最新のGPUでは、512x512画像のセグメンテーションに1秒未満しかかからない。完全な実装(Caffeベース)と学習済みネットワークは、http://lmb.informatik.uni-freiburg.de/people/ronneber/u-netで入手可能である。

UNet2DModel

class diffusers.UNet2DModel

< >

( sample_size: int | tuple[int, int] | None = Nonein_channels: int = 3out_channels: int = 3center_input_sample: bool = Falsetime_embedding_type: str = 'positional'time_embedding_dim: int | None = Nonefreq_shift: int = 0flip_sin_to_cos: bool = Truedown_block_types: tuple[str, ...] = ('DownBlock2D', 'AttnDownBlock2D', 'AttnDownBlock2D', 'AttnDownBlock2D')mid_block_type: str | None = 'UNetMidBlock2D'up_block_types: tuple[str, ...] = ('AttnUpBlock2D', 'AttnUpBlock2D', 'AttnUpBlock2D', 'UpBlock2D')block_out_channels: tuple[int, ...] = (224, 448, 672, 896)layers_per_block: int = 2mid_block_scale_factor: float = 1downsample_padding: int = 1downsample_type: str = 'conv'upsample_type: str = 'conv'dropout: float = 0.0act_fn: str = 'silu'attention_head_dim: int | None = 8norm_num_groups: int = 32attn_norm_num_groups: int | None = Nonenorm_eps: float = 1e-05resnet_time_scale_shift: str = 'default'add_attention: bool = Trueclass_embed_type: str | None = Nonenum_class_embeds: int | None = Nonenum_train_timesteps: int | None = None )

パラメータ

  • sample_size (int または tuple[int, int]、省略可、デフォルトは None) — 入力/出力サンプルの高さと幅。次元は 2 ** (len(block_out_channels) - 1) の倍数である必要があります。
  • in_channels (int、任意、デフォルトは 3) — 入力サンプルのチャネル数。
  • out_channels (int、省略可、デフォルトは 3) — 出力のチャネル数。
  • center_input_sample (bool、省略可、デフォルトはFalse) — 入力サンプルを中央揃けにするかどうか。
  • time_embedding_type (str、任意、デフォルトは "positional") — 使用する時間埋め込みの種類。
  • freq_shift (int、任意、デフォルト値は 0) — フーリエ時間埋め込み用の周波数シフト。
  • flip_sin_to_cos (bool、任意、デフォルトはTrue) — Fourier時間埋め込みにおいてsinをcosに反転させるかどうか。
  • down_block_types (tuple[str]、任意、デフォルトは ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")) — ダウンサンプルブロックの種類のタプルです。
  • mid_block_type (str、任意、デフォルトは "UNetMidBlock2D") — UNet の中央部分に使用するブロックのタイプです。UNetMidBlock2D または None のいずれかを指定できます。
  • up_block_types (tuple[str]、任意、デフォルトは ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")) — アップサンプルブロックの型のタプル。
  • block_out_channels (tuple[int]、任意、デフォルトは (224, 448, 672, 896)) — ブロックの出力チャネルのタプル。
  • layers_per_block (int、任意、デフォルトは 2) — ブロックあたりのレイヤー数。
  • mid_block_scale_factor (float、任意、デフォルトは 1) — mid block のスケール係数です。
  • downsample_padding (int、任意、デフォルトは1) — ダウンサンプル畳み込みのパディング。
  • downsample_type (str、任意、デフォルトは conv) — ダウンサンプリング層のダウンサンプリングタイプです。“conv” と “resnet” のいずれかを選択します。
  • upsample_type (str, 任意、デフォルトは conv) — アップサンプリング層のアップサンプリングタイプ。“conv” と “resnet” から選択します。
  • dropout (float、任意、デフォルト値は 0.0) — 使用する dropout の確率です。
  • act_fn (str、任意、デフォルトは "silu") — 使用する活性化関数。
  • attention_head_dim (int、任意、デフォルトは8) — アテンションヘッドの次元です。
  • norm_num_groups (int、任意、デフォルトは32) — 正規化のグループ数。
  • attn_norm_num_groups (int, 任意、デフォルトは None) — 整数が設定されている場合、mid block の Attention レイヤーに、指定されたグループ数を持つ group norm レイヤーが作成されます。None のままの場合、resnet_time_scale_shift が default に設定されている場合のみ group norm レイヤーが作成され、その場合は norm_num_groups 個のグループを持ちます。
  • norm_eps (float、任意、デフォルトは 1e-5) — 正規化用の epsilon です。
  • resnet_time_scale_shift (str、任意、デフォルトは"default") — ResNetブロック用のタイムスケールシフト設定(ResnetBlock2Dを参照)。defaultまたはscale_shiftから選択します。
  • class_embed_type (str、省略可、デフォルトは None) — 最終的に time embeddings と加算される class embedding の種類を指定します。None、"timestep"、または "identity" から選択してください。
  • num_class_embeds (int、任意、デフォルトは None) — class_embed_type が None の場合、クラス条件付けを行う際に time_embed_dim へ射影される学習可能な埋め込み行列の入力次元。

ノイズ付きサンプルとタイムステップを受け取り、サンプル形状の出力を返す2次元UNetモデル。

このモデルは ModelMixin を継承しています。すべてのモデルに実装されている汎用的なメソッド(ダウンロードや保存など)については、スーパークラスのドキュメントを参照してください。

forward

< >

( sample: Tensortimestep: typing.Union[torch.Tensor, float, int]class_labels: typing.Optional[torch.Tensor] = Nonereturn_dict: bool = True ) → UNet2DOutput or tuple

パラメータ

  • sample (torch.Tensor) — 形状が (batch, channel, height, width) のノイズ付き入力テンソルです。
  • timestep (torch.Tensor または float または int) — 入力のノイズ除去を行うステップ数。
  • class_labels (torch.Tensor、任意、デフォルトはNone) — 条件付け用の任意のクラスラベルです。その埋め込みは、タイムステップの埋め込みと加算されます。
  • return_dict (bool, optional, defaults to True) — Whether or not to return a UNet2DOutput instead of a plain tuple.

戻り値

UNet2DOutput または tuple

return_dict が True の場合、UNet2DOutput が返され、それ以外の場合は、最初の要素がサンプルテンソルである tuple が返されます。

UNet2DModelのforwardメソッドです。

UNet2DOutput

class diffusers.models.unets.unet_2d.UNet2DOutput

< >

( sample: Tensor )

パラメータ

  • sample (torch.Tensor、形状 (batch_size, num_channels, height, width)) — モデルの最終層から出力される隠れ状態です。

UNet2DModelの出力です。

Update on GitHub