Skip to main content

blip-inference

Pretrained BLIP with a similar API to CLIP.

BLIP tends to achieve slightly better accuracy than CLIP with similar inference speed. The CLIP API is much cleaner and more commonly used. This repo refactors BLIP to match the CLIP interface, so that it's easier for practitioners to switch between CLIP / BLIP models.

Install

From PyPI:

pip install blip-inference

From source:

pip install "blip_inference @ git+https://git@github.com/fkodom/blip-inference.git"

Usage

User-facing methods behave similarly to CLIP. A few underlying details change, which will only affect advanced users.

import torch
import blip_inference as blip
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = blip.load("feature_extractor", device=device)

raw_text = ["a diagram", "a dog", "a cat"]
text = blip.tokenize(raw_text).to(device)
image = preprocess(Image.open("kitten.jpeg")).unsqueeze(0).to(device)

with torch.no_grad():    
    logits_per_image, logits_per_text = model(image, text)
probs = torch.softmax(logits_per_image, dim=-1)

print("\nPredictions:\n")
for idx, value in enumerate(probs.squeeze()):
    print(f"{raw_text[idx]:>16s}: {100 * value.item():.2f}%")
probs = logits_per_image.softmax(dim=-1).cpu().numpy()

Zero-Shot Prediction

import blip_inference as blip
import torch
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = blip.load('base', device)

raw_text = ["a diagram", "a dog", "a cat"]
text = blip.tokenize(raw_text).to(device)
image = preprocess(Image.open("kitten.jpeg")).unsqueeze(0).to(device)

with torch.no_grad():
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)

image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
similarity = (100 * image_features @ text_features.T).softmax(dim=-1)

print("\nPredictions:\n")
for idx, value in enumerate(similarity.squeeze()):
    print(f"{raw_text[idx]:>16s}: {100 * value.item():.2f}%")

Linear Probe Evaluation

See this example from the CLIP repo. Everything should be identical, except for swapping:

  • import clip --> import blip_inference as blip
  • clip --> blip

API

Similar to CLIP, the blip_inference module provides the following methods:

blip_inference.available_models() -> List[str]

Returns the names of the available BLIP models.

blip_inference.load(name: str, device=...) -> Tuple[BLIP, Callable]

Returns the model and the TorchVision transform needed by the model, specified by the model name returned by blip_inference.available_models(). It will download the model as necessary. The name argument can also be a path to a local checkpoint.

The device to run the model can be optionally specified, and the default is to use the first CUDA device if there is any, otherwise the CPU.

blip_inference.tokenize(text: Union[str, List[str]], context_length: int = 35) -> BatchEncoding

Returns a dictionary with tokenized sequences of given text input(s). This can be used as the input to the model


The model returned by blip_inference.load() supports the following methods:

model.encode_image(image: Tensor) -> Tensor

Given a batch of images, returns the image features encoded by the vision portion of the BLIP model.

model.encode_text(text: BatchEncoding) -> Tensor

Given a batch of text tokens, returns the text features encoded by the language portion of the BLIP model.

model(image: Tensor, text: BatchEncoding) -> Tuple[Tensor, Tensor]

Given a batch of images and a batch of text tokens, returns two Tensors, containing the logit scores corresponding to each image and text input. The values are cosine similarities between the corresponding image and text features.

NOTE: Unlike CLIP, logits for BLIP models do not need to be multiplied by 100 before computing cosine similarity. That scaling factor is built into the BLIP model.

Metadata

Release files for blip-inference 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blip-inference 0.1.0
File Size Uploaded Provenance Actions
blip-inference-0.1.0.tar.gz 20.9 kB Without provenance Details for blip-inference-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for blip-inference 0.1.0
File Interpreter ABI Platform Actions
blip_inference-0.1.0-py3-none-any.whl Python 3 none any Details for blip_inference-0.1.0-py3-none-any.whl

Total release size: 41.6 kB

Release files / blip-inference-0.1.0.tar.gz

Download URL blip-inference-0.1.0.tar.gz
Size 20.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6dad3db5e69faff801110e03366c005921ffa92a8b2a02b6991163ee90d990d2
BLAKE2b-256 checksum
How to use checksums
33da26ed145d7779e356c73e046fd16c89aa1d9bf751328c2cc6ebc4cd1f8e5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / blip_inference-0.1.0-py3-none-any.whl

Download URL blip_inference-0.1.0-py3-none-any.whl
Size 20.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6fbd7447abc2ea1833fc4b4ebd3b81de94c9c496508433e75d4b1603732aefe8
BLAKE2b-256 checksum
How to use checksums
e55d818b15bb7a9f6de104f5bbc23a9014a5d8595387cbcbcf8bf565cd31a3ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Security for blip-inference 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page