Transformers 4.37 中文文档(九十六)(3)

简介: Transformers 4.37 中文文档(九十六)

Transformers 4.37 中文文档(九十六)(2)https://developer.aliyun.com/article/1564050


VisionTextDualEncoder

原文链接: huggingface.co/docs/transformers/v4.37.2/en/model_doc/vision-text-dual-encoder

概述

VisionTextDualEncoderModel 可以用于使用任何预训练的视觉自编码模型作为视觉编码器(如 ViT, BEiT,  DeiT)和任何预训练的文本自编码模型作为文本编码器(如 RoBERTa,  BERT)初始化视觉文本双编码器模型。在视觉和文本编码器的顶部添加了两个投影层,将输出嵌入投影到共享的潜在空间。投影层是随机初始化的,因此模型应该在下游任务上进行微调。该模型可用于使用类似  CLIP 的对比图像文本训练来对齐视觉文本嵌入,然后可用于零样本视觉任务,如图像分类或检索。

LiT: Zero-Shot Transfer with Locked-image Text Tuning 中展示了如何利用预训练的(锁定/冻结)图像和文本模型进行对比学习,从而在新的零样本视觉任务(如图像分类或检索)上取得显著改进。

VisionTextDualEncoderConfig

class transformers.VisionTextDualEncoderConfig

< source >

( projection_dim = 512 logit_scale_init_value = 2.6592 **kwargs )

参数

  • projection_dim (int, optional, 默认为 512) — 文本和视觉投影层的维度。
  • logit_scale_init_value (float, optional, 默认为 2.6592) — logit_scale 参数的初始值。默认值根据原始 CLIP 实现使用。
  • kwargs (optional) — 关键字参数的字典。

VisionTextDualEncoderConfig 是用于存储 VisionTextDualEncoderModel 配置的配置类。它用于根据指定的参数实例化 VisionTextDualEncoderModel 模型,定义文本模型和视觉模型配置。

配置对象继承自 PretrainedConfig,可用于控制模型输出。阅读来自 PretrainedConfig 的文档以获取更多信息。

示例:

>>> from transformers import ViTConfig, BertConfig, VisionTextDualEncoderConfig, VisionTextDualEncoderModel
>>> # Initializing a BERT and ViT configuration
>>> config_vision = ViTConfig()
>>> config_text = BertConfig()
>>> config = VisionTextDualEncoderConfig.from_vision_text_configs(config_vision, config_text, projection_dim=512)
>>> # Initializing a BERT and ViT model (with random weights)
>>> model = VisionTextDualEncoderModel(config=config)
>>> # Accessing the model configuration
>>> config_vision = model.config.vision_config
>>> config_text = model.config.text_config
>>> # Saving the model, including its configuration
>>> model.save_pretrained("vit-bert")
>>> # loading model and config from pretrained folder
>>> vision_text_config = VisionTextDualEncoderConfig.from_pretrained("vit-bert")
>>> model = VisionTextDualEncoderModel.from_pretrained("vit-bert", config=vision_text_config)
from_vision_text_configs

< source >

( vision_config: PretrainedConfig text_config: PretrainedConfig **kwargs ) → export const metadata = 'undefined';VisionTextDualEncoderConfig

返回

VisionTextDualEncoderConfig

配置对象的一个实例

从文本模型配置和视觉模型配置实例化一个 VisionTextDualEncoderConfig(或派生类)。

VisionTextDualEncoderProcessor

class transformers.VisionTextDualEncoderProcessor

< source >

( image_processor = None tokenizer = None **kwargs )

参数

  • image_processor (AutoImageProcessor, optional) — 图像处理器是必需的输入。
  • tokenizer(PreTrainedTokenizer,可选)— 标记器是必需的输入。

构建一个 VisionTextDualEncoder 处理器,将图像处理器和标记器包装成一个单一处理器。

VisionTextDualEncoderProcessor 提供了 AutoImageProcessor 和 AutoTokenizer 的所有功能。有关更多信息,请参阅__call__()和 decode()。

batch_decode

<来源>

( *args **kwargs )

该方法将其所有参数转发给 VisionTextDualEncoderTokenizer 的 batch_decode()。有关更多信息,请参阅此方法的文档字符串。

decode

<来源>

( *args **kwargs )

该方法将其所有参数转发给 VisionTextDualEncoderTokenizer 的 decode()。有关更多信息,请参阅此方法的文档字符串。

PytorchHide Pytorch 内容

VisionTextDualEncoderModel

class transformers.VisionTextDualEncoderModel

<来源>

( config: Optional = None vision_model: Optional = None text_model: Optional = None )

参数

  • config(VisionEncoderDecoderConfig)— 具有模型所有参数的模型配置类。使用配置文件初始化不会加载与模型关联的权重,只会加载配置。查看 from_pretrained()方法以加载模型权重。

此类可用于使用任何预训练的视觉自编码模型作为视觉编码器和任何预训练的文本模型作为文本编码器初始化视觉文本双编码器模型。视觉和文本编码器通过  from_pretrained()方法加载。投影层会自动添加到模型中,并应在下游任务(如对比图像文本建模)上进行微调。

LiT: Zero-Shot Transfer with Locked-image Text Tuning中展示了如何利用预训练(锁定/冻结)图像和文本模型进行对比学习,对新的零样本视觉任务(如图像分类或检索)产生了显著的改进。

训练/微调了这样一个 Vision-Text-Dual-Encoder 模型之后,它可以像任何其他模型一样保存/加载(有关更多信息,请参阅示例)。

该模型继承自 PreTrainedModel。查看超类文档以获取库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入、修剪头等)。

这个模型也是一个 PyTorch torch.nn.Module 的子类。将其用作常规的 PyTorch 模块,并参考 PyTorch 文档以获取与一般用法和行为相关的所有内容。

forward

< source >

( input_ids: Optional = None pixel_values: Optional = None attention_mask: Optional = None position_ids: Optional = None return_loss: Optional = None token_type_ids: Optional = None output_attentions: Optional = None output_hidden_states: Optional = None return_dict: Optional = None ) → export const metadata = 'undefined';transformers.models.clip.modeling_clip.CLIPOutput or tuple(torch.FloatTensor)
• 1

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — 词汇表中输入序列标记的索引。默认情况下将忽略填充。
    可以使用 AutoTokenizer 获取索引。有关详细信息,请参见 PreTrainedTokenizer.encode() 和 PreTrainedTokenizer.call()。
    什么是输入 ID?
  • attention_mask (torch.Tensor of shape (batch_size, sequence_length), 可选) — 避免在填充标记索引上执行注意力的掩码。掩码值选择在 [0, 1]
  • 对于未被“masked”的标记为 1,
  • 对于被masked的标记为 0。
  • 什么是注意力掩码?
  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), 可选) — 每个输入序列标记在位置嵌入中的位置索引。选择范围为 [0, config.max_position_embeddings - 1]
    什么是位置 ID?
  • pixel_values (torch.FloatTensor of shape (batch_size, num_channels, height, width)) — 像素值。默认情况下将忽略填充。可以使用图像处理器获取像素值(例如,如果您使用 ViT 作为编码器,应该使用 AutoImageProcessor)。有关详细信息,请参见 ViTImageProcessor.call()。
  • return_loss (bool, 可选) — 是否返回对比损失。
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回的张量下的 attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回的张量下的 hidden_states
  • return_dict (bool, 可选) — 是否返回一个 ModelOutput 而不是一个普通元组。

返回

transformers.models.clip.modeling_clip.CLIPOutputtuple(torch.FloatTensor)

一个 transformers.models.clip.modeling_clip.CLIPOutput 或一个 torch.FloatTensor 元组(如果传递了 return_dict=False 或当 config.return_dict=False 时)包含各种元素,这取决于配置(VisionTextDualEncoderConfig)和输入。

  • loss (torch.FloatTensor of shape (1,), 可选, 当 return_lossTrue 时返回) — 图像-文本相似性的对比损失。
  • logits_per_image:(torch.FloatTensor of shape (image_batch_size, text_batch_size)) — image_embedstext_embeds 之间的缩放点积分数。这代表了图像-文本相似性分数。
  • logits_per_text:(torch.FloatTensor of shape (text_batch_size, image_batch_size)) — text_embedsimage_embeds 之间的缩放点积分数。这代表了文本-图像相似性分数。
  • text_embeds(torch.FloatTensor of shape (batch_size, output_dim) — 通过将投影层应用于 CLIPTextModel 的汇总输出获得的文本嵌入。
  • image_embeds(torch.FloatTensor of shape (batch_size, output_dim) — 通过将投影层应用于 CLIPVisionModel 的汇总输出获得的图像嵌入。
  • text_model_output(BaseModelOutputWithPooling): CLIPTextModel 的输出。
  • vision_model_output(BaseModelOutputWithPooling): CLIPVisionModel 的输出。

VisionTextDualEncoderModel 的前向方法覆盖了__call__特殊方法。

尽管前向传递的步骤需要在此函数内定义,但应该在此之后调用Module实例,而不是在此处调用,因为前者会负责运行前后处理步骤,而后者会默默忽略它们。

示例:

>>> from PIL import Image
>>> import requests
>>> from transformers import (
...     VisionTextDualEncoderModel,
...     VisionTextDualEncoderProcessor,
...     AutoImageProcessor,
...     AutoTokenizer,
... )
>>> tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
>>> processor = VisionTextDualEncoderProcessor(image_processor, tokenizer)
>>> model = VisionTextDualEncoderModel.from_vision_text_pretrained(
...     "google/vit-base-patch16-224", "bert-base-uncased"
... )
>>> # contrastive training
>>> urls = [
...     "http://images.cocodataset.org/val2017/000000039769.jpg",
...     "https://farm3.staticflickr.com/2674/5850229113_4fe05d5265_z.jpg",
... ]
>>> images = [Image.open(requests.get(url, stream=True).raw) for url in urls]
>>> inputs = processor(
...     text=["a photo of a cat", "a photo of a dog"], images=images, return_tensors="pt", padding=True
... )
>>> outputs = model(
...     input_ids=inputs.input_ids,
...     attention_mask=inputs.attention_mask,
...     pixel_values=inputs.pixel_values,
...     return_loss=True,
... )
>>> loss, logits_per_image = outputs.loss, outputs.logits_per_image  # this is the image-text similarity score
>>> # save and load from pretrained
>>> model.save_pretrained("vit-bert")
>>> model = VisionTextDualEncoderModel.from_pretrained("vit-bert")
>>> # inference
>>> outputs = model(**inputs)
>>> logits_per_image = outputs.logits_per_image  # this is the image-text similarity score
>>> probs = logits_per_image.softmax(dim=1)  # we can take the softmax to get the label probabilities

TensorFlowHide TensorFlow content

FlaxVisionTextDualEncoderModel

class transformers.FlaxVisionTextDualEncoderModel

< source >

( config: VisionTextDualEncoderConfig input_shape: Optional = None seed: int = 0 dtype: dtype = <class 'jax.numpy.float32'> _do_init: bool = True **kwargs )

参数

  • config (VisionTextDualEncoderConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型关联的权重,只加载配置。查看 from_pretrained()方法以加载模型权重。
  • dtype (jax.numpy.dtype, 可选,默认为jax.numpy.float32) — 计算的数据类型。可以是jax.numpy.float32jax.numpy.float16(在 GPU 上)和jax.numpy.bfloat16(在 TPU 上)之一。
    这可以用于在 GPU 或 TPU 上启用混合精度训练或半精度推断。如果指定了dtype,则所有计算将使用给定的dtype执行。
    请注意,这仅指定计算的数据类型,不影响模型参数的数据类型。
    如果要更改模型参数的数据类型,请参阅 to_fp16()和 to_bf16()。

此类可用于使用任何预训练视觉自编码模型作为视觉编码器和任何预训练文本模型作为文本编码器初始化视觉文本双编码器模型。视觉和文本编码器通过 from_pretrained()方法加载。投影层会自动添加到模型中,并应在下游任务(如对比图像文本建模)上进行微调。

LiT: Zero-Shot Transfer with Locked-image Text Tuning中,展示了如何利用预训练(锁定/冻结)图像和文本模型进行对比学习,从而在新的零样本视觉任务(如图像分类或检索)上取得显著改进。

训练/微调了这样一个视觉文本双编码器模型后,它可以像其他模型一样保存/加载(有关更多信息,请参阅示例)。

此模型继承自 PreTrainedModel。查看超类文档以获取库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、修剪头等)。

此模型还是一个flax.linen.Module子类。将其用作常规的 Flax 亚麻模块,并参考 Flax 文档以获取与一般用法和行为相关的所有内容。

最后,此模型支持内在的 JAX 特性,例如:

__call__

<来源>

( input_ids pixel_values attention_mask = None position_ids = None token_type_ids = None params: dict = None dropout_rng: PRNGKey = None train: bool = False output_attentions: Optional = None output_hidden_states: Optional = None return_dict: Optional = None ) → export const metadata = 'undefined';transformers.models.clip.modeling_flax_clip.FlaxCLIPOutput or tuple(torch.FloatTensor)

参数

  • input_ids(形状为(batch_size, sequence_length)numpy.ndarray)— 词汇表中输入序列标记的索引。默认情况下将忽略填充。
    可以使用 AutoTokenizer 获取索引。有关详细信息,请参阅 PreTrainedTokenizer.encode()和 PreTrainedTokenizer.call()。
    什么是输入 ID?
  • attention_mask(形状为(batch_size, sequence_length)torch.Tensor可选)— 避免在填充标记索引上执行注意力的掩码。掩码值选择在[0, 1]之间:
  • 1 用于未被“掩码”的标记,
  • 0 用于被“掩码”的标记。
  • 什么是注意力掩码?
  • position_ids(形状为(batch_size, sequence_length)numpy.ndarray可选)— 每个输入序列标记在位置嵌入中的位置索引。选择范围为[0, config.max_position_embeddings - 1]
    什么是位置 ID?
  • pixel_values(形状为(batch_size, num_channels, height, width)torch.FloatTensor)— 像素值。默认情况下将忽略填充。可以使用图像处理器获取像素值(例如,如果您使用 ViT 作为编码器,则应使用 AutoImageProcessor)。有关详细信息,请参阅 ViTImageProcessor.call()。
  • output_attentionsbool可选)— 是否返回所有注意力层的注意力张量。有关更多详细信息,请参阅返回的张量下的attentions
  • output_hidden_statesbool可选)— 是否返回所有层的隐藏状态。有关更多详细信息,请参阅返回的张量下的hidden_states
  • return_dictbool可选)— 是否返回 ModelOutput 而不是普通元组。

返回

transformers.models.clip.modeling_flax_clip.FlaxCLIPOutputtuple(torch.FloatTensor)

一个transformers.models.clip.modeling_flax_clip.FlaxCLIPOutput或一个torch.FloatTensor元组(如果传递了return_dict=Falseconfig.return_dict=False时)包含根据配置(VisionTextDualEncoderConfig)和输入的各种元素。

  • logits_per_image:(jnp.ndarray of shape (image_batch_size, text_batch_size)) — image_embedstext_embeds之间的缩放点积分数。这代表图像-文本相似性分数。
  • logits_per_text:(jnp.ndarray of shape (text_batch_size, image_batch_size)) — text_embedsimage_embeds之间的缩放点积分数。这代表文本-图像相似性分数。
  • text_embeds(jnp.ndarray of shape (batch_size, output_dim) — 通过将 FlaxCLIPTextModel 的池化输出应用于投影层获得的文本嵌入。
  • image_embeds(jnp.ndarray of shape (batch_size, output_dim) — 通过将 FlaxCLIPVisionModel 的池化输出应用于投影层获得的图像嵌入。
  • text_model_output(FlaxBaseModelOutputWithPooling): FlaxCLIPTextModel 的输出。
  • vision_model_output(FlaxBaseModelOutputWithPooling): FlaxCLIPVisionModel 的输出。

FlaxVisionTextDualEncoderModel 的前向方法重写了__call__特殊方法。

虽然前向传递的步骤需要在此函数内定义,但应该在之后调用Module实例,而不是调用此函数,因为前者会负责运行前后处理步骤,而后者会默默忽略它们。

示例:

>>> from PIL import Image
>>> import requests
>>> import jax
>>> from transformers import (
...     FlaxVisionTextDualEncoderModel,
...     VisionTextDualEncoderProcessor,
...     AutoImageProcessor,
...     AutoTokenizer,
... )
>>> tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
>>> image_processor = AutoImageProcesor.from_pretrained("google/vit-base-patch16-224")
>>> processor = VisionTextDualEncoderProcessor(image_processor, tokenizer)
>>> model = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
...     "google/vit-base-patch16-224", "bert-base-uncased"
... )
>>> # contrastive training
>>> urls = [
...     "http://images.cocodataset.org/val2017/000000039769.jpg",
...     "https://farm3.staticflickr.com/2674/5850229113_4fe05d5265_z.jpg",
... ]
>>> images = [Image.open(requests.get(url, stream=True).raw) for url in urls]
>>> inputs = processor(
...     text=["a photo of a cat", "a photo of a dog"], images=images, return_tensors="np", padding=True
... )
>>> outputs = model(
...     input_ids=inputs.input_ids,
...     attention_mask=inputs.attention_mask,
...     pixel_values=inputs.pixel_values,
... )
>>> logits_per_image = outputs.logits_per_image  # this is the image-text similarity score
>>> # save and load from pretrained
>>> model.save_pretrained("vit-bert")
>>> model = FlaxVisionTextDualEncoderModel.from_pretrained("vit-bert")
>>> # inference
>>> outputs = model(**inputs)
>>> logits_per_image = outputs.logits_per_image  # this is the image-text similarity score
>>> probs = jax.nn.softmax(logits_per_image, axis=1)  # we can take the softmax to get the label probabilities

JAXHide JAX content

TFVisionTextDualEncoderModel

class transformers.TFVisionTextDualEncoderModel

< source >

( config: Optional[VisionTextDualEncoderConfig] = None vision_model: Optional[TFPreTrainedModel] = None text_model: Optional[TFPreTrainedModel] = None )

参数

  • config(VisionEncoderDecoderConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained()方法以加载模型权重。

此类可用于使用任何预训练的视觉自编码模型作为视觉编码器和任何预训练的文本模型作为文本编码器初始化视觉文本双编码器模型。视觉和文本编码器通过  from_pretrained()方法加载。投影层会自动添加到模型中,并应在下游任务(如对比图像-文本建模)上进行微调。

LiT: Zero-Shot Transfer with Locked-image Text Tuning中展示了如何利用预训练(锁定/冻结)的图像和文本模型进行对比学习,从而在新的零样本视觉任务(如图像分类或检索)上取得显著改进。

经过训练/微调的 Vision-Text-Dual-Encoder 模型可以像其他模型一样保存/加载(有关更多信息,请参见示例)。

该模型继承自 TFPreTrainedModel。查看超类文档以获取库为所有模型实现的通用方法(如下载或保存、调整输入嵌入、修剪头等)。

该模型也是 Keras Model子类。将其用作常规 Keras 模型,并参考 TF 文档以获取与一般使用和行为相关的所有信息。

call

<来源>

( input_ids: tf.Tensor | None = None pixel_values: tf.Tensor | None = None attention_mask: tf.Tensor | None = None position_ids: tf.Tensor | None = None return_loss: Optional[bool] = None token_type_ids: tf.Tensor | None = None output_attentions: Optional[bool] = None output_hidden_states: Optional[bool] = None return_dict: Optional[bool] = None training: bool = False ) → export const metadata = 'undefined';transformers.models.clip.modeling_tf_clip.TFCLIPOutput or tuple(tf.Tensor)

参数

  • input_ids (tf.Tensor of shape (batch_size, sequence_length)) — 词汇表中输入序列标记的索引。默认情况下,如果提供了填充,将被忽略。
    可以使用 AutoTokenizer 获取索引。有关详细信息,请参阅 PreTrainedTokenizer.encode()和 PreTrainedTokenizer.call()。
    什么是输入 ID?
  • attention_mask (tf.Tensor of shape (batch_size, sequence_length), 可选) — 避免在填充标记索引上执行注意力的掩码。选择的掩码值在[0, 1]中:
  • 对于未被masked的标记为 1,
  • 对于被masked的标记为 0。
  • 什么是注意力掩码?
  • position_ids (tf.Tensor of shape (batch_size, sequence_length), 可选) — 每个输入序列标记在位置嵌入中的位置索引。在范围[0, config.max_position_embeddings - 1]中选择。
    什么是位置 ID?
  • pixel_values (tf.Tensor of shape (batch_size, num_channels, height, width)) — 像素值。默认情况下,如果提供了填充,将被忽略。可以使用图像处理器获取像素值(例如,如果您使用 ViT 作为编码器,应该使用 AutoImageProcessor)。有关详细信息,请参阅 ViTImageProcessor.call()。
  • return_loss (bool, 可选) — 是否返回对比损失。
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参阅返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参阅返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是普通元组。

返回

transformers.models.clip.modeling_tf_clip.TFCLIPOutputtuple(tf.Tensor)

一个transformers.models.clip.modeling_tf_clip.TFCLIPOutput或一组tf.Tensor(如果传递了return_dict=False或当config.return_dict=False时)包括根据配置(VisionTextDualEncoderConfig)和输入的各种元素。

  • loss (tf.Tensor of shape (1,), 可选, 当return_lossTrue时返回) — 图像-文本相似性的对比损失。
  • logits_per_image:(tf.Tensor of shape (image_batch_size, text_batch_size)) — image_embedstext_embeds之间的缩放点积分数。这代表图像-文本相似性分数。
  • logits_per_text:(tf.Tensor of shape (text_batch_size, image_batch_size)) — text_embedsimage_embeds之间的缩放点积分数。这代表文本-图像相似性分数。
  • text_embeds(tf.Tensor of shape (batch_size, output_dim) — 通过将投影层应用于 TFCLIPTextModel 的汇总输出获得的文本嵌入。
  • image_embeds(tf.Tensor of shape (batch_size, output_dim) — 通过将投影层应用于 TFCLIPVisionModel 的汇总输出获得的图像嵌入。
  • text_model_output(~modeling_tf_utils.TFBaseModelOutputWithPooling): TFCLIPTextModel 的输出。
  • vision_model_output(~modeling_tf_utils.TFBaseModelOutputWithPooling): TFCLIPVisionModel 的输出。

TFVisionTextDualEncoderModel 的前向方法,覆盖了__call__特殊方法。

虽然前向传递的配方需要在这个函数内定义,但应该在此之后调用Module实例,而不是这个,因为前者负责运行前处理和后处理步骤,而后者则默默地忽略它们。

示例:

>>> from PIL import Image
>>> import requests
>>> from transformers import (
...     TFVisionTextDualEncoderModel,
...     VisionTextDualEncoderProcessor,
...     AutoImageProcessor,
...     AutoTokenizer,
... )
>>> tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
>>> processor = VisionTextDualEncoderProcessor(image_processor, tokenizer)
>>> model = TFVisionTextDualEncoderModel.from_vision_text_pretrained(
...     "google/vit-base-patch16-224", "bert-base-uncased"
... )
>>> # contrastive training
>>> urls = [
...     "http://images.cocodataset.org/val2017/000000039769.jpg",
...     "https://farm3.staticflickr.com/2674/5850229113_4fe05d5265_z.jpg",
... ]
>>> images = [Image.open(requests.get(url, stream=True).raw) for url in urls]
>>> inputs = processor(
...     text=["a photo of a cat", "a photo of a dog"], images=images, return_tensors="np", padding=True
... )
>>> outputs = model(
...     input_ids=inputs.input_ids,
...     attention_mask=inputs.attention_mask,
...     pixel_values=inputs.pixel_values,
...     return_loss=True,
... )
>>> loss, logits_per_image = outputs.loss, outputs.logits_per_image  # this is the image-text similarity score
>>> # save and load from pretrained
>>> model.save_pretrained("vit-bert")
>>> model = TFVisionTextDualEncoderModel.from_pretrained("vit-bert")
>>> # inference
>>> outputs = model(**inputs)
>>> logits_per_image = outputs.logits_per_image  # this is the image-text similarity score
>>> probs = tf.nn.softmax(logits_per_image, axis=1)  # we can take the softmax to get the label probabilities


Transformers 4.37 中文文档(九十六)(4)https://developer.aliyun.com/article/1564052

相关文章
|
2月前
|
数据挖掘 PyTorch 语音技术
Transformers 4.37 中文文档(八十五)(5)
Transformers 4.37 中文文档(八十五)
34 11
|
2月前
|
存储 编解码 自然语言处理
Transformers 4.37 中文文档(八十五)(4)
Transformers 4.37 中文文档(八十五)
26 9
|
2月前
|
自然语言处理 PyTorch 算法框架/工具
Transformers 4.37 中文文档(八十五)(3)
Transformers 4.37 中文文档(八十五)
22 5
|
2月前
|
存储 PyTorch 算法框架/工具
Transformers 4.37 中文文档(八十五)(1)
Transformers 4.37 中文文档(八十五)
23 4
|
2月前
|
PyTorch 语音技术 算法框架/工具
Transformers 4.37 中文文档(八十五)(2)
Transformers 4.37 中文文档(八十五)
25 3
|
2月前
|
机器学习/深度学习 存储 PyTorch
Transformers 4.37 中文文档(九十六)(4)
Transformers 4.37 中文文档(九十六)
18 1
|
2月前
|
PyTorch 算法框架/工具 索引
Transformers 4.37 中文文档(九十六)(5)
Transformers 4.37 中文文档(九十六)
12 1
|
2月前
|
存储 PyTorch TensorFlow
Transformers 4.37 中文文档(九十六)(2)
Transformers 4.37 中文文档(九十六)
19 1
|
2月前
|
存储 PyTorch TensorFlow
Transformers 4.37 中文文档(九十六)(1)
Transformers 4.37 中文文档(九十六)
25 1
|
2月前
|
自然语言处理 PyTorch 算法框架/工具
Transformers 4.37 中文文档(七十六)(5)
Transformers 4.37 中文文档(七十六)
22 1