-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/damian/benchmark_llm
- Loading branch information
Showing
18 changed files
with
613 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
tweepy==4.8.0 | ||
deepsparse>=1.1.0 | ||
deepsparse[transformers]>=1.5.2 | ||
rich>=12.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# CLIP Inference Pipelines | ||
|
||
DeepSparse allows inference on [CLIP](https://github.com/mlfoundations/open_clip) models. | ||
|
||
The CLIP integration currently supports the following task: | ||
- **Zero-shot Image Classification** - Classifying images given possible classes | ||
|
||
## Getting Started | ||
|
||
Before you start your adventure with the DeepSparse Engine, make sure that your machine is compatible with our [hardware requirements](https://docs.neuralmagic.com/deepsparse/source/hardware.html). | ||
|
||
### Installation | ||
```pip install deepsparse[clip]``` | ||
|
||
### Model Format | ||
By default, to deploy CLIP models using the DeepSparse Engine, it is required to supply the model in the ONNX format. This grants the engine the flexibility to serve any model in a framework-agnostic environment. To see examples of pulling CLIP models and exporting them to ONNX, please see the [sparseml documentation](https://github.com/neuralmagic/sparseml/tree/main/integrations/clip). For the Zero-shot image classification workflow, two ONNX models are required, a visual model for CLIP's visual branch, and a text model for CLIP's text branch. Both of these model should be produced through the sparseml integration linked above. | ||
|
||
### Deployment examples: | ||
The following example uses pipelines to run the CLIP models for inference. As input, the pipeline ingests a list of images and a list of possible classes. A class is returned for each of the provided images. | ||
|
||
If you don't have images ready, pull down the sample images using the following commands: | ||
|
||
```bash | ||
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 | ||
``` | ||
|
||
This will pull down two images, one with a happy dog and one with St.Peter's basilica. | ||
|
||
#### Zero-shot Prediction | ||
|
||
Let's run an example to clasify the images. We'll provide the images in a list with their file names as well as a list of possible classes. We'll also provide paths to the exported ONNX models. | ||
|
||
```python | ||
import numpy as np | ||
|
||
from deepsparse import BasePipeline | ||
from deepsparse.clip import ( | ||
CLIPTextInput, | ||
CLIPVisualInput, | ||
CLIPZeroShotInput | ||
) | ||
|
||
possible_classes = ["ice cream", "an elephant", "a dog", "a building", "a church"] | ||
images = ["basilica.jpg", "buddy.jpeg"] | ||
|
||
model_path_text = "zeroshot_research/text/model.onnx" | ||
model_path_visual = "zeroshot_research/visual/model.onnx" | ||
|
||
kwargs = { | ||
"visual_model_path": model_path_visual, | ||
"text_model_path": model_path_text, | ||
} | ||
pipeline = BasePipeline.create(task="clip_zeroshot", **kwargs) | ||
|
||
pipeline_input = CLIPZeroShotInput( | ||
image=CLIPVisualInput(images=images), | ||
text=CLIPTextInput(text=possible_classes), | ||
) | ||
|
||
output = pipeline(pipeline_input).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}") | ||
``` | ||
|
||
Running the code above, we get the following outuput: | ||
|
||
``` | ||
DeepSparse, Copyright 2021-present / Neuralmagic, Inc. version: 1.6.0.20230727 COMMUNITY | (3cb4a3e5) (optimized) (system=avx2, binary=avx2) | ||
Image basilica.jpg is a picture of a church | ||
Image buddy.jpeg is a picture of a dog | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# flake8: noqa | ||
|
||
from deepsparse.clip.text_pipeline import ( | ||
CLIPTextInput, | ||
CLIPTextOutput, | ||
CLIPTextPipeline, | ||
) | ||
from deepsparse.clip.visual_pipeline import ( | ||
CLIPVisualInput, | ||
CLIPVisualOutput, | ||
CLIPVisualPipeline, | ||
) | ||
from deepsparse.clip.zeroshot_pipeline import ( | ||
CLIPZeroShotInput, | ||
CLIPZeroShotOutput, | ||
CLIPZeroShotPipeline, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
__all__ = ["CLIP_RGB_MEANS", "CLIP_RGB_STDS"] | ||
|
||
CLIP_RGB_MEANS = [0.48145466, 0.4578275, 0.40821073] | ||
CLIP_RGB_STDS = [0.26862954, 0.26130258, 0.27577711] |
Oops, something went wrong.