快捷方式

clip_vision_encoder

torchtune.models.clip.clip_vision_encoder(tile_size: int, patch_size: int, embed_dim: int, num_layers: int, num_heads: int, activation: ~typing.Callable = <class 'torch.nn.modules.activation.SiLU'>, cls_output_dim: int = 512, attn_bias: bool = True, use_rope: bool = False, out_indices: ~typing.Optional[~typing.List[int]] = None, output_cls_projection: bool = False, max_num_tiles: int = 4, in_channels: int = 3, append_cls_token: bool = False) VisionTransformer[源代码]

构建与 clip 模型关联的视觉编码器。这包括

  • TransformerEncoderLayer(Transformer 编码器层)

  • 位置嵌入

  • CLS 投影(可选)

有关详细信息,请查看 torchtune.modules.vision_transformer.VisionTransformer 的文档。

参数:
  • tile_size (int) – 图像分块大小(如果图像已预先分块裁剪)。否则,为输入图像的大小。在这种情况下,该函数会将您的图像视为单个分块。

  • patch_size (int) – 每个补丁的大小。用于将分块划分为补丁。例如,对于 patch_size=40,形状为 (400, 400) 的分块将具有 10x10 的补丁网格,每个补丁的形状为 (40, 40)。

  • embed_dim (int) – 每个补丁嵌入(token)的维度。

  • num_layers (int) – Transformer 层的数量。

  • num_heads (int) – 每个 Transformer 层中注意力头的数量。

  • activation (Callable) – MLP 层中使用的激活函数。

  • cls_output_dim (int) – 来自 CLS 投影模块的输出张量的维度。

  • attn_bias (bool) – 布尔值,指示是否在注意力模块中使用偏置。默认为 True。

  • use_rope (bool) – 如果为 True,则在每个 Transformer 层的注意力中包含 2D rope。默认值:False

  • out_indices (Optional[List[int]]) – 要返回的隐藏层的索引。如果提供,它将返回 Transformer 层在进入下一层之前的中间结果。例如,out_indices=[0,3] 将返回 token 在进入第一层和第四层之前的结果。

  • output_cls_projection (bool) – 如果为 True,则仅输出 CLS token 投影,而不是所有 token。默认为 False。

  • max_num_tiles (int) – 可以处理的最大分块数。这用于确定位置嵌入的大小。

  • in_channels (int) – 图像输入通道数。

  • append_cls_token (bool) – 如果为 True,则将 CLS token 嵌入添加到视觉 Transformer 中序列的末尾。默认值为 False,即将 CLS token 添加到序列的开头。

返回:

一个 VisionTransformer 对象。

引发:

AssertionError – 如果 embed_dim 不能被 num_heads 整除。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源