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 ·
69113d7
1
Parent(s): a34ff2b
Switch to bus image with matching classes
Browse files49 unmodified lines505152535455565 unmodified lines626364656667682 unmodified lines717273747576777879808182838485868749 unmodified linestransforms.ToTensor(),])url = "https://raw.githubusercontent.com/google-deepmind/tips/main/scenic/images/example_image_2.jpg"image = Image.open(requests.get(url, stream=True).raw)pixel_values = transform(image).unsqueeze(0)out = model.encode_image(pixel_values)5 unmodified lines### 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```2 unmodified lines```pythonimport torch.nn.functional as Fclasses = ["cat", "dog", "car"]cls = 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)49 unmodified lines505152535455565 unmodified lines626364656667682 unmodified lines717273747576777879808182838485868749 unmodified linestransforms.ToTensor(),])url = "https://huggingface.co/spaces/google/tipsv2-gpu-explorer/resolve/main/examples/zeroseg/pascal_context_00049_image.png"image = Image.open(requests.get(url, stream=True).raw)pixel_values = transform(image).unsqueeze(0)out = model.encode_image(pixel_values)5 unmodified lines### Encode text```pythontext_emb = model.encode_text(["a photo of a bus", "a photo of a dog"])print(text_emb.shape) # (2, 1024) — one embedding per query```2 unmodified lines```pythonimport torch.nn.functional as Fclasses = ["bus", "car", "dog", "cat"]cls = 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()]) # bus — predicted class```### Zero-shot segmentation```pythonclasses = ["bus", "snow", "mountain", "house", "road"]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)
README.md+5-5