MiniCPM V 2

提供商openbmb
分类visual-question-answering
许可证Apache-2.0
下载量22.0K
星标48

简介

MiniCPM-V 2 是由 OpenBMB 团队打造的一款高性能端侧多模态模型。它在保持轻量级参数的同时,展现出了媲美 GPT-4V 的图像理解能力,特别是在高分辨率图像处理和细粒度视觉分析上表现出色。对于中国开发者而言,该模型最大的优势在于极高的推理效率,能够流畅运行在消费级 GPU 甚至部分端侧设备上,非常适合需要本地化部署、对隐私要求高且不希望依赖昂贵 API 的视觉问答或图像分析场景。

核心亮点

  • 端侧部署友好,推理速度快且资源占用低
  • 支持高分辨率图像输入,细节识别能力极强
  • 视觉问答表现出色,可替代部分昂贵闭源 API
  • 采用 Apache-2.0 协议,商业化集成门槛低

使用方法

安装依赖
# 安装 Hugging Face transformers
pip install transformers torch
SDK 使用
# 使用 transformers 加载模型
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("openbmb/MiniCPM-V-2")
tokenizer = AutoTokenizer.from_pretrained("openbmb/MiniCPM-V-2")

Hugging Face 下载

我们推荐使用命令行或者 Hugging Face Hub SDK 来进行模型的下载。

操作指引:在下载前,请先通过如下命令安装 huggingface_hub:

操作指引
pip install -U huggingface_hub

命令行下载

下载完整模型库

下载完整模型库
huggingface-cli download openbmb/MiniCPM-V-2

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)
huggingface-cli download openbmb/MiniCPM-V-2 config.json --local-dir ./dir

更多命令行下载选项,可参见官方文档

SDK 下载

SDK 下载
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('openbmb/MiniCPM-V-2')

Git 下载

请确保 lfs 已经被正确安装

Git 下载
git lfs install
git clone https://huggingface.co/openbmb/MiniCPM-V-2

如果您希望跳过 lfs 大文件下载,可以使用如下命令

跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/openbmb/MiniCPM-V-2

模型文件托管在 Hugging Face Hub,使用 HF CLI / SDK / Git 直接下载,不经过本站。

PyTorch / Transformers 使用

安装 Transformers

安装 Transformers
pip install -U transformers torch

模型加载和推理

模型加载和推理
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained('openbmb/MiniCPM-V-2')
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V-2')

模型下载

我们推荐使用命令行或者 ModelScope SDK 来进行模型的下载。

操作指引:在下载前,请先通过如下命令安装 ModelScope:

操作指引
pip install modelscope

命令行下载

下载完整模型库

下载完整模型库
modelscope download --model openbmb/MiniCPM-V-2

下载单个文件到指定本地文件夹(以下载 README.md 到当前路径下 dir 目录为例)

下载单个文件到指定本地文件夹(以下载 README.md 到当前路径下 dir 目录为例)
modelscope download --model openbmb/MiniCPM-V-2 README.md --local_dir ./dir

更多更丰富的命令行下载选项,可参见具体文档

SDK 下载

SDK 下载
# 模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('openbmb/MiniCPM-V-2')

Git 下载

请确保 lfs 已经被正确安装

Git 下载
git lfs install
git clone https://www.modelscope.cn/openbmb/MiniCPM-V-2.git

如果您希望跳过 lfs 大文件下载,可以使用如下命令

跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://www.modelscope.cn/openbmb/MiniCPM-V-2.git

ModelScope 模型页直接下载模型文件;无需将模型文件放在本站服务器。

Notebook 快速开发

下载并安装 ModelScope library

下载并安装 ModelScope library
pip install "modelscope[audio,cv,nlp,multi-modal,science]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

模型加载和推理

模型加载和推理
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

p = pipeline('text-generation', 'openbmb/MiniCPM-V-2')

完整文档

来源: HuggingFace

---
pipeline_tag: visual-question-answering
language:

  • en

  • zh

datasets:
  • HaoyeZhang/RLHF-V-Dataset

  • Yirany/UniMM-Chat

  • HuggingFaceM4/VQAv2

  • liuhaotian/LLaVA-Instruct-150K

---

GitHub | Demo

News <!-- omit in toc -->

  • [2025.01.14] 🔥 We open source MiniCPM-o 2.6, with significant performance improvement over MiniCPM-V 2.6, and support real-time speech-to-speech conversation and multimodal live streaming. Try it now.
  • [2024.08.06] 🔥 We open-source MiniCPM-V 2.6, which outperforms GPT-4V on single image, multi-image and video understanding. It advances popular features of MiniCPM-Llama3-V 2.5, and can support real-time video understanding on iPad.
  • [2024.04.23] MiniCPM-V 2.0 supports vLLM now!
  • [2024.04.18] We create a HuggingFace Space to host the demo of MiniCPM-V 2.0 at here!
  • [2024.04.17] MiniCPM-V 2.0 supports deploying WebUI Demo now!
  • [2024.04.15] MiniCPM-V 2.0 supports fine-tuning with the SWIFT framework!
  • [2024.04.12] We open-source MiniCPM-V-2.0, which achieves comparable performance with Gemini Pro in understanding scene text and outperforms strong Qwen-VL-Chat 9.6B and Yi-VL 34B on <a href="https://rank.opencompass.org.cn/leaderboard-multimodal">OpenCompass</a>, a comprehensive evaluation over 11 popular benchmarks. Click <a href="https://openbmb.vercel.app/minicpm-v-2">here</a> to view the MiniCPM-V 2.0 technical blog.

MiniCPM-V 2.0

MiniCPM-V 2.8B is a strong multimodal large language model for efficient end-side deployment. The model is built based on SigLip-400M and MiniCPM-2.4B, connected by a perceiver resampler. Our latest version, MiniCPM-V 2.0 has several notable features.

  • 🔥 State-of-the-art Performance.

MiniCPM-V 2.0 achieves state-of-the-art performance on multiple benchmarks (including OCRBench, TextVQA, MME, MMB, MathVista, etc) among models under 7B parameters. It even outperforms strong Qwen-VL-Chat 9.6B, CogVLM-Chat 17.4B, and Yi-VL 34B on OpenCompass, a comprehensive evaluation over 11 popular benchmarks. Notably, MiniCPM-V 2.0 shows strong OCR capability, achieving comparable performance to Gemini Pro in scene-text understanding, and state-of-the-art performance on OCRBench among open-source models.

  • 🏆 Trustworthy Behavior.

LMMs are known for suffering from hallucination, often generating text not factually grounded in images. MiniCPM-V 2.0 is the first end-side LMM aligned via multimodal RLHF for trustworthy behavior (using the recent RLHF-V [CVPR'24] series technique). This allows the model to match GPT-4V in preventing hallucinations on Object HalBench.

  • 🌟 High-Resolution Images at Any Aspect Raito.

MiniCPM-V 2.0 can accept 1.8 million pixels (e.g., 1344x1344) images at any aspect ratio. This enables better perception of fine-grained visual information such as small objects and optical characters, which is achieved via a recent technique from LLaVA-UHD.

  • ⚡️ High Efficiency.

MiniCPM-V 2.0 can be efficiently deployed on most GPU cards and personal computers, and even on end devices such as mobile phones. For visual encoding, we compress the image representations into much fewer tokens via a perceiver resampler. This allows MiniCPM-V 2.0 to operate with favorable memory cost and speed during inference even when dealing with high-resolution images.

  • 🙌 Bilingual Support.

MiniCPM-V 2.0 supports strong bilingual multimodal capabilities in both English and Chinese. This is enabled by generalizing multimodal capabilities across languages, a technique from VisCPM [ICLR'24].

Evaluation <!-- omit in toc -->

<div align="center">
<img src=/openbmb/MiniCPM-V-2.0/resolve/main/assets/minicpmv-2-peformance2.png width=100% />
</div>
Results on TextVQA, DocVQA, OCRBench, OpenCompass, MME, MMBench, MMMU, MathVista, LLaVA Bench, Object HalBench.
<div align="center">
<img src=/openbmb/MiniCPM-V-2.0/resolve/main/assets/minicpmv-2-benchmark.png width=140% />
</div>

Examples <!-- omit in toc -->

<table align="center">
<p align="center">
<img src="assets/minicpmv2-cases_2.png" width=95%/>
</p>
</table>

We deploy MiniCPM-V 2.0 on end devices. The demo video is the raw screen recording on a Xiaomi 14 Pro without edition.

<table align="center">
<p align="center">
<img src="assets/station.gif" width=40% style="display:inline-block;"/>
<img src="assets/london_car.gif" width=40% style="display:inline-block;"/>
</p>
</table>

Demo

Click here to try out the Demo of MiniCPM-V 2.0.

Deployment on Mobile Phone

MiniCPM-V 2.0 can be deployed on mobile phones with Android and Harmony operating systems. 🚀 Try it out here.

Inference with vLLM<a id="vllm"></a>

<details>
<summary>Click to see how to inference with vLLM </summary>
Because our pull request to vLLM is still waiting for reviewing, we fork this repository to build and test our vLLM demo. Here are the steps:

1. Clone our version of vLLM:

shell
git clone https://github.com/OpenBMB/vllm.git

2. Install vLLM:
shell
cd vllm
pip install -e .

3. Install timm:
shell
pip install timm=0.9.10

4. Run our demo:
shell
python examples/minicpmv_example.py

</details>

Usage

Inference using Huggingface transformers on Nivdia GPUs or Mac with MPS (Apple silicon or AMD GPUs). Requirements tested on python 3.10:
code
Pillow==10.1.0
timm==0.9.10
torch==2.1.2
torchvision==0.16.2
transformers==4.36.0
sentencepiece==0.1.99
python
# test.py
import torch
from PIL import Image
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained('openbmb/MiniCPM-V-2', trust_remote_code=True, torch_dtype=torch.bfloat16)

For Nvidia GPUs support BF16 (like A100, H100, RTX3090)


model = model.to(device='cuda', dtype=torch.bfloat16)

For Nvidia GPUs do NOT support BF16 (like V100, T4, RTX2080)


#model = model.to(device='cuda', dtype=torch.float16)

For Mac with MPS (Apple silicon or AMD GPUs).


Run with PYTORCH_ENABLE_MPS_FALLBACK=1 python test.py


#model = model.to(device='mps', dtype=torch.float16)

tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V-2', trust_remote_code=True)
model.eval()

image = Image.open('xx.jpg').convert('RGB')
question = 'What is in the image?'
msgs = [{'role': 'user', 'content': question}]

res, context, _ = model.chat(
image=image,
msgs=msgs,
context=None,
tokenizer=tokenizer,
sampling=True,
temperature=0.7
)
print(res)

Please look at GitHub for more detail about usage.

MiniCPM-V 1.0 <!-- omit in toc -->

Please see the info about MiniCPM-V 1.0 here.

License

#### Model License
  • The code in this repo is released under the Apache-2.0 License.
  • The models and weights of MiniCPM are completely free for academic research. after filling out a ["questionnaire"](https://m