Update README.md
Browse files
README.md
CHANGED
@@ -6,3 +6,111 @@ We introduce Xmodel-VLM, a cutting-edge multimodal vision language model. It is
|
|
6 |
Our work directly confronts a pivotal industry issue by grappling with the prohibitive service costs that hinder the broad adoption of large-scale multimodal systems.
|
7 |
|
8 |
Refer to [our paper](https://arxiv.org/pdf/2405.09215) and [github](https://github.com/XiaoduoAILab/XmodelVLM) for more details!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
Our work directly confronts a pivotal industry issue by grappling with the prohibitive service costs that hinder the broad adoption of large-scale multimodal systems.
|
7 |
|
8 |
Refer to [our paper](https://arxiv.org/pdf/2405.09215) and [github](https://github.com/XiaoduoAILab/XmodelVLM) for more details!
|
9 |
+
|
10 |
+
To use Xmodel_VLM for the inference, all you need to do is to input a few lines of codes as demonstrated below. **However, please make sure that you are using the latest code and related virtual environments.**
|
11 |
+
|
12 |
+
## Inference example
|
13 |
+
```
|
14 |
+
import sys
|
15 |
+
import torch
|
16 |
+
import argparse
|
17 |
+
from PIL import Image
|
18 |
+
from pathlib import Path
|
19 |
+
import time
|
20 |
+
sys.path.append(str(Path(__file__).parent.parent.resolve()))
|
21 |
+
|
22 |
+
from xmodelvlm.model.xmodelvlm import load_pretrained_model
|
23 |
+
from xmodelvlm.conversation import conv_templates, SeparatorStyle
|
24 |
+
from xmodelvlm.utils import disable_torch_init, process_images, tokenizer_image_token, KeywordsStoppingCriteria
|
25 |
+
from xmodelvlm.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN
|
26 |
+
|
27 |
+
def inference_once(args):
|
28 |
+
disable_torch_init()
|
29 |
+
model_name = args.model_path.split('/')[-1]
|
30 |
+
tokenizer, model, image_processor, context_len = load_pretrained_model(args.model_path, args.load_8bit, args.load_4bit)
|
31 |
+
|
32 |
+
images = [Image.open(args.image_file).convert("RGB")]
|
33 |
+
images_tensor = process_images(images, image_processor, model.config).to(model.device, dtype=torch.float16)
|
34 |
+
|
35 |
+
conv = conv_templates[args.conv_mode].copy()
|
36 |
+
conv.append_message(conv.roles[0], DEFAULT_IMAGE_TOKEN + "\n" + args.prompt)
|
37 |
+
conv.append_message(conv.roles[1], None)
|
38 |
+
prompt = conv.get_prompt()
|
39 |
+
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
|
40 |
+
# Input
|
41 |
+
input_ids = (tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).cuda())
|
42 |
+
stopping_criteria = KeywordsStoppingCriteria([stop_str], tokenizer, input_ids)
|
43 |
+
# Inference
|
44 |
+
with torch.inference_mode():
|
45 |
+
start_time = time.time()
|
46 |
+
output_ids = model.generate(
|
47 |
+
input_ids,
|
48 |
+
images=images_tensor,
|
49 |
+
do_sample=True if args.temperature > 0 else False,
|
50 |
+
temperature=args.temperature,
|
51 |
+
top_p=args.top_p,
|
52 |
+
num_beams=args.num_beams,
|
53 |
+
max_new_tokens=args.max_new_tokens,
|
54 |
+
use_cache=True,
|
55 |
+
stopping_criteria=[stopping_criteria],
|
56 |
+
)
|
57 |
+
end_time = time.time()
|
58 |
+
execution_time = end_time-start_time
|
59 |
+
print("the execution time (secend): ", execution_time)
|
60 |
+
# Result-Decode
|
61 |
+
input_token_len = input_ids.shape[1]
|
62 |
+
n_diff_input_output = (input_ids != output_ids[:, :input_token_len]).sum().item()
|
63 |
+
if n_diff_input_output > 0:
|
64 |
+
print(f"[Warning] {n_diff_input_output} output_ids are not the same as the input_ids")
|
65 |
+
outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0]
|
66 |
+
outputs = outputs.strip()
|
67 |
+
if outputs.endswith(stop_str):
|
68 |
+
outputs = outputs[: -len(stop_str)]
|
69 |
+
print(f"🚀 {model_name}: {outputs.strip()}\n")
|
70 |
+
if __name__ == '__main__':
|
71 |
+
model_path = "XiaoduoAILab/Xmodel_VLM" # model weight file
|
72 |
+
image_file = "assets/demo.jpg" # image file
|
73 |
+
prompt_str = "Who is the author of this book?\nAnswer the question using a single word or phrase."
|
74 |
+
# (or) What is the title of this book?
|
75 |
+
# (or) Is this book related to Education & Teaching?
|
76 |
+
|
77 |
+
args = type('Args', (), {
|
78 |
+
"model_path": model_path,
|
79 |
+
"image_file": image_file,
|
80 |
+
"prompt": prompt_str,
|
81 |
+
"conv_mode": "v1",
|
82 |
+
"temperature": 0,
|
83 |
+
"top_p": None,
|
84 |
+
"num_beams": 1,
|
85 |
+
"max_new_tokens": 512,
|
86 |
+
"load_8bit": False,
|
87 |
+
"load_4bit": False,
|
88 |
+
})()
|
89 |
+
|
90 |
+
inference_once(args)
|
91 |
+
```
|
92 |
+
<p align="center">
|
93 |
+
<textarea>Who is the author of this book?\nAnswer the question using a single word or phrase. </textarea>
|
94 |
+
<img src="https://github.com/XiaoduoAILab/XmodelVLM/blob/main/assets/demo.jpg" width="500"/>
|
95 |
+
<textarea>Susan Wise Bauer</textarea>
|
96 |
+
<p>
|
97 |
+
<br>
|
98 |
+
|
99 |
+
## Evaluation
|
100 |
+
|
101 |
+
We evaluate the multimodal performance across a variety of datasets: VizWiz, SQA$^\text{I}$, VQA$^\text{T}$, POPE, GQA, MMB, MMB$^\text{CN}$
|
102 |
+
, MM-Vet, and MME. Our analysis, as depicted in Table~\ref{tab:compare-with-sotas-vlms}.
|
103 |
+
|
104 |
+
| Method | LLM | Res. | VizWiz | SQA | VQA | POPE | GQA | MMB | MMB^{CN} | MM-Vet | MME |
|
105 |
+
|:--------------:|:----------------:|:----:|:------:|:----:|:----:|:----:|:----:|:----:|:--------:|:------:|:------:|
|
106 |
+
| Openflamingo | MPT-7B | 336 | - | - | 33.6 | - | - | 4.6 | - | - | - |
|
107 |
+
| BLIP-2 | Vicuna-13B | 224 | - | 61.0 | 42.5 | 85.3 | 41.0 | - | - | - | 1293.8 |
|
108 |
+
| MiniGPT-4 | Vicuna-7B | 224 | - | - | - | - | 32.2 | 23.0 | - | - | 581.7 |
|
109 |
+
| InstructBLIP | Vicuna-7B | 224 | - | 60.5 | 50.1 | - | 49.2 | - | - | - | - |
|
110 |
+
| InstructBLIP | Vicuna-13B | 224 | - | 63.1 | 50.7 | 78.9 | 49.5 | - | - | - | 1212.8 |
|
111 |
+
| Shikra | Vicuna-13B | 224 | - | - | - | - | - | 58.8 | - | - | - |
|
112 |
+
| Qwen-VL | Qwen-7B | 448 | - | 67.1 | 63.8 | - | 59.3 | 38.2 | - | - | 1487.6 |
|
113 |
+
| MiniGPT-v2 | LLaMA-7B | 448 | - | - | - | - | 60.3 | 12.2 | - | - | - |
|
114 |
+
| LLaVA-v1.5-13B | Vicuna-13B | 336 | 53.6 | 71.6 | 61.3 | 85.9 | 63.3 | 67.7 | 63.6 | 35.4 | 1531.3 |
|
115 |
+
| MobileVLM 1.7 | MobileLLaMA 1.4B | 336 | 26.3 | 54.7 | 41.5 | 84.5 | 56.1 | 53.2 | 16.67 | 21.7 | 1196.2 |
|
116 |
+
| Xmodel-VLM | Xmodel-LM 1.1B | 336 | 41.7 | 53.3 | 39.9 | 85.9 | 58.3 | 52.0 | 45.7 | 21.8 | 1250.7 |
|