🚀 BLIP-2, OPT-2.7b, pre-trained only
This is a BLIP-2 model that leverages OPT-2.7b, a large language model with 2.7 billion parameters. It was introduced in the paper BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models by Li et al. and first released in this repository.
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.
✨ Features
- Multi - model architecture: BLIP-2 consists of 3 models: a CLIP-like image encoder, a Querying Transformer (Q-Former), and a large language model.
- Versatile applications: It can be used for tasks like image captioning, visual question answering (VQA), and chat - like conversations.
📚 Documentation
Model description
BLIP-2 consists of 3 models: a CLIP-like image encoder, a Querying Transformer (Q-Former) and a large language model.
The authors initialize the weights of the image encoder and large language model from pre - trained checkpoints and keep them frozen while training the Querying Transformer, which is a BERT-like Transformer encoder that maps a set of "query tokens" to query embeddings, which bridge the gap between the embedding space of the image encoder and the large language model.
The goal for the model is simply to predict the next text token, given the query embeddings and the previous text.

This allows the model to be used for tasks like:
- image captioning
- visual question answering (VQA)
- chat - like conversations by feeding the image and the previous conversation as prompt to the model
Direct Use and Downstream Use
You can use the raw model for conditional text generation given an image and optional text. See the model hub to look for fine - tuned versions on a task that interests you.
Bias, Risks, Limitations, and Ethical Considerations
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.
Like other large language models for which the diversity (or lack thereof) of training
data induces downstream impact on the quality of our model, OPT-175B has limitations in terms
of bias and safety. OPT-175B can also have quality issues in terms of generation diversity and
hallucination. In general, OPT-175B is not immune from the plethora of issues that plague modern
large language models.
BLIP2 is fine - tuned on image - text datasets (e.g. LAION) 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.
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.
💻 Usage Examples
Basic Usage
You can refer to the documentation for code examples.
Running the model on CPU
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
Running the model on GPU
In full precision
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", device_map="auto")
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt").to("cuda")
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
In half precision (float16
)
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16, device_map="auto")
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
In 8 - bit precision (int8
)
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", load_in_8bit=True, device_map="auto")
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))
📄 License
This model is released under the MIT license.