Update README.md
Browse files
README.md
CHANGED
|
@@ -36,7 +36,7 @@ pipeline_tag: image-text-to-text
|
|
| 36 |
|
| 37 |
| Model Name | Base Model | Parameters | Download Link |
|
| 38 |
| ----------------------- | ------------------------- | ---------- | ----------------------------------------------------------- |
|
| 39 |
-
| SkyworkVL-2B | OpenGVLab/InternVL2_5-
|
| 40 |
| SkyworkVL-38B | OpenGVLab/InternVL2_5-38B | 38B | 🤗 [Download](https://huggingface.co/Skywork/SkyworkVL-38B) |
|
| 41 |
|
| 42 |
## Performance
|
|
@@ -54,6 +54,240 @@ pipeline_tag: image-text-to-text
|
|
| 54 |
|
| 55 |
Please refer to the [Guide](https://github.com/YourGitHub/SkyworkVL-38B) for detailed instructions on inference and integration.
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
## Citation
|
| 58 |
|
| 59 |
```BibTeX
|
|
|
|
| 36 |
|
| 37 |
| Model Name | Base Model | Parameters | Download Link |
|
| 38 |
| ----------------------- | ------------------------- | ---------- | ----------------------------------------------------------- |
|
| 39 |
+
| SkyworkVL-2B | OpenGVLab/InternVL2_5-2B | 2B | 🤗 [Download](https://huggingface.co/Skywork/SkyworkVL-2B) |
|
| 40 |
| SkyworkVL-38B | OpenGVLab/InternVL2_5-38B | 38B | 🤗 [Download](https://huggingface.co/Skywork/SkyworkVL-38B) |
|
| 41 |
|
| 42 |
## Performance
|
|
|
|
| 54 |
|
| 55 |
Please refer to the [Guide](https://github.com/YourGitHub/SkyworkVL-38B) for detailed instructions on inference and integration.
|
| 56 |
|
| 57 |
+
## Quick Start
|
| 58 |
+
|
| 59 |
+
We provide an example code to run `SkyworkVL-38B` using `transformers`
|
| 60 |
+
|
| 61 |
+
### Model Loading
|
| 62 |
+
|
| 63 |
+
#### 16-bit(bf16 / fp16)
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
import torch
|
| 67 |
+
from transformers import AutoTokenizer, AutoModel
|
| 68 |
+
path = "Skywork/SkyworkVL-38B"
|
| 69 |
+
model = AutoModel.from_pretrained(
|
| 70 |
+
path,
|
| 71 |
+
torch_dtype=torch.bfloat16,
|
| 72 |
+
low_cpu_mem_usage=True,
|
| 73 |
+
use_flash_attn=True,
|
| 74 |
+
trust_remote_code=True).eval().cuda()
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
#### BNB 8-bit Quantization
|
| 78 |
+
|
| 79 |
+
```python
|
| 80 |
+
import torch
|
| 81 |
+
from transformers import AutoTokenizer, AutoModel
|
| 82 |
+
path = "Skywork/SkyworkVL-38B"
|
| 83 |
+
model = AutoModel.from_pretrained(
|
| 84 |
+
path,
|
| 85 |
+
torch_dtype=torch.bfloat16,
|
| 86 |
+
load_in_8bit=True,
|
| 87 |
+
low_cpu_mem_usage=True,
|
| 88 |
+
use_flash_attn=True,
|
| 89 |
+
trust_remote_code=True).eval()
|
| 90 |
+
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
### Inference with Transformers
|
| 94 |
+
|
| 95 |
+
```python
|
| 96 |
+
import math
|
| 97 |
+
import numpy as np
|
| 98 |
+
import torch
|
| 99 |
+
import torchvision.transforms as T
|
| 100 |
+
from decord import VideoReader, cpu
|
| 101 |
+
from PIL import Image
|
| 102 |
+
from torchvision.transforms.functional import InterpolationMode
|
| 103 |
+
from transformers import AutoModel, AutoTokenizer
|
| 104 |
+
|
| 105 |
+
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
| 106 |
+
IMAGENET_STD = (0.229, 0.224, 0.225)
|
| 107 |
+
|
| 108 |
+
def build_transform(input_size):
|
| 109 |
+
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
|
| 110 |
+
transform = T.Compose([
|
| 111 |
+
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
| 112 |
+
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
|
| 113 |
+
T.ToTensor(),
|
| 114 |
+
T.Normalize(mean=MEAN, std=STD)
|
| 115 |
+
])
|
| 116 |
+
return transform
|
| 117 |
+
|
| 118 |
+
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
| 119 |
+
best_ratio_diff = float('inf')
|
| 120 |
+
best_ratio = (1, 1)
|
| 121 |
+
area = width * height
|
| 122 |
+
for ratio in target_ratios:
|
| 123 |
+
target_aspect_ratio = ratio[0] / ratio[1]
|
| 124 |
+
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
|
| 125 |
+
if ratio_diff < best_ratio_diff:
|
| 126 |
+
best_ratio_diff = ratio_diff
|
| 127 |
+
best_ratio = ratio
|
| 128 |
+
elif ratio_diff == best_ratio_diff:
|
| 129 |
+
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
|
| 130 |
+
best_ratio = ratio
|
| 131 |
+
return best_ratio
|
| 132 |
+
|
| 133 |
+
def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
|
| 134 |
+
orig_width, orig_height = image.size
|
| 135 |
+
aspect_ratio = orig_width / orig_height
|
| 136 |
+
|
| 137 |
+
# calculate the existing image aspect ratio
|
| 138 |
+
target_ratios = set(
|
| 139 |
+
(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
|
| 140 |
+
i * j <= max_num and i * j >= min_num)
|
| 141 |
+
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
| 142 |
+
|
| 143 |
+
# find the closest aspect ratio to the target
|
| 144 |
+
target_aspect_ratio = find_closest_aspect_ratio(
|
| 145 |
+
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
|
| 146 |
+
|
| 147 |
+
# calculate the target width and height
|
| 148 |
+
target_width = image_size * target_aspect_ratio[0]
|
| 149 |
+
target_height = image_size * target_aspect_ratio[1]
|
| 150 |
+
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
| 151 |
+
|
| 152 |
+
# resize the image
|
| 153 |
+
resized_img = image.resize((target_width, target_height))
|
| 154 |
+
processed_images = []
|
| 155 |
+
for i in range(blocks):
|
| 156 |
+
box = (
|
| 157 |
+
(i % (target_width // image_size)) * image_size,
|
| 158 |
+
(i // (target_width // image_size)) * image_size,
|
| 159 |
+
((i % (target_width // image_size)) + 1) * image_size,
|
| 160 |
+
((i // (target_width // image_size)) + 1) * image_size
|
| 161 |
+
)
|
| 162 |
+
# split the image
|
| 163 |
+
split_img = resized_img.crop(box)
|
| 164 |
+
processed_images.append(split_img)
|
| 165 |
+
assert len(processed_images) == blocks
|
| 166 |
+
if use_thumbnail and len(processed_images) != 1:
|
| 167 |
+
thumbnail_img = image.resize((image_size, image_size))
|
| 168 |
+
processed_images.append(thumbnail_img)
|
| 169 |
+
return processed_images
|
| 170 |
+
|
| 171 |
+
def load_image(image_file, input_size=448, max_num=12):
|
| 172 |
+
image = Image.open(image_file).convert('RGB')
|
| 173 |
+
transform = build_transform(input_size=input_size)
|
| 174 |
+
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
|
| 175 |
+
pixel_values = [transform(image) for image in images]
|
| 176 |
+
pixel_values = torch.stack(pixel_values)
|
| 177 |
+
return pixel_values
|
| 178 |
+
|
| 179 |
+
def split_model(model_name):
|
| 180 |
+
device_map = {}
|
| 181 |
+
world_size = torch.cuda.device_count()
|
| 182 |
+
num_layers = {
|
| 183 |
+
'SkyworkVL-2B': 24, 'SkyworkVL-38B': 64}[model_name]
|
| 184 |
+
num_layers_per_gpu = math.ceil(num_layers / (world_size - 0.5))
|
| 185 |
+
num_layers_per_gpu = [num_layers_per_gpu] * world_size
|
| 186 |
+
num_layers_per_gpu[0] = math.ceil(num_layers_per_gpu[0] * 0.5)
|
| 187 |
+
layer_cnt = 0
|
| 188 |
+
for i, num_layer in enumerate(num_layers_per_gpu):
|
| 189 |
+
for j in range(num_layer):
|
| 190 |
+
device_map[f'language_model.model.layers.{layer_cnt}'] = i
|
| 191 |
+
layer_cnt += 1
|
| 192 |
+
device_map['vision_model'] = 0
|
| 193 |
+
device_map['mlp1'] = 0
|
| 194 |
+
device_map['language_model.model.tok_embeddings'] = 0
|
| 195 |
+
device_map['language_model.model.embed_tokens'] = 0
|
| 196 |
+
device_map['language_model.output'] = 0
|
| 197 |
+
device_map['language_model.model.norm'] = 0
|
| 198 |
+
device_map['language_model.model.rotary_emb'] = 0
|
| 199 |
+
device_map['language_model.lm_head'] = 0
|
| 200 |
+
device_map[f'language_model.model.layers.{num_layers - 1}'] = 0
|
| 201 |
+
|
| 202 |
+
return device_map
|
| 203 |
+
|
| 204 |
+
path = 'Skywork/SkyworkVL-38B'
|
| 205 |
+
device_map = split_model('SkyworkVL-38B')
|
| 206 |
+
model = AutoModel.from_pretrained(
|
| 207 |
+
path,
|
| 208 |
+
torch_dtype=torch.bfloat16,
|
| 209 |
+
load_in_8bit=True,
|
| 210 |
+
low_cpu_mem_usage=True,
|
| 211 |
+
use_flash_attn=True,
|
| 212 |
+
trust_remote_code=True,
|
| 213 |
+
device_map=device_map).eval()
|
| 214 |
+
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
|
| 215 |
+
|
| 216 |
+
# set the max number of tiles in `max_num`
|
| 217 |
+
pixel_values = load_image('./demo/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 218 |
+
generation_config = dict(max_new_tokens=1024, do_sample=True)
|
| 219 |
+
|
| 220 |
+
# pure-text conversation (纯文本对话)
|
| 221 |
+
question = 'Hi, what can you do?'
|
| 222 |
+
response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
|
| 223 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 224 |
+
|
| 225 |
+
question = 'Can you explain quantum mechanics to me?'
|
| 226 |
+
response, history = model.chat(tokenizer, None, question, generation_config, history=history, return_history=True)
|
| 227 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 228 |
+
|
| 229 |
+
# single-image single-round conversation (单张图片单轮对话)
|
| 230 |
+
question = '<image>\nWhat do you see in this image?'
|
| 231 |
+
response = model.chat(tokenizer, pixel_values, question, generation_config)
|
| 232 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 233 |
+
|
| 234 |
+
# single-image multi-round conversation (单张图片多轮对话)
|
| 235 |
+
question = '<image>\nCan you provide a detailed description of the image?'
|
| 236 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
|
| 237 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 238 |
+
|
| 239 |
+
question = 'Based on the image, can you create a short story?'
|
| 240 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
|
| 241 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 242 |
+
|
| 243 |
+
# multi-image multi-round conversation, combined images (多张图片多轮对话, 拼接图片)
|
| 244 |
+
pixel_values1 = load_image('./demo/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 245 |
+
pixel_values2 = load_image('./demo/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 246 |
+
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
|
| 247 |
+
|
| 248 |
+
question = '<image>\nDescribe the two images in detail.'
|
| 249 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
|
| 250 |
+
history=None, return_history=True)
|
| 251 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 252 |
+
|
| 253 |
+
question = 'What are the main differences between these two images?'
|
| 254 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
|
| 255 |
+
history=history, return_history=True)
|
| 256 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 257 |
+
|
| 258 |
+
# multi-image multi-round conversation, separate images (多张图片多轮对话, 分割图片)
|
| 259 |
+
pixel_values1 = load_image('./demo/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 260 |
+
pixel_values2 = load_image('./demo/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 261 |
+
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
|
| 262 |
+
num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
|
| 263 |
+
|
| 264 |
+
question = 'Image-1: <image>\nImage-2: <image>\nDescribe the two images in detail.'
|
| 265 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
|
| 266 |
+
num_patches_list=num_patches_list,
|
| 267 |
+
history=None, return_history=True)
|
| 268 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 269 |
+
|
| 270 |
+
question = 'What are the similarities between these two images?'
|
| 271 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config,
|
| 272 |
+
num_patches_list=num_patches_list,
|
| 273 |
+
history=history, return_history=True)
|
| 274 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 275 |
+
|
| 276 |
+
# batch inference, single image per sample (批量推理, 每条数据一张图片)
|
| 277 |
+
pixel_values1 = load_image('./demo/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 278 |
+
pixel_values2 = load_image('./demo/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
|
| 279 |
+
num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
|
| 280 |
+
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
|
| 281 |
+
|
| 282 |
+
questions = ['<image>\nDescribe the image in detail.'] * len(num_patches_list)
|
| 283 |
+
responses = model.batch_chat(tokenizer, pixel_values,
|
| 284 |
+
num_patches_list=num_patches_list,
|
| 285 |
+
questions=questions,
|
| 286 |
+
generation_config=generation_config)
|
| 287 |
+
for question, response in zip(questions, responses):
|
| 288 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 289 |
+
```
|
| 290 |
+
|
| 291 |
## Citation
|
| 292 |
|
| 293 |
```BibTeX
|