Zero-Shot Image Classification
Transformers
Safetensors
tipsv2
feature-extraction
vision
image-text
contrastive-learning
zero-shot
custom_code
Instructions to use google/tipsv2-g14 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/tipsv2-g14 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("zero-shot-image-classification", model="google/tipsv2-g14", 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-g14", trust_remote_code=True) model = AutoModel.from_pretrained("google/tipsv2-g14", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Gabriele commited on
Commit ·
c1eb665
1
Parent(s): 1785a7e
Use cat photo, add print statements to code examples
Browse files49 unmodified lines50515253545556575859606162636465666768691 unmodified line717273747576777879803 unmodified lines8485868788895 unmodified lines959697989910049 unmodified linestransforms.ToTensor(),])url = "https://raw.githubusercontent.com/google-deepmind/tips/main/scenic/images/example_image.jpg"image = Image.open(requests.get(url, stream=True).raw)pixel_values = transform(image).unsqueeze(0)out = model.encode_image(pixel_values)out.cls_token # (B, 1, 1536)out.patch_tokens # (B, N, 1536)```### Encode text```pythontext_emb = model.encode_text(["a photo of a cat", "a photo of a dog"])# (2, 1536)```### Zero-shot classification1 unmodified line```pythonimport torch.nn.functional as Fcls = F.normalize(out.cls_token[:, 0, :], dim=-1)text_emb = F.normalize(model.encode_text(["cat", "dog", "car"]), dim=-1)similarity = cls @ text_emb.Tprediction = similarity.argmax(dim=-1)```### Zero-shot segmentation3 unmodified linespatch_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)```### Visualize spatial features5 unmodified linesspatial = out.patch_tokens.reshape(1, 32, 32, 1536)feat = spatial[0].detach().numpy().reshape(-1, 1536)rgb = PCA(n_components=3).fit_transform(feat).reshape(32, 32, 3)```### GPU inference49 unmodified lines50515253545556575859606162636465666768691 unmodified line71727374757677787980813 unmodified lines858687888990915 unmodified lines97989910010110210349 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)print(out.cls_token.shape) # (1, 1, 1536)print(out.patch_tokens.shape) # (1, 1024, 1536)```### Encode text```pythontext_emb = model.encode_text(["a photo of a cat", "a photo of a dog"])print(text_emb.shape) # (2, 1536)```### Zero-shot classification1 unmodified line```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```### Zero-shot segmentation3 unmodified linespatch_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, 1536)feat = spatial[0].detach().numpy().reshape(-1, 1536)rgb = PCA(n_components=3).fit_transform(feat).reshape(32, 32, 3)print(rgb.shape) # (32, 32, 3)```### GPU inference
README.md+9-6