Skip to content

Commit 98a850e

Browse files
tybulewiczCopilotmgumowsk
authored
feat: pass missing model_info (#472)
* feat: pass missing model_info * Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix code-style * Update tests/accuracy/test_accuracy.py Co-authored-by: Mariusz Gumowski <mariusz.gumowski@intel.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Mariusz Gumowski <mariusz.gumowski@intel.com>
1 parent 3fe6f2b commit 98a850e

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,32 @@ model = DetectionModel.create_model("~/.cache/omz/public/ssdlite_mobilenet_v2/FP
5959
model.save("serialized.xml")
6060
```
6161

62+
## Usage with generic OpenVINO models
63+
64+
ModelAPI uses custom field in `rt_info/model_info` section of OpenVINO IR to store metadata required for preprocessing and postprocessing. If you have a generic OpenVINO model without such metadata, you can provide that metadata in `configuration` argument of `create_model()` method:
65+
66+
```python
67+
from model_api.models import Model
68+
69+
# Create a model wrapper from a compatible model generated by OpenVINO Training Extensions
70+
model = Model.create_model(
71+
"model.xml",
72+
configuration={
73+
"model_type": "Segmentation",
74+
"blur_strength": 1,
75+
"labels": ["object"],
76+
"soft_threshold": 0.5,
77+
}
78+
)
79+
80+
# Run synchronous inference locally
81+
result = model(image) # image is numpy.ndarray
82+
83+
# Print results in model-specific format
84+
print(f"Inference result: {result}")
85+
86+
# Save the model with metadata already embedded (passing configuration is not required anymore)
87+
model.save("serialized_with_metadata.xml")
88+
```
89+
6290
For more details please refer to the [examples](https://github.com/openvinotoolkit/model_api/tree/master/examples) of this project.

src/model_api/models/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def create_model(
234234
["model_info", "model_type"],
235235
).astype(str)
236236
except RuntimeError:
237-
model_type = cls.detect_model_type(inference_adapter)
237+
model_type = configuration.get("model_type", cls.detect_model_type(inference_adapter))
238238
Model = cls.get_model_class(model_type)
239239
return Model(inference_adapter, configuration, preload)
240240

tests/accuracy/public_scope.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@
3535
}
3636
]
3737
},
38+
{
39+
"name": "otx_models/Lite-hrnet-s_mod2-without-model-info.xml",
40+
"type": "SegmentationModel",
41+
"configuration": {
42+
"blur_strength": 1,
43+
"labels": ["object"],
44+
"model_type": "Segmentation",
45+
"soft_threshold": 0.5
46+
},
47+
"test_data": [
48+
{
49+
"image": "coco128/images/train2017/000000000074.jpg",
50+
"reference": [
51+
"0: 0.563, 1: 0.437, [426,640,2], [0], [0]; object: 0.520, 26, 0, object: 0.530, 42, 0, object: 0.501, 4, 0, object: 0.507, 27, 0, object: 0.503, 8, 0, object: 0.502, 6, 0, object: 0.505, 18, 0, object: 0.504, 13, 0, object: 0.524, 87, 0, object: 0.521, 89, 0, object: 0.757, 2706, 2, "
52+
]
53+
}
54+
]
55+
},
3856
{
3957
"name": "otx_models/Lite-hrnet-s_mod2.onnx",
4058
"type": "SegmentationModel",

tests/accuracy/test_accuracy.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def read_config(fname):
7878
return json.load(f)
7979

8080

81-
def create_models(model_type, model_path, download_dir, force_onnx_adapter=False, device="CPU"):
81+
def create_models(model_type, model_path, download_dir, force_onnx_adapter=False, device="CPU", configuration=None):
8282
if model_path.endswith(".onnx") and force_onnx_adapter:
8383
wrapper_type = model_type.get_model_class(
8484
load_parameters_from_onnx(onnx.load(model_path))["model_info"]["model_type"],
@@ -92,16 +92,18 @@ def create_models(model_type, model_path, download_dir, force_onnx_adapter=False
9292
model.load()
9393
return [model]
9494

95+
configuration = configuration or {}
96+
9597
models = [
96-
model_type.create_model(model_path, device=device, download_dir=download_dir),
98+
model_type.create_model(model_path, device=device, download_dir=download_dir, configuration=configuration),
9799
]
98100
if model_path.endswith(".xml"):
99101
model = create_core().read_model(model_path)
100102
if model.has_rt_info(["model_info", "model_type"]):
101103
wrapper_type = model_type.get_model_class(
102104
model.get_rt_info(["model_info", "model_type"]).astype(str),
103105
)
104-
model = wrapper_type(OpenvinoAdapter(create_core(), model_path, device=device))
106+
model = wrapper_type(OpenvinoAdapter(create_core(), model_path, device=device), configuration=configuration)
105107
model.load()
106108
models.append(model)
107109
return models
@@ -287,6 +289,7 @@ def test_image_models(data, device, dump, result, model_data, results_dir): # n
287289
data,
288290
model_data.get("force_ort", False),
289291
device=device,
292+
configuration=model_data.get("configuration", None),
290293
):
291294
if "tiler" in model_data:
292295
if "extra_model" in model_data:

0 commit comments

Comments
 (0)