Commit
·
78010d6
1
Parent(s):
65c75be
Update README.md
Browse files
README.md
CHANGED
@@ -27,4 +27,61 @@ The results indicate that the OpenVINO™ optimization provides a consistent imp
|
|
27 |
|
28 |
## Usage
|
29 |
|
30 |
-
You can utilize this optimized model for faster inferences in environments where time is a critical factor. Ensure you have the necessary libraries and dependencies installed to leverage the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
## Usage
|
29 |
|
30 |
+
You can utilize this optimized model for faster inferences in environments where time is a critical factor. Ensure you have the necessary libraries and dependencies installed to leverage the usage of OpenVINO™.
|
31 |
+
|
32 |
+
```bash
|
33 |
+
pip install transformers huggingface_hub openvino-dev
|
34 |
+
```
|
35 |
+
|
36 |
+
Then use it for inference:
|
37 |
+
|
38 |
+
```python
|
39 |
+
import os
|
40 |
+
|
41 |
+
import numpy as np
|
42 |
+
from PIL import Image
|
43 |
+
from huggingface_hub import snapshot_download
|
44 |
+
from openvino.runtime import Core
|
45 |
+
from scipy.special import softmax
|
46 |
+
from transformers import CLIPProcessor
|
47 |
+
|
48 |
+
# Download the OV model
|
49 |
+
ov_path = snapshot_download(repo_id="scaleflex/clip-vit-base-patch32-openvino")
|
50 |
+
# Load preprocessor for model input
|
51 |
+
processor = CLIPProcessor.from_pretrained("scaleflex/clip-vit-base-patch32-openvino")
|
52 |
+
ov_model_xml = os.path.join(ov_path, "clip-vit-base-patch32.xml")
|
53 |
+
|
54 |
+
image = Image.open("face.png") # download this example image: http://sample.li/face.png
|
55 |
+
input_labels = [
|
56 |
+
"businessman",
|
57 |
+
"dog playing in the garden",
|
58 |
+
"beautiful woman",
|
59 |
+
"big city",
|
60 |
+
"lake in the mountain",
|
61 |
+
]
|
62 |
+
text_descriptions = [f"This is a photo of a {label}" for label in input_labels]
|
63 |
+
inputs = processor(
|
64 |
+
text=text_descriptions, images=[image], return_tensors="pt", padding=True
|
65 |
+
)
|
66 |
+
|
67 |
+
# Create OpenVINO core object instance
|
68 |
+
core = Core()
|
69 |
+
|
70 |
+
ov_model = core.read_model(model=ov_model_xml)
|
71 |
+
# Compile model for loading on device
|
72 |
+
compiled_model = core.compile_model(ov_model)
|
73 |
+
# Obtain output tensor for getting predictions
|
74 |
+
logits_per_image_out = compiled_model.output(0)
|
75 |
+
# Run inference on preprocessed data and get image-text similarity score
|
76 |
+
ov_logits_per_image = compiled_model(dict(inputs))[logits_per_image_out]
|
77 |
+
# Perform softmax on score
|
78 |
+
probs = softmax(ov_logits_per_image, axis=1)
|
79 |
+
max_index = np.argmax(probs)
|
80 |
+
|
81 |
+
# Use the index to get the corresponding label
|
82 |
+
label_with_max_prob = input_labels[max_index]
|
83 |
+
print(
|
84 |
+
f"The label with the highest probability is: '{label_with_max_prob}' with a probability of {probs[0][max_index] * 100:.2f}%"
|
85 |
+
)
|
86 |
+
# The label with the highest probability is: 'beautiful woman' with a probability of 97.87%
|
87 |
+
```
|