ONNX Model Zoo

RedHatAI/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-ds

By RedHatAI · ↓ 0 · ♥ 0 · Updated 2023-12-20

What's in this repo

This page lists the files in RedHatAI/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-ds, including textual.onnx, visual.onnx.

  • 2 ONNX models (.onnx files)
  • 7 files, including 3 .json, 2 .onnx, 1 .md
  • Last updated 2023-12-20

ONNX files

File Size Download
textual.onnx — Download
visual.onnx — Download

How to use

These ONNX files can be loaded with ONNX Runtime. To inspect a model graph, upload the file to the Netron viewer. Download links point to the original files on Hugging Face.

README

This is an unoptimized, exported version of https://huggingface.co/laion/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K that is ready to use with DeepSparse. It achieves 95.7% zero-shot top-1 accuracy on Imagenette.

Notebook for basic usage: Colab Notebook for Imagenette evaluation: Colab

Setup for usage

First, install DeepSparse with extensions for CLIP:

pip install deepsparse-nightly[clip]>=1.7.0.20231210

Download some test images of a church, a dog, and elephants:

wget -O basilica.jpg https://raw.githubusercontent.com/neuralmagic/deepsparse/main/src/deepsparse/yolo/sample_images/basilica.jpg
wget -O buddy.jpeg https://raw.githubusercontent.com/neuralmagic/deepsparse/main/tests/deepsparse/pipelines/sample_images/buddy.jpeg
wget -O thailand.jpg https://raw.githubusercontent.com/neuralmagic/deepsparse/main/src/deepsparse/yolact/sample_images/thailand.jpg

For this model there is a second input that is the length of tokens, so run this input override code before making a text pipeline:

import numpy as np
from deepsparse.clip import CLIPTextPipeline

def custom_process_inputs(self, inputs):
    if not isinstance(inputs.text, list):
        inputs.text = [inputs.text]
    if not isinstance(inputs.text[0], str):
        return inputs.text
    tokens = [np.array(t).astype(np.int32) for t in self.tokenizer(inputs.text)]
    tokens = np.stack(tokens, axis=0)
    tokens_lengths = np.array(tokens.shape[0] * [tokens.shape[1] - 1])
    return [tokens, tokens_lengths]

# This overrides the process_inputs function globally for all CLIPTextPipeline classes
CLIPTextPipeline.process_inputs = custom_process_inputs

Text embedding pipeline

Here is an example of how to create and use a DeepSparse pipeline for text embeddings.

from deepsparse import Pipeline
from huggingface_hub import snapshot_download

# Download the model from HF
model_folder = snapshot_download(repo_id="neuralmagic/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-quant-ds")

text_embed_pipeline = Pipeline.create(task="clip_text", model_path=model_folder + "/textual.onnx")

text = ["ice cream", "an elephant", "a dog", "a building", "a church"]

embeddings = text_embed_pipeline(text=text).text_embeddings
for i in range(len(embeddings)):
    print(embeddings[i].shape)
    print(embeddings[i])

Image embedding pipeline

Here is an example of how to create and use a DeepSparse pipeline for image embeddings.

from deepsparse import Pipeline
from huggingface_hub import snapshot_download

# Download the model from HF
model_folder = snapshot_download(repo_id="neuralmagic/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-quant-ds")

image_embed_pipeline = Pipeline.create(task="clip_visual", model_path=model_folder + "/visual.onnx")

images = ["basilica.jpg", "buddy.jpeg", "thailand.jpg"]

embeddings = image_embed_pipeline(images=images).image_embeddings
for i in range(len(embeddings)):
    print(embeddings[i].shape)
    print(embeddings[i])

Zero-shot image classification pipeline

Since CLIP trained both the text and image embedding models in tandem, we can generate embeddings for both and relate them together without retraining. Here is an example of how to create and use a DeepSparse pipeline for zero-shot image classification.

from deepsparse import Pipeline
from deepsparse.clip import (
    CLIPTextInput,
    CLIPVisualInput,
    CLIPZeroShotInput
)
from huggingface_hub import snapshot_download

# Download the model from HF
model_folder = snapshot_download(repo_id="neuralmagic/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-quant-ds")

possible_classes = ["ice cream", "an elephant", "a dog", "a building", "a church"]
images = ["basilica.jpg", "buddy.jpeg", "thailand.jpg"]

# Load the model into DeepSparse
pipeline = Pipeline.create(
    task="clip_zeroshot",
    visual_model_path=model_folder + "/visual.onnx",
    text_model_path=model_folder + "/textual.onnx"
)

# Infer
output = pipeline(
    image=CLIPVisualInput(images=images),
    text=CLIPTextInput(text=possible_classes),
).text_scores

for i in range(len(output)):
    prediction = possible_classes[np.argmax(output[i])]
    print(f"Image {images[i]} is a picture of {prediction}")

"""
Image basilica.jpg is a picture of a church
Image buddy.jpeg is a picture of a dog
Image thailand.jpg is a picture of an elephant
"""

Model info

onnx deepsparse zero-shot-classification base_model:laion/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K base_model:quantized:laion/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K region:us
↓ 0 ♥ 0

Created: 2023-12-20

Updated: 2023-12-20

View on Hugging Face

Files (7)

.gitattributes
README.md
open_clip_config.json
special_tokens_map.json
textual.onnx ONNX
tokenizer_config.json
visual.onnx ONNX

Related models