模型简介
模型特点
模型能力
使用案例
许可证: 其他 许可证名称: qwen 许可证链接: https://huggingface.co/Qwen/Qwen2.5-72B-Instruct/blob/main/LICENSE 任务标签: 图像文本到文本 库名称: transformers 基础模型:
- OpenGVLab/InternVL3-1B-Instruct 基础模型关系: 微调 数据集:
- OpenGVLab/MMPR-v1.2 语言:
- 多语言 标签:
- internvl
InternVL3-1B Transformers 🤗 实现
[📜 InternVL 1.0] [📜 InternVL 1.5] [📜 InternVL 2.5] [📜 InternVL2.5-MPO] [📜 InternVL3]
[🆕 博客] [🗨️ 聊天演示] [🤗 HF 演示] [🚀 快速开始] [📖 文档]

[!重要] 本仓库包含 OpenGVLab/InternVL3-1B 模型的 Hugging Face 🤗 Transformers 实现。 其功能旨在与原始 OpenGVLab 版本等效。 作为一个原生 Transformers 模型,它支持核心库功能,如各种注意力实现(包括 SDPA 和 FA2),并支持图像、视频和文本输入的批量推理。
简介
我们推出 InternVL3,这是一个先进的多模态大语言模型(MLLM)系列,展示了卓越的整体性能。 与 InternVL 2.5 相比,InternVL3 表现出更优的多模态感知和推理能力,同时进一步扩展其多模态能力,涵盖工具使用、GUI 代理、工业图像分析、3D 视觉感知等。 此外,我们将 InternVL3 与 Qwen2.5 Chat 模型进行比较,后者的预训练基础模型被用作 InternVL3 语言组件的初始化。得益于原生多模态预训练,InternVL3 系列在整体文本性能上甚至优于 Qwen2.5 系列。
您可以在原始检查点 OpenGVLab/InternVL3-1B 中找到更多关于 InternVL3 家族的信息。
使用示例
使用 Pipeline 进行推理
以下是使用 image-text-to-text
pipeline 在几行代码中执行 InternVL3
模型推理的方法:
>>> from transformers import pipeline
>>> messages = [
... {
... "role": "user",
... "content": [
... {
... "type": "image",
... "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg",
... },
... {"type": "text", "text": "描述这张图片。"},
... ],
... },
... ]
>>> pipe = pipeline("image-text-to-text", model="OpenGVLab/InternVL3-1B-hf")
>>> outputs = pipe(text=messages, max_new_tokens=50, return_full_text=False)
>>> outputs[0]["generated_text"]
'图片展示了一个充满活力的自然场景,有几朵花和一只蜜蜂。\n\n1. **前景花朵**: \n - 主要焦点是一朵大粉色波斯菊,中心有明显的黄色花蕊。花瓣柔软,略微'
单张图片推理
此示例展示了如何使用聊天模板在单张图片上执行 InternVL 模型的推理。
[!注意] 注意,模型已针对聊天训练了特定的提示格式。使用
processor.apply_chat_template(my_conversation_dict)
正确格式化您的提示。
>>> from transformers import AutoProcessor, AutoModelForImageTextToText
>>> import torch
>>> torch_device = "cuda"
>>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
>>> processor = AutoProcessor.from_pretrained(model_checkpoint)
>>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)
>>> messages = [
... {
... "role": "user",
... "content": [
... {"type": "image", "url": "http://images.cocodataset.org/val2017/000000039769.jpg"},
... {"type": "text", "text": "请明确描述这张图片。"},
... ],
... }
... ]
>>> inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
>>> generate_ids = model.generate(**inputs, max_new_tokens=50)
>>> decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
>>> decoded_output
'图片显示两只猫躺在一条粉色毯子上。左边的猫是一只虎斑猫,毛色为棕色、黑色和白色相间,似乎正在睡觉,头靠在毯子上。右边的猫'
纯文本生成
此示例展示了如何在不提供任何图像输入的情况下使用 InternVL 模型生成文本。
>>> from transformers import AutoProcessor, AutoModelForImageTextToText
>>> import torch
>>> torch_device = "cuda"
>>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
>>> processor = AutoProcessor.from_pretrained(model_checkpoint)
>>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)
>>> messages = [
... {
... "role": "user",
... "content": [
... {"type": "text", "text": "写一首俳句"},
... ],
... }
... ]
>>> inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(torch_device, dtype=torch.bfloat16)
>>> generate_ids = model.generate(**inputs, max_new_tokens=50)
>>> decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
>>> print(decoded_output)
"黎明的低语,\n夜晚的寂静低语,\n新日的光开始。"
批量图像和文本输入
InternVL 模型还支持批量图像和文本输入。
>>> from transformers import AutoProcessor, AutoModelForImageTextToText
>>> import torch
>>> torch_device = "cuda"
>>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
>>> processor = AutoProcessor.from_pretrained(model_checkpoint)
>>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)
>>> messages = [
... [
... {
... "role": "user",
... "content": [
... {"type": "image", "url": "https://llava-vl.github.io/static/images/view.jpg"},
... {"type": "text", "text": "为这张图片写一首俳句"},
... ],
... },
... ],
... [
... {
... "role": "user",
... "content": [
... {"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"},
... {"type": "text", "text": "描述这张图片"},
... ],
... },
... ],
... ]
>>> inputs = processor.apply_chat_template(messages, padding=True, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
>>> output = model.generate(**inputs, max_new_tokens=25)
>>> decoded_outputs = processor.batch_decode(output, skip_special_tokens=True)
>>> decoded_outputs
["用户\n\n为这张图片写一首俳句\n助手\n丝绸般的湖,\n木制的码头,\n自然的宁静。",
'用户\n\n描述这张图片\n助手\n图片显示了一个街道场景,有一个传统的中式拱门,被称为“中国门”或“中国门']
批量多图像输入
此 InternVL 模型的实现支持每个文本不同数量图像的批量文本-图像输入。
>>> from transformers import AutoProcessor, AutoModelForImageTextToText
>>> import torch
>>> torch_device = "cuda"
>>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
>>> processor = AutoProcessor.from_pretrained(model_checkpoint)
>>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)
>>> messages = [
... [
... {
... "role": "user",
... "content": [
... {"type": "image", "url": "https://llava-vl.github.io/static/images/view.jpg"},
... {"type": "text", "text": "为这张图片写一首俳句"},
... ],
... },
... ],
... [
... {
... "role": "user",
... "content": [
... {"type": "image", "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"},
... {"type": "image", "url": "https://thumbs.dreamstime.com/b/golden-gate-bridge-san-francisco-purple-flowers-california-echium-candicans-36805947.jpg"},
... {"type": "text", "text": "这些图片描绘了两个不同的地标。你能识别它们吗?"},
... ],
... },
... ],
>>> ]
>>> inputs = processor.apply_chat_template(messages, padding=True, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
>>> output = model.generate(**inputs, max_new_tokens=25)
>>> decoded_outputs = processor.batch_decode(output, skip_special_tokens=True)
>>> decoded_outputs
["用户\n\n为这张图片写一首俳句\n助手\n丝绸般的湖,\n木制的码头,\n自然的宁静。",
'用户\n\n\n这些图片描绘了两个不同的地标。你能识别它们吗?\n助手\n是的,这些图片描绘了自由女神像和金门大桥。']
视频输入
InternVL 模型还可以处理视频输入。以下是一个使用聊天模板在视频输入上执行推理的示例。
>>> from transformers import AutoProcessor, AutoModelForImageTextToText, BitsAndBytesConfig
>>> model_checkpoint = "OpenGVLab/InternVL3-8B-hf"
>>> quantization_config = BitsAndBytesConfig(load_in_4bit=True)
>>> processor = AutoProcessor.from_pretrained(model_checkpoint)
>>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, quantization_config=quantization_config)
>>> messages = [
... {
... "role": "user",
... "content": [
... {
... "type": "video",
... "url": "https://huggingface.co/datasets/hf-internal-testing/fixtures_videos/resolve/main/tennis.mp4",
... },
... {"type": "text", "text": "这个男人在做什么类型的击球?"},
... ],
... }
>>> ]
>>> inputs = processor.apply_chat_template(
... messages,
... return_tensors="pt",
... add_generation_prompt=True,
... tokenize=True,
... return_dict=True,
>>> ).to(model.device, dtype=torch.float16)
>>> output = model.generate(**inputs, max_new_tokens=25)
>>> decoded_output = processor.decode(output[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
>>> decoded_output
'这个男人正在做一个正手击球。'
交错图像和视频输入
此示例展示了如何使用聊天模板处理一批交错图像和视频输入的聊天对话。
>>> from transformers import AutoProcessor, AutoModelForImageTextToText, BitsAndBytesConfig








