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
Use relative imports for sibling modules (fixes local loading, save_pretrained, pickling)
Browse files1234567891011121314151617181920212223242526272829303132333440 unmodified lines7576777879808182838485863 unmodified lines909192939495964 unmodified lines101102103104105106107108109110111112113114"""TIPSv2 model for HuggingFace — wraps vision and text encoders."""import importlibimport osfrom dataclasses import dataclassfrom pathlib import Pathfrom typing import List, Optional, Unionimport numpy as npimport torchfrom huggingface_hub import hf_hub_downloadfrom transformers import PreTrainedModelfrom .configuration_tips import TIPSv2Config_this_dir = Path(__file__).parent_sibling_cache = {}def _load_sibling(name, repo_id=None):"""Import a sibling .py from the same dir, downloading from HF if needed."""if name in _sibling_cache:return _sibling_cache[name]path = _this_dir / f"{name}.py"if not path.exists() and repo_id:path = Path(hf_hub_download(repo_id, f"{name}.py"))spec = importlib.util.spec_from_file_location(name, str(path))mod = importlib.util.module_from_spec(spec)spec.loader.exec_module(mod)_sibling_cache[name] = modreturn mod@dataclass40 unmodified linesdef __init__(self, config: TIPSv2Config):super().__init__(config)repo_id = getattr(config, "_name_or_path", None)ie = _load_sibling("image_encoder", repo_id)te = _load_sibling("text_encoder", repo_id)build_fn = getattr(ie, config.vision_fn)self.vision_encoder = build_fn(img_size=config.img_size,patch_size=config.patch_size,ffn_layer=config.ffn_layer,3 unmodified linesinterpolate_offset=0.0,)self.text_encoder = te.TextEncoder(config={"hidden_size": config.text_hidden_size,"mlp_dim": config.text_mlp_dim,4 unmodified lines)self._tokenizer = Noneself._te_mod = tedef _load_tokenizer(self):"""Lazy-load the SentencePiece tokenizer."""tok_path = _this_dir / "tokenizer.model"if not tok_path.exists():tok_path = hf_hub_download(self.name_or_path, "tokenizer.model")return self._te_mod.Tokenizer(str(tok_path))@torch.no_grad()def encode_image(self, pixel_values: torch.Tensor) -> TIPSv2ImageOutput:123456789101112131415161718192021222340 unmodified lines646566676869703 unmodified lines747576777879804 unmodified lines85868788899091929394"""TIPSv2 model for HuggingFace — wraps vision and text encoders."""from dataclasses import dataclassfrom typing import List, Optional, Unionimport torchfrom transformers import PreTrainedModelfrom transformers.utils import cached_filefrom .configuration_tips import TIPSv2Configfrom .image_encoder import vit_base, vit_giant2, vit_large, vit_small, vit_so400mfrom .text_encoder import TextEncoder, Tokenizer_VISION_FACTORIES = {"vit_small": vit_small,"vit_base": vit_base,"vit_large": vit_large,"vit_so400m": vit_so400m,"vit_giant2": vit_giant2,}@dataclass40 unmodified linesdef __init__(self, config: TIPSv2Config):super().__init__(config)self.vision_encoder = _VISION_FACTORIES[config.vision_fn](img_size=config.img_size,patch_size=config.patch_size,ffn_layer=config.ffn_layer,3 unmodified linesinterpolate_offset=0.0,)self.text_encoder = TextEncoder(config={"hidden_size": config.text_hidden_size,"mlp_dim": config.text_mlp_dim,4 unmodified lines)self._tokenizer = Nonedef _load_tokenizer(self):"""Load the SentencePiece tokenizer shipped with the checkpoint."""return Tokenizer(cached_file(self.name_or_path, "tokenizer.model"))@torch.no_grad()def encode_image(self, pixel_values: torch.Tensor) -> TIPSv2ImageOutput:
modeling_tips.py+14-34