khang119966 commited on
Commit
4d0f481
·
verified ·
1 Parent(s): 2c26caa

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +179 -154
README.md CHANGED
@@ -1,199 +1,224 @@
1
  ---
2
  library_name: transformers
3
- tags: []
 
 
 
 
 
 
 
4
  ---
5
 
6
- # Model Card for Model ID
7
 
8
- <!-- Provide a quick summary of what the model is/does. -->
9
 
10
 
 
11
 
12
- ## Model Details
13
-
14
- ### Model Description
15
-
16
- <!-- Provide a longer summary of what this model is. -->
17
-
18
- This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
19
-
20
- - **Developed by:** [More Information Needed]
21
- - **Funded by [optional]:** [More Information Needed]
22
- - **Shared by [optional]:** [More Information Needed]
23
- - **Model type:** [More Information Needed]
24
- - **Language(s) (NLP):** [More Information Needed]
25
- - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
-
28
- ### Model Sources [optional]
29
-
30
- <!-- Provide the basic links for the model. -->
31
-
32
- - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
- - **Demo [optional]:** [More Information Needed]
35
-
36
- ## Uses
37
-
38
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
-
40
- ### Direct Use
41
-
42
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
-
44
- [More Information Needed]
45
-
46
- ### Downstream Use [optional]
47
-
48
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
-
50
- [More Information Needed]
51
-
52
- ### Out-of-Scope Use
53
-
54
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
-
56
- [More Information Needed]
57
-
58
- ## Bias, Risks, and Limitations
59
-
60
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
-
62
- [More Information Needed]
63
-
64
- ### Recommendations
65
-
66
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
-
68
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
-
70
- ## How to Get Started with the Model
71
-
72
- Use the code below to get started with the model.
73
 
74
- [More Information Needed]
75
 
76
- ## Training Details
77
 
78
- ### Training Data
79
-
80
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
-
82
- [More Information Needed]
83
-
84
- ### Training Procedure
85
-
86
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
-
88
- #### Preprocessing [optional]
89
-
90
- [More Information Needed]
91
-
92
-
93
- #### Training Hyperparameters
94
-
95
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
-
97
- #### Speeds, Sizes, Times [optional]
98
-
99
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
-
101
- [More Information Needed]
102
-
103
- ## Evaluation
104
-
105
- <!-- This section describes the evaluation protocols and provides the results. -->
106
-
107
- ### Testing Data, Factors & Metrics
108
-
109
- #### Testing Data
110
-
111
- <!-- This should link to a Dataset Card if possible. -->
112
-
113
- [More Information Needed]
114
-
115
- #### Factors
116
-
117
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
-
119
- [More Information Needed]
120
-
121
- #### Metrics
122
-
123
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
-
125
- [More Information Needed]
126
-
127
- ### Results
128
-
129
- [More Information Needed]
130
 
131
- #### Summary
 
 
132
 
133
 
 
134
 
135
- ## Model Examination [optional]
136
 
137
- <!-- Relevant interpretability work for the model goes here -->
 
138
 
139
- [More Information Needed]
140
 
141
- ## Environmental Impact
142
 
143
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
 
145
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
 
147
- - **Hardware Type:** [More Information Needed]
148
- - **Hours used:** [More Information Needed]
149
- - **Cloud Provider:** [More Information Needed]
150
- - **Compute Region:** [More Information Needed]
151
- - **Carbon Emitted:** [More Information Needed]
152
 
153
- ## Technical Specifications [optional]
154
 
155
- ### Model Architecture and Objective
156
 
157
- [More Information Needed]
 
 
158
 
159
- ### Compute Infrastructure
160
 
161
- [More Information Needed]
162
 
163
- #### Hardware
 
 
164
 
165
- [More Information Needed]
166
 
167
- #### Software
168
 
169
- [More Information Needed]
 
 
170
 
171
- ## Citation [optional]
172
 
173
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
 
175
- **BibTeX:**
 
 
176
 
177
- [More Information Needed]
178
 
179
- **APA:**
180
 
181
- [More Information Needed]
 
 
182
 
183
- ## Glossary [optional]
184
 
185
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
 
187
- [More Information Needed]
188
 
189
- ## More Information [optional]
190
 
191
- [More Information Needed]
 
 
192
 
193
- ## Model Card Authors [optional]
 
 
 
 
 
 
 
194
 
195
- [More Information Needed]
 
196
 
197
- ## Model Card Contact
 
 
 
 
 
 
 
 
198
 
199
- [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  library_name: transformers
3
+ license: mit
4
+ language:
5
+ - vi
6
+ - en
7
+ - zh
8
+ base_model:
9
+ - OpenGVLab/InternVL2_5-1B
10
+ pipeline_tag: image-text-to-text
11
  ---
12
 
13
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/6336b5c831efcb5647f00170/AxRFDUt8uft6HVxBWuXgJ.png)
14
 
15
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/6336b5c831efcb5647f00170/DrUCZuXuMz47uVU4zqnJ4.png)
16
 
17
 
18
+ ## Vintern-1B-v2 ❄️ (Viet-InternVL2-1B-v2) - The LLaVA 🌋 Challenger
19
 
20
+ We are excited to introduce **Vintern-1B-v2** the Vietnamese 🇻🇳 multimodal model that combines the advanced Vietnamese language model [Qwen2-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2-0.5B-Instruct)[1] with the latest visual model, [InternViT-300M-448px](https://huggingface.co/OpenGVLab/InternViT-300M-448px)[2], CVPR 2024. This model excels in tasks such as OCR-VQA, Doc-VQA, and Chart-VQA,... With only 1 billion parameters, it is **4096 context length** finetuned from the [Viet-InternVL2-1B](https://huggingface.co/5CD-AI/Viet-InternVL2-1B) model on over 3 million specialized image-question-answer pairs for optical character recognition 🔍, text recognition 🔤, document extraction 📑, and general VQA. The model can be integrated into various on-device applications 📱, demonstrating its versatility and robust capabilities.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ [**\[🤗 HF Demo\]**](https://huggingface.co/spaces/khang119966/Vintern-v2-Demo)
23
 
24
+ The special thing is that our model can be easily finetuned with a T4 GPU on Google Colab by following the instructions provided at the end of this section.
25
 
26
+ ## Model Details
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ | Model Name | Vision Part | Language Part |
29
+ | :------------------: | :---------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------: |
30
+ | Vintern-1B-v2 | [InternViT-300M-448px](https://huggingface.co/OpenGVLab/InternViT-300M-448px) | [Qwen2-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2-0.5B-Instruct) |
31
 
32
 
33
+ Vintern-1B-v2 is a multimodal large language model series, featuring models of various sizes. For each size, we release instruction-tuned models optimized for multimodal tasks. Vintern-1B-v2 consists of [InternViT-300M-448px](https://huggingface.co/OpenGVLab/InternViT-300M-448px), an MLP projector, and [Qwen2-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2-0.5B-Instruct).
34
 
35
+ ## Training details 📚
36
 
37
+ The fine-tuning dataset was meticulously sampled in part from the following datasets:
38
+ [Viet-OCR-VQA 📚](https://huggingface.co/datasets/5CD-AI/Viet-OCR-VQA), [Viet-Doc-VQA 📄](https://huggingface.co/datasets/5CD-AI/Viet-Doc-VQA), [Viet-Doc-VQA-II 📑](https://huggingface.co/datasets/5CD-AI/Viet-Doc-VQA-II), [Vista 🖼️](https://huggingface.co/datasets/Vi-VLM/Vista), [Viet-Receipt-VQA 🧾](https://huggingface.co/datasets/5CD-AI/Viet-Receipt-VQA), [Viet-Sketches-VQA ✏️](https://huggingface.co/datasets/5CD-AI/Viet-Sketches-VQA), [Viet-Geometry-VQA 📐](https://huggingface.co/datasets/5CD-AI/Viet-Geometry-VQA), [Viet-Wiki-Handwriting ✍️](https://huggingface.co/datasets/5CD-AI/Viet-Wiki-Handwriting), [Viet-ComputerScience-VQA 💻](https://huggingface.co/datasets/5CD-AI/Viet-ComputerScience-VQA), [Viet-Handwriting-gemini-VQA 🖋️](https://huggingface.co/datasets/5CD-AI/Viet-Handwriting-gemini-VQA), [Viet-Menu-gemini-VQA 🍽️](https://huggingface.co/datasets/5CD-AI/Viet-Menu-gemini-VQA), [Viet-Vintext-gemini-VQA 📜](https://huggingface.co/datasets/5CD-AI/Viet-Vintext-gemini-VQA), [Viet-OpenViVQA-gemini-VQA 🧠](https://huggingface.co/datasets/5CD-AI/Viet-OpenViVQA-gemini-VQA), [Viet-Resume-VQA 📃](https://huggingface.co/datasets/5CD-AI/Viet-Resume-VQA), [Viet-ViTextVQA-gemini-VQA 📑](https://huggingface.co/datasets/5CD-AI/Viet-ViTextVQA-gemini-VQA)
39
 
40
+ ## Benchmarks 📈
41
 
 
42
 
 
43
 
44
+ ## Examples
45
 
46
+ <div align="center">
47
+ <img src="ex_images/1.png" width="500"/>
48
+ </div>
 
 
49
 
50
+ ```
51
 
52
+ ```
53
 
54
+ <div align="center">
55
+ <img src="ex_images/4.jpg" width="500"/>
56
+ </div>
57
 
58
+ ```
59
 
60
+ ```
61
 
62
+ <div align="center">
63
+ <img src="ex_images/2.jpg" width="500"/>
64
+ </div>
65
 
66
+ ```
67
 
68
+ ```
69
 
70
+ <div align="center">
71
+ <img src="ex_images/3.png" width="400"/>
72
+ </div>
73
 
74
+ ```
75
 
76
+ ```
77
 
78
+ <div align="center">
79
+ <img src="ex_images/5.jpg" width="400"/>
80
+ </div>
81
 
82
+ ```
83
 
84
+ ```
85
 
86
+ <div align="center">
87
+ <img src="ex_images/6.png" width="400"/>
88
+ </div>
89
 
 
90
 
91
+ ```
92
 
93
+ ```
94
 
95
+ ## Quickstart
96
 
97
+ Here provides a code snippet to show you how to load the tokenizer and model and how to generate contents.
98
+ To run inference using the model, follow the steps outlined in our Colab inference notebook
99
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1ZD1oB56PF0lF66RCuTVJYLTEV0tM3CFf?usp=sharing)
100
 
101
+ ```python
102
+ import numpy as np
103
+ import torch
104
+ import torchvision.transforms as T
105
+ # from decord import VideoReader, cpu
106
+ from PIL import Image
107
+ from torchvision.transforms.functional import InterpolationMode
108
+ from transformers import AutoModel, AutoTokenizer
109
 
110
+ IMAGENET_MEAN = (0.485, 0.456, 0.406)
111
+ IMAGENET_STD = (0.229, 0.224, 0.225)
112
 
113
+ def build_transform(input_size):
114
+ MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
115
+ transform = T.Compose([
116
+ T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
117
+ T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
118
+ T.ToTensor(),
119
+ T.Normalize(mean=MEAN, std=STD)
120
+ ])
121
+ return transform
122
 
123
+ def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
124
+ best_ratio_diff = float('inf')
125
+ best_ratio = (1, 1)
126
+ area = width * height
127
+ for ratio in target_ratios:
128
+ target_aspect_ratio = ratio[0] / ratio[1]
129
+ ratio_diff = abs(aspect_ratio - target_aspect_ratio)
130
+ if ratio_diff < best_ratio_diff:
131
+ best_ratio_diff = ratio_diff
132
+ best_ratio = ratio
133
+ elif ratio_diff == best_ratio_diff:
134
+ if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
135
+ best_ratio = ratio
136
+ return best_ratio
137
+
138
+ def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
139
+ orig_width, orig_height = image.size
140
+ aspect_ratio = orig_width / orig_height
141
+
142
+ # calculate the existing image aspect ratio
143
+ target_ratios = set(
144
+ (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
145
+ i * j <= max_num and i * j >= min_num)
146
+ target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
147
+
148
+ # find the closest aspect ratio to the target
149
+ target_aspect_ratio = find_closest_aspect_ratio(
150
+ aspect_ratio, target_ratios, orig_width, orig_height, image_size)
151
+
152
+ # calculate the target width and height
153
+ target_width = image_size * target_aspect_ratio[0]
154
+ target_height = image_size * target_aspect_ratio[1]
155
+ blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
156
+
157
+ # resize the image
158
+ resized_img = image.resize((target_width, target_height))
159
+ processed_images = []
160
+ for i in range(blocks):
161
+ box = (
162
+ (i % (target_width // image_size)) * image_size,
163
+ (i // (target_width // image_size)) * image_size,
164
+ ((i % (target_width // image_size)) + 1) * image_size,
165
+ ((i // (target_width // image_size)) + 1) * image_size
166
+ )
167
+ # split the image
168
+ split_img = resized_img.crop(box)
169
+ processed_images.append(split_img)
170
+ assert len(processed_images) == blocks
171
+ if use_thumbnail and len(processed_images) != 1:
172
+ thumbnail_img = image.resize((image_size, image_size))
173
+ processed_images.append(thumbnail_img)
174
+ return processed_images
175
+
176
+ def load_image(image_file, input_size=448, max_num=12):
177
+ image = Image.open(image_file).convert('RGB')
178
+ transform = build_transform(input_size=input_size)
179
+ images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
180
+ pixel_values = [transform(image) for image in images]
181
+ pixel_values = torch.stack(pixel_values)
182
+ return pixel_values
183
+
184
+ model = AutoModel.from_pretrained(
185
+ "5CD-AI/Vintern-1B-v2",
186
+ torch_dtype=torch.bfloat16,
187
+ low_cpu_mem_usage=True,
188
+ trust_remote_code=True,
189
+ ).eval().cuda()
190
+ tokenizer = AutoTokenizer.from_pretrained("5CD-AI/Vintern-1B-v2", trust_remote_code=True, use_fast=False)
191
+
192
+ test_image = 'test-image.jpg'
193
+
194
+ pixel_values = load_image(test_image, max_num=12).to(torch.bfloat16).cuda()
195
+ generation_config = dict(max_new_tokens= 1024, do_sample=False, num_beams = 3, repetition_penalty=2.5)
196
+
197
+ question = '<image>\nMô tả hình ảnh một cách chi tiết.'
198
+
199
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
200
+ print(f'User: {question}\nAssistant: {response}')
201
+
202
+ #question = "Câu hỏi khác ......"
203
+ #response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
204
+ #print(f'User: {question}\nAssistant: {response}')
205
+ ```
206
+
207
+ ## Finetune on your Data
208
+
209
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1bK6fpWfResjv9UxWoKHDStXQ8bop3a6Z?usp=sharing)
210
+
211
+
212
+ ## Citation
213
+
214
+ ```
215
+ @misc{doan2024vintern1befficientmultimodallarge,
216
+ title={Vintern-1B: An Efficient Multimodal Large Language Model for Vietnamese},
217
+ 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},
218
+ year={2024},
219
+ eprint={2408.12480},
220
+ archivePrefix={arXiv},
221
+ primaryClass={cs.LG},
222
+ url={https://arxiv.org/abs/2408.12480},
223
+ }
224
+ ```