From 1144130b500c4ef84c59c47e068c4740dee9b3a0 Mon Sep 17 00:00:00 2001 From: Mikhail Ryzhov Date: Tue, 3 Nov 2020 16:18:43 +0300 Subject: [PATCH] Aligned python read_network() with C/C++ behaviour (#2916) Aligned python read_network() with C/C++ behaviour Excluded mandatory check of weights file, it is redundant * Added unit tests --- .../classification_sample_async/classification_sample_async.py | 10 ++-------- .../python/sample/hello_classification/hello_classification.py | 10 ++-------- .../object_detection_sample_ssd/object_detection_sample_ssd.py | 10 ++-------- .../sample/style_transfer_sample/style_transfer_sample.py | 10 ++-------- .../ie_bridges/python/src/openvino/inference_engine/ie_api.pyx | 2 +- inference-engine/ie_bridges/python/tests/test_IECore.py | 6 ++++++ 6 files changed, 15 insertions(+), 33 deletions(-) diff --git a/inference-engine/ie_bridges/python/sample/classification_sample_async/classification_sample_async.py b/inference-engine/ie_bridges/python/sample/classification_sample_async/classification_sample_async.py index 315d3e3..aa38d5a 100644 --- a/inference-engine/ie_bridges/python/sample/classification_sample_async/classification_sample_async.py +++ b/inference-engine/ie_bridges/python/sample/classification_sample_async/classification_sample_async.py @@ -105,14 +105,8 @@ def main(): # Read a model in OpenVINO Intermediate Representation (.xml and .bin files) or ONNX (.onnx file) format model = args.model - model_bin = None - model_name, model_ext = os.path.splitext(model) - log.info(f"Loading network files:\n\t{model}") - if model_ext == ".xml": - # Read .bin weights for IR format only - model_bin = model_name + ".bin" - log.info(f"\n\t{model_bin}") - net = ie.read_network(model=model, weights=model_bin) + log.info(f"Loading network:\n\t{model}") + net = ie.read_network(model=model) assert len(net.input_info.keys()) == 1, "Sample supports only single input topologies" assert len(net.outputs) == 1, "Sample supports only single output topologies" diff --git a/inference-engine/ie_bridges/python/sample/hello_classification/hello_classification.py b/inference-engine/ie_bridges/python/sample/hello_classification/hello_classification.py index 0de5732..091b463 100644 --- a/inference-engine/ie_bridges/python/sample/hello_classification/hello_classification.py +++ b/inference-engine/ie_bridges/python/sample/hello_classification/hello_classification.py @@ -60,14 +60,8 @@ def main(): # Read a model in OpenVINO Intermediate Representation (.xml and .bin files) or ONNX (.onnx file) format model = args.model - model_bin = None - model_name, model_ext = os.path.splitext(model) - log.info(f"Loading network files:\n\t{model}") - if model_ext == ".xml": - # Read .bin weights for IR format only - model_bin = model_name + ".bin" - log.info(f"\n\t{model_bin}") - net = ie.read_network(model=model, weights=model_bin) + log.info(f"Loading network:\n\t{model}") + net = ie.read_network(model=model) assert len(net.input_info.keys()) == 1, "Sample supports only single input topologies" assert len(net.outputs) == 1, "Sample supports only single output topologies" diff --git a/inference-engine/ie_bridges/python/sample/object_detection_sample_ssd/object_detection_sample_ssd.py b/inference-engine/ie_bridges/python/sample/object_detection_sample_ssd/object_detection_sample_ssd.py index 35ecd58..081a5fd 100644 --- a/inference-engine/ie_bridges/python/sample/object_detection_sample_ssd/object_detection_sample_ssd.py +++ b/inference-engine/ie_bridges/python/sample/object_detection_sample_ssd/object_detection_sample_ssd.py @@ -56,14 +56,8 @@ def main(): # ---1. Read a model in OpenVINO Intermediate Representation (.xml and .bin files) or ONNX (.onnx file) format --- model = args.model - model_bin = None - model_name, model_ext = os.path.splitext(model) - log.info(f"Loading network files:\n\t{model}") - if model_ext == ".xml": - # Read .bin weights for IR format only - model_bin = model_name + ".bin" - log.info(f"\n\t{model_bin}") - net = ie.read_network(model=model, weights=model_bin) + log.info(f"Loading network:\n\t{model}") + net = ie.read_network(model=model) func = ng.function_from_cnn(net) ops = func.get_ordered_ops() # ----------------------------------------------------------------------------------------------------- diff --git a/inference-engine/ie_bridges/python/sample/style_transfer_sample/style_transfer_sample.py b/inference-engine/ie_bridges/python/sample/style_transfer_sample/style_transfer_sample.py index ce1d221..22905fa 100644 --- a/inference-engine/ie_bridges/python/sample/style_transfer_sample/style_transfer_sample.py +++ b/inference-engine/ie_bridges/python/sample/style_transfer_sample/style_transfer_sample.py @@ -64,14 +64,8 @@ def main(): # Read a model in OpenVINO Intermediate Representation (.xml and .bin files) or ONNX (.onnx file) format model = args.model - model_bin = None - model_name, model_ext = os.path.splitext(model) - log.info(f"Loading network files:\n\t{model}") - if model_ext == ".xml": - # Read .bin weights for IR format only - model_bin = model_name + ".bin" - log.info(f"\n\t{model_bin}") - net = ie.read_network(model=model, weights=model_bin) + log.info(f"Loading network:\n\t{model}") + net = ie.read_network(model=model) assert len(net.input_info.keys()) == 1, "Sample supports only single input topologies" assert len(net.outputs) == 1, "Sample supports only single output topologies" diff --git a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api.pyx b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api.pyx index 67c2ed6..a883b46 100644 --- a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api.pyx +++ b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api.pyx @@ -276,7 +276,7 @@ cdef class IECore: raise Exception("Path to the model {} doesn't exist or it's a directory".format(model)) model_ = model.encode() - if not (fnmatch(model, "*.onnx") or fnmatch(model, "*.prototxt")): + if not (fnmatch(model, "*.onnx") or fnmatch(model, "*.prototxt")) and weights: weights = os.fspath(weights) if not os.path.isfile(weights): raise Exception("Path to the weights {} doesn't exist or it's a directory".format(weights)) diff --git a/inference-engine/ie_bridges/python/tests/test_IECore.py b/inference-engine/ie_bridges/python/tests/test_IECore.py index 90c4120..3e020d8 100644 --- a/inference-engine/ie_bridges/python/tests/test_IECore.py +++ b/inference-engine/ie_bridges/python/tests/test_IECore.py @@ -155,6 +155,9 @@ def test_read_network_from_xml(): net = ie.read_network(model=test_net_xml, weights=test_net_bin) assert isinstance(net, IENetwork) + net = ie.read_network(model=test_net_xml) + assert isinstance(net, IENetwork) + def test_read_network_as_path(): ie = IECore() @@ -165,6 +168,9 @@ def test_read_network_as_path(): net = ie.read_network(model=test_net_xml, weights=Path(test_net_bin)) assert isinstance(net, IENetwork) + net = ie.read_network(model=Path(test_net_xml)) + assert isinstance(net, IENetwork) + def test_read_network_from_onnx(): ie = IECore() -- 2.7.4