ybelkada commited on
Commit
dd2cb0a
1 Parent(s): c49a011

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +118 -0
README.md ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ pipeline_tag: image-to-text
5
+ inference: false
6
+ arxiv: 2304.08485
7
+ ---
8
+ # VipLLaVA Model Card
9
+
10
+ ![image/png](https://github.com/mu-cai/ViP-LLaVA/blob/main/images/vip-llava_arch.png?raw=true)
11
+
12
+ Below is the model card of VipLlava model 7b, which is copied from the original Llava model card that you can find [here](https://huggingface.co/liuhaotian/llava-v1.5-13b).
13
+
14
+ Check out also the Google Colab demo to run Llava on a free-tier Google Colab instance (the model works similarly as Llava): [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1qsl6cd2c8gGtEW1xV5io7S8NHh-Cp1TV?usp=sharing)
15
+
16
+ Or check out our Spaces demo! [![Open in Spaces](https://huggingface.co/datasets/huggingface/badges/resolve/main/open-in-hf-spaces-md-dark.svg)](https://huggingface.co/spaces/llava-hf/llava-4bit)
17
+
18
+
19
+ ## Model details
20
+
21
+ **Model type:**
22
+ LLaVA is an open-source chatbot trained by fine-tuning LLaMA/Vicuna on GPT-generated multimodal instruction-following data.
23
+ It is an auto-regressive language model, based on the transformer architecture.
24
+
25
+ **Model date:**
26
+ LLaVA-v1.5-7B was trained in September 2023.
27
+
28
+ **Paper or resources for more information:**
29
+ https://llava-vl.github.io/
30
+
31
+ ## How to use the model
32
+
33
+ First, make sure to have `transformers >= 4.35.3`.
34
+ The model supports multi-image and multi-prompt generation. Meaning that you can pass multiple images in your prompt. Make sure also to follow the correct prompt template (`USER: xxx\nASSISTANT:`) and add the token `<image>` to the location where you want to query images:
35
+
36
+ ### Using `pipeline`:
37
+
38
+ Below we used [`"llava-hf/llava-1.5-7b-hf"`](https://huggingface.co/llava-hf/llava-1.5-7b-hf) checkpoint.
39
+
40
+ ```python
41
+ from transformers import pipeline
42
+ from PIL import Image
43
+ import requests
44
+
45
+ model_id = "llava-hf/vip-llava-7b-hf"
46
+ pipe = pipeline("image-to-text", model=model_id)
47
+ url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
48
+
49
+ image = Image.open(requests.get(url, stream=True).raw)
50
+ prompt = "USER: <image>\nWhat does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nASSISTANT:"
51
+
52
+ outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
53
+ print(outputs)
54
+ ```
55
+
56
+ ### Using pure `transformers`:
57
+
58
+ Below is an example script to run generation in `float16` precision on a GPU device:
59
+
60
+ ```python
61
+ import requests
62
+ from PIL import Image
63
+
64
+ import torch
65
+ from transformers import AutoProcessor, VipLlavaForConditionalGeneration
66
+
67
+ model_id = "llava-hf/vip-llava-7b-hf"
68
+
69
+ prompt = "USER: <image>\nWhat are these?\nASSISTANT:"
70
+ image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
71
+
72
+ model = VipLlavaForConditionalGeneration.from_pretrained(
73
+ model_id,
74
+ torch_dtype=torch.float16,
75
+ low_cpu_mem_usage=True,
76
+ ).to(0)
77
+
78
+ processor = AutoProcessor.from_pretrained(model_id)
79
+
80
+
81
+ raw_image = Image.open(requests.get(image_file, stream=True).raw)
82
+ inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
83
+
84
+ output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
85
+ print(processor.decode(output[0][2:], skip_special_tokens=True))
86
+ ```
87
+
88
+ ### Model optimization
89
+
90
+ #### 4-bit quantization through `bitsandbytes` library
91
+
92
+ First make sure to install `bitsandbytes`, `pip install bitsandbytes` and make sure to have access to a CUDA compatible GPU device. Simply change the snippet above with:
93
+
94
+ ```diff
95
+ model = VipLlavaForConditionalGeneration.from_pretrained(
96
+ model_id,
97
+ torch_dtype=torch.float16,
98
+ low_cpu_mem_usage=True,
99
+ + load_in_4bit=True
100
+ )
101
+ ```
102
+
103
+ #### Use Flash-Attention 2 to further speed-up generation
104
+
105
+ First make sure to install `flash-attn`. Refer to the [original repository of Flash Attention](https://github.com/Dao-AILab/flash-attention) regarding that package installation. Simply change the snippet above with:
106
+
107
+ ```diff
108
+ model = VipLlavaForConditionalGeneration.from_pretrained(
109
+ model_id,
110
+ torch_dtype=torch.float16,
111
+ low_cpu_mem_usage=True,
112
+ + use_flash_attention_2=True
113
+ ).to(0)
114
+ ```
115
+
116
+ ## License
117
+ Llama 2 is licensed under the LLAMA 2 Community License,
118
+ Copyright (c) Meta Platforms, Inc. All Rights Reserved.