Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- en
|
4 |
+
pipeline_tag: image-to-text
|
5 |
+
inference: false
|
6 |
+
arxiv: 2304.08485
|
7 |
+
---
|
8 |
+
# LLaVA Model Card
|
9 |
+
|
10 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/62441d1d9fdefb55a0b7d12c/FPshq08TKYD0e-qwPLDVO.png)
|
11 |
+
|
12 |
+
Below is the model card of Llava model 13b, which is copied from the original Llava model card that you can find [here](https://huggingface.co/liuhaotian/llava-v1.5-13b).
|
13 |
+
|
14 |
+
## Model details
|
15 |
+
|
16 |
+
**Model type:**
|
17 |
+
LLaVA is an open-source chatbot trained by fine-tuning LLaMA/Vicuna on GPT-generated multimodal instruction-following data.
|
18 |
+
It is an auto-regressive language model, based on the transformer architecture.
|
19 |
+
|
20 |
+
**Model date:**
|
21 |
+
LLaVA-v1.5-13B was trained in September 2023.
|
22 |
+
|
23 |
+
**Paper or resources for more information:**
|
24 |
+
https://llava-vl.github.io/
|
25 |
+
|
26 |
+
## How to use the model
|
27 |
+
|
28 |
+
First, make sure to have `transformers >= 4.35.3`.
|
29 |
+
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:
|
30 |
+
|
31 |
+
### Using `pipeline`:
|
32 |
+
|
33 |
+
Below we used [`"llava-hf/bakLlava-v1-hf"`](https://huggingface.co/llava-hf/bakLlava-v1-hf) checkpoint.
|
34 |
+
|
35 |
+
```python
|
36 |
+
from transformers import pipeline
|
37 |
+
from PIL import Image
|
38 |
+
import request
|
39 |
+
|
40 |
+
model_id = "llava-hf/bakLlava-v1-hf"
|
41 |
+
pipe = pipeline("image-to-text", model=model_id)
|
42 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
|
43 |
+
|
44 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
45 |
+
prompt = "<image>\nUSER: What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nASSISTANT:"
|
46 |
+
|
47 |
+
outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
|
48 |
+
print(outputs)
|
49 |
+
>>> {"generated_text": "\nUSER: What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nASSISTANT: Lava"}
|
50 |
+
```
|
51 |
+
|
52 |
+
### Using pure `transformers`:
|
53 |
+
|
54 |
+
Below is an example script to run generation in `float16` precision on a GPU device:
|
55 |
+
|
56 |
+
```python
|
57 |
+
import requests
|
58 |
+
from PIL import Image
|
59 |
+
|
60 |
+
import torch
|
61 |
+
from transformers import AutoProcessor, LlavaForConditionalGeneration
|
62 |
+
|
63 |
+
model_id = "llava-hf/llava-1.5-7b-hf"
|
64 |
+
|
65 |
+
prompt = "<image> \nUSER: What are these?\nASSISTANT:"
|
66 |
+
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
67 |
+
|
68 |
+
model = LlavaForConditionalGeneration.from_pretrained(
|
69 |
+
model_id,
|
70 |
+
torch_dtype=torch.float16,
|
71 |
+
low_cpu_mem_usage=True,
|
72 |
+
).to(0)
|
73 |
+
|
74 |
+
|
75 |
+
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
76 |
+
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
|
77 |
+
|
78 |
+
output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
|
79 |
+
print(processor.decode(output[0][2:], skip_special_tokens=True))
|
80 |
+
```
|
81 |
+
|
82 |
+
### Model optimization
|
83 |
+
|
84 |
+
#### 4-bit quantization through `bitsandbytes` library
|
85 |
+
|
86 |
+
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:
|
87 |
+
|
88 |
+
```diff
|
89 |
+
model = LlavaForConditionalGeneration.from_pretrained(
|
90 |
+
model_id,
|
91 |
+
torch_dtype=torch.float16,
|
92 |
+
low_cpu_mem_usage=True,
|
93 |
+
+ load_in_4bit=True
|
94 |
+
)
|
95 |
+
```
|
96 |
+
|
97 |
+
#### Use Flash-Attention 2 to further speed-up generation
|
98 |
+
|
99 |
+
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:
|
100 |
+
|
101 |
+
```diff
|
102 |
+
model = LlavaForConditionalGeneration.from_pretrained(
|
103 |
+
model_id,
|
104 |
+
torch_dtype=torch.float16,
|
105 |
+
low_cpu_mem_usage=True,
|
106 |
+
+ use_flash_attention_2=True
|
107 |
+
).to(0)
|
108 |
+
```
|
109 |
+
|
110 |
+
## License
|
111 |
+
Llama 2 is licensed under the LLAMA 2 Community License,
|
112 |
+
Copyright (c) Meta Platforms, Inc. All Rights Reserved.
|