Skip to content
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

[POC PYTHON API] Implement read_network #21

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ngraph/python/src/openvino/inference_engine/ie_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,8 @@ 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)
28 changes: 28 additions & 0 deletions ngraph/python/src/pyopenvino/inference_engine/ie_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,40 @@ 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<uint8_t>(tensorDesc);
weights_blob->allocate();
memcpy(weights_blob->rwmap().as<uint8_t*>(), 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) {
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("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,
Expand Down
12 changes: 12 additions & 0 deletions ngraph/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
99 changes: 97 additions & 2 deletions ngraph/python/tests/test_inference_engine/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
from ngraph.impl.op import Parameter
from ngraph.impl import Function, Shape, Type
from openvino.inference_engine import IECore, TensorDesc, Blob, IENetwork, ExecutableNetwork
from ..conftest import model_path, plugins_path
from openvino.inference_engine.ie_api import blob_from_file
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():
Expand Down Expand Up @@ -76,9 +80,100 @@ 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)


def test_read_network_from_blob():
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)
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))
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_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()
Expand Down