Home

Awesome

<div align="center">

InternVL Family: Closing the Gap to Commercial Multimodal Models with Open-Source Suites โ€”โ€” A Pioneering Open-Source Alternative to GPT-4o

<div align="center"> <img width="500" alt="image" src="https://github.com/user-attachments/assets/930e6814-8a9f-43e1-a284-118a5732daa4"> <br> </div>

[๐Ÿ†• Blog] [๐Ÿค” FAQs] [๐Ÿ—จ๏ธ Chat Demo] [๐Ÿค— HF Demo] [๐Ÿ“– Document] [๐ŸŒ API] [๐Ÿš€ Quick Start]

[๐Ÿ”ฅ InternVL2.5 Report] [Mini-InternVL Paper] [InternVL2 Blog] [๐Ÿ“œ InternVL 1.5 Paper] [๐Ÿ“œ InternVL 1.0 Paper]

[๐Ÿ“– 2.0 ไธญๆ–‡่งฃ่ฏป] [๐Ÿ“– 1.5 ไธญๆ–‡่งฃ่ฏป] [๐Ÿ“– 1.0 ไธญๆ–‡่งฃ่ฏป]

Switch to the Chinese version (ๅˆ‡ๆข่‡ณไธญๆ–‡็‰ˆ)

<a href="https://trendshift.io/repositories/9803" target="_blank"><img src="https://trendshift.io/api/badge/repositories/9803" alt="OpenGVLab%2FInternVL | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a> <img height="55" alt="image" src="https://github.com/user-attachments/assets/bd62ab46-f0ea-40c6-ab10-7fde671716cc">

<!-- ![perform](https://github.com/user-attachments/assets/aab987fd-8811-4ff9-b5e0-1d08f0ad1f7a) -->

perform

</div>

News ๐Ÿš€๐Ÿš€๐Ÿš€

TODO List

Documents

Compared with SOTA VLLMs

waic_performance

Model Zoo

Multimodal Large Language Model (InternVL 2.5)

<table> <tr> <th>Model Name</th> <th>Vision Part</th> <th>Language Part</th> <th>HF&nbsp;Link</th> </tr> <tr> <td>InternVL2_5-1B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px-V2_5">InternViT-300M-448px-V2_5</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct">Qwen2.5-0.5B-Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-1B">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-2B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px-V2_5">InternViT-300M-448px-V2_5</a></td> <td><a href="https://huggingface.co/internlm/internlm2_5-1_8b-chat">internlm2_5-1_8b-chat</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-2B">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-4B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px-V2_5">InternViT-300M-448px-V2_5</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2.5-3B-Instruct">Qwen2.5-3B-Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-4B">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-8B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px-V2_5">InternViT-300M-448px-V2_5</a></td> <td><a href="https://huggingface.co/internlm/internlm2_5-7b-chat">internlm2_5-7b-chat</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-8B">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-26B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V2_5">InternViT-6B-448px-V2_5</a></td> <td><a href="https://huggingface.co/internlm/internlm2_5-20b-chat">internlm2_5-20b-chat</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-26B">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-38B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V2_5">InternViT-6B-448px-V2_5</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2.5-32B-Instruct">Qwen2.5-32B-Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-38B">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-78B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V2_5">InternViT-6B-448px-V2_5</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2.5-72B-Instruct">Qwen2.5-72B-Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-78B">๐Ÿค— link</a></td> </tr> </table> <table> <tr> <th>Model Name</th> <th>Vision Part</th> <th>Language Part</th> <th>HF&nbsp;Link</th> </tr> <tr> <td>InternVL2_5-1B-MPO</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px-V2_5">InternViT-300M-448px-V2_5</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct">Qwen2.5-0.5B-Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-1B-MPO">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-2B-MPO</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px-V2_5">InternViT-300M-448px-V2_5</a></td> <td><a href="https://huggingface.co/internlm/internlm2_5-1_8b-chat">internlm2_5-1_8b-chat</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-2B-MPO">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-4B-MPO</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px-V2_5">InternViT-300M-448px-V2_5</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2.5-3B-Instruct">Qwen2.5-3B-Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-4B-MPO">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-8B-MPO</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px-V2_5">InternViT-300M-448px-V2_5</a></td> <td><a href="https://huggingface.co/internlm/internlm2_5-7b-chat">internlm2_5-7b-chat</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-8B-MPO">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-26B-MPO</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V2_5">InternViT-6B-448px-V2_5</a></td> <td><a href="https://huggingface.co/internlm/internlm2_5-20b-chat">internlm2_5-20b-chat</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-26B-MPO">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-38B-MPO</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V2_5">InternViT-6B-448px-V2_5</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2.5-32B-Instruct">Qwen2.5-32B-Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-38B-MPO">๐Ÿค— link</a></td> </tr> <tr> <td>InternVL2_5-78B-MPO</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V2_5">InternViT-6B-448px-V2_5</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2.5-72B-Instruct">Qwen2.5-72B-Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2_5-78B-MPO">๐Ÿค— link</a></td> </tr> </table>

Multimodal Large Language Model (InternVL 2.0)

<table> <tr> <th>Model Name</th> <th>Vision Part</th> <th>Language Part</th> <th>HF&nbsp;Link</th> <th>MS&nbsp;Link</th> <th>Document</th> </tr> <tr> <td>InternVL2&#8209;1B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px">InternViT&#8209;300M&#8209;448px</a></td> <td><a href="https://huggingface.co/Qwen/Qwen2-0.5B-Instruct">Qwen2&#8209;0.5B&#8209;Instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2-1B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL2-1B">๐Ÿค– link</a></td> <td><a href="https://internvl.readthedocs.io/en/latest/internvl2.0/introduction.html">๐Ÿ“– doc</a></td> </tr> <tr> <td>InternVL2&#8209;2B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px">InternViT&#8209;300M&#8209;448px</a></td> <td><a href="https://huggingface.co/internlm/internlm2-chat-1_8b">internlm2&#8209;chat&#8209;1&#8209;8b</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2-2B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL2-2B">๐Ÿค– link</a></td> <td><a href="https://internvl.readthedocs.io/en/latest/internvl2.0/introduction.html">๐Ÿ“– doc</a></td> </tr> <tr> <td>InternVL2&#8209;4B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px">InternViT&#8209;300M&#8209;448px</a></td> <td><a href="https://huggingface.co/microsoft/Phi-3-mini-128k-instruct">Phi&#8209;3&#8209;mini&#8209;128k&#8209;instruct</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2-4B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL2-4B">๐Ÿค– link</a></td> <td><a href="https://internvl.readthedocs.io/en/latest/internvl2.0/introduction.html">๐Ÿ“– doc</a></td> </tr> <tr> <td>InternVL2&#8209;8B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px">InternViT&#8209;300M&#8209;448px</a></td> <td><a href="https://huggingface.co/internlm/internlm2_5-7b-chat">internlm2_5&#8209;7b&#8209;chat</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2-8B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL2-8B">๐Ÿค– link</a></td> <td><a href="https://internvl.readthedocs.io/en/latest/internvl2.0/introduction.html">๐Ÿ“– doc</a></td> </tr> <tr> <td>InternVL2&#8209;26B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-5">InternViT&#8209;6B&#8209;448px&#8209;V1&#8209;5</a></td> <td><a href="https://huggingface.co/internlm/internlm2-chat-20b">internlm2&#8209;chat&#8209;20b</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2-26B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL2-26B">๐Ÿค– link</a></td> <td><a href="https://internvl.readthedocs.io/en/latest/internvl2.0/introduction.html">๐Ÿ“– doc</a></td> </tr> <tr> <td>InternVL2&#8209;40B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-5">InternViT&#8209;6B&#8209;448px&#8209;V1&#8209;5</a></td> <td><a href="https://huggingface.co/NousResearch/Nous-Hermes-2-Yi-34B">Nous&#8209;Hermes&#8209;2&#8209;Yi&#8209;34B</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2-40B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL2-40B">๐Ÿค– link</a></td> <td><a href="https://internvl.readthedocs.io/en/latest/internvl2.0/introduction.html">๐Ÿ“– doc</a></td> </tr> <tr> <td>InternVL2-Llama3-76B</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-5">InternViT&#8209;6B&#8209;448px&#8209;V1&#8209;5</a></td> <td><a href="https://huggingface.co/NousResearch/Hermes-2-Theta-Llama-3-70B">Hermesโ€‘2โ€‘Thetaโ€‘<br>Llamaโ€‘3โ€‘70B</a></td> <td><a href="https://huggingface.co/OpenGVLab/InternVL2-Llama3-76B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL2-Llama3-76B">๐Ÿค– link</a></td> <td><a href="https://internvl.readthedocs.io/en/latest/internvl2.0/introduction.html">๐Ÿ“– doc</a></td> </tr> </table>

Multimodal Large Language Model (InternVL 1.0-1.5)

<table> <tr> <th>Model</th> <th>Date</th> <th>HF&nbsp;Link</th> <th>MS&nbsp;Link</th> <th>Note</th> </tr> <tr> <tr> <td>Mini&#8209;InternVL&#8209;Chat&#8209;4B&#8209;V1&#8209;5</td> <td>2024.05.28</td> <td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL-Chat-4B-V1-5">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/Mini-InternVL-Chat-4B-V1-5">๐Ÿค– link</a></td> <td>๐Ÿš€๐Ÿš€ 16% of the model size, 90% of the performance</td> </tr> <tr> <td>Mini&#8209;InternVL&#8209;Chat&#8209;2B&#8209;V1&#8209;5</td> <td>2024.05.19</td> <td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL-Chat-2B-V1-5">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/Mini-InternVL-Chat-2B-V1-5">๐Ÿค– link</a></td> <td>๐Ÿš€ 8% of the model size, 80% of the performance</td> </tr> <tr> <td>InternVL&#8209;Chat&#8209;V1&#8209;5</td> <td>2024.04.18</td> <td><a href="https://huggingface.co/OpenGVLab/InternVL-Chat-V1-5">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL-Chat-V1-5">๐Ÿค– link</a></td> <td>support 4K image; super strong OCR; Approaching the performance of GPT-4V and Gemini Pro on various benchmarks like MMMU, DocVQA, ChartQA, MathVista, etc.</td> </tr> <tr> <td>InternVL&#8209;Chat&#8209;V1&#8209;2&#8209;Plus</td> <td>2024.02.21</td> <td><a href="https://huggingface.co/OpenGVLab/InternVL-Chat-V1-2-Plus">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL-Chat-V1-2-Plus">๐Ÿค– link</a></td> <td>more SFT data and stronger</td> </tr> <tr> <td>InternVL&#8209;Chat&#8209;V1&#8209;2</td> <td>2024.02.11</td> <td><a href="https://huggingface.co/OpenGVLab/InternVL-Chat-V1-2">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL-Chat-V1-2">๐Ÿค– link</a></td> <td>scaling up LLM to 34B</td> </tr> <tr> <td>InternVL&#8209;Chat&#8209;V1&#8209;1</td> <td>2024.01.24</td> <td><a href="https://huggingface.co/OpenGVLab/InternVL-Chat-V1-1">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL-Chat-V1-1">๐Ÿค– link</a></td> <td>support Chinese and stronger OCR</td> </tr> <tr> <td>InternVL&#8209;Chat&#8209;19B</td> <td>2023.12.25</td> <td><a href="https://huggingface.co/OpenGVLab/InternVL-Chat-ViT-6B-Vicuna-13B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL-Chat-ViT-6B-Vicuna-13B">๐Ÿค– link</a></td> <td>English multimodal dialogue</td> </tr> <tr> <td>InternVL&#8209;Chat&#8209;13B</td> <td>2023.12.25</td> <td><a href="https://huggingface.co/OpenGVLab/InternVL-Chat-ViT-6B-Vicuna-7B">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL-Chat-ViT-6B-Vicuna-7B">๐Ÿค– link</a></td> <td>English multimodal dialogue</td> </tr> </table>

Vision Foundation Model (InternVL 1.0-2.5)

<table> <tr> <th>Model</th> <th>Date</th> <th>HF&nbsp;Link</th> <th>MS&nbsp;Link</th> <th>Note</th> </tr> <td>InternViT-300M-448px-V2_5</td> <td>2024.12.05</td> <td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL-Chat-4B-V1-5">๐Ÿค— link</a></td> <td></td> <td>๐Ÿš€๐Ÿš€ A more powerful lightweight visual encoder. (๐Ÿ”ฅnew)</td> </tr> <td>InternViT-6B-448px-V2_5</td> <td>2024.12.05</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V2_5">๐Ÿค— link</a></td> <td></td> <td>๐Ÿš€๐Ÿš€ A stronger visual encoder to extract visual features. (๐Ÿ”ฅnew)</td> </tr> <tr> <td>Mini&#8209;InternVL&#8209;Chat&#8209;4B&#8209;V1&#8209;5</td> <td>2024.05.28</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-300M-448px">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternViT-300M-448px">๐Ÿค– link</a></td> <td>distilled small vision foundation model with 300M parameters </td> </tr> <tr> <td>InternViT&#8209;6B&#8209;448px&#8209;V1&#8209;5</td> <td>2024.04.20</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-5">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternViT-6B-448px-V1-5">๐Ÿค– link</a></td> <td>support dynamic resolution and super strong OCR feature extraction capability by incremental pre-training</td> </tr> <tr> <td>InternViT&#8209;6B&#8209;448px&#8209;V1&#8209;2</td> <td>2024.02.11</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-2">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternViT-6B-448px-V1-2">๐Ÿค– link</a></td> <td>support 448 resolution by incremental pre-training</td> </tr> <tr> <td>InternViT&#8209;6B&#8209;448px&#8209;V1&#8209;0</td> <td>2024.01.30</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-0">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternViT-6B-448px-V1-0">๐Ÿค– link</a></td> <td>support 448 resolution by incremental pre-training</td> </tr> <tr> <td>InternViT&#8209;6B&#8209;224px</td> <td>2023.12.22</td> <td><a href="https://huggingface.co/OpenGVLab/InternViT-6B-224px">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternViT-6B-224px">๐Ÿค– link</a></td> <td>the first version of InternViT-6B, extracted from InternVLโ€‘14Bโ€‘224px</td> </tr> </table>

Vision-Language Foundation Model (InternVL 1.0)

<table> <tr> <th>Model</th> <th>Date</th> <th>HF&nbsp;Link</th> <th>MS&nbsp;Link</th> <th>Note</th> </tr> <tr> <td>InternVL&#8209;14B&#8209;224px</td> <td>2023.12.22</td> <td><a href="https://huggingface.co/OpenGVLab/InternVL-14B-224px">๐Ÿค— link</a></td> <td><a href="https://modelscope.cn/models/OpenGVLab/InternVL-14B-224px">๐Ÿค– link</a></td> <td>vision-language foundation model, InternViT-6B + QLLaMA, can be used for image-text retrieval like CLIP</td> </tr> </table>

What can InternVL do?

<details> <summary>Visual Perception (click to expand)</summary> </details> <details> <summary>Cross-Modal Retrieval (click to expand)</summary> </details> <details> <summary>Multimodal Dialogue</summary>

See "Compared with SOTA VLLMs" section.

</details>

Quick Start with HuggingFace

<details> <summary>using InternViT-6B for visual feature extraction (click to expand)</summary>
import torch
from PIL import Image
from transformers import AutoModel, CLIPImageProcessor

model = AutoModel.from_pretrained(
    'OpenGVLab/InternViT-6B-448px-V1-5',
    torch_dtype=torch.bfloat16,
    low_cpu_mem_usage=True,
    trust_remote_code=True).cuda().eval()

image = Image.open('./examples/image1.jpg').convert('RGB')

image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternViT-6B-448px-V1-5')

pixel_values = image_processor(images=image, return_tensors='pt').pixel_values
pixel_values = pixel_values.to(torch.bfloat16).cuda()

outputs = model(pixel_values)
</details> <details> <summary>using InternVL-C(ontrastive) and InternVL-G(enerative) for cross-modal retrieval (click to expand)</summary>
import torch
from PIL import Image
from transformers import AutoModel, CLIPImageProcessor
from transformers import AutoTokenizer


model = AutoModel.from_pretrained(
    'OpenGVLab/InternVL-14B-224px',
    torch_dtype=torch.bfloat16,
    low_cpu_mem_usage=True,
    trust_remote_code=True).cuda().eval()

image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternVL-14B-224px')

tokenizer = AutoTokenizer.from_pretrained(
    'OpenGVLab/InternVL-14B-224px', use_fast=False, add_eos_token=True)
tokenizer.pad_token_id = 0  # set pad_token_id to 0

images = [
    Image.open('./examples/image1.jpg').convert('RGB'),
    Image.open('./examples/image2.jpg').convert('RGB'),
    Image.open('./examples/image3.jpg').convert('RGB')
]
prefix = 'summarize:'
texts = [
    prefix + 'a photo of a red panda',  # English
    prefix + 'ไธ€ๅผ ็†Š็Œซ็š„็…ง็‰‡',  # Chinese
    prefix + 'ไบŒๅŒนใฎ็Œซใฎๅ†™็œŸ'  # Japanese
]

pixel_values = image_processor(images=images, return_tensors='pt').pixel_values
pixel_values = pixel_values.to(torch.bfloat16).cuda()
input_ids = tokenizer(texts, return_tensors='pt', max_length=80,
                      truncation=True, padding='max_length').input_ids.cuda()

# InternVL-C
logits_per_image, logits_per_text = model(
    image=pixel_values, text=input_ids, mode='InternVL-C')
probs = logits_per_image.softmax(dim=-1)
# tensor([[9.9609e-01, 5.2185e-03, 6.0070e-08],
#         [2.2949e-02, 9.7656e-01, 5.9903e-06],
#         [3.2932e-06, 7.4863e-05, 1.0000e+00]], device='cuda:0',
#        dtype=torch.bfloat16, grad_fn=<SoftmaxBackward0>)

# InternVL-G
logits_per_image, logits_per_text = model(
    image=pixel_values, text=input_ids, mode='InternVL-G')
probs = logits_per_image.softmax(dim=-1)
# tensor([[9.9609e-01, 3.1738e-03, 3.6322e-08],
#         [8.6060e-03, 9.9219e-01, 2.8759e-06],
#         [1.7583e-06, 3.1233e-05, 1.0000e+00]], device='cuda:0',
#        dtype=torch.bfloat16, grad_fn=<SoftmaxBackward0>)

# please set add_eos_token to False for generation
tokenizer.add_eos_token = False
image = Image.open('./examples/image1.jpg').convert('RGB')
pixel_values = image_processor(images=image, return_tensors='pt').pixel_values
pixel_values = pixel_values.to(torch.bfloat16).cuda()

tokenized = tokenizer("English caption:", return_tensors='pt')
pred = model.generate(
    pixel_values=pixel_values,
    input_ids=tokenized.input_ids.cuda(),
    attention_mask=tokenized.attention_mask.cuda(),
    num_beams=5,
    min_new_tokens=8,
)
caption = tokenizer.decode(pred[0].cpu(), skip_special_tokens=True).strip()
# English caption: a red panda sitting on top of a wooden platform
</details> <details> <summary>using InternVL-Chat for multimodal chat (click to expand)</summary>

Here, we take the smaller OpenGVLab/InternVL2-8B as an example:

import numpy as np
import torch
import torchvision.transforms as T
from decord import VideoReader, cpu
from PIL import Image
from torchvision.transforms.functional import InterpolationMode
from transformers import AutoModel, AutoTokenizer

IMAGENET_MEAN = (0.485, 0.456, 0.406)
IMAGENET_STD = (0.229, 0.224, 0.225)

def build_transform(input_size):
    MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
    transform = T.Compose([
        T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
        T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
        T.ToTensor(),
        T.Normalize(mean=MEAN, std=STD)
    ])
    return transform

def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
    best_ratio_diff = float('inf')
    best_ratio = (1, 1)
    area = width * height
    for ratio in target_ratios:
        target_aspect_ratio = ratio[0] / ratio[1]
        ratio_diff = abs(aspect_ratio - target_aspect_ratio)
        if ratio_diff < best_ratio_diff:
            best_ratio_diff = ratio_diff
            best_ratio = ratio
        elif ratio_diff == best_ratio_diff:
            if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
                best_ratio = ratio
    return best_ratio

def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
    orig_width, orig_height = image.size
    aspect_ratio = orig_width / orig_height

    # calculate the existing image aspect ratio
    target_ratios = set(
        (i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
        i * j <= max_num and i * j >= min_num)
    target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])

    # find the closest aspect ratio to the target
    target_aspect_ratio = find_closest_aspect_ratio(
        aspect_ratio, target_ratios, orig_width, orig_height, image_size)

    # calculate the target width and height
    target_width = image_size * target_aspect_ratio[0]
    target_height = image_size * target_aspect_ratio[1]
    blocks = target_aspect_ratio[0] * target_aspect_ratio[1]

    # resize the image
    resized_img = image.resize((target_width, target_height))
    processed_images = []
    for i in range(blocks):
        box = (
            (i % (target_width // image_size)) * image_size,
            (i // (target_width // image_size)) * image_size,
            ((i % (target_width // image_size)) + 1) * image_size,
            ((i // (target_width // image_size)) + 1) * image_size
        )
        # split the image
        split_img = resized_img.crop(box)
        processed_images.append(split_img)
    assert len(processed_images) == blocks
    if use_thumbnail and len(processed_images) != 1:
        thumbnail_img = image.resize((image_size, image_size))
        processed_images.append(thumbnail_img)
    return processed_images

def load_image(image_file, input_size=448, max_num=12):
    image = Image.open(image_file).convert('RGB')
    transform = build_transform(input_size=input_size)
    images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
    pixel_values = [transform(image) for image in images]
    pixel_values = torch.stack(pixel_values)
    return pixel_values

# If you have an 80G A100 GPU, you can put the entire model on a single GPU.
# Otherwise, you need to load a model using multiple GPUs, please refer to the `Multiple GPUs` section.
path = 'OpenGVLab/InternVL2-8B'
model = AutoModel.from_pretrained(
    path,
    torch_dtype=torch.bfloat16,
    low_cpu_mem_usage=True,
    trust_remote_code=True).eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)

# set the max number of tiles in `max_num`
pixel_values = load_image('./examples/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
generation_config = dict(max_new_tokens=1024, do_sample=False)

# pure-text conversation (็บฏๆ–‡ๆœฌๅฏน่ฏ)
question = 'Hello, who are you?'
response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
print(f'User: {question}\nAssistant: {response}')

question = 'Can you tell me a story?'
response, history = model.chat(tokenizer, None, question, generation_config, history=history, return_history=True)
print(f'User: {question}\nAssistant: {response}')

# single-image single-round conversation (ๅ•ๅ›พๅ•่ฝฎๅฏน่ฏ)
question = '<image>\nPlease describe the image shortly.'
response = model.chat(tokenizer, pixel_values, question, generation_config)
print(f'User: {question}\nAssistant: {response}')

# single-image multi-round conversation (ๅ•ๅ›พๅคš่ฝฎๅฏน่ฏ)
question = '<image>\nPlease describe the image in detail.'
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
print(f'User: {question}\nAssistant: {response}')

question = 'Please write a poem according to the image.'
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
print(f'User: {question}\nAssistant: {response}')

# multi-image multi-round conversation, combined images (ๅคšๅ›พๅคš่ฝฎๅฏน่ฏ๏ผŒๆ‹ผๆŽฅๅ›พๅƒ)
pixel_values1 = load_image('./examples/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
pixel_values2 = load_image('./examples/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)

question = '<image>\nDescribe the two images in detail.'
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
                               history=None, return_history=True)
print(f'User: {question}\nAssistant: {response}')

question = 'What are the similarities and differences between these two images.'
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
                               history=history, return_history=True)
print(f'User: {question}\nAssistant: {response}')

# multi-image multi-round conversation, separate images (ๅคšๅ›พๅคš่ฝฎๅฏน่ฏ๏ผŒ็‹ฌ็ซ‹ๅ›พๅƒ)
pixel_values1 = load_image('./examples/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
pixel_values2 = load_image('./examples/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]

question = 'Image-1: <image>\nImage-2: <image>\nDescribe the two images in detail.'
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
                               num_patches_list=num_patches_list,
                               history=None, return_history=True)
print(f'User: {question}\nAssistant: {response}')

question = 'What are the similarities and differences between these two images.'
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
                               num_patches_list=num_patches_list,
                               history=history, return_history=True)
print(f'User: {question}\nAssistant: {response}')

# batch inference, single image per sample (ๅ•ๅ›พๆ‰นๅค„็†)
pixel_values1 = load_image('./examples/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
pixel_values2 = load_image('./examples/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)

questions = ['<image>\nDescribe the image in detail.'] * len(num_patches_list)
responses = model.batch_chat(tokenizer, pixel_values,
                             num_patches_list=num_patches_list,
                             questions=questions,
                             generation_config=generation_config)
for question, response in zip(questions, responses):
    print(f'User: {question}\nAssistant: {response}')

# video multi-round conversation (่ง†้ข‘ๅคš่ฝฎๅฏน่ฏ)
def get_index(bound, fps, max_frame, first_idx=0, num_segments=32):
    if bound:
        start, end = bound[0], bound[1]
    else:
        start, end = -100000, 100000
    start_idx = max(first_idx, round(start * fps))
    end_idx = min(round(end * fps), max_frame)
    seg_size = float(end_idx - start_idx) / num_segments
    frame_indices = np.array([
        int(start_idx + (seg_size / 2) + np.round(seg_size * idx))
        for idx in range(num_segments)
    ])
    return frame_indices

def load_video(video_path, bound=None, input_size=448, max_num=1, num_segments=32):
    vr = VideoReader(video_path, ctx=cpu(0), num_threads=1)
    max_frame = len(vr) - 1
    fps = float(vr.get_avg_fps())

    pixel_values_list, num_patches_list = [], []
    transform = build_transform(input_size=input_size)
    frame_indices = get_index(bound, fps, max_frame, first_idx=0, num_segments=num_segments)
    for frame_index in frame_indices:
        img = Image.fromarray(vr[frame_index].asnumpy()).convert('RGB')
        img = dynamic_preprocess(img, image_size=input_size, use_thumbnail=True, max_num=max_num)
        pixel_values = [transform(tile) for tile in img]
        pixel_values = torch.stack(pixel_values)
        num_patches_list.append(pixel_values.shape[0])
        pixel_values_list.append(pixel_values)
    pixel_values = torch.cat(pixel_values_list)
    return pixel_values, num_patches_list

video_path = './examples/red-panda.mp4'
pixel_values, num_patches_list = load_video(video_path, num_segments=8, max_num=1)
pixel_values = pixel_values.to(torch.bfloat16).cuda()
video_prefix = ''.join([f'Frame{i+1}: <image>\n' for i in range(len(num_patches_list))])
question = video_prefix + 'What is the red panda doing?'
# Frame1: <image>\nFrame2: <image>\n...\nFrame8: <image>\n{question}
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
                               num_patches_list=num_patches_list, history=None, return_history=True)
print(f'User: {question}\nAssistant: {response}')

question = 'Describe this video in detail. Don\'t repeat.'
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
                               num_patches_list=num_patches_list, history=history, return_history=True)
print(f'User: {question}\nAssistant: {response}')
</details>

License

This project is released under the MIT license. Parts of this project contain code and models from other sources, which are subject to their respective licenses.

Citation

If you find this project useful in your research, please consider cite:

@article{chen2024expanding,
  title={Expanding Performance Boundaries of Open-Source Multimodal Models with Model, Data, and Test-Time Scaling},
  author={Chen, Zhe and Wang, Weiyun and Cao, Yue and Liu, Yangzhou and Gao, Zhangwei and Cui, Erfei and Zhu, Jinguo and Ye, Shenglong and Tian, Hao and Liu, Zhaoyang and others},
  journal={arXiv preprint arXiv:2412.05271},
  year={2024}
}
@article{wang2024mpo,
  title={Enhancing the Reasoning Ability of Multimodal Large Language Models via Mixed Preference Optimization},
  author={Wang, Weiyun and Chen, Zhe and Wang, Wenhai and Cao, Yue and Liu, Yangzhou and Gao, Zhangwei and Zhu, Jinguo and Zhu, Xizhou and Lu, Lewei and Qiao, Yu and Dai, Jifeng},
  journal={arXiv preprint arXiv:2411.10442},
  year={2024}
}
@article{gao2024mini,
  title={Mini-internvl: A flexible-transfer pocket multimodal model with 5\% parameters and 90\% performance},
  author={Gao, Zhangwei and Chen, Zhe and Cui, Erfei and Ren, Yiming and Wang, Weiyun and Zhu, Jinguo and Tian, Hao and Ye, Shenglong and He, Junjun and Zhu, Xizhou and others},
  journal={arXiv preprint arXiv:2410.16261},
  year={2024}
}
@article{chen2024far,
  title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
  author={Chen, Zhe and Wang, Weiyun and Tian, Hao and Ye, Shenglong and Gao, Zhangwei and Cui, Erfei and Tong, Wenwen and Hu, Kongzhi and Luo, Jiapeng and Ma, Zheng and others},
  journal={arXiv preprint arXiv:2404.16821},
  year={2024}
}
@inproceedings{chen2024internvl,
  title={Internvl: Scaling up vision foundation models and aligning for generic visual-linguistic tasks},
  author={Chen, Zhe and Wu, Jiannan and Wang, Wenhai and Su, Weijie and Chen, Guo and Xing, Sen and Zhong, Muyan and Zhang, Qinglong and Zhu, Xizhou and Lu, Lewei and others},
  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  pages={24185--24198},
  year={2024}
}

Acknowledgement

InternVL is built with reference to the code of the following projects: OpenAI CLIP, Open CLIP, CLIP Benchmark, EVA, InternImage, ViT-Adapter, MMSegmentation, Transformers, DINOv2, BLIP-2, Qwen-VL, and LLaVA-1.5. Thanks for their awesome work!


Scan the following QR Code, join our WeChat group.

<p align="center"><img width="300" alt="image" src="https://github.com/user-attachments/assets/f776df09-ebba-4fd5-80c2-fec4ff1518be"></p>