Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLIP]: Zeroshot Pipeline #1098

Merged
merged 34 commits into from
Aug 2, 2023
Merged

[CLIP]: Zeroshot Pipeline #1098

merged 34 commits into from
Aug 2, 2023

Conversation

dsikka
Copy link
Contributor

@dsikka dsikka commented Jun 27, 2023

Summary

NOTE: Pipeline has been tested with MLR's Zershot models. However, these are not yet available on sparsezoo.

Screenshot 2023-07-30 at 9 35 00 PM
  • This PR is based on the Pipeline refactor and uses the new BasePipeline class
  • This PR introduces 3 new pipelines: CLIPVisualPipeline, CLIPTextPipeline and CLIPZeroShotPipeline to run zero-shot classification.
  • The text pipeline uses a tokenizer provided by OpenCLIP

Testing

  • Mock engine tests were written to test all 3 pipelines and can be found in test_clip.py

Tested with MLR's models (on the following images):

basilica.jpg
basilica
buddy.jpeg
buddy


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 = ["buddy.jpeg", “basilica.jpg”]

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}")


Produces the output:

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

@dsikka dsikka marked this pull request as ready for review June 30, 2023 15:49
Copy link
Contributor

@bfineran bfineran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great pending comments and getting a test model / files up

src/deepsparse/clip/pipelines.py Outdated Show resolved Hide resolved
src/deepsparse/clip/pipelines.py Outdated Show resolved Hide resolved
@dsikka dsikka force-pushed the clip_zshot branch 5 times, most recently from 981bd2e to a06dd80 Compare July 13, 2023 17:42
Base automatically changed from pipeline_refactor to main July 18, 2023 11:16
@dsikka dsikka force-pushed the clip_zshot branch 2 times, most recently from a1c5c4a to 651595c Compare July 18, 2023 14:25
@dsikka
Copy link
Contributor Author

dsikka commented Jul 21, 2023

@bfineran Rebased and updated as per PR comments.

@dsikka dsikka requested review from bfineran and Satrat and removed request for KSGulin July 21, 2023 19:00
@dsikka
Copy link
Contributor Author

dsikka commented Jul 28, 2023

FYI This has been tested/verified with models by MLR

@dsikka
Copy link
Contributor Author

dsikka commented Jul 30, 2023

Updated with a README.md showing examples

dbogunowicz
dbogunowicz previously approved these changes Jul 31, 2023
src/deepsparse/clip/zeroshot_pipeline.py Show resolved Hide resolved
src/deepsparse/clip/zeroshot_pipeline.py Show resolved Hide resolved
src/deepsparse/clip/zeroshot_pipeline.py Show resolved Hide resolved
src/deepsparse/clip/visual_pipeline.py Outdated Show resolved Hide resolved
tests/deepsparse/pipelines/test_clip.py Show resolved Hide resolved
dbogunowicz
dbogunowicz previously approved these changes Jul 31, 2023
src/deepsparse/clip/zeroshot_pipeline.py Show resolved Hide resolved
bfineran
bfineran previously approved these changes Jul 31, 2023
@dsikka dsikka dismissed stale reviews from bfineran and dbogunowicz via 06e958e July 31, 2023 20:18
@bfineran bfineran merged commit ed9b1ee into main Aug 2, 2023
6 of 7 checks passed
@bfineran bfineran deleted the clip_zshot branch August 2, 2023 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants