toshi456 commited on
Commit
4894483
1 Parent(s): d0521fd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +134 -0
README.md CHANGED
@@ -1,3 +1,137 @@
1
  ---
2
  license: cc-by-nc-4.0
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: cc-by-nc-4.0
3
+ datasets:
4
+ - toshi456/LLaVA-CC3M-Pretrain-595K-JA
5
+ - turing-motors/LLaVA-Instruct-150K-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-CC3M-Pretrain-595K-JA](https://huggingface.co/datasets/toshi456/LLaVA-CC3M-Pretrain-595K-JA) and STAIR Captions. <br>
27
+ In the second phase, it was fine-tuned with LLaVA-Instruct-150K-JA and Japanese Visual Genome.
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-siglip-so400m-patch14-384'
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=1024,
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
+ if device == "cuda":
82
+ image_tensor = model.get_model().vision_tower.image_processor(image, return_tensors='pt')['pixel_values'].half().cuda().to(torch_dtype)
83
+ else:
84
+ image_tensor = model.get_model().vision_tower.image_processor(image, return_tensors='pt')['pixel_values'].to(torch_dtype)
85
+
86
+ # create prompt
87
+ # ユーザー: <image>\n{prompt}
88
+ prompt = "猫の隣には何がありますか?"
89
+ inp = DEFAULT_IMAGE_TOKEN + '\n' + prompt
90
+ conv.append_message(conv.roles[0], inp)
91
+ conv.append_message(conv.roles[1], None)
92
+ prompt = conv.get_prompt()
93
+
94
+ input_ids = tokenizer_image_token(
95
+ prompt,
96
+ tokenizer,
97
+ IMAGE_TOKEN_INDEX,
98
+ return_tensors='pt'
99
+ ).unsqueeze(0)
100
+ if device == "cuda":
101
+ input_ids = input_ids.to(device)
102
+
103
+ input_ids = input_ids[:, :-1] # </sep>がinputの最後に入るので削除する
104
+ stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
105
+ keywords = [stop_str]
106
+ streamer = TextStreamer(tokenizer, skip_prompt=True, timeout=20.0)
107
+
108
+ # predict
109
+ with torch.inference_mode():
110
+ model.generate(
111
+ inputs=input_ids,
112
+ images=image_tensor,
113
+ do_sample=True,
114
+ temperature=0.01,
115
+ top_p=1.0,
116
+ max_new_tokens=256,
117
+ streamer=streamer,
118
+ use_cache=True,
119
+ )
120
+ """猫の隣にはノートパソコンがある。<EOD|LLM-jp>"""
121
+ ```
122
+
123
+ ## Training dataset
124
+ **Stage1 Pretrain**
125
+ - [LLaVA-CC3M-Pretrain-595K-JA](https://huggingface.co/datasets/toshi456/LLaVA-CC3M-Pretrain-595K-JA)
126
+ - [Japanese STAIR Captions](http://captions.stair.center/)
127
+
128
+ **Stage2 Fine-tuning**
129
+ - [LLaVA-Instruct-150K-JA](https://huggingface.co/datasets/turing-motors/LLaVA-Instruct-150K-JA)
130
+ - [Japanese Visual Genome VQA dataset](https://github.com/yahoojapan/ja-vg-vqa)
131
+
132
+ ## Acknowledgement
133
+ - [LLaVA](https://llava-vl.github.io/)
134
+ - [LLM-jp](https://llm-jp.nii.ac.jp/)
135
+
136
+ ## License
137
+ cc-by-nc-4.0