阿里最新开源多模态大模型Ovis部署

Ovis是由阿里国际AI团队推出一款先进的多模态大模型,它在处理和理解多种类型的数据输入方面表现出色,比如文本、图像等。

Ovis模型不仅能够执行数学推理问答、物体识别、文本提取等任务,还能够在复杂的决策场景中发挥作用。

Ovis模型具备创新架构设计,引入了可学习的视觉嵌入词表,将连续的视觉特征转换为概率化的视觉token,并通过视觉嵌入词表加权生成结构化的视觉嵌入。

此外,Ovis支持高分图像处理,能够兼容极端长宽比及高分辨率图像,显示出出色的图像理解能力。

为了保证模型的全面性和实用性,Ovis训练时采用了广泛的多方向数据集,涵盖了Caption(标题)、VQA(视觉问答)、OCR(光学字符识别)、Table(表格)以及Chart(图表)等多种多模态数据。

根据OpenCompass平台的评测数据显示,Ovis 1.6-Gemma2-9B版本在30亿参数以下的模型中综合排名第一,超过了包括MiniCPM-V-2.6在内的多个行业优秀大模型。

github项目地址:https://github.com/AIDC-AI/Ovis。

一、环境安装

1、python环境

建议安装python版本在3.10以上。

2、pip库安装

pip install torch==2.2.0+cu118 torchvision==0.17.0+cu118 torchaudio==2.2.0 --extra-index-url https://download.pytorch.org/whl/cu118

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install -e .

3、Ovis1.6-Gemma2-9B 模型下载

git lfs install

git clone https://huggingface.co/AIDC-AI/Ovis1.6-Gemma2-9B

二**、功能测试**

1、运行测试

(1)python代码调用测试

复制代码
from dataclasses import dataclass, field
from typing import Optional, Union, List
import logging

import torch
from PIL import Image

from ovis.model.modeling_ovis import Ovis
from ovis.util.constants import IMAGE_TOKEN


@dataclass
class RunnerArguments:
    model_path: str
    max_new_tokens: int = field(default=512)
    do_sample: bool = field(default=False)
    top_p: Optional[float] = field(default=0.95)
    top_k: Optional[int] = field(default=50)
    temperature: Optional[float] = field(default=1.0)
    max_partition: int = field(default=9)

class OvisRunner:
    def __init__(self, args: RunnerArguments):
        self.model_path = args.model_path
        self.device = torch.cuda.current_device() if torch.cuda.is_available() else 'cpu'
        self.dtype = torch.bfloat16
        self.model = Ovis.from_pretrained(self.model_path, torch_dtype=self.dtype, multimodal_max_length=8192)
        self.model = self.model.eval().to(device=self.device)
        
        self.eos_token_id = self.model.generation_config.eos_token_id
        self.text_tokenizer = self.model.get_text_tokenizer()
        self.pad_token_id = self.text_tokenizer.pad_token_id
        self.visual_tokenizer = self.model.get_visual_tokenizer()
        self.conversation_formatter = self.model.get_conversation_formatter()
        self.image_placeholder = IMAGE_TOKEN
        self.max_partition = args.max_partition
        
        self.gen_kwargs = dict(
            max_new_tokens=args.max_new_tokens,
            do_sample=args.do_sample,
            top_p=args.top_p,
            top_k=args.top_k,
            temperature=args.temperature,
            repetition_penalty=None,
            eos_token_id=self.eos_token_id,
            pad_token_id=self.pad_token_id,
            use_cache=True
        )

    def preprocess(self, inputs: List[Union[Image.Image, str]]):
        if len(inputs) == 2 and isinstance(inputs[0], str) and isinstance(inputs[1], Image.Image):
            inputs = list(reversed(inputs))

        query = ''
        images = []
        for data in inputs:
            if isinstance(data, Image.Image):
                query += self.image_placeholder + '\n'
                images.append(data)
            elif isinstance(data, str):
                query += data.replace(self.image_placeholder, '')
            elif data is not None:
                raise ValueError(f'Invalid input type, expected `PIL.Image.Image` or `str`, but got {type(data)}')

        prompt, input_ids, pixel_values = self.model.preprocess_inputs(query, images, max_partition=self.max_partition)
        attention_mask = torch.ne(input_ids, self.text_tokenizer.pad_token_id)
        input_ids = input_ids.unsqueeze(0).to(device=self.device)
        attention_mask = attention_mask.unsqueeze(0).to(device=self.device)
        pixel_values = [pv.to(device=self.device, dtype=self.dtype) if pv is not None else None for pv in pixel_values]

        return prompt, input_ids, attention_mask, pixel_values

    def run(self, inputs: List[Union[Image.Image, str]]):
        try:
            prompt, input_ids, attention_mask, pixel_values = self.preprocess(inputs)
            output_ids = self.model.generate(
                input_ids,
                pixel_values=pixel_values,
                attention_mask=attention_mask,
                **self.gen_kwargs
            )
            output = self.text_tokenizer.decode(output_ids[0], skip_special_tokens=True)
            input_token_len = input_ids.shape[1]
            output_token_len = output_ids.shape[1]

            response = {
                'prompt': prompt,
                'output': output,
                'prompt_tokens': input_token_len,
                'total_tokens': input_token_len + output_token_len
            }
            return response
        except Exception as e:
            logging.error(f"An error occurred: {e}")
            raise

if __name__ == '__main__':
    runner_args = RunnerArguments(model_path='<model_path>')
    runner = OvisRunner(runner_args)
    image = Image.open('<image_path>')
    text = '<prompt>'
    response = runner.run([image, text])
    print(response['output'])

未完......

更多详细的欢迎关注:杰哥新技术

相关推荐
Slaughter信仰8 小时前
图解大模型_生成式AI原理与实战学习笔记前四张问答(7题)
人工智能·笔记·学习
龙腾亚太8 小时前
大模型十大高频问题之五:如何低成本部署大模型?有哪些开源框架推荐?
人工智能·langchain·llm·智能体·大模型培训
信息快讯8 小时前
【人工智能与数据驱动方法加速金属材料设计与应用】
人工智能·材料工程·金属材料·结构材料设计
c#上位机8 小时前
halcon图像增强——emphasize
图像处理·人工智能·计算机视觉·c#·上位机·halcon
老蒋新思维9 小时前
创客匠人峰会洞察:私域 AI 化重塑知识变现 —— 创始人 IP 的私域增长新引擎
大数据·网络·人工智能·网络协议·tcp/ip·创始人ip·创客匠人
知行力9 小时前
【GitHub每日速递 20251209】Next.js融合AI,让draw.io图表创建、修改、可视化全靠自然语言!
javascript·人工智能·github
冷yan~9 小时前
OpenAI Codex CLI 完全指南:AI 编程助手的终端革命
人工智能·ai·ai编程
菜鸟‍9 小时前
【论文学习】通过编辑习得分数函数实现扩散模型中的图像隐藏
人工智能·学习·机器学习
AKAMAI9 小时前
无服务器计算架构的优势
人工智能·云计算