Zero-Shot Image Classification
Transformers
Safetensors
tipsv2
feature-extraction
vision
image-text
contrastive-learning
zero-shot
custom_code
Instructions to use google/tipsv2-l14 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/tipsv2-l14 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("zero-shot-image-classification", model="google/tipsv2-l14", trust_remote_code=True) pipe( "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png", candidate_labels=["animals", "humans", "landscape"], )# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModel processor = AutoProcessor.from_pretrained("google/tipsv2-l14", trust_remote_code=True) model = AutoModel.from_pretrained("google/tipsv2-l14", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload README.md with huggingface_hub
Browse files11 unmodified lines12131415161718151617181923202122232425293026272823 unmodified lines52535460615556575859606667616270636465664 unmodified lines7172738174757677787987888081828393948495968586873 unmodified lines91929310694959697989911211310010110211611710310410511 unmodified lines
# TIPSv2 — L/14
TIPSv2 (Text-Image Pre-training with Spatial awareness) is a family ofcontrastive vision-language models that produce spatially rich image featuresaligned with text embeddings. This is the **Large** variant with a ViT-L/14vision encoder and a matching text encoder.TIPSv2 (Text-Image Pre-training with Spatial awareness) is a family of contrastive vision-language models that produce spatially rich image features aligned with text embeddings. This is the Large variant with a ViT-L/14 vision encoder and a matching text encoder.
| Variant | Vision params | Text params | Embed dim ||---------|--------------|-------------|-----------|| B/14 | 86M | 110M | 768 || **L/14 (this)** | **303M** | **184M** | **1024** || L/14 | 303M | 184M | 1024 || SO400m/14 | 412M | 448M | 1152 || g/14 | 1.1B | 389M | 1536 |
## Usage
### Install dependencies
```bashpip install transformers torch torchvision sentencepiece```23 unmodified linesimage = transform(Image.open("photo.jpg")).unsqueeze(0)out = model.encode_image(image)
out.cls_token # (B, 1, 1024) — global CLS featureout.patch_tokens # (B, 1024, 1024) — spatial features (32x32 grid)out.cls_token # (B, 1, 1024)out.patch_tokens # (B, 1024, 1024)```
### Encode text
Pass strings directly — tokenization is handled internally.
```pythontext_emb = model.encode_text(["a photo of a cat", "a photo of a dog"])# text_emb: (2, 1024)# (2, 1024)```
### Zero-shot classification4 unmodified linescls = F.normalize(out.cls_token[:, 0, :], dim=-1)text_emb = F.normalize(model.encode_text(["cat", "dog", "car"]), dim=-1)
similarity = cls @ text_emb.T # (B, num_classes)similarity = cls @ text_emb.Tprediction = similarity.argmax(dim=-1)```
### Zero-shot segmentation
Use spatial patch tokens for dense prediction tasks.
```pythonimport numpy as npfrom sklearn.decomposition import PCA
# Spatial featuresspatial = out.patch_tokens.reshape(1, 32, 32, 1024) # (B, H, W, D)spatial = out.patch_tokens.reshape(1, 32, 32, 1024)
# PCA visualizationfeat = spatial[0].detach().numpy().reshape(-1, 1024)rgb = PCA(n_components=3).fit_transform(feat).reshape(32, 32, 3)```3 unmodified lines```pythonmodel = model.cuda()out = model.encode_image(image.cuda())text_emb = model.encode_text(["a city"]) # auto-moved to model devicetext_emb = model.encode_text(["a city"])```
## Model details
- **Architecture**: ViT-L/14 vision encoder (24 layers) + Transformer text encoder (12 layers)- **Pre-training**: Contrastive image-text learning with spatial awareness- **Image preprocessing**: Resize to 448x448, convert to `[0, 1]` (no normalization)- **Image preprocessing**: resize to any resolution, convert to `[0, 1]` (no ImageNet normalization)- **Text preprocessing**: SentencePiece tokenizer, lowercased, max 64 tokens- **Patch size**: 14x14 pixels- **Register tokens**: 1- **Position embeddings**: Interpolated at inference, supports any resolution
## License
README.md+9-24