toshi456 commited on
Commit
aa48271
1 Parent(s): 23cb5d2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +135 -0
README.md CHANGED
@@ -1,3 +1,138 @@
1
  ---
2
  license: cc-by-nc-4.0
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: cc-by-nc-4.0
3
+ language:
4
+ - ja
5
+ datasets:
6
+ - toshi456/LLaVA-CC3M-Pretrain-595K-JA
7
+ - turing-motors/LLaVA-Instruct-150K-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.
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'
57
+ model_args.vision_tower = "openai/clip-vit-large-patch14-336"
58
+ device = "cuda" if torch.cuda.is_available() else "cpu"
59
+ torch_dtype = torch.bfloat16 if device=="cuda" else torch.float32
60
+
61
+ model = LlavaGpt2ForCausalLM.from_pretrained(
62
+ model_path,
63
+ low_cpu_mem_usage=True,
64
+ use_safetensors=True,
65
+ torch_dtype=torch_dtype,
66
+ device_map=device,
67
+ )
68
+ tokenizer = transformers.AutoTokenizer.from_pretrained(
69
+ model_path,
70
+ model_max_length=1024,
71
+ padding_side="right",
72
+ use_fast=False,
73
+ )
74
+ model.eval()
75
+
76
+ conv_mode = "v1"
77
+ conv = conv_templates[conv_mode].copy()
78
+
79
+ # image pre-process
80
+ image_url = "https://huggingface.co/rinna/bilingual-gpt-neox-4b-minigpt4/resolve/main/sample.jpg"
81
+ image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
82
+ if device == "cuda":
83
+ image_tensor = model.get_model().vision_tower.image_processor(image, return_tensors='pt')['pixel_values'].half().cuda().to(torch_dtype)
84
+ else:
85
+ image_tensor = model.get_model().vision_tower.image_processor(image, return_tensors='pt')['pixel_values'].to(torch_dtype)
86
+
87
+ # create prompt
88
+ # ユーザー: <image>\n{prompt}
89
+ prompt = "猫の隣には何がありますか?"
90
+ inp = DEFAULT_IMAGE_TOKEN + '\n' + prompt
91
+ conv.append_message(conv.roles[0], inp)
92
+ conv.append_message(conv.roles[1], None)
93
+ prompt = conv.get_prompt()
94
+
95
+ input_ids = tokenizer_image_token(
96
+ prompt,
97
+ tokenizer,
98
+ IMAGE_TOKEN_INDEX,
99
+ return_tensors='pt'
100
+ ).unsqueeze(0)
101
+ if device == "cuda":
102
+ input_ids = input_ids.to(device)
103
+
104
+ input_ids = input_ids[:, :-1] # </sep>がinputの最後に入るので削除する
105
+ stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
106
+ keywords = [stop_str]
107
+ streamer = TextStreamer(tokenizer, skip_prompt=True, timeout=20.0)
108
+
109
+ # predict
110
+ with torch.inference_mode():
111
+ model.generate(
112
+ inputs=input_ids,
113
+ images=image_tensor,
114
+ do_sample=True,
115
+ temperature=0.01,
116
+ top_p=1.0,
117
+ max_new_tokens=256,
118
+ streamer=streamer,
119
+ use_cache=True,
120
+ )
121
+ """ノートパソコン"""
122
+ ```
123
+
124
+ ## Training dataset
125
+ **Stage1 Pretrain**
126
+ - [LLaVA-CC3M-Pretrain-595K-JA](https://huggingface.co/datasets/toshi456/LLaVA-CC3M-Pretrain-595K-JA)
127
+ - [Japanese STAIR Captions](http://captions.stair.center/)
128
+
129
+ **Stage2 Fine-tuning**
130
+ - [LLaVA-Instruct-150K-JA](https://huggingface.co/datasets/turing-motors/LLaVA-Instruct-150K-JA)
131
+ - [Japanese Visual Genome VQA dataset](https://github.com/yahoojapan/ja-vg-vqa)
132
+
133
+ ## Acknowledgement
134
+ - [LLaVA](https://llava-vl.github.io/)
135
+ - [LLM-jp](https://llm-jp.nii.ac.jp/)
136
+
137
+ ## License
138
+ cc-by-nc-4.0