Transformers 文件
MLCD
並獲得增強的文件體驗
開始使用
MLCD
概述
MLCD 模型由 DeepGlint-AI 團隊在 unicom 中釋出,該模型專注於利用 LAION400M 和 COYO700M 等大規模資料集構建大型多模態語言模型的基礎視覺模型,並採用樣本到聚類的對比學習來最佳化效能。MLCD 模型主要用於多模態視覺大型語言模型,如 LLaVA。
🔥MLCD-ViT-bigG🔥 系列是採用 2D 旋轉位置嵌入 (RoPE2D) 增強的先進視覺轉換器模型,在文件理解和視覺問答任務上取得了卓越效能。該模型由 DeepGlint AI 開發,展示了在處理複雜視覺-語言互動方面的卓越能力。
技巧
我們採用了官方的 LLaVA-NeXT 和官方訓練資料集 LLaVA-NeXT-Data 來評估基礎視覺模型。
語言模型是 Qwen2.5-7B。
結果
視覺模組 | 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 (
str
或function
, 可選, 預設為"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.BaseModelOutputWithPooling 或 tuple(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.BaseModelOutputWithPooling 或 tuple(torch.FloatTensor)
transformers.modeling_outputs.BaseModelOutputWithPooling 或 torch.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}")