toshi456 commited on
Commit
0ce0dd2
1 Parent(s): 14ad8b7

Update README.md

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