tipsv2-l14 (ONNX)
ONNX export of google/tipsv2-l14.
Usage
import numpy as np
import onnxruntime as ort
from PIL import Image
from tokenizers import Tokenizer
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
vision = ort.InferenceSession("onnx/vision_model_fp16.onnx", providers=providers)
text = ort.InferenceSession("onnx/text_model_fp16.onnx", providers=providers)
scale = float(vision.get_modelmeta().custom_metadata_map["logit_scale"])
tokenizer = Tokenizer.from_file("tokenizer.json")
tokenizer.enable_padding(pad_id=0, pad_token="<pad>", length=64)
tokenizer.enable_truncation(64)
labels = ["a photo of a cat", "a photo of a dog", "a photo of a car"]
enc = tokenizer.encode_batch(labels)
input_ids = np.array([e.ids for e in enc], np.int64)
attention_mask = np.array([e.attention_mask for e in enc], np.int64)
image = Image.open("image.jpg").convert("RGB").resize((448, 448), Image.BILINEAR)
pixel_values = (np.asarray(image, np.float32) / 255).transpose(2, 0, 1)[None]
img_emb = vision.run(["image_embeds"], {"pixel_values": pixel_values})[0]
txt_emb = text.run(["text_embeds"], {"input_ids": input_ids, "attention_mask": attention_mask})[0]
img_emb /= np.linalg.norm(img_emb, axis=-1, keepdims=True)
txt_emb /= np.linalg.norm(txt_emb, axis=-1, keepdims=True)
logits = scale * img_emb @ txt_emb.T
probs = np.exp(logits - logits.max(-1, keepdims=True))
probs /= probs.sum(-1, keepdims=True)
print(dict(zip(labels, probs[0].round(3))))
Notes
logit_scale(1 / temperature fromconfig.json) is stored in the ONNX metadata.vision_modelaccepts any height and width that are multiples of 14, up to 1792.- Files:
onnx/{text,vision}_model.onnx(fp32),_fp16(fp16 weights, float32 I/O),_int8(8-bit weight-onlyMatMulNBits, row-wise int8 token embedding; requires ONNX Runtime). Opset 23 (ONNX Runtime 1.22+). - Changes from the original: exported the image and text encoders as separate graphs; the position-embedding interpolation always runs so the input size is dynamic (identity at 448x448); the computation is unchanged.
- Downloads last month
- 37
Model tree for AX1Y2JP/tipsv2-l14-ONNX
Base model
google/tipsv2-l14