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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
pipeline_tag: image-text-to-text
|
| 4 |
+
library_name: transformers
|
| 5 |
+
base_model:
|
| 6 |
+
- OpenGVLab/InternVL2-4B
|
| 7 |
+
base_model_relation: merge
|
| 8 |
+
language:
|
| 9 |
+
- multilingual
|
| 10 |
+
tags:
|
| 11 |
+
- internvl
|
| 12 |
+
- custom_code
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# Mini-InternVL
|
| 16 |
+
|
| 17 |
+
[\[📂 GitHub\]](https://github.com/OpenGVLab/InternVL) [\[🆕 Blog\]](https://internvl.github.io/blog/) [\[📜 Mini-InternVL\]](https://arxiv.org/abs/2410.16261) [\[📜 InternVL 1.0\]](https://arxiv.org/abs/2312.14238) [\[📜 InternVL 1.5\]](https://arxiv.org/abs/2404.16821) [\[📜 InternVL 2.5\]](https://huggingface.co/papers/2412.05271)
|
| 18 |
+
|
| 19 |
+
[\[🗨️ InternVL Chat Demo\]](https://internvl.opengvlab.com/) [\[🤗 HF Demo\]](https://huggingface.co/spaces/OpenGVLab/InternVL) [\[🚀 Quick Start\]](#quick-start) [\[📖 中文解读\]](https://zhuanlan.zhihu.com/p/706547971) [\[📖 Documents\]](https://internvl.readthedocs.io/en/latest/internvl2.0/domain_adaptation.html#data-preparation)
|
| 20 |
+
|
| 21 |
+

|
| 22 |
+
|
| 23 |
+
## Introduction
|
| 24 |
+
|
| 25 |
+
We release the adaptation models for the specific domains: autonomous driving, medical images, and remote sensing.
|
| 26 |
+
|
| 27 |
+
These models are built upon Mini-InternVL and fine-tuned using a unified adaptation framework, achieving good performance on tasks in specific domains.
|
| 28 |
+
|
| 29 |
+

|
| 30 |
+
|
| 31 |
+
<table>
|
| 32 |
+
<tr>
|
| 33 |
+
<th>Model Name</th>
|
| 34 |
+
<th>HF Link</th>
|
| 35 |
+
<th>Note</th>
|
| 36 |
+
</tr>
|
| 37 |
+
<tr>
|
| 38 |
+
<td>Mini-InternVL2-DA-Drivelm</td>
|
| 39 |
+
<td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-1B-DA-Drivelm">🤗1B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-2B-DA-Drivelm">🤗2B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-4B-DA-Drivelm">🤗4B</a></td>
|
| 40 |
+
<td> Adaptation for <a href="https://github.com/OpenDriveLab/DriveLM/tree/main/challenge"> CVPR 2024 Autonomous Driving Challenge </a></td>
|
| 41 |
+
</tr>
|
| 42 |
+
<tr>
|
| 43 |
+
<td>Mini-InternVL2-DA-BDD</td>
|
| 44 |
+
<td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-1B-DA-BDD">🤗1B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-2B-DA-BDD">🤗2B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-4B-DA-BDD">🤗4B</a></td>
|
| 45 |
+
<td> Fine-tuning with data constructed by <a href="https://tonyxuqaq.github.io/projects/DriveGPT4/"> DriveGPT4 </a></td>
|
| 46 |
+
</tr>
|
| 47 |
+
<tr>
|
| 48 |
+
<td>Mini-InternVL2-DA-RS</td>
|
| 49 |
+
<td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-1B-DA-RS">🤗1B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-2B-DA-RS">🤗2B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-4B-DA-RS">🤗4B</a></td>
|
| 50 |
+
<td> Adaptation for remote sensing domain </td>
|
| 51 |
+
</tr>
|
| 52 |
+
<tr>
|
| 53 |
+
<td>Mini-InternVL2-DA-Medical</td>
|
| 54 |
+
<td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-1B-DA-Medical">🤗1B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-2B-DA-Medical">🤗2B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-4B-DA-Medical">🤗4B</a></td>
|
| 55 |
+
<td> Fine-tuning using our <a href="https://huggingface.co/datasets/OpenGVLab/InternVL-Domain-Adaptation-Data/blob/main/train_meta/internvl_1_2_finetune_medical.json">medical data</a>.</td>
|
| 56 |
+
</tr>
|
| 57 |
+
</table>
|
| 58 |
+
|
| 59 |
+
The script for evaluation is in the [document](https://internvl.readthedocs.io/en/latest/internvl2.0/domain_adaptation.html#id3).
|
| 60 |
+
|
| 61 |
+
## Training datasets
|
| 62 |
+
|
| 63 |
+
- General domain dataset:
|
| 64 |
+
|
| 65 |
+
ShareGPT4V, AllSeeingV2, LLaVA-Instruct-ZH, DVQA, ChartQA, AI2D, DocVQA, GeoQA+, SynthDoG-EN
|
| 66 |
+
|
| 67 |
+
- Autonomous driving dataset:
|
| 68 |
+
|
| 69 |
+
[DriveGPT4](https://tonyxuqaq.github.io/projects/DriveGPT4/).
|
| 70 |
+
|
| 71 |
+
## Quick Start
|
| 72 |
+
|
| 73 |
+
We provide an example code to run `Mini-InternVL2-4B` using `transformers`.
|
| 74 |
+
|
| 75 |
+
> Please use transformers>=4.37.2 to ensure the model works normally.
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
import numpy as np
|
| 80 |
+
import torch
|
| 81 |
+
import torchvision.transforms as T
|
| 82 |
+
from decord import VideoReader, cpu
|
| 83 |
+
from PIL import Image
|
| 84 |
+
from torchvision.transforms.functional import InterpolationMode
|
| 85 |
+
from transformers import AutoModel, AutoTokenizer
|
| 86 |
+
|
| 87 |
+
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
| 88 |
+
IMAGENET_STD = (0.229, 0.224, 0.225)
|
| 89 |
+
|
| 90 |
+
def build_transform(input_size):
|
| 91 |
+
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
|
| 92 |
+
transform = T.Compose([
|
| 93 |
+
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
| 94 |
+
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
|
| 95 |
+
T.ToTensor(),
|
| 96 |
+
T.Normalize(mean=MEAN, std=STD)
|
| 97 |
+
])
|
| 98 |
+
return transform
|
| 99 |
+
|
| 100 |
+
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
| 101 |
+
best_ratio_diff = float('inf')
|
| 102 |
+
best_ratio = (1, 1)
|
| 103 |
+
area = width * height
|
| 104 |
+
for ratio in target_ratios:
|
| 105 |
+
target_aspect_ratio = ratio[0] / ratio[1]
|
| 106 |
+
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
|
| 107 |
+
if ratio_diff < best_ratio_diff:
|
| 108 |
+
best_ratio_diff = ratio_diff
|
| 109 |
+
best_ratio = ratio
|
| 110 |
+
elif ratio_diff == best_ratio_diff:
|
| 111 |
+
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
|
| 112 |
+
best_ratio = ratio
|
| 113 |
+
return best_ratio
|
| 114 |
+
|
| 115 |
+
def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
|
| 116 |
+
orig_width, orig_height = image.size
|
| 117 |
+
aspect_ratio = orig_width / orig_height
|
| 118 |
+
|
| 119 |
+
# calculate the existing image aspect ratio
|
| 120 |
+
target_ratios = set(
|
| 121 |
+
(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
|
| 122 |
+
i * j <= max_num and i * j >= min_num)
|
| 123 |
+
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
| 124 |
+
|
| 125 |
+
# find the closest aspect ratio to the target
|
| 126 |
+
target_aspect_ratio = find_closest_aspect_ratio(
|
| 127 |
+
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
|
| 128 |
+
|
| 129 |
+
# calculate the target width and height
|
| 130 |
+
target_width = image_size * target_aspect_ratio[0]
|
| 131 |
+
target_height = image_size * target_aspect_ratio[1]
|
| 132 |
+
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
| 133 |
+
|
| 134 |
+
# resize the image
|
| 135 |
+
resized_img = image.resize((target_width, target_height))
|
| 136 |
+
processed_images = []
|
| 137 |
+
for i in range(blocks):
|
| 138 |
+
box = (
|
| 139 |
+
(i % (target_width // image_size)) * image_size,
|
| 140 |
+
(i // (target_width // image_size)) * image_size,
|
| 141 |
+
((i % (target_width // image_size)) + 1) * image_size,
|
| 142 |
+
((i // (target_width // image_size)) + 1) * image_size
|
| 143 |
+
)
|
| 144 |
+
# split the image
|
| 145 |
+
split_img = resized_img.crop(box)
|
| 146 |
+
processed_images.append(split_img)
|
| 147 |
+
assert len(processed_images) == blocks
|
| 148 |
+
if use_thumbnail and len(processed_images) != 1:
|
| 149 |
+
thumbnail_img = image.resize((image_size, image_size))
|
| 150 |
+
processed_images.append(thumbnail_img)
|
| 151 |
+
return processed_images
|
| 152 |
+
|
| 153 |
+
def load_image(image_file, input_size=448, max_num=12):
|
| 154 |
+
image = Image.open(image_file).convert('RGB')
|
| 155 |
+
transform = build_transform(input_size=input_size)
|
| 156 |
+
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
|
| 157 |
+
pixel_values = [transform(image) for image in images]
|
| 158 |
+
pixel_values = torch.stack(pixel_values)
|
| 159 |
+
return pixel_values
|
| 160 |
+
|
| 161 |
+
# If you want to load a model using multiple GPUs, please refer to the `Multiple GPUs` section.
|
| 162 |
+
path = 'OpenGVLab/Mini-InternVL2-4B-DA-BDD'
|
| 163 |
+
model = AutoModel.from_pretrained(
|
| 164 |
+
path,
|
| 165 |
+
torch_dtype=torch.bfloat16,
|
| 166 |
+
low_cpu_mem_usage=True,
|
| 167 |
+
use_flash_attn=True,
|
| 168 |
+
trust_remote_code=True).eval().cuda()
|
| 169 |
+
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
|
| 170 |
+
|
| 171 |
+
# set the max number of tiles in `max_num`
|
| 172 |
+
pixel_values = load_image('path/to/image.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 173 |
+
generation_config = dict(max_new_tokens=1024, do_sample=True)
|
| 174 |
+
|
| 175 |
+
# pure-text conversation (纯文本对话)
|
| 176 |
+
question = 'Hello, who are you?'
|
| 177 |
+
response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
|
| 178 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 179 |
+
|
| 180 |
+
question = 'Can you tell me a story?'
|
| 181 |
+
response, history = model.chat(tokenizer, None, question, generation_config, history=history, return_history=True)
|
| 182 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 183 |
+
|
| 184 |
+
# single-image single-round conversation (单图单轮对话)
|
| 185 |
+
question = '<image>\nPlease describe the image shortly.'
|
| 186 |
+
response = model.chat(tokenizer, pixel_values, question, generation_config)
|
| 187 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 188 |
+
|
| 189 |
+
# single-image multi-round conversation (单图多轮对话)
|
| 190 |
+
question = '<image>\nPlease describe the image in detail.'
|
| 191 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
|
| 192 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 193 |
+
|
| 194 |
+
question = 'Please write a poem according to the image.'
|
| 195 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
|
| 196 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 197 |
+
|
| 198 |
+
# multi-image multi-round conversation, combined images (多图多轮对话,拼接图像)
|
| 199 |
+
pixel_values1 = load_image('path/to/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 200 |
+
pixel_values2 = load_image('path/to/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 201 |
+
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
|
| 202 |
+
|
| 203 |
+
question = '<image>\nDescribe the two images in detail.'
|
| 204 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
|
| 205 |
+
history=None, return_history=True)
|
| 206 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 207 |
+
|
| 208 |
+
question = 'What are the similarities and differences between these two images.'
|
| 209 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
|
| 210 |
+
history=history, return_history=True)
|
| 211 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 212 |
+
|
| 213 |
+
# multi-image multi-round conversation, separate images (多图多轮对话,独立图像)
|
| 214 |
+
pixel_values1 = load_image('path/to/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 215 |
+
pixel_values2 = load_image('path/to/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 216 |
+
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
|
| 217 |
+
num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
|
| 218 |
+
|
| 219 |
+
question = 'Image-1: <image>\nImage-2: <image>\nDescribe the two images in detail.'
|
| 220 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
|
| 221 |
+
num_patches_list=num_patches_list,
|
| 222 |
+
history=None, return_history=True)
|
| 223 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 224 |
+
|
| 225 |
+
question = 'What are the similarities and differences between these two images.'
|
| 226 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
|
| 227 |
+
num_patches_list=num_patches_list,
|
| 228 |
+
history=history, return_history=True)
|
| 229 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 230 |
+
|
| 231 |
+
# batch inference, single image per sample (单图批处理)
|
| 232 |
+
pixel_values1 = load_image('path/to/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 233 |
+
pixel_values2 = load_image('path/to/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 234 |
+
num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
|
| 235 |
+
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
|
| 236 |
+
|
| 237 |
+
questions = ['<image>\nDescribe the image in detail.'] * len(num_patches_list)
|
| 238 |
+
responses = model.batch_chat(tokenizer, pixel_values,
|
| 239 |
+
num_patches_list=num_patches_list,
|
| 240 |
+
questions=questions,
|
| 241 |
+
generation_config=generation_config)
|
| 242 |
+
for question, response in zip(questions, responses):
|
| 243 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 244 |
+
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
## Citation
|
| 248 |
+
|
| 249 |
+
If you find this project useful in your research, please consider citing:
|
| 250 |
+
|
| 251 |
+
```BibTeX
|
| 252 |
+
@article{gao2024mini,
|
| 253 |
+
title={Mini-internvl: A flexible-transfer pocket multimodal model with 5\% parameters and 90\% performance},
|
| 254 |
+
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},
|
| 255 |
+
journal={arXiv preprint arXiv:2410.16261},
|
| 256 |
+
year={2024}
|
| 257 |
+
}
|
| 258 |
+
@article{chen2024expanding,
|
| 259 |
+
title={Expanding Performance Boundaries of Open-Source Multimodal Models with Model, Data, and Test-Time Scaling},
|
| 260 |
+
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},
|
| 261 |
+
journal={arXiv preprint arXiv:2412.05271},
|
| 262 |
+
year={2024}
|
| 263 |
+
}
|
| 264 |
+
@article{chen2024far,
|
| 265 |
+
title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
|
| 266 |
+
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},
|
| 267 |
+
journal={arXiv preprint arXiv:2404.16821},
|
| 268 |
+
year={2024}
|
| 269 |
+
}
|
| 270 |
+
@inproceedings{chen2024internvl,
|
| 271 |
+
title={Internvl: Scaling up vision foundation models and aligning for generic visual-linguistic tasks},
|
| 272 |
+
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},
|
| 273 |
+
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
|
| 274 |
+
pages={24185--24198},
|
| 275 |
+
year={2024}
|
| 276 |
+
}
|
| 277 |
+
```
|