mgoin commited on
Commit
17680c0
1 Parent(s): 398d001

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +47 -5
README.md CHANGED
@@ -7,7 +7,7 @@ tags:
7
  ---
8
  This is a quantized version of https://huggingface.co/laion/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K that is ready to use with [DeepSparse](https://github.com/neuralmagic/deepsparse). It achieves 71.1% one-shot accuracy on ImageNet.
9
 
10
- ## Usage
11
  [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1ZvU9ZSHJKSeJyH5bgxo_A-GSVIUcSt2E?usp=sharing)
12
  First, install DeepSparse with extensions for CLIP:
13
  ```
@@ -21,7 +21,7 @@ wget -O buddy.jpeg https://raw.githubusercontent.com/neuralmagic/deepsparse/main
21
  wget -O thailand.jpg https://raw.githubusercontent.com/neuralmagic/deepsparse/main/src/deepsparse/yolact/sample_images/thailand.jpg
22
  ```
23
 
24
- For this model there is a second input that is the length of tokens, so run this input override before making the pipeline:
25
  ```python
26
  import numpy as np
27
  from deepsparse.clip import CLIPTextPipeline
@@ -40,7 +40,49 @@ def custom_process_inputs(self, inputs):
40
  CLIPTextPipeline.process_inputs = custom_process_inputs
41
  ```
42
 
43
- Then make and run a pipeline in Python:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  ```python
45
  from deepsparse import Pipeline
46
  from deepsparse.clip import (
@@ -58,8 +100,8 @@ images = ["basilica.jpg", "buddy.jpeg", "thailand.jpg"]
58
 
59
  # Load the model into DeepSparse
60
  pipeline = Pipeline.create(
61
- task="clip_zeroshot",
62
- visual_model_path=model_folder + "/visual.onnx",
63
  text_model_path=model_folder + "/textual.onnx"
64
  )
65
 
 
7
  ---
8
  This is a quantized version of https://huggingface.co/laion/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K that is ready to use with [DeepSparse](https://github.com/neuralmagic/deepsparse). It achieves 71.1% one-shot accuracy on ImageNet.
9
 
10
+ ## Setup for usage
11
  [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1ZvU9ZSHJKSeJyH5bgxo_A-GSVIUcSt2E?usp=sharing)
12
  First, install DeepSparse with extensions for CLIP:
13
  ```
 
21
  wget -O thailand.jpg https://raw.githubusercontent.com/neuralmagic/deepsparse/main/src/deepsparse/yolact/sample_images/thailand.jpg
22
  ```
23
 
24
+ 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:
25
  ```python
26
  import numpy as np
27
  from deepsparse.clip import CLIPTextPipeline
 
40
  CLIPTextPipeline.process_inputs = custom_process_inputs
41
  ```
42
 
43
+ ## Text embedding pipeline
44
+
45
+ Here is an example of how to create and use a [DeepSparse pipeline for text embeddings](https://github.com/neuralmagic/deepsparse/blob/main/src/deepsparse/clip/text_pipeline.py).
46
+ ```python
47
+ from deepsparse import Pipeline
48
+ from huggingface_hub import snapshot_download
49
+
50
+ # Download the model from HF
51
+ model_folder = snapshot_download(repo_id="neuralmagic/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-quant-ds")
52
+
53
+ text_embed_pipeline = Pipeline.create(task="clip_text", model_path=model_folder + "/textual.onnx")
54
+
55
+ text = ["ice cream", "an elephant", "a dog", "a building", "a church"]
56
+
57
+ embeddings = text_embed_pipeline(text=text).text_embeddings
58
+ for i in range(len(embeddings)):
59
+ print(embeddings[i].shape)
60
+ print(embeddings[i])
61
+ ```
62
+
63
+ ## Image embedding pipeline
64
+
65
+ Here is an example of how to create and use a [DeepSparse pipeline for image embeddings](https://github.com/neuralmagic/deepsparse/blob/main/src/deepsparse/clip/visual_pipeline.py).
66
+ ```python
67
+ from deepsparse import Pipeline
68
+ from huggingface_hub import snapshot_download
69
+
70
+ # Download the model from HF
71
+ model_folder = snapshot_download(repo_id="neuralmagic/CLIP-ViT-B-32-256x256-DataComp-s34B-b86K-quant-ds")
72
+
73
+ image_embed_pipeline = Pipeline.create(task="clip_visual", model_path=model_folder + "/visual.onnx")
74
+
75
+ images = ["basilica.jpg", "buddy.jpeg", "thailand.jpg"]
76
+
77
+ embeddings = image_embed_pipeline(images=images).image_embeddings
78
+ for i in range(len(embeddings)):
79
+ print(embeddings[i].shape)
80
+ print(embeddings[i])
81
+ ```
82
+
83
+ ## Zero-shot image classification pipeline
84
+
85
+ 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](https://github.com/neuralmagic/deepsparse/blob/main/src/deepsparse/clip/zeroshot_pipeline.py).
86
  ```python
87
  from deepsparse import Pipeline
88
  from deepsparse.clip import (
 
100
 
101
  # Load the model into DeepSparse
102
  pipeline = Pipeline.create(
103
+ task="clip_zeroshot",
104
+ visual_model_path=model_folder + "/visual.onnx",
105
  text_model_path=model_folder + "/textual.onnx"
106
  )
107