Registry / ai-ml / clip-anytorch

clip-anytorch

JSON →
library2.6.0pypypi✓ verified 86d ago

CLIP (Contrastive Language-Image Pre-training) is a neural network trained on a variety of (image, text) pairs, allowing zero-shot visual classification and multimodal embeddings. The `clip-anytorch` library provides an easy-to-use PyTorch implementation, often used as a direct port of the original OpenAI CLIP model. It is currently at version 2.6.0 with a consistent minor release cadence.

pip install clip-anytorch
INSTALL
IMPORT
SIG · CLIP-ANYTORCH
C
clip-anytorch
ai-mlpythonv2.6.0
Install
68.7s avg
Import
13481ms
Disk
4915MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 80.55s
py 3.11
✕ build_error
✓ 73.1s
py 3.12
✕ build_error
✓ 62.95s
py 3.13
✕ build_error
✓ 58.05s
py 3.9
✕ build_error
✕ timeout
4915MB installed
● package 4915MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

clip
import clip
load
model, preprocess = clip.load(...)
`load` is a function directly accessible from the `clip` module, not imported as `from clip import load`.
tokenize
text = clip.tokenize(...)
`tokenize` is a function directly accessible from the `clip` module, not imported as `from clip import tokenize`.

This quickstart demonstrates how to load a pre-trained CLIP model, preprocess an image and text, and then use the model to compute similarity scores (logits) between the image and various text descriptions. It handles device selection (GPU/CPU) and includes a minimal dummy image creation for standalone execution.

import torch import clip from PIL import Image import os # Added for sample image path # Ensure you have a sample image, e.g., 'sample.jpg' in the current directory # For demonstration, let's create a dummy image if not present: if not os.path.exists("sample.jpg"): from PIL import ImageDraw img = Image.new('RGB', (60, 30), color = 'red') d = ImageDraw.Draw(img) d.text((10,10), "Hello", fill=(255,255,0)) img.save("sample.jpg") device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Using device: {device}") # Load the CLIP model and its preprocessing function model, preprocess = clip.load("ViT-B/32", device=device) # Preprocess an image image_path = "sample.jpg" image = preprocess(Image.open(image_path)).unsqueeze(0).to(device) # Tokenize text text = clip.tokenize(["a photo of a cat", "a photo of a dog", "a red square with text"]).to(device) with torch.no_grad(): # Encode image and text to get features image_features = model.encode_image(image) text_features = model.encode_text(text) # Calculate similarity scores logits_per_image, logits_per_text = model(image, text) probs = logits_per_image.softmax(dim=-1).cpu().numpy() print("Label probabilities (for image vs text captions):") for i, p in enumerate(probs[0]): print(f" '{clip.tokenize(["a photo of a cat", "a photo of a dog", "a red square with text"])[i][0].text}': {p:.4f}")
Debug
Known issues
gotchaCLIP models, especially larger variants (e.g., ViT-L/14), require significant GPU memory. Running on CPU can be very slow.
fix
Use a GPU-enabled environment. For memory errors, try smaller models (e.g., ViT-B/32) or reduce batch sizes if possible.
affects: All versions
gotchaEnsure your PyTorch installation is compatible with your CUDA drivers and GPU hardware. Mismatched versions can lead to `CUDA error` or `Device 'cuda:0' not found`.
fix
Refer to the PyTorch installation guide (pytorch.org) to install the correct PyTorch+CUDA version for your system. Upgrade GPU drivers if necessary.
affects: All versions
gotchaPre-trained CLIP models are downloaded on the first `clip.load()` call, which requires an active internet connection and available disk space (typically 200MB - 1GB depending on the model).
fix
Ensure stable internet access. If behind a proxy, configure `HTTPS_PROXY` environment variables. If disk space is an issue, consider alternative model storage locations if the library supports it (not directly via `clip.load`).
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'clip'
The `clip-anytorch` package was not installed or the environment where it was installed is not active.
fix
Run `pip install clip-anytorch` in your active Python environment.
RuntimeError: CUDA out of memory. Tried to allocate X GiB (GPU Y; X GiB total capacity; Z GiB already allocated; W GiB free; P MiB reserved in total by PyTorch)
The GPU does not have enough memory to load the model or process the current batch size.
fix
Use a smaller CLIP model (e.g., 'ViT-B/32' instead of 'ViT-L/14'), reduce your batch size if processing multiple items, or acquire a GPU with more VRAM.
ValueError: Unknown model name '...' (Available models are: 'RN50', 'RN101', 'RN50x4', 'RN50x16', 'RN50x64', 'ViT-B/32', 'ViT-B/16', 'ViT-L/14', 'ViT-L/14@336px')
The model name passed to `clip.load()` is misspelled or not a valid pre-trained model supported by the library.
fix
Check the official documentation or the error message itself for the list of available model names and correct the spelling.
Upgrade
Version history
2.6.0latest on PyPI · released Jan 13, 2024
Audit
Dependencies
torchrequiredCore deep learning framework for model execution.
torchvisionrequiredProvides dataset and model preprocessing utilities, especially for image handling.
ftfyrequiredUsed for fixing unicode text prior to tokenization.
regexrequiredAdvanced regular expression operations for text processing.
tqdmrequiredProgress bar for model downloads and processing.
Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
1
Resources
clip-anytorch — pip install clip-anytorch · libregistry