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 files12343645961178910111213161718192021222324252627282930311415161718192021222340 unmodified lines646566787980818283676869703 unmodified lines74757693777879804 unmodified lines85868710488891071081091101119091929394"""TIPSv2 model for HuggingFace — wraps vision and text encoders."""
import importlibimport osfrom dataclasses import dataclassfrom pathlib import Pathfrom typing import List, Optional, Union
import numpy as npimport torchfrom huggingface_hub import hf_hub_downloadfrom transformers import PreTrainedModelfrom transformers.utils import cached_file
from .configuration_tips import TIPSv2Configfrom .image_encoder import vit_base, vit_giant2, vit_large, vit_small, vit_so400mfrom .text_encoder import TextEncoder, Tokenizer
_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] = mod return mod_VISION_FACTORIES = { "vit_small": vit_small, "vit_base": vit_base, "vit_large": vit_large, "vit_so400m": vit_so400m, "vit_giant2": vit_giant2,}
@dataclass40 unmodified lines def __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( 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 lines interpolate_offset=0.0, )
self.text_encoder = te.TextEncoder( self.text_encoder = TextEncoder( config={ "hidden_size": config.text_hidden_size, "mlp_dim": config.text_mlp_dim,4 unmodified lines )
self._tokenizer = None self._te_mod = te
def _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)) """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