ydshieh HF staff commited on
Commit
d7bcf98
1 Parent(s): 1390b5a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +129 -0
README.md CHANGED
@@ -207,6 +207,135 @@ Here is the annotated image:
207
  <a href="https://huggingface.co/ydshieh/kosmos-2-patch14-224/resolve/main/annotated_snowman.jpg" target="_blank"><img src="https://huggingface.co/ydshieh/kosmos-2-patch14-224/resolve/main/annotated_snowman.jpg" width="500"></a>
208
 
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  ## Running the Flask Server
211
  _flask_kosmos2.py_ shows the implementation of a Flask server for the model.
212
  It allowes the model to be approached as a REST API.
 
207
  <a href="https://huggingface.co/ydshieh/kosmos-2-patch14-224/resolve/main/annotated_snowman.jpg" target="_blank"><img src="https://huggingface.co/ydshieh/kosmos-2-patch14-224/resolve/main/annotated_snowman.jpg" width="500"></a>
208
 
209
 
210
+ ## Different tasks
211
+
212
+ This model is capable of performing different tasks through changing the prompts.
213
+
214
+ First, let's define a function to run a prompt.
215
+
216
+ ```python
217
+ import requests
218
+
219
+ from PIL import Image
220
+ from transformers import AutoProcessor, AutoModelForVision2Seq
221
+
222
+
223
+ model = AutoModelForVision2Seq.from_pretrained("ydshieh/kosmos-2-patch14-224", trust_remote_code=True)
224
+ processor = AutoProcessor.from_pretrained("ydshieh/kosmos-2-patch14-224", trust_remote_code=True)
225
+
226
+ url = "https://huggingface.co/ydshieh/kosmos-2-patch14-224/resolve/main/snowman.png"
227
+ image = Image.open(requests.get(url, stream=True).raw)
228
+
229
+ def run_example(prompt):
230
+
231
+ inputs = processor(text=prompt, images=image, return_tensors="pt")
232
+ generated_ids = model.generate(
233
+ pixel_values=inputs["pixel_values"],
234
+ input_ids=inputs["input_ids"][:, :-1],
235
+ attention_mask=inputs["attention_mask"][:, :-1],
236
+ img_features=None,
237
+ img_attn_mask=inputs["img_attn_mask"][:, :-1],
238
+ use_cache=True,
239
+ max_new_tokens=64,
240
+ )
241
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
242
+ _processed_text = processor.post_process_generation(generated_text, cleanup_and_extract=False)
243
+ processed_text, entities = processor.post_process_generation(generated_text)
244
+ print(processed_text)
245
+ print(entities)
246
+ print(_processed_text)
247
+ ```
248
+
249
+ Here are the tasks `Kosmos-2` could perform:
250
+
251
+ ### Multimodal Grounding
252
+
253
+ #### • Phrase Grounding
254
+ ```python
255
+ prompt = "<grounding><phrase> a snowman</phrase>"
256
+ run_example(prompt)
257
+
258
+ # a snowman is warming himself by the fire
259
+ # [('a snowman', (0, 9), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('the fire', (32, 40), [(0.203125, 0.015625, 0.453125, 0.859375)])]
260
+
261
+ # <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>
262
+ ```
263
+
264
+ #### • Referring Expression Comprehension
265
+ ```python
266
+ prompt = "<grounding><phrase> a snowman</phrase>"
267
+ run_example(prompt)
268
+
269
+ # a snowman is warming himself by the fire
270
+ # [('a snowman', (0, 9), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('the fire', (32, 40), [(0.203125, 0.015625, 0.453125, 0.859375)])]
271
+
272
+ # <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>
273
+ ```
274
+
275
+ ### Multimodal Referring
276
+
277
+ #### • Referring expression generation
278
+ ```python
279
+ prompt = "<grounding><phrase> It</phrase><object><patch_index_0044><patch_index_0863></object> is"
280
+ run_example(prompt)
281
+
282
+ # It is snowman in a hat and scarf
283
+ # [('It', (0, 2), [(0.390625, 0.046875, 0.984375, 0.828125)])]
284
+
285
+ # <grounding><phrase> It</phrase><object><patch_index_0044><patch_index_0863></object> is snowman in a hat and scarf
286
+ ```
287
+
288
+ ### Perception-Language Tasks
289
+
290
+ #### • Grounded VQA
291
+ ```python
292
+ prompt = "<grounding> Question: What is special about this image? Answer:"
293
+ run_example(prompt)
294
+
295
+ # Question: What is special about this image? Answer: The image features a snowman sitting by a campfire in the snow.
296
+ # [('a snowman', (71, 80), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('a campfire', (92, 102), [(0.109375, 0.640625, 0.546875, 0.984375)])]
297
+
298
+ # <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.
299
+ ```
300
+
301
+ #### • Grounded VQA with multimodal referring via bounding boxes
302
+ ```python
303
+ prompt = "<grounding> Question: Where is<phrase> the fire</phrase><object><patch_index_0005><patch_index_0911></object> next to? Answer:"
304
+ run_example(prompt)
305
+
306
+ # Question: Where is the fire next to? Answer: Near the snowman.
307
+ # [('the fire', (19, 27), [(0.171875, 0.015625, 0.484375, 0.890625)]), ('the snowman', (50, 61), [(0.390625, 0.046875, 0.984375, 0.828125)])]
308
+
309
+ # <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>.
310
+ ```
311
+
312
+ ### Grounded Image captioning
313
+
314
+ #### • Brief
315
+
316
+ ```python
317
+ prompt = "<grounding> An image of"
318
+ run_example(prompt)
319
+
320
+ # An image of a snowman warming himself by a campfire.
321
+ # [('a snowman', (12, 21), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('a campfire', (41, 51), [(0.109375, 0.640625, 0.546875, 0.984375)])]
322
+
323
+ # <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>.
324
+ ```
325
+
326
+ #### • Detailed
327
+
328
+ ```python
329
+ prompt = "<grounding> Describe this image in detail:"
330
+ run_example(prompt)
331
+
332
+ # 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
333
+ # [('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)])]
334
+
335
+ # <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>
336
+ ```
337
+
338
+
339
  ## Running the Flask Server
340
  _flask_kosmos2.py_ shows the implementation of a Flask server for the model.
341
  It allowes the model to be approached as a REST API.