Mediocreatmybest commited on
Commit
c9f557a
1 Parent(s): ca44a38

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +160 -0
README.md ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ inference: false
11
+ ---
12
+
13
+ # BLIP-2, Flan T5-xxl, pre-trained only
14
+
15
+ BLIP-2 model, leveraging [Flan T5-xxl](https://huggingface.co/google/flan-t5-xxl) (a large language model).
16
+ 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).
17
+
18
+ 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.
19
+
20
+ ## Model description
21
+
22
+ BLIP-2 consists of 3 models: a CLIP-like image encoder, a Querying Transformer (Q-Former) and a large language model.
23
+
24
+ The authors initialize the weights of the image encoder and large language model from pre-trained checkpoints and keep them frozen
25
+ while training the Querying Transformer, which is a BERT-like Transformer encoder that maps a set of "query tokens" to query embeddings,
26
+ which bridge the gap between the embedding space of the image encoder and the large language model.
27
+
28
+ The goal for the model is simply to predict the next text token, giving the query embeddings and the previous text.
29
+
30
+ <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/blip2_architecture.jpg"
31
+ alt="drawing" width="600"/>
32
+
33
+ This allows the model to be used for tasks like:
34
+
35
+ - image captioning
36
+ - visual question answering (VQA)
37
+ - chat-like conversations by feeding the image and the previous conversation as prompt to the model
38
+
39
+ ## Direct Use and Downstream Use
40
+
41
+ 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
42
+ fine-tuned versions on a task that interests you.
43
+
44
+ ## Bias, Risks, Limitations, and Ethical Considerations
45
+
46
+ BLIP2-FlanT5 uses off-the-shelf Flan-T5 as the language model. It inherits the same risks and limitations from [Flan-T5](https://arxiv.org/pdf/2210.11416.pdf):
47
+
48
+ > Language models, including Flan-T5, can potentially be used for language generation in a harmful way, according to Rae et al. (2021). Flan-T5 should not be used directly in any application, without a prior assessment of safety and fairness concerns specific to the application.
49
+
50
+ 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.
51
+
52
+ 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.
53
+
54
+
55
+ ### How to use
56
+
57
+ For code examples, we refer to the [documentation](https://huggingface.co/docs/transformers/main/en/model_doc/blip-2#transformers.Blip2ForConditionalGeneration.forward.example), or refer to the snippets below depending on your usecase:
58
+
59
+ #### Running the model on CPU
60
+
61
+ <details>
62
+ <summary> Click to expand </summary>
63
+
64
+ ```python
65
+ import requests
66
+ from PIL import Image
67
+ from transformers import BlipProcessor, Blip2ForConditionalGeneration
68
+
69
+ processor = BlipProcessor.from_pretrained("Salesforce/blip2-flan-t5-xxl")
70
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl")
71
+
72
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
73
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
74
+
75
+ question = "how many dogs are in the picture?"
76
+ inputs = processor(raw_image, question, return_tensors="pt")
77
+
78
+ out = model.generate(**inputs)
79
+ print(processor.decode(out[0], skip_special_tokens=True))
80
+ ```
81
+ </details>
82
+
83
+ #### Running the model on GPU
84
+
85
+ ##### In full precision
86
+
87
+ <details>
88
+ <summary> Click to expand </summary>
89
+
90
+ ```python
91
+ # pip install accelerate
92
+ import requests
93
+ from PIL import Image
94
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
95
+
96
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xxl")
97
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl", device_map="auto")
98
+
99
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
100
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
101
+
102
+ question = "how many dogs are in the picture?"
103
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda")
104
+
105
+ out = model.generate(**inputs)
106
+ print(processor.decode(out[0], skip_special_tokens=True))
107
+ ```
108
+ </details>
109
+
110
+ ##### In half precision (`float16`)
111
+
112
+ <details>
113
+ <summary> Click to expand </summary>
114
+
115
+ ```python
116
+ # pip install accelerate
117
+ import torch
118
+ import requests
119
+ from PIL import Image
120
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
121
+
122
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xxl")
123
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl", torch_dtype=torch.float16, device_map="auto")
124
+
125
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
126
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
127
+
128
+ question = "how many dogs are in the picture?"
129
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
130
+
131
+ out = model.generate(**inputs)
132
+ print(processor.decode(out[0], skip_special_tokens=True))
133
+ ```
134
+ </details>
135
+
136
+ ##### In 8-bit precision (`int8`)
137
+
138
+ <details>
139
+ <summary> Click to expand </summary>
140
+
141
+ ```python
142
+ # pip install accelerate bitsandbytes
143
+ import torch
144
+ import requests
145
+ from PIL import Image
146
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
147
+
148
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xxl")
149
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl", load_in_8bit=True, device_map="auto")
150
+
151
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
152
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
153
+
154
+ question = "how many dogs are in the picture?"
155
+ inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
156
+
157
+ out = model.generate(**inputs)
158
+ print(processor.decode(out[0], skip_special_tokens=True))
159
+ ```
160
+ </details>