Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
# For reference on model card metadata, see the spec: https://github.com/huggingface/hub-docs/blob/main/modelcard.md?plain=1
|
3 |
+
# Doc / guide: https://huggingface.co/docs/hub/model-cards
|
4 |
+
{}
|
5 |
+
---
|
6 |
+
|
7 |
+
# InternVL-X-2B
|
8 |
+
|
9 |
+
## How to Get Started with the Model
|
10 |
+
|
11 |
+
```
|
12 |
+
import numpy as np
|
13 |
+
import time
|
14 |
+
import math
|
15 |
+
import torch
|
16 |
+
import torchvision.transforms as T
|
17 |
+
from decord import VideoReader, cpu
|
18 |
+
from PIL import Image
|
19 |
+
from torchvision.transforms.functional import InterpolationMode
|
20 |
+
from transformers import AutoModel, AutoTokenizer
|
21 |
+
import os
|
22 |
+
|
23 |
+
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
24 |
+
IMAGENET_STD = (0.229, 0.224, 0.225)
|
25 |
+
|
26 |
+
|
27 |
+
def build_transform(input_size):
|
28 |
+
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
|
29 |
+
transform = T.Compose([
|
30 |
+
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
31 |
+
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
|
32 |
+
T.ToTensor(),
|
33 |
+
T.Normalize(mean=MEAN, std=STD)
|
34 |
+
])
|
35 |
+
return transform
|
36 |
+
|
37 |
+
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
38 |
+
best_ratio_diff = float('inf')
|
39 |
+
best_ratio = (1, 1)
|
40 |
+
area = width * height
|
41 |
+
for ratio in target_ratios:
|
42 |
+
target_aspect_ratio = ratio[0] / ratio[1]
|
43 |
+
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
|
44 |
+
if ratio_diff < best_ratio_diff:
|
45 |
+
best_ratio_diff = ratio_diff
|
46 |
+
best_ratio = ratio
|
47 |
+
elif ratio_diff == best_ratio_diff:
|
48 |
+
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
|
49 |
+
best_ratio = ratio
|
50 |
+
return best_ratio
|
51 |
+
|
52 |
+
def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
|
53 |
+
orig_width, orig_height = image.size
|
54 |
+
aspect_ratio = orig_width / orig_height
|
55 |
+
|
56 |
+
block_h = math.ceil(orig_height / image_size)
|
57 |
+
block_w = math.ceil(orig_width / image_size)
|
58 |
+
max_num_new = block_h * block_w
|
59 |
+
if max_num_new > max_num:
|
60 |
+
max_num_new = max_num
|
61 |
+
max_num = max_num_new
|
62 |
+
|
63 |
+
# calculate the existing image aspect ratio
|
64 |
+
target_ratios = set(
|
65 |
+
(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
|
66 |
+
i * j <= max_num and i * j >= min_num)
|
67 |
+
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
68 |
+
|
69 |
+
# find the closest aspect ratio to the target
|
70 |
+
target_aspect_ratio = find_closest_aspect_ratio(
|
71 |
+
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
|
72 |
+
|
73 |
+
# calculate the target width and height
|
74 |
+
target_width = image_size * target_aspect_ratio[0]
|
75 |
+
target_height = image_size * target_aspect_ratio[1]
|
76 |
+
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
77 |
+
|
78 |
+
# resize the image
|
79 |
+
resized_img = image.resize((target_width, target_height))
|
80 |
+
processed_images = []
|
81 |
+
for i in range(blocks):
|
82 |
+
box = (
|
83 |
+
(i % (target_width // image_size)) * image_size,
|
84 |
+
(i // (target_width // image_size)) * image_size,
|
85 |
+
((i % (target_width // image_size)) + 1) * image_size,
|
86 |
+
((i // (target_width // image_size)) + 1) * image_size
|
87 |
+
)
|
88 |
+
# split the image
|
89 |
+
split_img = resized_img.crop(box)
|
90 |
+
processed_images.append(split_img)
|
91 |
+
assert len(processed_images) == blocks
|
92 |
+
if use_thumbnail and len(processed_images) != 1:
|
93 |
+
thumbnail_img = image.resize((image_size, image_size))
|
94 |
+
processed_images.append(thumbnail_img)
|
95 |
+
return processed_images
|
96 |
+
|
97 |
+
def load_image(image_file, input_size=448, max_num=12):
|
98 |
+
image = Image.open(image_file).convert('RGB')
|
99 |
+
transform = build_transform(input_size=input_size)
|
100 |
+
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
|
101 |
+
pixel_values = [transform(image) for image in images]
|
102 |
+
pixel_values = torch.stack(pixel_values)
|
103 |
+
return pixel_values
|
104 |
+
|
105 |
+
|
106 |
+
path = 'InternVL-X-2B'
|
107 |
+
model = AutoModel.from_pretrained(
|
108 |
+
path,
|
109 |
+
torch_dtype=torch.bfloat16,
|
110 |
+
low_cpu_mem_usage=True,
|
111 |
+
use_flash_attention_2=False,
|
112 |
+
trust_remote_code=True).eval().cuda()
|
113 |
+
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
|
114 |
+
generation_config = dict(max_new_tokens=1024, do_sample=False)
|
115 |
+
|
116 |
+
pixel_values = load_image('examples/image1.jpg', max_num=1).to(torch.bfloat16).cuda()
|
117 |
+
|
118 |
+
# single-image single-round conversation (单图单轮对话)
|
119 |
+
question = '<image>\nDescribe this image in datail'
|
120 |
+
response = model.chat(tokenizer, pixel_values, question, generation_config)
|
121 |
+
print(f'User: {question}\nAssistant: {response}')
|
122 |
+
|
123 |
+
# single-image multi-round conversation (单图多轮对话)
|
124 |
+
question = '<image>\nPlease describe the image in detail.'
|
125 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
|
126 |
+
print(f'User: {question}\nAssistant: {response}')
|
127 |
+
|
128 |
+
question = 'Please write a story according to the image.'
|
129 |
+
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
|
130 |
+
print(f'User: {question}\nAssistant: {response}')
|
131 |
+
```
|