Chandans01 commited on
Commit
9d392e7
·
verified ·
1 Parent(s): 0ea577e

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +174 -0
README.md ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ license: mit
4
+ tags:
5
+ - vision
6
+ - image-to-text
7
+ - image-captioning
8
+ - visual-question-answering
9
+ pipeline_tag: image-to-text
10
+ ---
11
+
12
+ # BLIP-2, OPT-2.7b, pre-trained only
13
+
14
+ BLIP-2 model, leveraging [OPT-2.7b](https://huggingface.co/facebook/opt-2.7b) (a large language model with 2.7 billion parameters).
15
+ It was introduced in the paper [BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models](https://arxiv.org/abs/2301.12597) by Li et al. and first released in [this repository](https://github.com/salesforce/LAVIS/tree/main/projects/blip2).
16
+
17
+ Disclaimer: The team releasing BLIP-2 did not write a model card for this model so this model card has been written by the Hugging Face team.
18
+
19
+ ## Model description
20
+
21
+ BLIP-2 consists of 3 models: a CLIP-like image encoder, a Querying Transformer (Q-Former) and a large language model.
22
+
23
+ The authors initialize the weights of the image encoder and large language model from pre-trained checkpoints and keep them frozen
24
+ while training the Querying Transformer, which is a BERT-like Transformer encoder that maps a set of "query tokens" to query embeddings,
25
+ which bridge the gap between the embedding space of the image encoder and the large language model.
26
+
27
+ The goal for the model is simply to predict the next text token, giving the query embeddings and the previous text.
28
+
29
+ <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/blip2_architecture.jpg"
30
+ alt="drawing" width="600"/>
31
+
32
+ This allows the model to be used for tasks like:
33
+
34
+ - image captioning
35
+ - visual question answering (VQA)
36
+ - chat-like conversations by feeding the image and the previous conversation as prompt to the model
37
+
38
+ ## Direct Use and Downstream Use
39
+
40
+ You can use the raw model for conditional text generation given an image and optional text. See the [model hub](https://huggingface.co/models?search=Salesforce/blip) to look for
41
+ fine-tuned versions on a task that interests you.
42
+
43
+ ## Bias, Risks, Limitations, and Ethical Considerations
44
+
45
+ BLIP2-OPT uses off-the-shelf OPT as the language model. It inherits the same risks and limitations as mentioned in Meta's model card.
46
+
47
+ > Like other large language models for which the diversity (or lack thereof) of training
48
+ > data induces downstream impact on the quality of our model, OPT-175B has limitations in terms
49
+ > of bias and safety. OPT-175B can also have quality issues in terms of generation diversity and
50
+ > hallucination. In general, OPT-175B is not immune from the plethora of issues that plague modern
51
+ > large language models.
52
+ >
53
+ BLIP2 is fine-tuned on image-text datasets (e.g. [LAION](https://laion.ai/blog/laion-400-open-dataset/) ) collected from the internet. As a result the model itself is potentially vulnerable to generating equivalently inappropriate content or replicating inherent biases in the underlying data.
54
+
55
+ BLIP2 has not been tested in real world applications. It should not be directly deployed in any applications. Researchers should first carefully assess the safety and fairness of the model in relation to the specific context they’re being deployed within.
56
+
57
+
58
+ ### How to use
59
+
60
+ For code examples, we refer to the [documentation](https://huggingface.co/docs/transformers/main/en/model_doc/blip-2#transformers.Blip2ForConditionalGeneration.forward.example).
61
+
62
+ ### Memory requirements
63
+
64
+ The memory requirements differ based on the precision one uses. One can use 4-bit inference using [Bitsandbytes](https://huggingface.co/blog/4bit-transformers-bitsandbytes), which greatly reduce the memory requirements.
65
+
66
+ | dtype | Largest Layer or Residual Group | Total Size | Training using Adam |
67
+ |-------------------|---------------------------------|------------|----------------------|
68
+ | float32 | 490.94 MB | 14.43 GB | 57.72 GB |
69
+ | float16/bfloat16 | 245.47 MB | 7.21 GB | 28.86 GB |
70
+ | int8 | 122.73 MB | 3.61 GB | 14.43 GB |
71
+ | int4 | 61.37 MB | 1.8 GB | 7.21 GB |
72
+
73
+ #### Running the model on CPU
74
+
75
+ <details>
76
+ <summary> Click to expand </summary>
77
+
78
+ ```python
79
+ import requests
80
+ from PIL import Image
81
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
82
+
83
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
84
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
85
+
86
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
87
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
88
+
89
+ question = "how many dogs are in the picture?"
90
+ inputs = processor(raw_image, question, return_tensors="pt")
91
+
92
+ out = model.generate(**inputs)
93
+ print(processor.decode(out[0], skip_special_tokens=True).strip())
94
+ ```
95
+ </details>
96
+
97
+ #### Running the model on GPU
98
+
99
+ ##### In full precision
100
+
101
+ <details>
102
+ <summary> Click to expand </summary>
103
+
104
+ ```python
105
+ # pip install accelerate
106
+ import requests
107
+ from PIL import Image
108
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
109
+
110
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
111
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", device_map="auto")
112
+
113
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
114
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
115
+
116
+ question = "how many dogs are in the picture?"
117
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda")
118
+
119
+ out = model.generate(**inputs)
120
+ print(processor.decode(out[0], skip_special_tokens=True).strip())
121
+ ```
122
+ </details>
123
+
124
+ ##### In half precision (`float16`)
125
+
126
+ <details>
127
+ <summary> Click to expand </summary>
128
+
129
+ ```python
130
+ # pip install accelerate
131
+ import torch
132
+ import requests
133
+ from PIL import Image
134
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
135
+
136
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
137
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16, device_map="auto")
138
+
139
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
140
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
141
+
142
+ question = "how many dogs are in the picture?"
143
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
144
+
145
+ out = model.generate(**inputs)
146
+ print(processor.decode(out[0], skip_special_tokens=True).strip())
147
+ ```
148
+ </details>
149
+
150
+ ##### In 8-bit precision (`int8`)
151
+
152
+ <details>
153
+ <summary> Click to expand </summary>
154
+
155
+ ```python
156
+ # pip install accelerate bitsandbytes
157
+ import torch
158
+ import requests
159
+ from PIL import Image
160
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
161
+
162
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
163
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", load_in_8bit=True, device_map="auto")
164
+
165
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
166
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
167
+
168
+ question = "how many dogs are in the picture?"
169
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
170
+
171
+ out = model.generate(**inputs)
172
+ print(processor.decode(out[0], skip_special_tokens=True).strip())
173
+ ```
174
+ </details>