-
Notifications
You must be signed in to change notification settings - Fork 218
Raghav/image embedding #3429
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
Open
Raghavyadav17
wants to merge
32
commits into
openvinotoolkit:main
Choose a base branch
from
Raghavyadav17:raghav/image-embedding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Raghav/image embedding #3429
Changes from 6 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
df317c5
Created a model download + Openvino IR format conversion script, used…
Raghavyadav17 730d0d1
Add image embedding pipeline with model conversion, preprocessing, po…
Raghavyadav17 39e7126
Fix: Use numpy<2.0.0 in Dockerfile for compatibility
Raghavyadav17 9c3be96
Cleanup: Removed venvs and config/make.save files per mentor feedback
Raghavyadav17 ad2151f
Removed stray convert_clip.py from old PR (not needed)
Raghavyadav17 9c61416
Fix: updated MediaPipe graph based on mentor feedback
Raghavyadav17 df9413d
Updated files corrected under Damian's supervision in the meeting
Raghavyadav17 30bc6a9
Restore Dockerfile.ubuntu after accidental deletion
Raghavyadav17 38ad1f3
fully functional grpc client + requirements.txt
Raghavyadav17 f92dd9c
Conversion scripts for dino and laion models to openvino IR
Raghavyadav17 11c17b5
Updated pre.py
Raghavyadav17 d2519fa
Updated post.py
Raghavyadav17 012583a
updated .json file for all models and their respective mediapipe
Raghavyadav17 a2d7460
Final grpc script, updated clip conversion script, updated requiremen…
Raghavyadav17 b835e8a
Demo images folder which contains all the images required to build ve…
Raghavyadav17 709e1f7
mediapipe for clip model
Raghavyadav17 e119826
mediapipe for dino
Raghavyadav17 a2fd941
mediapipe for laion
Raghavyadav17 b0368b2
demo image
Raghavyadav17 629fa64
search logic
Raghavyadav17 12bb4a6
demo frontend
Raghavyadav17 57b2bc9
config json for the project
Raghavyadav17 8367268
readme for the project
Raghavyadav17 04f4a53
Final grpc script with cli argument + loading bar as discussed in the…
Raghavyadav17 259e4ac
demo img for testing
Raghavyadav17 24196cb
final dino conversion script based on copilot suggestions
Raghavyadav17 cfc063a
final pre.py based on copilt suggestion
Raghavyadav17 d1f24b4
fixed dockerfile
Raghavyadav17 1a5129b
Merge branch 'main' into raghav/image-embedding
Raghavyadav17 00c8fb2
Merge branch 'main' into raghav/image-embedding
Raghavyadav17 6e0f4d0
Update demos/python_demos/image_embeddings/Readme.md
Raghavyadav17 212485d
Update demos/python_demos/image_embeddings/Readme.md
Raghavyadav17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
demos/python_demos/image_embeddings/model_conversion/clip_conversion.py
This file contains hidden or 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,29 @@ | ||
from transformers import CLIPProcessor, CLIPModel | ||
from PIL import Image | ||
import openvino as ov | ||
import os | ||
|
||
model_id="openai/clip-vit-base-patch32" | ||
print(f"Downloading pretrained model {model_id}...") | ||
|
||
full_model=CLIPModel.from_pretrained(model_id) | ||
model=full_model.vision_model | ||
processor=CLIPProcessor.from_pretrained(model_id) | ||
|
||
image=Image.new("RGB",(224,224)) | ||
inputs=processor(images=image,return_tensors="pt")["pixel_values"] | ||
|
||
print("Converting model...") | ||
ov_model=ov.convert_model(model,example_input=inputs) | ||
ov.save_model(ov_model,"clip_image_encoder.xml") | ||
print("Model saved!") | ||
|
||
mod_path="saved_mod/1" | ||
os.makedirs(mod_path,exist_ok=True) | ||
os.replace("clip_image_encoder.xml", f"{mod_path}/clip_image_encoder.xml") | ||
os.replace("clip_image_encoder.bin", f"{mod_path}/clip_image_encoder.bin") | ||
print("Model ready for OVMS") | ||
|
||
|
||
|
||
|
15 changes: 15 additions & 0 deletions
15
demos/python_demos/image_embeddings/servable/config_model.json
This file contains hidden or 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,15 @@ | ||
{ | ||
"model_config_list": [ | ||
{ | ||
"config": { | ||
"name": "clip", | ||
"base_path": "/saved_mod/" | ||
} | ||
}], | ||
"mediapipe_config_list": [ | ||
{ | ||
"name": "python_model", | ||
"graph_path": "/workspace/grph_pipeline.pbtxt" | ||
} | ||
] | ||
} |
72 changes: 72 additions & 0 deletions
72
demos/python_demos/image_embeddings/servable/graph_pipeline.pbtxt
This file contains hidden or 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,72 @@ | ||
input_stream: "OVMS_PY_TENSOR:image" | ||
output_stream: "OVMS_PY_TENSOR:embedding" | ||
|
||
node{ | ||
name: "ImagePreprocessor" | ||
calculator: "PythonExecutorCalculator" | ||
input_stream: "OVMS_PY_TENSOR:image" | ||
output_stream: "OVMS_PY_TENSOR:pixel_values" | ||
node_options:{ | ||
[type.googleapis.com/mediapipe.PythonExecutorCalculatorOptions]:{ | ||
handler_path: "/workspace/pre.py" | ||
} | ||
} | ||
} | ||
|
||
node{ | ||
name: "PixelValuesConverter" | ||
calculator: "PyTensorOvTensorConverterCalculator" | ||
input_stream: "OVMS_PY_TENSOR:pixel_values" | ||
output_stream: "OVTENSOR:40" | ||
} | ||
|
||
node { | ||
calculator: "OpenVINOModelServerSessionCalculator" | ||
output_side_packet: "SESSION:session" | ||
node_options: { | ||
[type.googleapis.com/mediapipe.OpenVINOModelServerSessionCalculatorOptions]: { | ||
servable_name: "clip" | ||
servable_version: "1" | ||
} | ||
} | ||
} | ||
|
||
node { | ||
calculator: "OpenVINOInferenceCalculator" | ||
input_side_packet: "SESSION:session" | ||
input_stream: "OVTENSOR:40" | ||
output_stream: "OVTENSOR:pooler_output" | ||
node_options: { | ||
[type.googleapis.com/mediapipe.OpenVINOInferenceCalculatorOptions]: { | ||
tag_to_input_tensor_names { | ||
key: "OVTENSOR" | ||
value: "40" | ||
} | ||
tag_to_output_tensor_names { | ||
key: "OVTENSOR" | ||
value: "pooler_output" | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
node { | ||
name: "EmbeddingConverter" | ||
calculator: "OvTensorPyTensorConverterCalculator" | ||
input_stream: "OVTENSOR:pooler_output" | ||
output_stream: "OVMS_PY_TENSOR:pooler_output" | ||
} | ||
|
||
|
||
node { | ||
name: "Postprocessor" | ||
calculator: "PythonExecutorCalculator" | ||
input_stream: "OVMS_PY_TENSOR:pooler_output" | ||
output_stream: "OVMS_PY_TENSOR:embedding" | ||
node_options: { | ||
[type.googleapis.com/mediapipe.PythonExecutorCalculatorOptions]: { | ||
handler_path: "/workspace/post.py" | ||
} | ||
} | ||
} |
This file contains hidden or 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,16 @@ | ||
from pyovms import Tensor | ||
import numpy as np | ||
from scipy.special import softmax | ||
from tritonclient.utils import deserialize_bytes_tensor | ||
|
||
class OvmsPythonModel: | ||
|
||
def initialize(self, kwargs:dict): | ||
pass | ||
|
||
def execute(self, inputs:list): | ||
embedding=inputs[0].as_numpy() | ||
norm=np.linalg.norm(embedding, axis=1, keepdims=True) | ||
normalized_embedding=embedding/norm | ||
|
||
return[Tensor(name="embedding", data=normalized_embedding.astype(np.float32))] |
This file contains hidden or 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,20 @@ | ||||||
from pyovms import Tensor | ||||||
from transformers import CLIPProcessor | ||||||
from PIL import Image | ||||||
import numpy as np | ||||||
from io import BytesIO | ||||||
from tritonclient.utils import deserialize_bytes_tensor | ||||||
|
||||||
class OvmsPythonModel: | ||||||
def initialize(self, kwargs:dict): | ||||||
model_id="openai/clip-vit-base-patch32" | ||||||
self.processor=CLIPProcessor.from_pretrained(model_id) | ||||||
|
||||||
def execute(self,inputs: list): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space after comma in function parameter. Should be
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
image_bytes = deserialize_bytes_tensor(bytes(inputs[0]))[0] | ||||||
|
||||||
image=Image.open(BytesIO(image_bytes)).convert("RGB") | ||||||
processed=self.processor(images=image,return_tensors="np") | ||||||
pixel_values=processed["pixel_values"].astype(np.float32) | ||||||
return[Tensor(name="40",data=pixel_values)] | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.