Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- en
|
4 |
+
pipeline_tag: image-to-text
|
5 |
+
inference: false
|
6 |
+
arxiv: 2304.08485
|
7 |
+
---
|
8 |
+
# BakLLaVA Model Card
|
9 |
+
|
10 |
+
BakLlava is a model that is derived from the original Llava architecture, that uses Mistral-7b as a text backbone.
|
11 |
+
|
12 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/64b7e345f92b20f7a38bf47a/V5lpOHWGGYJ2yPpEo_8i1.png)
|
13 |
+
|
14 |
+
Below is the model card of BakLlava model 7b, which is copied from the original BakLlava model card that you can find [here](https://huggingface.co/SkunkworksAI/BakLLaVA-1).
|
15 |
+
|
16 |
+
> BakLLaVA 1 is a Mistral 7B base augmented with the LLaVA 1.5 architecture. In this first version, we showcase that a Mistral 7B base outperforms Llama 2 13B on several benchmarks.
|
17 |
+
You can run BakLLaVA-1 on our repo. We are currently updating it to make it easier for you to finetune and inference. (https://github.com/SkunkworksAI/BakLLaVA).
|
18 |
+
|
19 |
+
> Note: BakLLaVA-1 is fully open-source but was trained on certain data that includes LLaVA's corpus which is not commercially permissive. We will fix this in the upcoming release.
|
20 |
+
|
21 |
+
> BakLLaVA 2 is cooking with a significantly larger (commercially viable) dataset and a novel architecture that expands beyond the current LLaVA method. BakLLaVA-2 will do away with the restrictions of BakLLaVA-1.
|
22 |
+
|
23 |
+
|
24 |
+
## How to use the model
|
25 |
+
|
26 |
+
First, make sure to have `transformers >= 4.35.3`.
|
27 |
+
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:
|
28 |
+
|
29 |
+
### Using `pipeline`:
|
30 |
+
|
31 |
+
|
32 |
+
```python
|
33 |
+
from transformers import pipeline
|
34 |
+
from PIL import Image
|
35 |
+
import request
|
36 |
+
|
37 |
+
model_id = "llava-hf/bakLlava-v1-hf"
|
38 |
+
pipe = pipeline("image-to-text", model=model_id)
|
39 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
|
40 |
+
|
41 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
42 |
+
prompt = "<image>\nUSER: What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nASSISTANT:"
|
43 |
+
|
44 |
+
outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
|
45 |
+
print(outputs)
|
46 |
+
>>> {"generated_text": "\nUSER: What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nASSISTANT: Lava"}
|
47 |
+
```
|
48 |
+
|
49 |
+
### Using pure `transformers`:
|
50 |
+
|
51 |
+
Below is an example script to run generation in `float16` precision on a GPU device:
|
52 |
+
|
53 |
+
```python
|
54 |
+
import requests
|
55 |
+
from PIL import Image
|
56 |
+
|
57 |
+
import torch
|
58 |
+
from transformers import AutoProcessor, LlavaForConditionalGeneration
|
59 |
+
|
60 |
+
model_id = "llava-hf/bakLlava-v1-hf"
|
61 |
+
|
62 |
+
prompt = "<image> \nUSER: What are these?\nASSISTANT:"
|
63 |
+
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
64 |
+
|
65 |
+
model = LlavaForConditionalGeneration.from_pretrained(
|
66 |
+
model_id,
|
67 |
+
torch_dtype=torch.float16,
|
68 |
+
low_cpu_mem_usage=True,
|
69 |
+
).to(0)
|
70 |
+
|
71 |
+
|
72 |
+
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
73 |
+
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
|
74 |
+
|
75 |
+
output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
|
76 |
+
print(processor.decode(output[0][2:], skip_special_tokens=True))
|
77 |
+
```
|
78 |
+
|
79 |
+
### Model optimization
|
80 |
+
|
81 |
+
#### 4-bit quantization through `bitsandbytes` library
|
82 |
+
|
83 |
+
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:
|
84 |
+
|
85 |
+
```diff
|
86 |
+
model = LlavaForConditionalGeneration.from_pretrained(
|
87 |
+
model_id,
|
88 |
+
torch_dtype=torch.float16,
|
89 |
+
low_cpu_mem_usage=True,
|
90 |
+
+ load_in_4bit=True
|
91 |
+
)
|
92 |
+
```
|
93 |
+
|
94 |
+
#### Use Flash-Attention 2 to further speed-up generation
|
95 |
+
|
96 |
+
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:
|
97 |
+
|
98 |
+
```diff
|
99 |
+
model = LlavaForConditionalGeneration.from_pretrained(
|
100 |
+
model_id,
|
101 |
+
torch_dtype=torch.float16,
|
102 |
+
low_cpu_mem_usage=True,
|
103 |
+
+ use_flash_attention_2=True
|
104 |
+
).to(0)
|
105 |
+
```
|
106 |
+
|
107 |
+
# Evaluations
|
108 |
+
|
109 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/64b7e345f92b20f7a38bf47a/qdYubrBmF7ztAHgdfkkwG.png)
|
110 |
+
|
111 |
+
# Training dataset
|
112 |
+
|
113 |
+
- 558K filtered image-text pairs from LAION/CC/SBU, captioned by BLIP.
|
114 |
+
- 158K GPT-generated multimodal instruction-following data.
|
115 |
+
- 450K academic-task-oriented VQA data mixture.
|
116 |
+
- 40K ShareGPT data.
|
117 |
+
- Additional private data (permissive)
|
118 |
+
|
119 |
+
## License
|
120 |
+
Llama 2 is licensed under the LLAMA 2 Community License,
|
121 |
+
Copyright (c) Meta Platforms, Inc. All Rights Reserved.
|