Transformers 文档

MLCD

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

MLCD

PyTorch SDPA

概述

MLCD 模型由 DeepGlint-AI 团队在 unicom 中发布,该模型专注于利用 LAION400M 和 COYO700M 等大规模数据集构建大型多模态语言模型的基础视觉模型,并采用样本到聚类的对比学习来优化性能。MLCD 模型主要用于多模态视觉大型语言模型,如 LLaVA。

🔥MLCD-ViT-bigG🔥 系列是采用 2D 旋转位置嵌入 (RoPE2D) 增强的先进视觉转换器模型,在文档理解和视觉问答任务上取得了卓越性能。该模型由 DeepGlint AI 开发,展示了在处理复杂视觉-语言交互方面的卓越能力。

技巧

结果

视觉模块 RoPE2D 图表问答 DocVQA InfoVQA OCRBench MMMU
CLIP (ViT-L-14-336px) × 66.52 75.21 38.88 525.00 44.20
SigLIP (ViT-SO400M-384px) × 69.28 76.71 41.38 554.00 46.78
DFN5B (ViT-H-14-378px) × 64.36 70.87 38.59 473.00 48.00
MLCD (ViT-L-14-336px) × 67.84 76.46 43.48 531.00 44.30
MLCD (ViT-bigG-14-336px) 71.07 79.63 44.38 572.00 46.78
MLCD (ViT-bigG-14-448px) 73.80 83.34 46.59 582.00 46.00

用法

import requests
from PIL import Image
from transformers import AutoProcessor, MLCDVisionModel

# Load model and processor
model = MLCDVisionModel.from_pretrained("DeepGlint-AI/mlcd-vit-bigG-patch14-448")
processor = AutoProcessor.from_pretrained("DeepGlint-AI/mlcd-vit-bigG-patch14-448")

# Process single image
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(images=image, return_tensors="pt")

# Generate outputs
with torch.no_grad():
    outputs = model(**inputs)

# Get visual features
features = outputs.last_hidden_state

print(f"Extracted features shape: {features.shape}")

MLCDVisionConfig

class transformers.MLCDVisionConfig

< >

( hidden_size = 1664 intermediate_size = 8192 num_hidden_layers = 48 num_attention_heads = 16 num_key_value_groups = 1 num_channels = 3 image_size = 336 patch_size = 14 hidden_act = 'gelu' layer_norm_eps = 1e-05 attention_dropout = 0.0 initializer_range = 0.02 initializer_factor = 1.0 **kwargs )

参数

  • hidden_size (int, 可选, 默认为 1664) — 编码器层和池化层的维度。
  • intermediate_size (int, 可选, 默认为 8192) — Transformer 编码器中“中间”(即前馈)层的维度。
  • projection_dim (int, 可选, 默认为 1024) — 文本和视觉投影层的维度。
  • num_hidden_layers (int, 可选, 默认为 48) — Transformer 编码器中隐藏层的数量。
  • num_attention_heads (int, 可选, 默认为 16) — Transformer 编码器中每个注意力层的注意力头数量。
  • num_channels (int, 可选, 默认为 3) — 输入通道的数量。
  • image_size (int, 可选, 默认为 336) — 每张图像的大小(分辨率)。
  • patch_size (int, 可选, 默认为 14) — 每个补丁的大小(分辨率)。
  • hidden_act (strfunction, 可选, 默认为 "gelu") — 编码器和池化器中的非线性激活函数(函数或字符串)。如果为字符串,支持 "gelu", "relu", "selu""gelu_new" "quick_gelu"
  • layer_norm_eps (float, 可选, 默认为 1e-05) — 层归一化层使用的 epsilon 值。
  • attention_dropout (float, 可选, 默认为 0.0) — 注意力概率的 dropout 比率。
  • initializer_range (float, 可选, 默认为 0.02) — 用于初始化所有权重矩阵的截断正态初始化器的标准差。
  • initializer_factor (float, 可选, 默认为 1.0) — 用于初始化所有权重矩阵的因子(应保持为 1,内部用于初始化测试)。

这是配置类,用于存储 MLCDVisionModel 的配置。它用于根据指定的参数实例化 MLCD 视觉编码器,定义模型架构。使用默认值实例化配置将产生与 MLCD DeepGlint-AI/mlcd-vit-bigG-patch14-336 架构的视觉编码器类似的配置。

配置对象继承自 PretrainedConfig,可用于控制模型输出。有关更多信息,请参阅 PretrainedConfig 的文档。

示例

>>> from transformers import MLCDVisionConfig, MLCDVisionModel

>>> # Initializing a MLCDVisionConfig with DeepGlint-AI/mlcd-vit-bigG-patch14-336 style configuration
>>> configuration = MLCDVisionConfig()

>>> # Initializing a MLCDVisionModel (with random weights) from the DeepGlint-AI/mlcd-vit-bigG-patch14-336 style configuration
>>> model = MLCDVisionModel(configuration)

>>> # Accessing the model configuration
>>> configuration = model.config

MLCDVisionModel

class transformers.MLCDVisionModel

< >

( config: MLCDVisionConfig )

参数

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

不带任何头部或投影的 M_L_C_D 视觉模型。

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

此模型也是 PyTorch torch.nn.Module 子类。将其作为常规 PyTorch 模块使用,有关通用用法和行为的所有事项,请参阅 PyTorch 文档。

forward

< >

( pixel_values: typing.Optional[torch.FloatTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.modeling_outputs.BaseModelOutputWithPoolingtuple(torch.FloatTensor)

参数

  • pixel_values (torch.FloatTensor,形状为 (batch_size, num_channels, image_size, image_size), 可选) — 对应输入图像的张量。像素值可以使用 {image_processor_class} 获取。有关详细信息,请参阅 {image_processor_class}.__call__{processor_class} 使用 {image_processor_class} 处理图像)。
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参阅返回张量下的 attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参阅返回张量下的 hidden_states
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是普通元组。

返回

transformers.modeling_outputs.BaseModelOutputWithPoolingtuple(torch.FloatTensor)

transformers.modeling_outputs.BaseModelOutputWithPoolingtorch.FloatTensor 的元组(如果传入 return_dict=False 或当 config.return_dict=False 时),包含根据配置 (MLCDVisionConfig) 和输入的不同元素。

  • last_hidden_state (torch.FloatTensor, 形状为 (batch_size, sequence_length, hidden_size)) — 模型最后一层输出的隐藏状态序列。

  • pooler_output (torch.FloatTensor,形状为 (batch_size, hidden_size)) — 序列中第一个标记(分类标记)的最后一层隐藏状态,经过用于辅助预训练任务的层进一步处理。例如,对于 BERT 家族模型,这会在经过线性层和 tanh 激活函数处理后返回分类标记。线性层权重在预训练期间根据下一句子预测(分类)目标进行训练。

  • hidden_states (tuple(torch.FloatTensor), 可选, 当传入 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — torch.FloatTensor 的元组(如果模型有嵌入层,则为嵌入层输出一个,每个层输出一个),形状为 (batch_size, sequence_length, hidden_size)

    模型在每个层输出的隐藏状态以及可选的初始嵌入输出。

  • attentions (tuple(torch.FloatTensor), 可选, 当传入 output_attentions=True 或当 config.output_attentions=True 时返回) — torch.FloatTensor 的元组(每个层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力 softmax 后的注意力权重,用于计算自注意力头中的加权平均值。

MLCDVisionModel 的 forward 方法,重写了 __call__ 特殊方法。

尽管前向传播的配方需要在此函数中定义,但在此之后应调用 Module 实例,因为前者会处理运行预处理和后处理步骤,而后者会默默地忽略它们。

示例

>>> import requests
>>> from PIL import Image
>>> from transformers import AutoProcessor, MLCDVisionModel
>>> model = MLCDVisionModel.from_pretrained("DeepGlint-AI/mlcd-vit-bigG-patch14-448")
>>> processor = AutoProcessor.from_pretrained("DeepGlint-AI/mlcd-vit-bigG-patch14-448")

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> inputs = processor(images=image, return_tensors="pt")

>>> with torch.no_grad():
...     outputs = model(**inputs, output_attentions=True)

>>> features = outputs.last_hidden_state
>>> print(f"Extracted features shape: {features.shape}")
>>> print(f"Number of attention layers: {len(outputs.attentions)}")
>>> print(f"Attention shape: {outputs.attentions[0].shape}")
< > 在 GitHub 上更新