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+
license: mit
|
3 |
+
datasets:
|
4 |
+
- laion/laion2B-en
|
5 |
+
- laion/laion-coco
|
6 |
+
- laion/laion2B-multi
|
7 |
+
- kakaobrain/coyo-700m
|
8 |
+
- conceptual_captions
|
9 |
+
- wanng/wukong100m
|
10 |
+
pipeline_tag: visual-question-answering
|
11 |
+
---
|
12 |
+
|
13 |
+
# Model Card for Mini-InternVL-Chat-V1.5
|
14 |
+
<p align="center">
|
15 |
+
<img src="https://cdn-uploads.huggingface.co/production/uploads/64119264f0f81eb569e0d569/D60YzQBIzvoCvLRp2gZ0A.jpeg" alt="Image Description" width="300" height="300" />
|
16 |
+
</p>
|
17 |
+
|
18 |
+
> _Two interns holding hands, symbolizing the integration of InternViT and InternLM._
|
19 |
+
|
20 |
+
\[[InternVL 1.5 Technical Report](https://arxiv.org/abs/2404.16821)\] \[[CVPR Paper](https://arxiv.org/abs/2312.14238)\] \[[GitHub](https://github.com/OpenGVLab/InternVL)\] \[[Chat Demo](https://internvl.opengvlab.com/)\] \[[中文解读](https://zhuanlan.zhihu.com/p/675877376)]
|
21 |
+
|
22 |
+
We introduce InternVL 1.5, an open-source multimodal large language model (MLLM) to bridge the capability gap between open-source and proprietary commercial models in multimodal understanding.
|
23 |
+
We introduce three simple designs:
|
24 |
+
1. Strong Vision Encoder: we explored a continuous learning strategy for the large-scale vision foundation model---InternViT-6B, boosting its visual understanding capabilities, and making it can be transferred and reused in different LLMs.
|
25 |
+
2. Dynamic High-Resolution: we divide images into tiles ranging from 1 to 40 of 448 × 448 pixels according to the aspect ratio and resolution of the input images, which supports up to 4K resolution input.
|
26 |
+
3. High-Quality Bilingual Dataset: we carefully collected a high-quality bilingual dataset that covers common scenes, document images, and annotated them with English and Chinese question-answer pairs, significantly enhancing performance in OCR- and Chinese-related tasks.
|
27 |
+
|
28 |
+
|
29 |
+
## Model Details
|
30 |
+
- **Model Type:** multimodal large language model (MLLM)
|
31 |
+
- **Model Stats:**
|
32 |
+
- Architecture: InternViT-300M-448px + MLP + [InternLM2-Chat-1.8B](https://huggingface.co/internlm/internlm2-chat-1_8b)
|
33 |
+
- Image size: dynamic resolution, max to 40 tiles of 448 x 448 (4K resolution).
|
34 |
+
- Params: 25.5B
|
35 |
+
|
36 |
+
- **Training Strategy:**
|
37 |
+
- Learnable component in the pretraining stage: ViT + MLP
|
38 |
+
- Learnable component in the finetuning stage: ViT + MLP + LLM
|
39 |
+
- For more details on training hyperparameters, take a look at our code: [pretrain]() | [finetune]()
|
40 |
+
|
41 |
+
## Released Models
|
42 |
+
|
43 |
+
| Model | Vision Foundation Model | Release Date |Note |
|
44 |
+
| :---------------------------------------------------------:|:--------------------------------------------------------------------------: |:----------------------:| :---------------------------------- |
|
45 |
+
| InternVL-Chat-V1.5(🤗 [HF link](https://huggingface.co/OpenGVLab/InternVL-Chat-V1-5)) | InternViT-6B-448px-V1-5(🤗 [HF link](https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-5)) |2024.04.18 | support 4K image; super strong OCR; Approaching the performance of GPT-4V and Gemini Pro on various benchmarks like MMMU, DocVQA, ChartQA, MathVista, etc. (🔥new)|
|
46 |
+
| InternVL-Chat-V1.2-Plus(🤗 [HF link](https://huggingface.co/OpenGVLab/InternVL-Chat-V1-2-Plus) ) |InternViT-6B-448px-V1-2(🤗 [HF link](https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-2)) |2024.02.21 | more SFT data and stronger |
|
47 |
+
| InternVL-Chat-V1.2(🤗 [HF link](https://huggingface.co/OpenGVLab/InternVL-Chat-V1-2) ) |InternViT-6B-448px-V1-2(🤗 [HF link](https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-2)) |2024.02.11 | scaling up LLM to 34B |
|
48 |
+
| InternVL-Chat-V1.1(🤗 [HF link](https://huggingface.co/OpenGVLab/InternVL-Chat-V1-1)) |InternViT-6B-448px-V1-0(🤗 [HF link](https://huggingface.co/OpenGVLab/InternViT-6B-448px-V1-0)) |2024.01.24 | support Chinese and stronger OCR |
|
49 |
+
|
50 |
+
## Architecture
|
51 |
+
|
52 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/64119264f0f81eb569e0d569/YLvX3V-L0kwsyRn3Lhciw.png)
|
53 |
+
|
54 |
+
## Performance
|
55 |
+
|
56 |
+
TODO
|
57 |
+
|
58 |
+
## Examples
|
59 |
+
|
60 |
+
TODO
|
61 |
+
|
62 |
+
## Model Usage
|
63 |
+
|
64 |
+
We provide an example code to run Mini-InternVL-Chat-V1.5 using `transformers`.
|
65 |
+
|
66 |
+
You also can use our [online demo](https://internvl.opengvlab.com/) for a quick experience of this model.
|
67 |
+
|
68 |
+
> Please use transformers==4.37.2 to ensure the model works normally.
|
69 |
+
|
70 |
+
```python
|
71 |
+
from transformers import AutoTokenizer, AutoModel
|
72 |
+
import torch
|
73 |
+
import torchvision.transforms as T
|
74 |
+
from PIL import Image
|
75 |
+
|
76 |
+
from torchvision.transforms.functional import InterpolationMode
|
77 |
+
|
78 |
+
|
79 |
+
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
80 |
+
IMAGENET_STD = (0.229, 0.224, 0.225)
|
81 |
+
|
82 |
+
|
83 |
+
def build_transform(input_size):
|
84 |
+
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
|
85 |
+
transform = T.Compose([
|
86 |
+
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
87 |
+
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
|
88 |
+
T.ToTensor(),
|
89 |
+
T.Normalize(mean=MEAN, std=STD)
|
90 |
+
])
|
91 |
+
return transform
|
92 |
+
|
93 |
+
|
94 |
+
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
95 |
+
best_ratio_diff = float('inf')
|
96 |
+
best_ratio = (1, 1)
|
97 |
+
area = width * height
|
98 |
+
for ratio in target_ratios:
|
99 |
+
target_aspect_ratio = ratio[0] / ratio[1]
|
100 |
+
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
|
101 |
+
if ratio_diff < best_ratio_diff:
|
102 |
+
best_ratio_diff = ratio_diff
|
103 |
+
best_ratio = ratio
|
104 |
+
elif ratio_diff == best_ratio_diff:
|
105 |
+
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
|
106 |
+
best_ratio = ratio
|
107 |
+
return best_ratio
|
108 |
+
|
109 |
+
|
110 |
+
def dynamic_preprocess(image, min_num=1, max_num=6, image_size=448, use_thumbnail=False):
|
111 |
+
orig_width, orig_height = image.size
|
112 |
+
aspect_ratio = orig_width / orig_height
|
113 |
+
|
114 |
+
# calculate the existing image aspect ratio
|
115 |
+
target_ratios = set(
|
116 |
+
(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
|
117 |
+
i * j <= max_num and i * j >= min_num)
|
118 |
+
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
119 |
+
|
120 |
+
# find the closest aspect ratio to the target
|
121 |
+
target_aspect_ratio = find_closest_aspect_ratio(
|
122 |
+
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
|
123 |
+
|
124 |
+
# calculate the target width and height
|
125 |
+
target_width = image_size * target_aspect_ratio[0]
|
126 |
+
target_height = image_size * target_aspect_ratio[1]
|
127 |
+
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
128 |
+
|
129 |
+
# resize the image
|
130 |
+
resized_img = image.resize((target_width, target_height))
|
131 |
+
processed_images = []
|
132 |
+
for i in range(blocks):
|
133 |
+
box = (
|
134 |
+
(i % (target_width // image_size)) * image_size,
|
135 |
+
(i // (target_width // image_size)) * image_size,
|
136 |
+
((i % (target_width // image_size)) + 1) * image_size,
|
137 |
+
((i // (target_width // image_size)) + 1) * image_size
|
138 |
+
)
|
139 |
+
# split the image
|
140 |
+
split_img = resized_img.crop(box)
|
141 |
+
processed_images.append(split_img)
|
142 |
+
assert len(processed_images) == blocks
|
143 |
+
if use_thumbnail and len(processed_images) != 1:
|
144 |
+
thumbnail_img = image.resize((image_size, image_size))
|
145 |
+
processed_images.append(thumbnail_img)
|
146 |
+
return processed_images
|
147 |
+
|
148 |
+
|
149 |
+
def load_image(image_file, input_size=448, max_num=6):
|
150 |
+
image = Image.open(image_file).convert('RGB')
|
151 |
+
transform = build_transform(input_size=input_size)
|
152 |
+
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
|
153 |
+
pixel_values = [transform(image) for image in images]
|
154 |
+
pixel_values = torch.stack(pixel_values)
|
155 |
+
return pixel_values
|
156 |
+
|
157 |
+
|
158 |
+
path = "OpenGVLab/Mini-InternVL-Chat-V1-5"
|
159 |
+
model = AutoModel.from_pretrained(
|
160 |
+
path,
|
161 |
+
torch_dtype=torch.bfloat16,
|
162 |
+
low_cpu_mem_usage=True,
|
163 |
+
trust_remote_code=True).eval().cuda()
|
164 |
+
|
165 |
+
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
166 |
+
# set the max number of tiles in `max_num`
|
167 |
+
pixel_values = load_image('./examples/image1.jpg', max_num=6).to(torch.bfloat16).cuda()
|
168 |
+
|
169 |
+
generation_config = dict(
|
170 |
+
num_beams=1,
|
171 |
+
max_new_tokens=512,
|
172 |
+
do_sample=False,
|
173 |
+
)
|
174 |
+
|
175 |
+
# single-round single-image conversation
|
176 |
+
question = "请详细描述图片" # Please describe the picture in detail
|
177 |
+
response = model.chat(tokenizer, pixel_values, question, generation_config)
|
178 |
+
print(question, response)
|
179 |
+
|
180 |
+
# multi-round single-image conversation
|
181 |
+
question = "请详细描述图片" # Please describe the picture in detail
|
182 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
|
183 |
+
print(question, response)
|
184 |
+
|
185 |
+
question = "请根据图片写一首诗" # Please write a poem according to the picture
|
186 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
|
187 |
+
print(question, response)
|
188 |
+
|
189 |
+
# multi-round multi-image conversation
|
190 |
+
pixel_values1 = load_image('./examples/image1.jpg', max_num=6).to(torch.bfloat16).cuda()
|
191 |
+
pixel_values2 = load_image('./examples/image2.jpg', max_num=6).to(torch.bfloat16).cuda()
|
192 |
+
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
|
193 |
+
|
194 |
+
question = "详细描述这两张图片" # Describe the two pictures in detail
|
195 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
|
196 |
+
print(question, response)
|
197 |
+
|
198 |
+
question = "这两张图片的相同点和区别分别是什么" # What are the similarities and differences between these two pictures
|
199 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
|
200 |
+
print(question, response)
|
201 |
+
|
202 |
+
# batch inference (single image per sample)
|
203 |
+
pixel_values1 = load_image('./examples/image1.jpg', max_num=6).to(torch.bfloat16).cuda()
|
204 |
+
pixel_values2 = load_image('./examples/image2.jpg', max_num=6).to(torch.bfloat16).cuda()
|
205 |
+
image_counts = [pixel_values1.size(0), pixel_values2.size(0)]
|
206 |
+
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
|
207 |
+
|
208 |
+
questions = ["Describe the image in detail."] * len(image_counts)
|
209 |
+
responses = model.batch_chat(tokenizer, pixel_values,
|
210 |
+
image_counts=image_counts,
|
211 |
+
questions=questions,
|
212 |
+
generation_config=generation_config)
|
213 |
+
for question, response in zip(questions, responses):
|
214 |
+
print(question)
|
215 |
+
print(response)
|
216 |
+
```
|
217 |
+
|
218 |
+
## Citation
|
219 |
+
|
220 |
+
If you find this project useful in your research, please consider citing:
|
221 |
+
|
222 |
+
```BibTeX
|
223 |
+
@article{chen2023internvl,
|
224 |
+
title={InternVL: Scaling up Vision Foundation Models and Aligning for Generic Visual-Linguistic Tasks},
|
225 |
+
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 Li, Bin and Luo, Ping and Lu, Tong and Qiao, Yu and Dai, Jifeng},
|
226 |
+
journal={arXiv preprint arXiv:2312.14238},
|
227 |
+
year={2023}
|
228 |
+
}
|
229 |
+
```
|
230 |
+
|
231 |
+
## License
|
232 |
+
|
233 |
+
This project is released under the MIT license.
|
234 |
+
|
235 |
+
## Acknowledgement
|
236 |
+
|
237 |
+
InternVL is built with reference to the code of the following projects: [OpenAI CLIP](https://github.com/openai/CLIP), [Open CLIP](https://github.com/mlfoundations/open_clip), [CLIP Benchmark](https://github.com/LAION-AI/CLIP_benchmark), [EVA](https://github.com/baaivision/EVA/tree/master), [InternImage](https://github.com/OpenGVLab/InternImage), [ViT-Adapter](https://github.com/czczup/ViT-Adapter), [MMSegmentation](https://github.com/open-mmlab/mmsegmentation), [Transformers](https://github.com/huggingface/transformers), [DINOv2](https://github.com/facebookresearch/dinov2), [BLIP-2](https://github.com/salesforce/LAVIS/tree/main/projects/blip2), [Qwen-VL](https://github.com/QwenLM/Qwen-VL/tree/master/eval_mm), and [LLaVA-1.5](https://github.com/haotian-liu/LLaVA). Thanks for their awesome work!
|