Views
No views yet




| Benchmark | InternVL2_5 1B | Vintern-1B-v2 | Vintern-1B-v3.5 |
|---|---|---|---|
| vi-MTVQA | 24.8 | 37.4 | 41.9 |
| DocVQAtest | 84.8 | 72.5 | 78.8 |
| InfoVQAtest | 56.0 | 38.9 | 46.4 |
| TextVQAval | 72.0 | 64.0 | 68.2 |
| ChartQAtest | 75.9 | 34.1 | 65.7 |
| OCRBench | 785 | 628 | 706 |





1import numpy as np
2import torch
3import torchvision.transforms as T
4# from decord import VideoReader, cpu
5from PIL import Image
6from torchvision.transforms.functional import InterpolationMode
7from transformers import AutoModel, AutoTokenizer
8
9IMAGENET_MEAN = (0.485, 0.456, 0.406)
10IMAGENET_STD = (0.229, 0.224, 0.225)
11
12def build_transform(input_size):
13 MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
14 transform = T.Compose([
15 T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
16 T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
17 T.ToTensor(),
18 T.Normalize(mean=MEAN, std=STD)
19 ])
20 return transform
21
22def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
23 best_ratio_diff = float('inf')
24 best_ratio = (1, 1)
25 area = width * height
26 for ratio in target_ratios:
27 target_aspect_ratio = ratio[0] / ratio[1]
28 ratio_diff = abs(aspect_ratio - target_aspect_ratio)
29 if ratio_diff < best_ratio_diff:
30 best_ratio_diff = ratio_diff
31 best_ratio = ratio
32 elif ratio_diff == best_ratio_diff:
33 if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
34 best_ratio = ratio
35 return best_ratio
36
37def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
38 orig_width, orig_height = image.size
39 aspect_ratio = orig_width / orig_height
40
41 # calculate the existing image aspect ratio
42 target_ratios = set(
43 (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
44 i * j <= max_num and i * j >= min_num)
45 target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
46
47 # find the closest aspect ratio to the target
48 target_aspect_ratio = find_closest_aspect_ratio(
49 aspect_ratio, target_ratios, orig_width, orig_height, image_size)
50
51 # calculate the target width and height
52 target_width = image_size * target_aspect_ratio[0]
53 target_height = image_size * target_aspect_ratio[1]
54 blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
55
56 # resize the image
57 resized_img = image.resize((target_width, target_height))
58 processed_images = []
59 for i in range(blocks):
60 box = (
61 (i % (target_width // image_size)) * image_size,
62 (i // (target_width // image_size)) * image_size,
63 ((i % (target_width // image_size)) + 1) * image_size,
64 ((i // (target_width // image_size)) + 1) * image_size
65 )
66 # split the image
67 split_img = resized_img.crop(box)
68 processed_images.append(split_img)
69 assert len(processed_images) == blocks
70 if use_thumbnail and len(processed_images) != 1:
71 thumbnail_img = image.resize((image_size, image_size))
72 processed_images.append(thumbnail_img)
73 return processed_images
74
75def load_image(image_file, input_size=448, max_num=12):
76 image = Image.open(image_file).convert('RGB')
77 transform = build_transform(input_size=input_size)
78 images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
79 pixel_values = [transform(image) for image in images]
80 pixel_values = torch.stack(pixel_values)
81 return pixel_values
82
83model = AutoModel.from_pretrained(
84 "5CD-AI/Vintern-1B-v3_5",
85 torch_dtype=torch.bfloat16,
86 low_cpu_mem_usage=True,
87 trust_remote_code=True,
88 use_flash_attn=False,
89).eval().cuda()
90
91tokenizer = AutoTokenizer.from_pretrained("5CD-AI/Vintern-1B-v3_5", trust_remote_code=True, use_fast=False)
92
93test_image = 'test-image.jpg'
94
95pixel_values = load_image(test_image, max_num=6).to(torch.bfloat16).cuda()
96generation_config = dict(max_new_tokens= 1024, do_sample=False, num_beams = 3, repetition_penalty=2.5)
97
98question = '<image>\nTrích xuất thông tin chính trong ảnh và trả về dạng markdown.'
99
100response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
101print(f'User: {question}\nAssistant: {response}')
102
103#question = "Câu hỏi khác ......"
104#response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
105#print(f'User: {question}\nAssistant: {response}')@misc{doan2024vintern1befficientmultimodallarge,
title={Vintern-1B: An Efficient Multimodal Large Language Model for Vietnamese},
author={Khang T. Doan and Bao G. Huynh and Dung T. Hoang and Thuc D. Pham and Nhat H. Pham and Quan T. M. Nguyen and Bang Q. Vo and Suong N. Hoang},
year={2024},
eprint={2408.12480},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2408.12480},
}