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
Gabriele commited on
Commit ·
a34ff2b
1
Parent(s): 44dd5af
Improve code examples: better classes, descriptive comments
Browse files54 unmodified lines5556575859606162636465666768695 unmodified lines75767778798081828384858687888990915 unmodified lines97989910010110210354 unmodified linespixel_values = transform(image).unsqueeze(0)out = model.encode_image(pixel_values)print(out.cls_token.shape) # (1, 1, 1024)print(out.patch_tokens.shape) # (1, 1024, 1024)```### Encode text```pythontext_emb = model.encode_text(["a photo of a cat", "a photo of a dog"])print(text_emb.shape) # (2, 1024)```### Zero-shot classification5 unmodified linescls = F.normalize(out.cls_token[:, 0, :], dim=-1)text_emb = F.normalize(model.encode_text(classes), dim=-1)similarity = cls @ text_emb.Tprint(classes[similarity.argmax()]) # cat```### Zero-shot segmentation```pythonclasses = ["cat", "dog", "grass", "sky"]patch_feats = F.normalize(out.patch_tokens, dim=-1)text_emb = F.normalize(model.encode_text(classes), dim=-1)seg_map = (patch_feats @ text_emb.T).reshape(32, 32, len(classes)).argmax(dim=-1)print(seg_map.shape) # (32, 32)```### Visualize spatial features5 unmodified linesspatial = out.patch_tokens.reshape(1, 32, 32, 1024)feat = spatial[0].detach().numpy().reshape(-1, 1024)rgb = PCA(n_components=3).fit_transform(feat).reshape(32, 32, 3)print(rgb.shape) # (32, 32, 3)```### GPU inference54 unmodified lines5556575859606162636465666768695 unmodified lines75767778798081828384858687888990915 unmodified lines97989910010110210354 unmodified linespixel_values = transform(image).unsqueeze(0)out = model.encode_image(pixel_values)print(out.cls_token.shape) # (1, 1, 1024) — global image embeddingprint(out.patch_tokens.shape) # (1, 1024, 1024) — per-patch spatial features```### Encode text```pythontext_emb = model.encode_text(["a photo of a cat", "a photo of a dog"])print(text_emb.shape) # (2, 1024) — one embedding per query```### Zero-shot classification5 unmodified linescls = F.normalize(out.cls_token[:, 0, :], dim=-1)text_emb = F.normalize(model.encode_text(classes), dim=-1)similarity = cls @ text_emb.Tprint(classes[similarity.argmax()]) # cat — predicted class```### Zero-shot segmentation```pythonclasses = ["cat", "carpet", "floor", "furniture"]patch_feats = F.normalize(out.patch_tokens, dim=-1)text_emb = F.normalize(model.encode_text(classes), dim=-1)seg_map = (patch_feats @ text_emb.T).reshape(32, 32, len(classes)).argmax(dim=-1)print(seg_map.shape) # (32, 32) — per-patch class prediction```### Visualize spatial features5 unmodified linesspatial = out.patch_tokens.reshape(1, 32, 32, 1024)feat = spatial[0].detach().numpy().reshape(-1, 1024)rgb = PCA(n_components=3).fit_transform(feat).reshape(32, 32, 3)print(rgb.shape) # (32, 32, 3) — PCA of patch features as RGB```### GPU inference
README.md+7-7