Zero-Shot Image Classification
Transformers
Safetensors
tipsv2
feature-extraction
vision
image-text
contrastive-learning
zero-shot
custom_code
Instructions to use google/tipsv2-b14 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/tipsv2-b14 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("zero-shot-image-classification", model="google/tipsv2-b14", trust_remote_code=True) pipe( "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png", candidate_labels=["animals", "humans", "landscape"], )# Load model directly from transformers import AutoProcessor, AutoModel processor = AutoProcessor.from_pretrained("google/tipsv2-b14", trust_remote_code=True) model = AutoModel.from_pretrained("google/tipsv2-b14", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Use relative imports for sibling modules (fixes local loading, save_pretrained, pickling)
Browse files- modeling_tips.py +14 -34
modeling_tips.py
CHANGED
|
@@ -1,34 +1,23 @@
|
|
| 1 |
"""TIPSv2 model for HuggingFace — wraps vision and text encoders."""
|
| 2 |
|
| 3 |
-
import importlib
|
| 4 |
-
import os
|
| 5 |
from dataclasses import dataclass
|
| 6 |
-
from pathlib import Path
|
| 7 |
from typing import List, Optional, Union
|
| 8 |
|
| 9 |
-
import numpy as np
|
| 10 |
import torch
|
| 11 |
-
from huggingface_hub import hf_hub_download
|
| 12 |
from transformers import PreTrainedModel
|
|
|
|
| 13 |
|
| 14 |
from .configuration_tips import TIPSv2Config
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
""
|
| 22 |
-
|
| 23 |
-
return _sibling_cache[name]
|
| 24 |
-
path = _this_dir / f"{name}.py"
|
| 25 |
-
if not path.exists() and repo_id:
|
| 26 |
-
path = Path(hf_hub_download(repo_id, f"{name}.py"))
|
| 27 |
-
spec = importlib.util.spec_from_file_location(name, str(path))
|
| 28 |
-
mod = importlib.util.module_from_spec(spec)
|
| 29 |
-
spec.loader.exec_module(mod)
|
| 30 |
-
_sibling_cache[name] = mod
|
| 31 |
-
return mod
|
| 32 |
|
| 33 |
|
| 34 |
@dataclass
|
|
@@ -75,12 +64,7 @@ class TIPSv2Model(PreTrainedModel):
|
|
| 75 |
def __init__(self, config: TIPSv2Config):
|
| 76 |
super().__init__(config)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
ie = _load_sibling("image_encoder", repo_id)
|
| 80 |
-
te = _load_sibling("text_encoder", repo_id)
|
| 81 |
-
|
| 82 |
-
build_fn = getattr(ie, config.vision_fn)
|
| 83 |
-
self.vision_encoder = build_fn(
|
| 84 |
img_size=config.img_size,
|
| 85 |
patch_size=config.patch_size,
|
| 86 |
ffn_layer=config.ffn_layer,
|
|
@@ -90,7 +74,7 @@ class TIPSv2Model(PreTrainedModel):
|
|
| 90 |
interpolate_offset=0.0,
|
| 91 |
)
|
| 92 |
|
| 93 |
-
self.text_encoder =
|
| 94 |
config={
|
| 95 |
"hidden_size": config.text_hidden_size,
|
| 96 |
"mlp_dim": config.text_mlp_dim,
|
|
@@ -101,14 +85,10 @@ class TIPSv2Model(PreTrainedModel):
|
|
| 101 |
)
|
| 102 |
|
| 103 |
self._tokenizer = None
|
| 104 |
-
self._te_mod = te
|
| 105 |
|
| 106 |
def _load_tokenizer(self):
|
| 107 |
-
"""
|
| 108 |
-
|
| 109 |
-
if not tok_path.exists():
|
| 110 |
-
tok_path = hf_hub_download(self.name_or_path, "tokenizer.model")
|
| 111 |
-
return self._te_mod.Tokenizer(str(tok_path))
|
| 112 |
|
| 113 |
@torch.no_grad()
|
| 114 |
def encode_image(self, pixel_values: torch.Tensor) -> TIPSv2ImageOutput:
|
|
|
|
| 1 |
"""TIPSv2 model for HuggingFace — wraps vision and text encoders."""
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from dataclasses import dataclass
|
|
|
|
| 4 |
from typing import List, Optional, Union
|
| 5 |
|
|
|
|
| 6 |
import torch
|
|
|
|
| 7 |
from transformers import PreTrainedModel
|
| 8 |
+
from transformers.utils import cached_file
|
| 9 |
|
| 10 |
from .configuration_tips import TIPSv2Config
|
| 11 |
+
from .image_encoder import vit_base, vit_giant2, vit_large, vit_small, vit_so400m
|
| 12 |
+
from .text_encoder import TextEncoder, Tokenizer
|
| 13 |
|
| 14 |
+
_VISION_FACTORIES = {
|
| 15 |
+
"vit_small": vit_small,
|
| 16 |
+
"vit_base": vit_base,
|
| 17 |
+
"vit_large": vit_large,
|
| 18 |
+
"vit_so400m": vit_so400m,
|
| 19 |
+
"vit_giant2": vit_giant2,
|
| 20 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
@dataclass
|
|
|
|
| 64 |
def __init__(self, config: TIPSv2Config):
|
| 65 |
super().__init__(config)
|
| 66 |
|
| 67 |
+
self.vision_encoder = _VISION_FACTORIES[config.vision_fn](
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
img_size=config.img_size,
|
| 69 |
patch_size=config.patch_size,
|
| 70 |
ffn_layer=config.ffn_layer,
|
|
|
|
| 74 |
interpolate_offset=0.0,
|
| 75 |
)
|
| 76 |
|
| 77 |
+
self.text_encoder = TextEncoder(
|
| 78 |
config={
|
| 79 |
"hidden_size": config.text_hidden_size,
|
| 80 |
"mlp_dim": config.text_mlp_dim,
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
self._tokenizer = None
|
|
|
|
| 88 |
|
| 89 |
def _load_tokenizer(self):
|
| 90 |
+
"""Load the SentencePiece tokenizer shipped with the checkpoint."""
|
| 91 |
+
return Tokenizer(cached_file(self.name_or_path, "tokenizer.model"))
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
@torch.no_grad()
|
| 94 |
def encode_image(self, pixel_values: torch.Tensor) -> TIPSv2ImageOutput:
|