这是 Yi-VL-6B 模型的开源版本。
您可以使用此模型在下游任务中进行微调,我们建议使用我们高效的微调工具包:https://github.com/hiyouga/LLaMA-Factory。
使用方法:
import requests
from PIL import Image
import torch
from transformers import AutoModelForVision2Seq, LlavaConfig
from openmind import AutoProcessor
import transformers
from openmind_hub import snapshot_download
from torch import nn
class LlavaMultiModalProjectorYiVL(nn.Module):
def __init__(self, config: "LlavaConfig"):
super().__init__()
self.linear_1 = nn.Linear(config.vision_config.hidden_size, config.text_config.hidden_size, bias=True)
self.linear_2 = nn.LayerNorm(config.text_config.hidden_size, bias=True)
self.linear_3 = nn.Linear(config.text_config.hidden_size, config.text_config.hidden_size, bias=True)
self.linear_4 = nn.LayerNorm(config.text_config.hidden_size, bias=True)
self.act = nn.GELU()
def forward(self, image_features):
hidden_states = self.linear_1(image_features)
hidden_states = self.linear_2(hidden_states)
hidden_states = self.act(hidden_states)
hidden_states = self.linear_3(hidden_states)
hidden_states = self.linear_4(hidden_states)
return hidden_states
# Monkey patch of LlavaMultiModalProjector is mandatory
transformers.models.llava.modeling_llava.LlavaMultiModalProjector = LlavaMultiModalProjectorYiVL
model_id = snapshot_download("AI-Research/Yi-VL-6B-hf", revision="main", resume_download=True,
ignore_patterns=["*.h5", "*.ot", "*.mspack"])
messages = [
{ "role": "user", "content": "<image>What's in the picture?" }
]
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
model = AutoModelForVision2Seq.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
).to("npu")
processor = AutoProcessor.from_pretrained(model_id)
text = [processor.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)]
images = [Image.open(requests.get(image_file, stream=True).raw)]
inputs = processor(text=text, images=images, return_tensors='pt').to("npu", torch.float16)
output = model.generate(**inputs, max_new_tokens=200)
output = processor.batch_decode(output, skip_special_tokens=True)
print(output[0].split("Assistant:")[-1].strip())您也可以选择使用 LLaMA-Factory 中的 CLI 命令启动 Web 演示。
llamafactory-cli webchat \
--model_name_or_path BUAADreamer/Yi-VL-6B-hf \
--template yivl \
--visual_inputs| 指标 | 数值 |
|---|---|
| MMMU_val | 36.8 |
| CMMMU_val | 32.2 |