十、多模态大语言模型(MLLM)

1 多模态大语言模型(Multimodal Large Language Models)

  • 模态的定义
    模态(modal)是事情经历和发生的方式,我们生活在一个由多种模态(Multimodal)信息构成的世界,包括视觉信息、听觉信息、文本信息、嗅觉信息等
  • MLLMs的定义
    由LLM扩展而来具有接收和推理多模态信息能力的模型

2 模型概念区分

  • 跨模态模型
  • 单模态大模型
  • 多模态模型
  • 多模态语言大模型

跨模态模型

单模态大模型

多模态大模型

多模态大语言模型

3 多模态的发展历程

四个关键里程碑

1 Vision Transformer(ViT)

图片格子的线性映射

DL

Mask Image Modeling 无监督图像特征学习

2 基于transformer架构的图像-文本联合建模

3 大规模 图-文 Token对齐模型CLIP

通过余弦距离将文和图转换至同一向量空间。将图像的分类闭集引入至开集

python 复制代码
from transformers import CLIPProcessor, CLIPModel
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
python 复制代码
from IPython.display import Image, display
display(Image(filename="bus.jpg"))
python 复制代码
from PIL import Image
image = Image.open("bus.jpg")
cls_list = ["dog", "woman", "man", "bus", "truck","person",
            "a black truck", "a white truck", "cat"]
input = processor(text=cls_list, images=image,
                  return_tensors="pt", padding=True)
outputs = model(**input)
print(outputs.keys())
python 复制代码
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1)

for i in range(len(cls_list)):
    print(f"{cls_list[i]}: {probs[0][i]}")

4 多模态大语言模型OpenAI GPTv4

支持图文交替输出,输入文本或图像,输出自然语言

特点如下:

  • 遵循文字提示
  • 理解视觉指向和参考
  • 支持视觉和文本联合提示
  • 少样本上下文学习
  • 强大的视觉认知能力




    5 Google Gemini 原生多模态
    • 输入:文本、语音、图像和视频信息
    • 输出:自然语言、图像
  • 支持多模态内容输出
  • 复杂图像理解与代码生成
bash 复制代码
!pip install google-generativeai -i https://pypi.tuna.tsinghua.edu.cn/simple
python 复制代码
import gradio as gr
from openai import OpenAI
import base64
from PIL import Image
import io
import os
import google.generativeai as genai

# Function to encode the image to base64


def encode_image_to_base64(image):
    buffered = io.BytesIO()
    image.save(buffered, format="JPEG")
    return base64.b64encode(buffered.getvalue()).decode('utf-8')

# Function to query GPT-4 Vision


def query_gpt4_vision(text, image1, image2, image3):
    client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
    messages = [{"role": "user", "content": [{"type": "text", "text": text}]}]

    images = [image1, image2, image3]
    for image in images:
        if image is not None:
            base64_image = encode_image_to_base64(image)
            image_message = {
                "type": "image_url",
                "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
            }
            messages[0]["content"].append(image_message)

    response = client.chat.completions.create(
        model="gpt-4-vision-preview",
        messages=messages,
        max_tokens=1024,
    )
    return response.choices[0].message.content

# Function to query Gemini-Pro


def query_gemini_vision(text, image1, image2, image3):
    # Or use `os.getenv('GOOGLE_API_KEY')` to fetch an environment variable.
    # GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')
    GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
    genai.configure(api_key=GOOGLE_API_KEY)
    model = genai.GenerativeModel('gemini-pro-vision')

    images = [image1, image2, image3]
    query = [text]
    for image in images:
        if image is not None:
            query.append(image)
    response = model.generate_content(query, stream=False)
    response.resolve()

    return response.text

# 由于Gradio 2.0及以上版本的界面构建方式有所不同,这里使用blocks API来创建更复杂的UI


def main():
    with gr.Blocks() as demo:
        gr.Markdown("### 输入文本")
        input_text = gr.Textbox(lines=2, label="输入文本")
        input_images = [
            gr.Image(type="pil", label="Upload Image", tool="editor") for i in range(3)]
        output_gpt4 = gr.Textbox(label="GPT-4 输出")
        output_other_api = gr.Textbox(label="Gemini-Pro 输出")
        btn_gpt4 = gr.Button("调用GPT-4")
        btn_other_api = gr.Button("调用Gemini-Pro")

        btn_gpt4.click(fn=query_gpt4_vision, inputs=[
                       input_text] + input_images, outputs=output_gpt4)
        btn_other_api.click(fn=query_gemini_vision, inputs=[
                            input_text] + input_images, outputs=output_other_api)

    demo.launch(share=True)


if __name__ == "__main__":
    main()


4 多模态大语言模型的应用

  • 工业
  • 医疗
  • 视觉内容认知与编辑
  • 具身智能
  • 新一代人机交互
相关推荐
池央34 分钟前
AI性能极致体验:通过阿里云平台高效调用满血版DeepSeek-R1模型
人工智能·阿里云·云计算
我们的五年35 分钟前
DeepSeek 和 ChatGPT 在特定任务中的表现:逻辑推理与创意生成
人工智能·chatgpt·ai作画·deepseek
Yan-英杰36 分钟前
百度搜索和文心智能体接入DeepSeek满血版——AI搜索的新纪元
图像处理·人工智能·python·深度学习·deepseek
Fuweizn38 分钟前
富唯智能可重构柔性装配产线:以智能协同赋能制造业升级
人工智能·智能机器人·复合机器人
taoqick3 小时前
对PosWiseFFN的改进: MoE、PKM、UltraMem
人工智能·pytorch·深度学习
suibian52353 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
预测模型的开发与应用研究4 小时前
数据分析的AI+流程(个人经验)
人工智能·数据挖掘·数据分析
源大模型4 小时前
OS-Genesis:基于逆向任务合成的 GUI 代理轨迹自动化生成
人工智能·gpt·智能体
PowerBI学谦5 小时前
Python in Excel高级分析:一键RFM分析
大数据·人工智能·pandas
运维开发王义杰6 小时前
AI: Unsloth + Llama 3 微调实践,基于Colab
人工智能·llama