From d0ded5e12bb897d82085bcd8c27f6f2283c1f041 Mon Sep 17 00:00:00 2001 From: Alexey Lebedev Date: Thu, 22 Apr 2021 11:03:57 +0300 Subject: [PATCH 1/6] Add read_network(model,blob) --- .../python/src/pyopenvino/inference_engine/ie_core.cpp | 6 ++++++ ngraph/python/tests/test_inference_engine/test_core.py | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp b/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp index ae58ee8d7a4de4..cdf8a555352d08 100644 --- a/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp +++ b/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp @@ -59,6 +59,12 @@ void regclass_IECore(py::module m) return self.ReadNetwork(model, weights); }, py::arg("model"), py::arg("weights")=""); + cls.def("read_network", [](InferenceEngine::Core& self, + const std::string& model, + py::handle blob) { + return self.ReadNetwork(model, Common::cast_to_blob(blob)); + }, py::arg("model"), py::arg("blob")); + cls.def("import_network", [](InferenceEngine::Core& self, const std::string& model_file, const std::string& device_name, diff --git a/ngraph/python/tests/test_inference_engine/test_core.py b/ngraph/python/tests/test_inference_engine/test_core.py index a26812e97ce627..4f518b2b63f748 100644 --- a/ngraph/python/tests/test_inference_engine/test_core.py +++ b/ngraph/python/tests/test_inference_engine/test_core.py @@ -80,6 +80,16 @@ def test_read_network(): assert isinstance(net, IENetwork) +def test_read_network_from_memory(): + ie_core = IECore() + model = open(test_net_xml).read() + array = np.fromfile(test_net_bin,dtype=np.uint8) + tensor_desc = TensorDesc("U8", array.shape, "C") + blob = Blob(tensor_desc,array) + net = ie_core.read_network(model, blob) + assert isinstance(net, IENetwork) + + def test_get_version(device): ie = IECore() version = ie.get_versions(device) From 710457edae3bad090a7166ffbd1ee864d687e3d2 Mon Sep 17 00:00:00 2001 From: Alexey Lebedev Date: Fri, 23 Apr 2021 12:24:55 +0300 Subject: [PATCH 2/6] Add blob_from_file helper --- ngraph/python/src/openvino/inference_engine/ie_api.py | 6 ++++++ ngraph/python/tests/test_inference_engine/test_core.py | 5 ++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ngraph/python/src/openvino/inference_engine/ie_api.py b/ngraph/python/src/openvino/inference_engine/ie_api.py index bac818bf5b1473..2bd91c77d8ee63 100644 --- a/ngraph/python/src/openvino/inference_engine/ie_api.py +++ b/ngraph/python/src/openvino/inference_engine/ie_api.py @@ -105,3 +105,9 @@ def __new__(cls, tensor_desc: TensorDesc, arr: np.ndarray = None): return TBlobUint8(tensor_desc, arr, arr_size) else: raise AttributeError(f'Unsupported precision {precision} for Blob') + +def blob_from_file(path_to_bin_file : str) -> BlobWrapper: + array = np.fromfile(path_to_bin_file,dtype=np.uint8) + tensor_desc = TensorDesc("U8", array.shape, "C") + return BlobWrapper(tensor_desc,array) + \ No newline at end of file diff --git a/ngraph/python/tests/test_inference_engine/test_core.py b/ngraph/python/tests/test_inference_engine/test_core.py index 4f518b2b63f748..f8198d06ecf36d 100644 --- a/ngraph/python/tests/test_inference_engine/test_core.py +++ b/ngraph/python/tests/test_inference_engine/test_core.py @@ -6,6 +6,7 @@ from ngraph.impl.op import Parameter from ngraph.impl import Function, Shape, Type from openvino.inference_engine import IECore, TensorDesc, Blob, IENetwork, ExecutableNetwork +from openvino.inference_engine.ie_api import blob_from_file from ..conftest import model_path, plugins_path import os import pytest @@ -83,9 +84,7 @@ def test_read_network(): def test_read_network_from_memory(): ie_core = IECore() model = open(test_net_xml).read() - array = np.fromfile(test_net_bin,dtype=np.uint8) - tensor_desc = TensorDesc("U8", array.shape, "C") - blob = Blob(tensor_desc,array) + blob = blob_from_file(test_net_bin) net = ie_core.read_network(model, blob) assert isinstance(net, IENetwork) From 5ebd3a7936fc924076cbd940b03814409cf612a8 Mon Sep 17 00:00:00 2001 From: Alexey Lebedev Date: Tue, 27 Apr 2021 20:10:35 +0300 Subject: [PATCH 3/6] Add read from Path --- ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp b/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp index cdf8a555352d08..5a6421119a8b07 100644 --- a/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp +++ b/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp @@ -65,6 +65,12 @@ void regclass_IECore(py::module m) return self.ReadNetwork(model, Common::cast_to_blob(blob)); }, py::arg("model"), py::arg("blob")); + cls.def("read_network", [](InferenceEngine::Core& self, + py::object model, + py::object weights) { + return self.ReadNetwork(py::str(model), py::str(weights)); + }, py::arg("model"), py::arg("weights")=""); + cls.def("import_network", [](InferenceEngine::Core& self, const std::string& model_file, const std::string& device_name, From 004ec9ce02f0868f8317d744a964b4cebb8bf233 Mon Sep 17 00:00:00 2001 From: Alexey Lebedev Date: Thu, 29 Apr 2021 09:23:14 +0300 Subject: [PATCH 4/6] Add tests --- .../src/openvino/inference_engine/ie_api.py | 1 + ngraph/python/tests/conftest.py | 12 +++++ .../tests/test_inference_engine/test_core.py | 48 +++++++++++++++++-- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/ngraph/python/src/openvino/inference_engine/ie_api.py b/ngraph/python/src/openvino/inference_engine/ie_api.py index 2bd91c77d8ee63..61a3960c2755d4 100644 --- a/ngraph/python/src/openvino/inference_engine/ie_api.py +++ b/ngraph/python/src/openvino/inference_engine/ie_api.py @@ -110,4 +110,5 @@ def blob_from_file(path_to_bin_file : str) -> BlobWrapper: array = np.fromfile(path_to_bin_file,dtype=np.uint8) tensor_desc = TensorDesc("U8", array.shape, "C") return BlobWrapper(tensor_desc,array) + \ No newline at end of file diff --git a/ngraph/python/tests/conftest.py b/ngraph/python/tests/conftest.py index 959fd005f2c1e6..7e631daf7b284b 100644 --- a/ngraph/python/tests/conftest.py +++ b/ngraph/python/tests/conftest.py @@ -26,6 +26,18 @@ def model_path(is_myriad=False): return (test_xml, test_bin) +def model_onnx_path(): + path_to_repo = os.environ["MODELS_PATH"] + test_onnx = os.path.join(path_to_repo, "models", "test_model", 'test_model.onnx') + return test_onnx + + +def model_prototxt_path(): + path_to_repo = os.environ["MODELS_PATH"] + test_prototxt = os.path.join(path_to_repo, "models", "test_model", 'test_model.prototxt') + return test_prototxt + + def plugins_path(): path_to_repo = os.environ["DATA_PATH"] plugins_xml = os.path.join(path_to_repo, 'ie_class', 'plugins.xml') diff --git a/ngraph/python/tests/test_inference_engine/test_core.py b/ngraph/python/tests/test_inference_engine/test_core.py index f8198d06ecf36d..275ab568b53120 100644 --- a/ngraph/python/tests/test_inference_engine/test_core.py +++ b/ngraph/python/tests/test_inference_engine/test_core.py @@ -7,13 +7,16 @@ from ngraph.impl import Function, Shape, Type from openvino.inference_engine import IECore, TensorDesc, Blob, IENetwork, ExecutableNetwork from openvino.inference_engine.ie_api import blob_from_file -from ..conftest import model_path, plugins_path +from ..conftest import model_path, model_onnx_path, model_prototxt_path, plugins_path import os import pytest from sys import platform +from pathlib import Path test_net_xml, test_net_bin = model_path() +test_net_onnx = model_onnx_path() +test_net_prototxt = model_prototxt_path() plugins_xml, plugins_win_xml, plugins_osx_xml = plugins_path() def test_blobs(): @@ -77,7 +80,10 @@ def test_load_network(device): def test_read_network(): ie_core = IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + assert isinstance(net, IENetwork) + + net = ie_core.read_network(model=test_net_xml) assert isinstance(net, IENetwork) @@ -85,10 +91,46 @@ def test_read_network_from_memory(): ie_core = IECore() model = open(test_net_xml).read() blob = blob_from_file(test_net_bin) - net = ie_core.read_network(model, blob) + net = ie_core.read_network(model=model, blob=blob) assert isinstance(net, IENetwork) +def test_read_network_as_path(): + ie_core = IECore() + net = ie_core.read_network(model=Path(test_net_xml), weights=Path(test_net_bin)) + assert isinstance(net, IENetwork) + + net = ie_core.read_network(model=test_net_xml,weights=Path(test_net_bin)) + assert isinstance(net,IENetwork) + + net = ie_core.read_network(model=Path(test_net_xml)) + assert isinstance(net, IENetwork) + + +def test_read_network_from_onnx(): + ie_core = IECore() + net = ie_core.read_network(model=test_net_onnx) + assert isinstance(net, IENetwork) + + +def test_read_network_from_onnx_as_path(): + ie_core = IECore() + net = ie_core.read_network(model=Path(test_net_onnx)) + assert isinstance(net, IENetwork) + + +def test_read_network_from_prototxt(): + ie_core = IECore() + net = ie_core.read_network(model=test_net_prototxt) + assert isinstance(net, IENetwork) + + +def test_read_network_from_prototxt_as_path(): + ie_core = IECore() + net = ie_core.read_network(model=Path(test_net_prototxt)) + assert isinstance(net, IENetwork) + + def test_get_version(device): ie = IECore() version = ie.get_versions(device) From 945b30bf4818aae1e25843142fd34f5c1a148aca Mon Sep 17 00:00:00 2001 From: Alexey Lebedev Date: Thu, 13 May 2021 19:27:41 +0300 Subject: [PATCH 5/6] Add read_network from bytes --- .../pyopenvino/inference_engine/ie_core.cpp | 16 +++++++ .../tests/test_inference_engine/test_core.py | 46 ++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp b/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp index 5a6421119a8b07..bd23db298390dd 100644 --- a/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp +++ b/ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp @@ -53,6 +53,22 @@ void regclass_IECore(py::module m) return self.GetVersions(device_name); }, py::arg("device_name")); + cls.def("read_network", [](InferenceEngine::Core& self, + py::bytes model, + py::bytes weights) { + InferenceEngine::MemoryBlob::Ptr weights_blob; + if(weights){ + std::string weights_bytes = weights; + uint8_t* bin = (uint8_t*)weights_bytes.c_str(); + size_t bin_size = weights_bytes.length(); + InferenceEngine::TensorDesc tensorDesc(InferenceEngine::Precision::U8, { bin_size }, InferenceEngine::Layout::C); + weights_blob = InferenceEngine::make_shared_blob(tensorDesc); + weights_blob->allocate(); + memcpy(weights_blob->rwmap().as(), bin, bin_size); + } + return self.ReadNetwork(model,weights_blob); + }, py::arg("model"), py::arg("weights")); + cls.def("read_network", [](InferenceEngine::Core& self, const std::string& model, const std::string& weights) { diff --git a/ngraph/python/tests/test_inference_engine/test_core.py b/ngraph/python/tests/test_inference_engine/test_core.py index 275ab568b53120..6fed912815c36b 100644 --- a/ngraph/python/tests/test_inference_engine/test_core.py +++ b/ngraph/python/tests/test_inference_engine/test_core.py @@ -87,7 +87,7 @@ def test_read_network(): assert isinstance(net, IENetwork) -def test_read_network_from_memory(): +def test_read_network_from_blob(): ie_core = IECore() model = open(test_net_xml).read() blob = blob_from_file(test_net_bin) @@ -95,6 +95,22 @@ def test_read_network_from_memory(): assert isinstance(net, IENetwork) +def test_read_network_from_blob_valid(): + ie_core = IECore() + model = open(test_net_xml).read() + blob = blob_from_file(test_net_bin) + net = ie_core.read_network(model=model, blob=blob) + ref_net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + assert net.name == ref_net.name + assert net.batch_size == ref_net.batch_size + ii_net = net.input_info + ii_net2 = ref_net.input_info + o_net = net.outputs + o_net2 = ref_net.outputs + assert ii_net.keys() == ii_net2.keys() + assert o_net.keys() == o_net2.keys() + + def test_read_network_as_path(): ie_core = IECore() net = ie_core.read_network(model=Path(test_net_xml), weights=Path(test_net_bin)) @@ -131,6 +147,34 @@ def test_read_network_from_prototxt_as_path(): assert isinstance(net, IENetwork) +def test_read_net_from_buffer(): + ie_core = IECore() + with open(test_net_bin, 'rb') as f: + bin = f.read() + with open(model_path()[0], 'rb') as f: + xml = f.read() + net = ie_core.read_network(model=xml, weights=bin) + assert isinstance(net, IENetwork) + + +def test_net_from_buffer_valid(): + ie_core = IECore() + with open(test_net_bin, 'rb') as f: + bin = f.read() + with open(model_path()[0], 'rb') as f: + xml = f.read() + net = ie_core.read_network(model=xml, weights=bin) + ref_net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + assert net.name == ref_net.name + assert net.batch_size == ref_net.batch_size + ii_net = net.input_info + ii_net2 = ref_net.input_info + o_net = net.outputs + o_net2 = ref_net.outputs + assert ii_net.keys() == ii_net2.keys() + assert o_net.keys() == o_net2.keys() + + def test_get_version(device): ie = IECore() version = ie.get_versions(device) From 74a17f1fd9052db11e362ced1d8ef6a5bd3fa6d6 Mon Sep 17 00:00:00 2001 From: Alexey Lebedev Date: Thu, 20 May 2021 20:56:58 +0300 Subject: [PATCH 6/6] reset new line --- ngraph/python/src/openvino/inference_engine/ie_api.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/ngraph/python/src/openvino/inference_engine/ie_api.py b/ngraph/python/src/openvino/inference_engine/ie_api.py index 61a3960c2755d4..49a94415e13249 100644 --- a/ngraph/python/src/openvino/inference_engine/ie_api.py +++ b/ngraph/python/src/openvino/inference_engine/ie_api.py @@ -110,5 +110,3 @@ def blob_from_file(path_to_bin_file : str) -> BlobWrapper: array = np.fromfile(path_to_bin_file,dtype=np.uint8) tensor_desc = TensorDesc("U8", array.shape, "C") return BlobWrapper(tensor_desc,array) - - \ No newline at end of file