Views
No views yet
pip install deepsparse-nightly[clip]>=1.7.0.20231210wget -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.jpg1import numpy as np
2from deepsparse.clip import CLIPTextPipeline
3
4def custom_process_inputs(self, inputs):
5 if not isinstance(inputs.text, list):
6 inputs.text = [inputs.text]
7 if not isinstance(inputs.text[0], str):
8 return inputs.text
9 tokens = [np.array(t).astype(np.int32) for t in self.tokenizer(inputs.text)]
10 tokens = np.stack(tokens, axis=0)
11 tokens_lengths = np.array(tokens.shape[0] * [tokens.shape[1] - 1])
12 return [tokens, tokens_lengths]
13
14# This overrides the process_inputs function globally for all CLIPTextPipeline classes
15CLIPTextPipeline.process_inputs = custom_process_inputs1from deepsparse import Pipeline
2from huggingface_hub import snapshot_download
3
4# Download the model from HF
5model_folder = snapshot_download(repo_id="neuralmagic/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-quant-ds")
6
7text_embed_pipeline = Pipeline.create(task="clip_text", model_path=model_folder + "/textual.onnx")
8
9text = ["ice cream", "an elephant", "a dog", "a building", "a church"]
10
11embeddings = text_embed_pipeline(text=text).text_embeddings
12for i in range(len(embeddings)):
13 print(embeddings[i].shape)
14 print(embeddings[i])1from deepsparse import Pipeline
2from huggingface_hub import snapshot_download
3
4# Download the model from HF
5model_folder = snapshot_download(repo_id="neuralmagic/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-quant-ds")
6
7image_embed_pipeline = Pipeline.create(task="clip_visual", model_path=model_folder + "/visual.onnx")
8
9images = ["basilica.jpg", "buddy.jpeg", "thailand.jpg"]
10
11embeddings = image_embed_pipeline(images=images).image_embeddings
12for i in range(len(embeddings)):
13 print(embeddings[i].shape)
14 print(embeddings[i])1from deepsparse import Pipeline
2from deepsparse.clip import (
3 CLIPTextInput,
4 CLIPVisualInput,
5 CLIPZeroShotInput
6)
7from huggingface_hub import snapshot_download
8
9# Download the model from HF
10model_folder = snapshot_download(repo_id="neuralmagic/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-quant-ds")
11
12possible_classes = ["ice cream", "an elephant", "a dog", "a building", "a church"]
13images = ["basilica.jpg", "buddy.jpeg", "thailand.jpg"]
14
15# Load the model into DeepSparse
16pipeline = Pipeline.create(
17 task="clip_zeroshot",
18 visual_model_path=model_folder + "/visual.onnx",
19 text_model_path=model_folder + "/textual.onnx"
20)
21
22# Infer
23output = pipeline(
24 image=CLIPVisualInput(images=images),
25 text=CLIPTextInput(text=possible_classes),
26).text_scores
27
28for i in range(len(output)):
29 prediction = possible_classes[np.argmax(output[i])]
30 print(f"Image {images[i]} is a picture of {prediction}")
31
32"""
33Image basilica.jpg is a picture of a church
34Image buddy.jpeg is a picture of a dog
35Image thailand.jpg is a picture of an elephant
36"""