toshi456 commited on
Commit
5f0fa15
1 Parent(s): 03c4bc2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +145 -3
README.md CHANGED
@@ -1,3 +1,145 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ datasets:
4
+ - toshi456/llava-jp-instruct-108k
5
+ - turing-motors/LLaVA-Pretrain-JA
6
+ language:
7
+ - ja
8
+ pipeline_tag: image-to-text
9
+ ---
10
+
11
+ # LLaVA-JP Model Card
12
+
13
+ ## Model detail
14
+
15
+ **Model type:**
16
+
17
+ LLaVA-JP is a vision-language model that can converse about input images.<br>
18
+ This model is an LVLM model trained using [google/siglip-so400m-patch14-384](https://huggingface.co/google/siglip-so400m-patch14-384) as the image encoder and [llm-jp/llm-jp-1.3b-v1.0](https://huggingface.co/llm-jp/llm-jp-1.3b-v1.0) as the text decoder. supports the input of 768 x 768 high resolution images by scaling_on_scales method.
19
+
20
+ **Training:**
21
+
22
+ This model was initially trained with the Vision Projector using LLaVA-Pretrain-JA.<br>
23
+ In the second phase, it was fine-tuned with LLaVA-JP-Instruct-108K.
24
+
25
+ resources for more information: https://github.com/tosiyuki/LLaVA-JP/tree/main
26
+
27
+ **Comparing VLMs**
28
+
29
+ ## How to use the model
30
+ **1. Download dependencies**
31
+ ```
32
+ git clone https://github.com/tosiyuki/LLaVA-JP.git
33
+ ```
34
+
35
+ **2. Inference**
36
+ ```python
37
+ import requests
38
+ import torch
39
+ import transformers
40
+ from PIL import Image
41
+
42
+ from transformers.generation.streamers import TextStreamer
43
+ from llava.constants import DEFAULT_IMAGE_TOKEN, IMAGE_TOKEN_INDEX
44
+ from llava.conversation import conv_templates, SeparatorStyle
45
+ from llava.model.llava_gpt2 import LlavaGpt2ForCausalLM
46
+ from llava.train.arguments_dataclass import ModelArguments, DataArguments, TrainingArguments
47
+ from llava.train.dataset import tokenizer_image_token
48
+
49
+
50
+ if __name__ == "__main__":
51
+ model_path = 'toshi456/llava-jp-1.3b-v1.1.1'
52
+ device = "cuda" if torch.cuda.is_available() else "cpu"
53
+ torch_dtype = torch.bfloat16 if device=="cuda" else torch.float32
54
+
55
+ model = LlavaGpt2ForCausalLM.from_pretrained(
56
+ model_path,
57
+ low_cpu_mem_usage=True,
58
+ use_safetensors=True,
59
+ torch_dtype=torch_dtype,
60
+ device_map=device,
61
+ )
62
+ tokenizer = transformers.AutoTokenizer.from_pretrained(
63
+ model_path,
64
+ model_max_length=1532,
65
+ padding_side="right",
66
+ use_fast=False,
67
+ )
68
+ model.eval()
69
+
70
+ conv_mode = "v1"
71
+ conv = conv_templates[conv_mode].copy()
72
+
73
+ # image pre-process
74
+ image_url = "https://huggingface.co/rinna/bilingual-gpt-neox-4b-minigpt4/resolve/main/sample.jpg"
75
+ image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
76
+
77
+ image_size = model.get_model().vision_tower.image_processor.size["height"]
78
+ if model.get_model().vision_tower.scales is not None:
79
+ image_size = model.get_model().vision_tower.image_processor.size["height"] * len(model.get_model().vision_tower.scales)
80
+
81
+ if device == "cuda":
82
+ image_tensor = model.get_model().vision_tower.image_processor(
83
+ image,
84
+ return_tensors='pt',
85
+ size={"height": image_size, "width": image_size}
86
+ )['pixel_values'].half().cuda().to(torch_dtype)
87
+ else:
88
+ image_tensor = model.get_model().vision_tower.image_processor(
89
+ image,
90
+ return_tensors='pt',
91
+ size={"height": image_size, "width": image_size}
92
+ )['pixel_values'].to(torch_dtype)
93
+
94
+ # create prompt
95
+ # ユーザー: <image>\n{prompt}
96
+ prompt = "猫の隣には何がありますか?"
97
+ inp = DEFAULT_IMAGE_TOKEN + '\n' + prompt
98
+ conv.append_message(conv.roles[0], inp)
99
+ conv.append_message(conv.roles[1], None)
100
+ prompt = conv.get_prompt()
101
+
102
+ input_ids = tokenizer_image_token(
103
+ prompt,
104
+ tokenizer,
105
+ IMAGE_TOKEN_INDEX,
106
+ return_tensors='pt'
107
+ ).unsqueeze(0)
108
+ if device == "cuda":
109
+ input_ids = input_ids.to(device)
110
+
111
+ input_ids = input_ids[:, :-1] # </sep>がinputの最後に入るので削除する
112
+ stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
113
+ keywords = [stop_str]
114
+ streamer = TextStreamer(tokenizer, skip_prompt=True, timeout=20.0)
115
+
116
+ # predict
117
+ with torch.inference_mode():
118
+ model.generate(
119
+ inputs=input_ids,
120
+ images=image_tensor,
121
+ do_sample=True,
122
+ temperature=0.1,
123
+ top_p=1.0,
124
+ max_new_tokens=256,
125
+ streamer=streamer,
126
+ use_cache=True,
127
+ )
128
+ """猫の隣にはノートパソコンがあります。"""
129
+
130
+ ```
131
+
132
+ ## Training dataset
133
+ **Stage1 Pretrain**
134
+ - [LLaVA-Pretrain-JA](https://huggingface.co/datasets/turing-motors/LLaVA-Pretrain-JA)
135
+
136
+ **Stage2 Fine-tuning**
137
+ - [LLaVA-JP-Instruct-108K](https://huggingface.co/datasets/toshi456/LLaVA-JP-Instruct-108K)
138
+
139
+ ## Acknowledgement
140
+ - [LLaVA](https://llava-vl.github.io/)
141
+ - [LLM-jp](https://llm-jp.nii.ac.jp/)
142
+ - [scaling_on_scales](https://github.com/bfshi/scaling_on_scales/tree/master)
143
+
144
+ ## License
145
+ Apache License 2.0