-
Notifications
You must be signed in to change notification settings - Fork 231
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
base: main
Are you sure you want to change the base?
Raghav/image embedding #3429
Changes from 13 commits
df317c5
730d0d1
39e7126
9c3be96
ad2151f
9c61416
df9413d
30bc6a9
38ad1f3
f92dd9c
11c17b5
d2519fa
012583a
a2d7460
b835e8a
709e1f7
e119826
a2fd941
b0368b2
629fa64
12bb4a6
57b2bc9
8367268
04f4a53
259e4ac
24196cb
cfc063a
d1f24b4
1a5129b
00c8fb2
6e0f4d0
212485d
0aeb55e
df521d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||
| import sys | ||||||
| sys.path.append("../../common/python") | ||||||
| import tritonclient.grpc as grpcclient | ||||||
| from tritonclient.grpc import service_pb2 | ||||||
| from tritonclient.grpc import service_pb2_grpc | ||||||
| from tritonclient.utils import serialize_byte_tensor | ||||||
|
|
||||||
| import argparse | ||||||
| import datetime | ||||||
| import numpy as np | ||||||
| from client_utils import print_statistics | ||||||
| from urllib.request import urlretrieve | ||||||
| from pathlib import Path | ||||||
| import os | ||||||
| import grpc | ||||||
| import time | ||||||
|
|
||||||
| parser = argparse.ArgumentParser(description='GRPC client for clip example') | ||||||
|
|
||||||
| parser.add_argument('--timeout', required=False, default='15', | ||||||
| help='Specify timeout to wait for models readiness on the server in seconds. default 15 seconds.') | ||||||
| parser.add_argument('--url', required=False, default='localhost:9000', | ||||||
| help='Specify url to grpc service. default:localhost:9000') | ||||||
|
|
||||||
| parser.add_argument('--image_url', required=False, default='https://images.unsplash.com/photo-1716467891152-1b43a96de578?q=80&w=1481&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', | ||||||
| help='Specify image_url to send to the CLIP model. default:https://images.unsplash.com/photo-1716467891152-1b43a96de578?q=80&w=1481&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D') | ||||||
|
|
||||||
| args = vars(parser.parse_args()) | ||||||
|
|
||||||
| iterations = args.get('iterations') | ||||||
| iteration = 0 | ||||||
|
|
||||||
| timeout = int(args.get('timeout')) | ||||||
| # Check models ready | ||||||
| client = grpcclient.InferenceServerClient(args['url']) | ||||||
| channel = grpc.insecure_channel(args['url']) | ||||||
| grpc_stub = service_pb2_grpc.GRPCInferenceServiceStub(channel) | ||||||
|
|
||||||
| while(timeout): | ||||||
| request = service_pb2.ServerReadyRequest() | ||||||
| response = grpc_stub.ServerReady(request) | ||||||
| print("Server Ready: {}".format(response.ready)) | ||||||
| if response.ready: | ||||||
| break | ||||||
| time.sleep(1) | ||||||
| timeout-=1 | ||||||
|
|
||||||
| if not response.ready: | ||||||
| print("Models are not ready. Increase timeout or check server setup and errors.") | ||||||
| exit(-1) | ||||||
|
|
||||||
| image_url = args['image_url'] | ||||||
| print(f"Using image_url:\n{image_url}\n") | ||||||
|
|
||||||
| input_name = image_url.split("/")[-1] | ||||||
| sample_path = Path(os.path.join("data", input_name)) | ||||||
| if not os.path.exists(sample_path): | ||||||
| sample_path.parent.mkdir(parents=True, exist_ok=True) | ||||||
| urlretrieve( | ||||||
| image_url, | ||||||
| sample_path, | ||||||
| ) | ||||||
|
|
||||||
| with open(sample_path, "rb") as f: | ||||||
| image_data = f.read() | ||||||
|
|
||||||
| image_np = np.array([image_data], dtype=np.object_) | ||||||
|
|
||||||
| # Set up inference input correctly | ||||||
| image_input = grpcclient.InferInput("image", [1], "BYTES") | ||||||
| image_input.set_data_from_numpy(image_np) | ||||||
|
|
||||||
| processing_times = [] | ||||||
| start_time = datetime.datetime.now() | ||||||
| results = client.infer("python_model", [image_input]) | ||||||
|
||||||
| results = client.infer("python_model", [image_input]) | |
| results = client.infer(args['model'], [image_input]) |
please add --model CLI param. with this, user will not need to modify client code, but simply select model with --model dino_graph for example. you can keep some default for easier of use. im sure current python_model will not work, since you serve:
- clip_graph
- dino_graph
- laion_graph
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dkalinowski sure i will do this
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| print(f"Detection:\n{results.as_numpy('embedding')}\n") | |
| print(f"Embeddings:\n{results.as_numpy('embedding')}\n") |
| 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") | ||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from transformers import AutoImageProcessor, AutoModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from PIL import Image | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import openvino as ov | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model_id="facebook/dinov2-base" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Downloading pretrained model {model_id}...") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model=AutoModel.from_pretrained(model_id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| processor=AutoImageProcessor.from_pretrained(model_id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| image=Image.new("RGB",(224,224)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inputs=processor(images=image,return_tensors="pt")["pixel_values"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("Converting models...") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ov_model=ov.convert_model(model,example_input=inputs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ov.save_model(ov_model,"dino_image_encoder.xml") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("Model saved!") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mod_path="saved_mod/dino/1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.makedirs(mod_path,exist_ok=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model_id="facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model=AutoModel.from_pretrained(model_id) | |
| processor=AutoImageProcessor.from_pretrained(model_id) | |
| image=Image.new("RGB",(224,224)) | |
| inputs=processor(images=image,return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model=ov.convert_model(model,example_input=inputs) | |
| ov.save_model(ov_model,"dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path="saved_mod/dino/1" | |
| os.makedirs(mod_path,exist_ok=True) | |
| model_id = "facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model = AutoModel.from_pretrained(model_id) | |
| processor = AutoImageProcessor.from_pretrained(model_id) | |
| image = Image.new("RGB", (224, 224)) | |
| inputs = processor(images=image, return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model = ov.convert_model(model, example_input=inputs) | |
| ov.save_model(ov_model, "dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path = "saved_mod/dino/1" | |
| os.makedirs(mod_path, exist_ok=True) |
Outdated
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operators and after commas throughout the file. Should follow PEP 8 spacing conventions.
| model_id="facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model=AutoModel.from_pretrained(model_id) | |
| processor=AutoImageProcessor.from_pretrained(model_id) | |
| image=Image.new("RGB",(224,224)) | |
| inputs=processor(images=image,return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model=ov.convert_model(model,example_input=inputs) | |
| ov.save_model(ov_model,"dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path="saved_mod/dino/1" | |
| os.makedirs(mod_path,exist_ok=True) | |
| model_id = "facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model = AutoModel.from_pretrained(model_id) | |
| processor = AutoImageProcessor.from_pretrained(model_id) | |
| image = Image.new("RGB", (224, 224)) | |
| inputs = processor(images=image, return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model = ov.convert_model(model, example_input=inputs) | |
| ov.save_model(ov_model, "dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path = "saved_mod/dino/1" | |
| os.makedirs(mod_path, exist_ok=True) |
Outdated
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operators and after commas throughout the file. Should follow PEP 8 spacing conventions.
| model_id="facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model=AutoModel.from_pretrained(model_id) | |
| processor=AutoImageProcessor.from_pretrained(model_id) | |
| image=Image.new("RGB",(224,224)) | |
| inputs=processor(images=image,return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model=ov.convert_model(model,example_input=inputs) | |
| ov.save_model(ov_model,"dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path="saved_mod/dino/1" | |
| os.makedirs(mod_path,exist_ok=True) | |
| model_id = "facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model = AutoModel.from_pretrained(model_id) | |
| processor = AutoImageProcessor.from_pretrained(model_id) | |
| image = Image.new("RGB", (224, 224)) | |
| inputs = processor(images=image, return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model = ov.convert_model(model, example_input=inputs) | |
| ov.save_model(ov_model, "dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path = "saved_mod/dino/1" | |
| os.makedirs(mod_path, exist_ok=True) |
Outdated
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operators and after commas throughout the file. Should follow PEP 8 spacing conventions.
| model_id="facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model=AutoModel.from_pretrained(model_id) | |
| processor=AutoImageProcessor.from_pretrained(model_id) | |
| image=Image.new("RGB",(224,224)) | |
| inputs=processor(images=image,return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model=ov.convert_model(model,example_input=inputs) | |
| ov.save_model(ov_model,"dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path="saved_mod/dino/1" | |
| os.makedirs(mod_path,exist_ok=True) | |
| model_id = "facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model = AutoModel.from_pretrained(model_id) | |
| processor = AutoImageProcessor.from_pretrained(model_id) | |
| image = Image.new("RGB", (224, 224)) | |
| inputs = processor(images=image, return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model = ov.convert_model(model, example_input=inputs) | |
| ov.save_model(ov_model, "dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path = "saved_mod/dino/1" | |
| os.makedirs(mod_path, exist_ok=True) |
Outdated
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operators and after commas throughout the file. Should follow PEP 8 spacing conventions.
| model_id="facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model=AutoModel.from_pretrained(model_id) | |
| processor=AutoImageProcessor.from_pretrained(model_id) | |
| image=Image.new("RGB",(224,224)) | |
| inputs=processor(images=image,return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model=ov.convert_model(model,example_input=inputs) | |
| ov.save_model(ov_model,"dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path="saved_mod/dino/1" | |
| os.makedirs(mod_path,exist_ok=True) | |
| model_id = "facebook/dinov2-base" | |
| print(f"Downloading pretrained model {model_id}...") | |
| model = AutoModel.from_pretrained(model_id) | |
| processor = AutoImageProcessor.from_pretrained(model_id) | |
| image = Image.new("RGB", (224, 224)) | |
| inputs = processor(images=image, return_tensors="pt")["pixel_values"] | |
| print("Converting models...") | |
| ov_model = ov.convert_model(model, example_input=inputs) | |
| ov.save_model(ov_model, "dino_image_encoder.xml") | |
| print("Model saved!") | |
| mod_path = "saved_mod/dino/1" | |
| os.makedirs(mod_path, exist_ok=True) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from transformers import CLIPProcessor, CLIPModel | ||
| from PIL import Image | ||
| import openvino as ov | ||
| import torch | ||
| import os | ||
|
|
||
| # Replace this with your LAION model | ||
| model_id = "laion/CLIP-ViT-B-32-laion2B-s34B-b79K" | ||
| print(f"Downloading pretrained model: {model_id}") | ||
|
|
||
| # Load processor and model | ||
| processor = CLIPProcessor.from_pretrained(model_id) | ||
| full_model = CLIPModel.from_pretrained(model_id) | ||
| image_encoder = full_model.vision_model | ||
| image_encoder.eval() | ||
|
|
||
| # Dummy image input for tracing | ||
| image = Image.new("RGB", (224, 224)) | ||
| inputs = processor(images=image, return_tensors="pt")["pixel_values"] | ||
|
|
||
| # Convert to OpenVINO IR | ||
| print("Converting image encoder to OpenVINO IR...") | ||
| ov_model = ov.convert_model(image_encoder, example_input=inputs) | ||
| ov.save_model(ov_model, "clip_image_encoder.xml") | ||
| print("Model saved!") | ||
|
|
||
| # Move to proper OVMS path | ||
| mod_path = "saved_mod/laion/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(f"Model ready at {mod_path} for OpenVINO Model Server") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| tritonclient[all]==2.51.0 | ||
| numpy<2.0.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "model_config_list": [ | ||
| { | ||
| "config": { | ||
| "name": "clip", | ||
| "base_path": "/saved_mod/clip" | ||
| } | ||
| }, | ||
| { | ||
| "config": { | ||
| "name": "dino", | ||
| "base_path": "/saved_mod/dino" | ||
| } | ||
| }, | ||
| { | ||
| "config": { | ||
| "name": "laion", | ||
| "base_path": "/saved_mod/laion" | ||
| } | ||
| } | ||
| ], | ||
| "mediapipe_config_list": [ | ||
| { | ||
| "name": "clip_graph", | ||
| "graph_path": "/workspace/graph_clip.pbtxt" | ||
| }, | ||
| { | ||
| "name": "dino_graph", | ||
| "graph_path": "/workspace/graph_dino.pbtxt" | ||
| }, | ||
| { | ||
| "name": "laion_graph", | ||
| "graph_path": "/workspace/graph_laion.pbtxt" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| input_stream: "OVMS_PY_TENSOR:image" | ||
| output_stream: "OVMS_PY_TENSOR:embedding" | ||
|
|
||
| node{ | ||
| name: "ImagePreprocessor" | ||
| calculator: "PythonExecutorCalculator" | ||
| input_side_packet: "PYTHON_NODE_RESOURCES:py" | ||
| 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:image_em" | ||
| } | ||
|
|
||
| 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:image_em" | ||
| 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: "PyTensorOvTensorConverterCalculator" | ||
| input_stream: "OVTENSOR:pooler_output" | ||
| output_stream: "OVMS_PY_TENSOR:pooler_output_em" | ||
| node_options: { | ||
| [type.googleapis.com/mediapipe.PyTensorOvTensorConverterCalculatorOptions]: { | ||
| tag_to_output_tensor_names{ | ||
| key: "OVMS_PY_TENSOR" | ||
| value: "pooler_output_em" | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| node { | ||
| name: "Postprocessor" | ||
| calculator: "PythonExecutorCalculator" | ||
| input_side_packet: "PYTHON_NODE_RESOURCES:py" | ||
| input_stream: "OVMS_PY_TENSOR:pooler_output_em" | ||
| output_stream: "OVMS_PY_TENSOR:embedding" | ||
| node_options: { | ||
| [type.googleapis.com/mediapipe.PythonExecutorCalculatorOptions]: { | ||
| handler_path: "/workspace/post.py" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from pyovms import Tensor | ||
| import numpy as np | ||
|
|
||
| class OvmsPythonModel: | ||
| def initialize(self, kwargs: dict): | ||
| self.node_name = kwargs.get("node_name", "") | ||
| if "clip" in self.node_name.lower(): | ||
| self.mode = "clip" | ||
|
Collaborator
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. is self.mode really needed? |
||
| elif "dino" in self.node_name.lower(): | ||
| self.mode = "dino" | ||
| elif "laion" in self.node_name.lower(): | ||
| self.mode = "laion" | ||
| else: | ||
| raise ValueError(f"Unsupported model type in node name: {self.node_name}") | ||
|
|
||
| def execute(self, inputs: list) -> list: | ||
| try: | ||
| tensor = inputs[0] | ||
| embedding = np.frombuffer(tensor.data, dtype=np.float32).reshape(tensor.shape) | ||
| norm = np.linalg.norm(embedding, axis=1, keepdims=True) + 1e-10 | ||
| normalized = embedding / norm | ||
| return [Tensor(name="embedding", buffer=normalized.astype(np.float32))] | ||
| except Exception as e: | ||
| print(">>> ERROR in Postprocessor:", str(e)) | ||
| raise | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from pyovms import Tensor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from transformers import CLIPProcessor,AutoImageProcessor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from transformers import CLIPProcessor,AutoImageProcessor | |
| from transformers import CLIPProcessor, AutoImageProcessor |
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operator and after comma. Should be self.node_name = kwargs.get(\"node_name\", \"\").
| self.node_name=kwargs.get("node_name","") | |
| self.node_name = kwargs.get("node_name", "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is self.mode really needed?
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operators throughout the method. Should be self.processor = ... and self.mode = ....
| self.node_name=kwargs.get("node_name","") | |
| if "clip" in self.node_name.lower(): | |
| self.processor=CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| self.mode="clip" | |
| elif "dino" in self.node_name.lower(): | |
| self.processor=AutoImageProcessor.from_pretrained("facebook/dinov2-base") | |
| self.mode="dino" | |
| elif "laion" in self.node_name.lower(): | |
| self.processor=CLIPProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K") | |
| self.mode="laion" | |
| self.node_name = kwargs.get("node_name", "") | |
| if "clip" in self.node_name.lower(): | |
| self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| self.mode = "clip" | |
| elif "dino" in self.node_name.lower(): | |
| self.processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base") | |
| self.mode = "dino" | |
| elif "laion" in self.node_name.lower(): | |
| self.processor = CLIPProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K") | |
| self.mode = "laion" |
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operators throughout the method. Should be self.processor = ... and self.mode = ....
| self.node_name=kwargs.get("node_name","") | |
| if "clip" in self.node_name.lower(): | |
| self.processor=CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| self.mode="clip" | |
| elif "dino" in self.node_name.lower(): | |
| self.processor=AutoImageProcessor.from_pretrained("facebook/dinov2-base") | |
| self.mode="dino" | |
| elif "laion" in self.node_name.lower(): | |
| self.processor=CLIPProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K") | |
| self.mode="laion" | |
| self.node_name = kwargs.get("node_name", "") | |
| if "clip" in self.node_name.lower(): | |
| self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| self.mode = "clip" | |
| elif "dino" in self.node_name.lower(): | |
| self.processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base") | |
| self.mode = "dino" | |
| elif "laion" in self.node_name.lower(): | |
| self.processor = CLIPProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K") | |
| self.mode = "laion" |
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operators throughout the method. Should be self.processor = ... and self.mode = ....
| self.node_name=kwargs.get("node_name","") | |
| if "clip" in self.node_name.lower(): | |
| self.processor=CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| self.mode="clip" | |
| elif "dino" in self.node_name.lower(): | |
| self.processor=AutoImageProcessor.from_pretrained("facebook/dinov2-base") | |
| self.mode="dino" | |
| elif "laion" in self.node_name.lower(): | |
| self.processor=CLIPProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K") | |
| self.mode="laion" | |
| self.node_name = kwargs.get("node_name", "") | |
| if "clip" in self.node_name.lower(): | |
| self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| self.mode = "clip" | |
| elif "dino" in self.node_name.lower(): | |
| self.processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base") | |
| self.mode = "dino" | |
| elif "laion" in self.node_name.lower(): | |
| self.processor = CLIPProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K") | |
| self.mode = "laion" |
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after comma in function parameter. Should be execute(self, inputs: list).
| def execute(self,inputs: list): | |
| def execute(self, inputs: list): |
Copilot
AI
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around assignment operators and after commas. Should be image = Image.open(...), processed = self.processor(images=image, return_tensors=\"np\"), pixel_values = processed[...], and return [Tensor(name=\"pixel_values\", buffer=pixel_values)].
| self.node_name=kwargs.get("node_name","") | |
| if "clip" in self.node_name.lower(): | |
| self.processor=CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| self.mode="clip" | |
| elif "dino" in self.node_name.lower(): | |
| self.processor=AutoImageProcessor.from_pretrained("facebook/dinov2-base") | |
| self.mode="dino" | |
| elif "laion" in self.node_name.lower(): | |
| self.processor=CLIPProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K") | |
| self.mode="laion" | |
| else: | |
| raise ValueError(f"Unsupported model type in node name: {self.node_name}") | |
| def execute(self,inputs: list): | |
| 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="pixel_values",buffer=pixel_values)] | |
| self.node_name = kwargs.get("node_name", "") | |
| if "clip" in self.node_name.lower(): | |
| self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| self.mode = "clip" | |
| elif "dino" in self.node_name.lower(): | |
| self.processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base") | |
| self.mode = "dino" | |
| elif "laion" in self.node_name.lower(): | |
| self.processor = CLIPProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K") | |
| self.mode = "laion" | |
| else: | |
| raise ValueError(f"Unsupported model type in node name: {self.node_name}") | |
| def execute(self, inputs: list): | |
| 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="pixel_values", buffer=pixel_values)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't edit Dockerfile.ubuntu in this pull request (this makes conflicts and is really not required)