Mediocreatmybest commited on
Commit
d1fe554
1 Parent(s): cbc7a5d

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +170 -2
README.md CHANGED
@@ -1,5 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
1
  __Quality of Life duplication:__
2
 
3
- _duplicated_from: ybelkada/blip2-opt-2.7b-fp16-sharded_
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- _Tokenizer duplicated from: Salesforce/blip2-opt-2.7b_
 
 
 
 
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
  __Quality of Life duplication:__
13
 
14
+ _duplicated_from: ybelkada/blip2-opt-2.7b-fp16-sharded_
15
+
16
+ _Tokenizer duplicated from: Salesforce/blip2-opt-2.7b_
17
+
18
+ _Safetensors Added_
19
+ 🥱- _Mediocre_
20
+
21
+
22
+ # BLIP-2, OPT-2.7b, pre-trained only
23
+
24
+ BLIP-2 model, leveraging [OPT-2.7b](https://huggingface.co/facebook/opt-2.7b) (a large language model with 2.7 billion parameters).
25
+ 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).
26
+
27
+ 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.
28
+
29
+ ## Model description
30
+
31
+ BLIP-2 consists of 3 models: a CLIP-like image encoder, a Querying Transformer (Q-Former) and a large language model.
32
+
33
+ The authors initialize the weights of the image encoder and large language model from pre-trained checkpoints and keep them frozen
34
+ while training the Querying Transformer, which is a BERT-like Transformer encoder that maps a set of "query tokens" to query embeddings,
35
+ which bridge the gap between the embedding space of the image encoder and the large language model.
36
+
37
+ The goal for the model is simply to predict the next text token, giving the query embeddings and the previous text.
38
+
39
+ <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/blip2_architecture.jpg"
40
+ alt="drawing" width="600"/>
41
+
42
+ This allows the model to be used for tasks like:
43
+
44
+ - image captioning
45
+ - visual question answering (VQA)
46
+ - chat-like conversations by feeding the image and the previous conversation as prompt to the model
47
+
48
+ ## Direct Use and Downstream Use
49
+
50
+ 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
51
+ fine-tuned versions on a task that interests you.
52
+
53
+ ## Bias, Risks, Limitations, and Ethical Considerations
54
+
55
+ 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.
56
+
57
+ > Like other large language models for which the diversity (or lack thereof) of training
58
+ > data induces downstream impact on the quality of our model, OPT-175B has limitations in terms
59
+ > of bias and safety. OPT-175B can also have quality issues in terms of generation diversity and
60
+ > hallucination. In general, OPT-175B is not immune from the plethora of issues that plague modern
61
+ > large language models.
62
+ >
63
+ 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.
64
+
65
+ 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.
66
+
67
+
68
+ ### How to use
69
+
70
+ For code examples, we refer to the [documentation](https://huggingface.co/docs/transformers/main/en/model_doc/blip-2#transformers.Blip2ForConditionalGeneration.forward.example).
71
+
72
+ #### Running the model on CPU
73
+
74
+ <details>
75
+ <summary> Click to expand </summary>
76
+
77
+ ```python
78
+ import requests
79
+ from PIL import Image
80
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
81
+
82
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
83
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
84
+
85
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
86
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
87
+
88
+ question = "how many dogs are in the picture?"
89
+ inputs = processor(raw_image, question, return_tensors="pt")
90
+
91
+ out = model.generate(**inputs)
92
+ print(processor.decode(out[0], skip_special_tokens=True))
93
+ ```
94
+ </details>
95
+
96
+ #### Running the model on GPU
97
+
98
+ ##### In full precision
99
+
100
+ <details>
101
+ <summary> Click to expand </summary>
102
+
103
+ ```python
104
+ # pip install accelerate
105
+ import requests
106
+ from PIL import Image
107
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
108
+
109
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
110
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", device_map="auto")
111
+
112
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
113
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
114
+
115
+ question = "how many dogs are in the picture?"
116
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda")
117
+
118
+ out = model.generate(**inputs)
119
+ print(processor.decode(out[0], skip_special_tokens=True))
120
+ ```
121
+ </details>
122
+
123
+ ##### In half precision (`float16`)
124
+
125
+ <details>
126
+ <summary> Click to expand </summary>
127
+
128
+ ```python
129
+ # pip install accelerate
130
+ import torch
131
+ import requests
132
+ from PIL import Image
133
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
134
+
135
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
136
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16, device_map="auto")
137
+
138
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
139
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
140
+
141
+ question = "how many dogs are in the picture?"
142
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
143
+
144
+ out = model.generate(**inputs)
145
+ print(processor.decode(out[0], skip_special_tokens=True))
146
+ ```
147
+ </details>
148
+
149
+ ##### In 8-bit precision (`int8`)
150
+
151
+ <details>
152
+ <summary> Click to expand </summary>
153
+
154
+ ```python
155
+ # pip install accelerate bitsandbytes
156
+ import torch
157
+ import requests
158
+ from PIL import Image
159
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
160
+
161
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
162
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", load_in_8bit=True, device_map="auto")
163
+
164
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
165
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
166
+
167
+ question = "how many dogs are in the picture?"
168
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
169
 
170
+ out = model.generate(**inputs)
171
+ print(processor.decode(out[0], skip_special_tokens=True))
172
+ ```
173
+ </details>