ydshieh HF staff commited on
Commit
6016943
·
1 Parent(s): 8bc83fb

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +193 -0
README.md ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ # For reference on model card metadata, see the spec: https://github.com/huggingface/hub-docs/blob/main/modelcard.md?plain=1
3
+ # Doc / guide: https://huggingface.co/docs/hub/model-cards
4
+ {}
5
+ ---
6
+ # Kosmos-2: Grounding Multimodal Large Language Models to the World
7
+
8
+ <a href="https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/annotated_snowman.jpg" target="_blank"><figure><img src="https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/annotated_snowman.jpg" width="384"><figcaption><b>[An image of a snowman warming himself by a fire.]</b></figcaption></figure></a>
9
+
10
+
11
+ This Hub repository contains a HuggingFace's `transformers` implementation of [the original Kosmos-2 model](https://github.com/microsoft/unilm/tree/master/kosmos-2) from Microsoft.
12
+
13
+ ## How to Get Started with the Model
14
+
15
+ Use the code below to get started with the model.
16
+
17
+ ```python
18
+ import requests
19
+
20
+ from PIL import Image
21
+ from transformers import AutoProcessor, AutoModelForVision2Seq
22
+
23
+
24
+ model = AutoModelForVision2Seq.from_pretrained("microsoft/kosmos-2-patch14-224")
25
+ processor = AutoProcessor.from_pretrained("microsoft/kosmos-2-patch14-224")
26
+
27
+ prompt = "<grounding>An image of"
28
+
29
+ url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.png"
30
+ image = Image.open(requests.get(url, stream=True).raw)
31
+
32
+ # The original Kosmos-2 demo saves the image first then reload it. For some images, this will give slightly different image input and change the generation outputs.
33
+ image.save("new_image.jpg")
34
+ image = Image.open("new_image.jpg")
35
+
36
+ inputs = processor(text=prompt, images=image, return_tensors="pt", add_eos_token=False)
37
+
38
+ generated_ids = model.generate(
39
+ pixel_values=inputs["pixel_values"],
40
+ input_ids=inputs["input_ids"],
41
+ attention_mask=inputs["attention_mask"],
42
+ image_embeds=None,
43
+ image_embeds_position_mask=inputs["image_embeds_position_mask"],
44
+ use_cache=True,
45
+ max_new_tokens=128,
46
+ )
47
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
48
+
49
+ # Specify `cleanup_and_extract=False` in order to see the raw model generation.
50
+ processed_text = processor.post_process_generation(generated_text, cleanup_and_extract=False)
51
+
52
+ print(processed_text)
53
+ # `<grounding> An image of<phrase> a snowman</phrase><object><patch_index_0044><patch_index_0863></object> warming himself by<phrase> a fire</phrase><object><patch_index_0005><patch_index_0911></object>.`
54
+
55
+ # By default, the generated text is cleanup and the entities are extracted.
56
+ processed_text, entities = processor.post_process_generation(generated_text)
57
+
58
+ print(processed_text)
59
+ # `An image of a snowman warming himself by a fire.`
60
+
61
+ print(entities)
62
+ # `[('a snowman', (12, 21), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('a fire', (41, 47), [(0.171875, 0.015625, 0.484375, 0.890625)])]`
63
+
64
+ ```
65
+
66
+
67
+ ## Tasks
68
+
69
+ This model is capable of performing different tasks through changing the prompts.
70
+
71
+ First, let's define a function to run a prompt.
72
+
73
+ ```python
74
+ import requests
75
+
76
+ from PIL import Image
77
+ from transformers import AutoProcessor, AutoModelForVision2Seq
78
+
79
+
80
+ model = AutoModelForVision2Seq.from_pretrained("microsoft/kosmos-2-patch14-224")
81
+ processor = AutoProcessor.from_pretrained("microsoft/kosmos-2-patch14-224")
82
+
83
+ url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.png"
84
+ image = Image.open(requests.get(url, stream=True).raw)
85
+
86
+ def run_example(prompt):
87
+
88
+ inputs = processor(text=prompt, images=image, return_tensors="pt")
89
+ generated_ids = model.generate(
90
+ pixel_values=inputs["pixel_values"],
91
+ input_ids=inputs["input_ids"][:, :-1],
92
+ attention_mask=inputs["attention_mask"][:, :-1],
93
+ img_features=None,
94
+ img_attn_mask=inputs["img_attn_mask"][:, :-1],
95
+ use_cache=True,
96
+ max_new_tokens=64,
97
+ )
98
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
99
+ _processed_text = processor.post_process_generation(generated_text, cleanup_and_extract=False)
100
+ processed_text, entities = processor.post_process_generation(generated_text)
101
+ print(processed_text)
102
+ print(entities)
103
+ print(_processed_text)
104
+ ```
105
+
106
+ Here are the tasks `Kosmos-2` could perform:
107
+
108
+ ### Multimodal Grounding
109
+
110
+ #### • Phrase Grounding
111
+ ```python
112
+ prompt = "<grounding><phrase> a snowman</phrase>"
113
+ run_example(prompt)
114
+
115
+ # a snowman is warming himself by the fire
116
+ # [('a snowman', (0, 9), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('the fire', (32, 40), [(0.203125, 0.015625, 0.453125, 0.859375)])]
117
+
118
+ # <grounding><phrase> a snowman</phrase><object><patch_index_0044><patch_index_0863></object> is warming himself by<phrase> the fire</phrase><object><patch_index_0006><patch_index_0878></object>
119
+ ```
120
+
121
+ #### • Referring Expression Comprehension
122
+ ```python
123
+ prompt = "<grounding><phrase> a snowman next to a fire</phrase>"
124
+ run_example(prompt)
125
+
126
+ # a snowman next to a fire
127
+ # [('a snowman next to a fire', (0, 24), [(0.390625, 0.046875, 0.984375, 0.828125)])]
128
+
129
+ # <grounding><phrase> a snowman next to a fire</phrase><object><patch_index_0044><patch_index_0863></object>
130
+ ```
131
+
132
+ ### Multimodal Referring
133
+
134
+ #### • Referring expression generation
135
+ ```python
136
+ prompt = "<grounding><phrase> It</phrase><object><patch_index_0044><patch_index_0863></object> is"
137
+ run_example(prompt)
138
+
139
+ # It is snowman in a hat and scarf
140
+ # [('It', (0, 2), [(0.390625, 0.046875, 0.984375, 0.828125)])]
141
+
142
+ # <grounding><phrase> It</phrase><object><patch_index_0044><patch_index_0863></object> is snowman in a hat and scarf
143
+ ```
144
+
145
+ ### Perception-Language Tasks
146
+
147
+ #### • Grounded VQA
148
+ ```python
149
+ prompt = "<grounding> Question: What is special about this image? Answer:"
150
+ run_example(prompt)
151
+
152
+ # Question: What is special about this image? Answer: The image features a snowman sitting by a campfire in the snow.
153
+ # [('a snowman', (71, 80), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('a campfire', (92, 102), [(0.109375, 0.640625, 0.546875, 0.984375)])]
154
+
155
+ # <grounding> Question: What is special about this image? Answer: The image features<phrase> a snowman</phrase><object><patch_index_0044><patch_index_0863></object> sitting by<phrase> a campfire</phrase><object><patch_index_0643><patch_index_1009></object> in the snow.
156
+ ```
157
+
158
+ #### • Grounded VQA with multimodal referring via bounding boxes
159
+ ```python
160
+ prompt = "<grounding> Question: Where is<phrase> the fire</phrase><object><patch_index_0005><patch_index_0911></object> next to? Answer:"
161
+ run_example(prompt)
162
+
163
+ # Question: Where is the fire next to? Answer: Near the snowman.
164
+ # [('the fire', (19, 27), [(0.171875, 0.015625, 0.484375, 0.890625)]), ('the snowman', (50, 61), [(0.390625, 0.046875, 0.984375, 0.828125)])]
165
+
166
+ # <grounding> Question: Where is<phrase> the fire</phrase><object><patch_index_0005><patch_index_0911></object> next to? Answer: Near<phrase> the snowman</phrase><object><patch_index_0044><patch_index_0863></object>.
167
+ ```
168
+
169
+ ### Grounded Image captioning
170
+
171
+ #### • Brief
172
+
173
+ ```python
174
+ prompt = "<grounding> An image of"
175
+ run_example(prompt)
176
+
177
+ # An image of a snowman warming himself by a campfire.
178
+ # [('a snowman', (12, 21), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('a campfire', (41, 51), [(0.109375, 0.640625, 0.546875, 0.984375)])]
179
+
180
+ # <grounding> An image of<phrase> a snowman</phrase><object><patch_index_0044><patch_index_0863></object> warming himself by<phrase> a campfire</phrase><object><patch_index_0643><patch_index_1009></object>.
181
+ ```
182
+
183
+ #### • Detailed
184
+
185
+ ```python
186
+ prompt = "<grounding> Describe this image in detail:"
187
+ run_example(prompt)
188
+
189
+ # Describe this image in detail: The image features a snowman sitting by a campfire in the snow. He is wearing a hat, scarf, and gloves, with a pot nearby and a cup
190
+ # [('a campfire', (71, 81), [(0.171875, 0.015625, 0.484375, 0.984375)]), ('a hat', (109, 114), [(0.515625, 0.046875, 0.828125, 0.234375)]), ('scarf', (116, 121), [(0.515625, 0.234375, 0.890625, 0.578125)]), ('gloves', (127, 133), [(0.515625, 0.390625, 0.640625, 0.515625)]), ('a pot', (140, 145), [(0.078125, 0.609375, 0.265625, 0.859375)])]
191
+
192
+ # <grounding> Describe this image in detail: The image features a snowman sitting by<phrase> a campfire</phrase><object><patch_index_0005><patch_index_1007></object> in the snow. He is wearing<phrase> a hat</phrase><object><patch_index_0048><patch_index_0250></object>,<phrase> scarf</phrase><object><patch_index_0240><patch_index_0604></object>, and<phrase> gloves</phrase><object><patch_index_0400><patch_index_0532></object>, with<phrase> a pot</phrase><object><patch_index_0610><patch_index_0872></object> nearby and<phrase> a cup</phrase><object>
193
+ ```