ybelkada commited on
Commit
c018164
1 Parent(s): 0018076

Create README.md

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