Image and text embeddings
CLIP ViT-B/32
Puts photos and text in one space of vectors: search photos by describing them, or label them with any words you choose. OpenAI's original CLIP.
About this model
CLIP turns a photo and a piece of text into vectors of 512 numbers that are close when they match. Compare a photo with a few descriptions to label it with words chosen at run time (zero-shot classification), or compare one description with many photos to search them.
This is OpenAI's ViT-B/32 model: an image encoder (88 million parameters) and a text encoder (63 million), trained on 400 million image and text pairs from the internet. It comes as two packs. The text pack carries CLIP's tokenizer, so the app passes plain text.
Slab prepares the photo as OpenAI's CLIP does (short side to 224 pixels, bicubic, center crop, CLIP's mean and standard deviation) and tokenizes the text as CLIP's tokenizer does (lowercase, byte-level BPE, 77 tokens with the start and end tokens).
What you can build
- Search a photo library with words
- Label photos with categories chosen at run time, without training
- Find similar or duplicate photos (compare image embeddings)
- Sort or filter photos by what they show
Zero-shot labels: golden retriever dog
f16Karen Arnold, CC0, https://commons.wikimedia.org/wiki/File:Golden-retriever-dog-1362597631o6g.jpg
The photo against ten labels ("a photo of a dog", "a photo of a cat", ... "a photo of a flower"), scored as softmax over 100 × cosine similarity, on Metal. Top three: a dog 1.00, a cat 0.00, a musical instrument 0.00.Zero-shot labels: sailboat
f16Kbuihner, Public domain, https://commons.wikimedia.org/wiki/File:Kite_sailboat_dinghy.jpg
The photo against ten labels ("a photo of a dog", "a photo of a cat", ... "a photo of a flower"), scored as softmax over 100 × cosine similarity, on Metal. Top three: a boat 1.00, a musical instrument 0.00, a bird 0.00.Zero-shot labels: acoustic guitar
f16Wilfredor, CC0, https://commons.wikimedia.org/wiki/File:Man_playing_an_acoustic_brazilian_guitar_(Viol%C3%A3o)_on_Marco_Zero_Square,_Refice,_Pernambuco,_Brazil.jpg
The photo against ten labels ("a photo of a dog", "a photo of a cat", ... "a photo of a flower"), scored as softmax over 100 × cosine similarity, on Metal. Top three: a musical instrument 0.99, a bicycle 0.00, a boat 0.00.Photo search: "something to drink coffee from"
f16something to drink coffee from
The query against the 53 gallery photos (public-domain and CC0, Wikimedia Commons), by cosine similarity, on Metal. Best three: coffee mug (b19-coffee-mug.jpg, 0.280); espresso cup (a02-espresso-cup.jpg, 0.270); teapot (b20-teapot.jpg, 0.257).
Photo search: "a vehicle for fighting fires"
f16a vehicle for fighting fires
The query against the 53 gallery photos (public-domain and CC0, Wikimedia Commons), by cosine similarity, on Metal. Best three: pickup truck (b49-pickup-truck.jpg, 0.286); fire truck (b31-fire-truck.jpg, 0.250); horse (b04-horse.jpg, 0.240).
Photo search: "an animal that lives in the sea"
f16an animal that lives in the sea
The query against the 53 gallery photos (public-domain and CC0, Wikimedia Commons), by cosine similarity, on Metal. Best three: jellyfish (b11-jellyfish.jpg, 0.267); goldfish (b00-goldfish.jpg, 0.263); drum kit (b29-drum-kit.jpg, 0.235).
Variants & downloads
Choose the build for your appModel files are hosted on Google Drive. Large files may open a confirmation page before downloading.
Standard
SlabWeights stored in FP16: half the size of FP32, the same embeddings (see Accuracy).
Platforms: macos · android
clip-vit-b-32-image-f16.slab
170 MB · Hosted download
SHA-256 checksum
98fa39a9c391feb3886b9e716c2f24e32ee0be719503168a9d36899b1adb6b80clip-vit-b-32-text-f16.slab
125 MB · Hosted download
SHA-256 checksum
a5d8dff1b9c04bd5bcbedb99c194cb6913fb910870070cebd93cc37b03ada66bLICENSE-CLIP.txt
1 KB
SHA-256 checksum
987e63b32f6c89ff5160e429458a872ff048e6860b590a3912e938f9da8f14dbConversion details
- command
- hf download openai/clip-vit-base-patch32 --revision 3d74acf9a28c67741b2f4f2ea7635f0aaf6f0268 --local-dir clip-vit-base-patch32 --include "*.json" --include pytorch_model.bin python -c "import torch; from transformers import AutoModel; torch.set_grad_enabled(False); m = AutoModel.from_pretrained('clip-vit-base-patch32').eval(); I = type('I', (torch.nn.Module,), {'forward': lambda s, pixel_values: s.m.get_image_features(pixel_values=pixel_values).pooler_output}); T = type('T', (torch.nn.Module,), {'forward': lambda s, input_ids: s.m.get_text_features(input_ids=input_ids).pooler_output}); i = I(); i.m = m; t = T(); t.m = m; torch.onnx.export(i, (torch.zeros(1, 3, 224, 224),), 'clip-vit-b-32-image.onnx', input_names=['pixel_values'], output_names=['image_embeds'], opset_version=17, dynamo=False); torch.onnx.export(t, (torch.zeros(1, 77, dtype=torch.long),), 'clip-vit-b-32-text.onnx', input_names=['input_ids'], output_names=['text_embeds'], opset_version=17, dynamo=False)" slab-load convert clip-vit-b-32-image.onnx clip-vit-b-32-image.slab --input pixel_values=1x3x224x224 --meta task=embed --meta resize=center_crop --meta resize_size:i64=224 --meta interpolation=bicubic --meta antialias:bool=true --meta normalize=clip --meta color=rgb slab-load convert clip-vit-b-32-text.onnx clip-vit-b-32-text.slab --input input_ids=1x77 --meta task=embed slab-load quantize clip-vit-b-32-image.slab clip-vit-b-32-image-f16.slab --f16 slab-load quantize clip-vit-b-32-text.slab clip-vit-b-32-text-f16.slab --f16 slab-load tokenizer clip-vit-b-32-text-f16.slab clip-vit-base-patch32/tokenizer.json --pad "<|endoftext|>" slab-load meta set clip-vit-b-32-image-f16.slab default_backend.macos=gpu slab-load meta set clip-vit-b-32-text-f16.slab default_backend.macos=gpu
Integration examples
Python
python
import numpy as np, slab
cfg = slab.Config(cache_dir="/tmp/slab_cache", license=open("license.slablic.txt").read())
images = slab.Session(cfg, "clip-vit-b-32-image-f16.slab")
texts = slab.Session(cfg, "clip-vit-b-32-text-f16.slab") # carries the tokenizer
def embed(session, inp):
out = session.run(inp)
out.normalize_l2()
return out.numpy(0).reshape(-1)
photo = embed(images, slab.Input(frame=slab.Frame.open("photo.jpg")))
labels = ["a photo of a dog", "a photo of a cat", "a photo of a car"]
sims = np.array([photo @ embed(texts, slab.Input(text=t)) for t in labels]) # cosine similarities
logits = 100 * sims
probs = np.exp(logits - logits.max()); probs /= probs.sum() # softmax, as CLIP
print({l: round(float(p), 3) for l, p in zip(labels, probs)})
Device & backend compatibility
Pack default: macos / GPU. The fastest backend on the Mac that prepares quickly: an image in 7.3 ms and a short text in 6.5 ms. The Neural Engine runs only the image pack and takes about 7 s to prepare it.
| Platform | Engine | Status | Precision |
|---|---|---|---|
| apple | CPU CPU | Supported Measured on macOS: both packs. | f32 |
| apple | Metal GPU | Supported Measured on macOS: both packs. The default. | f32 |
| apple | Apple Neural Engine NPU | Supported Measured on macOS: the image pack only (it computes in FP16 and takes about 7 s to prepare). The text pack takes token ids, which the Neural Engine path does not accept. | f16 |
| android | CPU CPU | Supported Measured on a Samsung Galaxy S23 Ultra (Snapdragon 8 Gen 2, Adreno 740), Android 17. | f32 |
| android | Vulkan GPU | Supported Measured on a Samsung Galaxy S23 Ultra (Snapdragon 8 Gen 2, Adreno 740), Android 17. | f32 |
| android | Qualcomm Hexagon NPU (QNN) NPU | Not supported Tried on a Samsung Galaxy S23 Ultra (Hexagon v73): QNN rejects one of the image encoder's StridedSlice nodes (validateOpConfig error 3110). | — |
Performance
Results depend on the device, runtime, and test conditions. Each measurement keeps its own context.
Mac with Apple M1 Max
Slab · CPU
macOS 26.6.2
25.1 ms
Measured time · scope below
Image encoder: median of 100 warm runs of the model (Slab's run time). From a 1920 x 1080 photo, resizing included: 25.5 ms median of 20. Peak memory after opening and one run: 434 MB.
Mac with Apple M1 Max
Slab · GPU
macOS 26.6.2
7.3 ms
Measured time · scope below
Image encoder: median of 100 warm runs of the model (Slab's run time). From a 1920 x 1080 photo, resizing included: 10.3 ms median of 20. Peak memory after opening and one run: 1151 MB.
Mac with Apple M1 Max
Slab · NPU
macOS 26.6.2
3.7 ms
Measured time · scope below
Image encoder: median of 100 warm runs of the model (Slab's run time). From a 1920 x 1080 photo, resizing included: 5.9 ms median of 20. Peak memory after opening and one run: 1151 MB.
Mac with Apple M1 Max
Slab · CPU
macOS 26.6.2
21.2 ms
Measured time · scope below
Text encoder: Input(text="a photo of a dog"), tokenizing included, median of 100. Peak memory after opening and one run: 368 MB.
Mac with Apple M1 Max
Slab · GPU
macOS 26.6.2
6.5 ms
Measured time · scope below
Text encoder: Input(text="a photo of a dog"), tokenizing included, median of 100. Peak memory after opening and one run: 1011 MB.
Samsung Galaxy S23 Ultra (Snapdragon 8 Gen 2)
Slab · CPU
Android 17
157.0 ms
Measured time · scope below
Image encoder: median of 71 warm runs of the model (Slab's run time). From a 1920 x 1080 photo, preprocessing included: 152 ms median. open_ms: opening the session. Peak memory (resident) after the runs: 395 MB. Text encoder, Input(text="a photo of a dog"): 108 ms median of 100.
Samsung Galaxy S23 Ultra (Snapdragon 8 Gen 2)
Slab · GPU
Android 17
62.3 ms
Measured time · scope below
Image encoder: median of 100 warm runs of the model (Slab's run time). From a 1920 x 1080 photo, preprocessing included: 67.7 ms median. open_ms: opening the session with the GPU programs already in the cache folder (the first open on a device compiles them, about 0.3 s more). Peak memory (resident) after the runs: 406 MB. Text encoder, Input(text="a photo of a dog"): 41.6 ms median of 100.
Accuracy checks
macos · CPU · f16
The upstream PyTorch model against Hugging Face's CLIPProcessor, which rounds the center crop down where OpenAI's pipeline rounds it to nearest (a pixel's shift on some photos), on 53 public-domain and CC0 photos (Wikimedia Commons) and the same texts; Slab's image path starts from the photo file (Frame)
Image embeddings: cosine similarity 0.999384 on average, 0.991175 on the worst of 53 photos. Text embeddings: 0.9999999 on average, 0.9999998 at worst (58 prompts and sentences in several languages). Zero-shot: the same label as upstream for 53 of 53 photos (each photo against all 53 labels as "a photo of a ..."); right for 45 (upstream 45). Against OpenAI's own preprocessing (torchvision Resize and CenterCrop on the PIL image): 0.99998 on average, 0.9989 on the worst photo.
macos · GPU · f16
The upstream PyTorch model against Hugging Face's CLIPProcessor, which rounds the center crop down where OpenAI's pipeline rounds it to nearest (a pixel's shift on some photos), on 53 public-domain and CC0 photos (Wikimedia Commons) and the same texts; Slab's image path starts from the photo file (Frame)
Image embeddings: cosine similarity 0.999384 on average, 0.991175 on the worst of 53 photos. Text embeddings: 0.9999999 on average, 0.9999998 at worst (58 prompts and sentences in several languages). Zero-shot: the same label as upstream for 53 of 53 photos (each photo against all 53 labels as "a photo of a ..."); right for 45 (upstream 45). Against OpenAI's own preprocessing (torchvision Resize and CenterCrop on the PIL image): 0.99998 on average, 0.9989 on the worst photo.
macos · NPU · f16
The upstream PyTorch model against Hugging Face's CLIPProcessor, which rounds the center crop down where OpenAI's pipeline rounds it to nearest (a pixel's shift on some photos), on 53 public-domain and CC0 photos (Wikimedia Commons) and the same texts; Slab's image path starts from the photo file (Frame)
Image embeddings: cosine similarity 0.999335 on average, 0.990871 on the worst of 53 photos. The text pack does not run on the Neural Engine.
Before you choose this model
- English only.
- OpenAI's model card puts any deployed use, commercial or not, out of scope unless the model is tested thoroughly for that use, and surveillance and face recognition always out of scope. The code and weights are MIT-licensed; weigh the model card before shipping.
- Weak at fine detail: counting, small text, fine-grained species or car models.
- Text beyond 77 tokens (about 55 words) is cut.
- Learned from internet data, with its biases (see OpenAI's model card).
- The text pack takes token ids, which Slab's Neural Engine path does not accept: it runs on the GPU or CPU. The image pack runs on all three.