clip-vit-base-patch32-onnx

This is an ONNX conversion of openai/clip-vit-base-patch32 at revision 3d74acf9a28c67741b2f4f2ea7635f0aaf6f0268, made with api/tools/export_onnx.py from VBLIS for use in the VBLIS app. It is not an official release of OpenAI. The weights are unchanged (fp32 values of the source model).

What was changed

  • The vision tower and the text tower are two separate graphs (vision.onnx, text.onnx), converted with the PyTorch dynamo exporter (torch.onnx.export(..., dynamo=True)), ONNX opset 18, fp32, dynamic batch size. The weights are stored in vision.onnx.data and text.onnx.data.
  • Every Reshape node has allowzero=0 instead of the exporter's allowzero=1. The result is the same, because no reshape shape contains a literal 0. The export fails if one does.
  • The graphs return the un-normalised features (image_features, text_features). L2-normalise them before comparing.

No other change was made to the model. config.json, preprocessor_config.json and the tokenizer files are copied unchanged from the source.

Inputs and outputs

vision.onnx

name dtype shape note
input pixel_values float32 (batch, 3, 224, 224) from the processor
output image_features float32 (batch, 512) un-normalised, 512 values per item

text.onnx

name dtype shape note
input input_ids int64 (batch, 77) from the processor
input attention_mask int64 (batch, 77) from the processor
output text_features float32 (batch, 512) un-normalised, 512 values per item

Text is lower-cased, tokenised with padding="max_length" and truncated to 77 tokens.

Usage

import numpy as np
import onnxruntime as ort
from huggingface_hub import snapshot_download
from PIL import Image
from transformers import AutoProcessor

path = snapshot_download("opoeci6/clip-vit-base-patch32-onnx")
processor = AutoProcessor.from_pretrained(path, use_fast=False)
providers = ["CPUExecutionProvider"]
vision = ort.InferenceSession(f"{path}/vision.onnx", providers=providers)
text = ort.InferenceSession(f"{path}/text.onnx", providers=providers)

def normalize(x):
    return x / np.linalg.norm(x, axis=-1, keepdims=True)

image = Image.open("photo.jpg").convert("RGB")
inputs = processor(images=[image], return_tensors="np")
image_features = normalize(vision.run(None, {i.name: inputs[i.name] for i in vision.get_inputs()})[0])

tokens = processor(text="a photo of a cat".lower(), return_tensors="np", padding="max_length", truncation=True, max_length=77)
text_features = normalize(text.run(None, {i.name: tokens[i.name] for i in text.get_inputs()})[0])

print(image_features @ text_features.T)  # cosine similarity

Files

File Size (bytes) SHA-256
config.json 4186 b575ef3c36f2a057fa19e221650105052d61cc9c1a972ec15019c6261ec98770
LICENSE 1064 987e63b32f6c89ff5160e429458a872ff048e6860b590a3912e938f9da8f14db
merges.txt 524657 f526393189112391ce6f9795d4695f704121ce452c3aad1f5335cc41337eba85
preprocessor_config.json 316 910e70b3956ac9879ebc90b22fb3bc8a75b6a0677814500101a4c072bd7857bd
special_tokens_map.json 389 f8c0d6c39aee3f8431078ef6646567b0aba7f2246e9c54b8b99d55c22b707cbf
text.onnx 1438115 3677da1a71a6bffe0e7fe4bb5846b56b77169d6d65305fe473d40543afb739ca
text.onnx.data 253719545 6bc16d1410a472360a8fd68b91eaf52c14d113f6eb1a3584426ecb4625e582c1
tokenizer.json 2224041 b556ac8c99757ffb677208af34bc8c6721572114111a6e0aaf5fa69ff0b8d842
tokenizer_config.json 592 34b7336e4bee12e0a9730eaf5189f582ef3c3eea5027f65730e5717256755aad
vision.onnx 1351258 e5c995fc14e4e7a1cc975127ca52380bee900bf1b44ffcc5ad6aae41cf693cc5
vision.onnx.data 351397264 928bf480a2c8fb5ddd65ce2c542755e32bf5bb6a285cd695aa5d6f76becd1671
vocab.json 862328 5047b556ce86ccaf6aa22b3ffccfc52d391ea4accdab9c2f2407da5b742d4363

vblis_onnx.json (the manifest, schema 1) lists the same files and the export settings; it also holds the SHA-256 of this README.

Parity check

ONNX Runtime 1.24.3 (CPU) against the PyTorch model on 12 generated pictures and 8 texts: minimum cosine similarity 1.00000000 (pictures) and 1.00000000 (texts).

License

The original model is released by OpenAI under the MIT License, Copyright (c) 2021 OpenAI; this conversion is distributed under the same licence (see LICENSE). The original model card and licence apply: https://huggingface.co/openai/clip-vit-base-patch32

Downloads last month
20
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for opoeci6/clip-vit-base-patch32-onnx

Quantized
(17)
this model