Add IE Python Dependency To MO (#4151)

* Added script that check IE existance

* Updated install_prerequisites.sh to check and insall IE python dependency

* Execute MOC transformation pipeline in subprocess

* Updated path to offline_transformations.py

* Updated IE dependency search; added libs search support

* Updated install_prerequisites.bat to check IE dependency

* Print warning if MO and IE versions do not match

* Set env before subprocess run

* Add mean and meta info to serialized IR

* Fix pylin import issue

* Update BOM file; added intel headers

* use default serialization; code style fix

* Codestyle

* Added subprocess timeout

* Check e2e status

* Update install_prerequisites.sh logic

* Update MO version to be generated even without cmake execution

* Update install_prereq to extract release version from python

* Simplify pyhton code

* Reduce memory consumtion to its original value

* Added OV find and search logic for windows batch script

* Remove model reading to avoid excess memory consumtption

* IMproved logging messages; simplified code

* Improved warnign message incase when MO and IE versions mismatch

* Fix test import

* Check IE bindings in the beginnig of MO pipeline

* Changed warning message; added RelWithDebInfo path

* Recomendation to build IE from source

* Resolve PR comments

* Apply review comments; Added CheckAPI pass

* Added path to IE local libs

* ModelOptimizer -> Model Optimizer (+IE)

* OpenVINO -> OpenVINO (TM) Toolkit

* Updated install_prerequisites.bat with new logic

* Move offline_transformations.py to back folder; improve warning mesages experience

* Update install_prerequisites.bat

* Move back some unnecessary changes

* Fix BOM

* Disable import errors for pylint

* Fixed grammar in messages

* pylint disable import error

* Final grammar fixes in bat and sh files
This commit is contained in:
Gleb Kazantaev authored and GitHub committed 2021-02-19 14:13:20 +03:00
1 parent a5aaa07721
commit 5d647e6fab
14 files changed
+596 -13

No files matched your search

@@ -23,3 +23,6 @@ def ApplyMOCTransformations(IENetwork network, bool cf):
def ApplyLowLatencyTransformation(IENetwork network):
C.ApplyLowLatencyTransformation(network.impl)
def CheckAPI():
C.CheckAPI()
@@ -8,9 +8,12 @@
#include <transformations/control_flow/unroll_tensor_iterator.hpp>
#include <ngraph/pass/constant_folding.hpp>
#include <ngraph/pass/low_latency.hpp>
#include <ngraph/pass/manager.hpp>
#include <ngraph/opsets/opset6.hpp>
void InferenceEnginePython::ApplyMOCTransformations(InferenceEnginePython::IENetwork network, bool cf) {
ngraph::pass::Manager manager;
manager.register_pass<ngraph::pass::MOCTransformations>(cf);
@@ -28,3 +31,20 @@ void InferenceEnginePython::ApplyLowLatencyTransformation(InferenceEnginePython:
});
manager.run_passes(network.actual->getFunction());
}
void InferenceEnginePython::CheckAPI() {
std::shared_ptr<ngraph::Function> f;
{
auto input = std::make_shared<ngraph::opset6::Parameter>(ngraph::element::f32, ngraph::Shape{1, 1000, 4});
auto reshape = std::make_shared<ngraph::opset6::Reshape>(input, std::make_shared<ngraph::opset6::ShapeOf>(input), true);
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{reshape}, ngraph::ParameterVector{input});
}
ngraph::pass::Manager m;
m.register_pass<ngraph::pass::ConstantFolding>();
m.run_passes(f);
assert(f->get_results().size() == 1);
auto reshape = f->get_result()->input_value(0).get_node_shared_ptr();
assert(std::dynamic_pointer_cast<ngraph::opset6::Parameter>(reshape->input_value(0).get_node_shared_ptr()));
assert(std::dynamic_pointer_cast<ngraph::opset6::Constant>(reshape->input_value(1).get_node_shared_ptr()));
}
@@ -13,4 +13,6 @@ void ApplyMOCTransformations(InferenceEnginePython::IENetwork network, bool cf);
void ApplyLowLatencyTransformation(InferenceEnginePython::IENetwork network);
void CheckAPI();
}; // namespace InferenceEnginePython
@@ -5,3 +5,5 @@ cdef extern from "offline_transformations_api_impl.hpp" namespace "InferenceEngi
cdef void ApplyMOCTransformations(IENetwork network, bool cf)
cdef void ApplyLowLatencyTransformation(IENetwork network)
cdef void CheckAPI()
@@ -769,6 +769,7 @@ mo/__init__.py
mo/back/__init__.py
mo/back/ie_ir_ver_2/__init__.py
mo/back/ie_ir_ver_2/emitter.py
mo/back/offline_transformations.py
mo/back/replacement.py
mo/front/__init__.py
mo/front/caffe/__init__.py
@@ -964,11 +965,14 @@ mo/pipeline/common.py
mo/pipeline/unified.py
mo/utils/__init__.py
mo/utils/broadcasting.py
mo/utils/check_ie_bindings.py
mo/utils/class_registration.py
mo/utils/cli_parser.py
mo/utils/custom_replacement_config.py
mo/utils/dsu.py
mo/utils/error.py
mo/utils/extract_release_version.py
mo/utils/find_ie_version.py
mo/utils/find_inputs.py
mo/utils/get_ov_update_message.py
mo/utils/graph.py
@@ -1,5 +1,5 @@
@echo off
:: Copyright (C) 2018-2020 Intel Corporation
:: Copyright (C) 2018-2021 Intel Corporation
::
:: Licensed under the Apache License, Version 2.0 (the "License");
:: you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ for /F "tokens=1,2,3 delims=. " %%a in ("%version%") do (
if "%Major%" geq "3" (
if "%Minor%" geq "5" (
set python_ver=okay
set python_ver=okay
)
)
if not "%python_ver%"=="okay" (
@@ -77,9 +77,86 @@ IF /I "%1%" EQU "" (
)
)
pip3 install --user -r ..\requirements%postfix%.txt
:: Chek MO version
set python_command='python %~dp0..\mo\utils\extract_release_version.py'
FOR /F "delims=" %%i IN (%python_command%) DO set mo_release_version=%%i
IF "%mo_release_version%" == "None.None" (
set mo_is_custom="true"
) ELSE (
set mo_is_custom="false"
)
:: Check if existing IE Python bindings satisfy requirements
set errorlevel=
python %~dp0..\mo\utils\find_ie_version.py
IF %errorlevel% EQU 0 goto ie_search_end
:: Check if OV already installed via pip
set errorlevel=
pip3 show openvino
IF %errorlevel% EQU 0 (
IF %mo_is_custom% == "true" (
echo [ WARNING ] OpenVINO ^(TM^) Toolkit version installed in pip is incompatible with the Model Optimizer
echo [ WARNING ] For the custom Model Optimizer version consider building Inference Engine Python API from sources ^(preferable^) or install the highest OpenVINO ^(TM^) toolkit version using "pip install openvino"
goto ie_search_end
)
IF %mo_is_custom% == "false" (
echo [ WARNING ] OpenVINO ^(TM^) Toolkit version installed in pip is incompatible with the Model Optimizer
echo [ WARNING ] For the release Model Optimizer version which is %mo_release_version% please install OpenVINO ^(TM^) toolkit using pip install openvino==%mo_release_version% or build Inference Engine Python API from sources
goto ie_search_end
)
)
echo [ WARNING ] Could not find the Inference Engine Python API. Installing OpenVINO ^(TM^) toolkit using pip
IF %mo_is_custom% == "true" (
echo [ WARNING ] Detected a custom Model Optimizer version
echo [ WARNING ] The desired version of the Inference Engine can be installed only for the release Model Optimizer version
echo [ WARNING ] The highest OpenVINO ^(TM^) toolkit version will be installed ^(may be incompatible with current Model Optimizer version^)
echo [ WARNING ] It is recommended to build the Inference Engine from sources even if the current installation is successful
goto install_last_ov
)
set errorlevel=
pip3 install openvino==%mo_release_version%
IF %errorlevel% NEQ 0 (
echo [ WARNING ] Could not find the OpenVINO ^(TM^) toolkit version %mo_release_version% in pip
echo [ WARNING ] The highest OpenVINO ^(TM^) toolkit version will be installed ^(may be incompatible with current Model Optimizer version^)
echo [ WARNING ] It is recommended to build the Inference Engine from sources even if the current installation is successful
goto install_last_ov
)
set errorlevel=
python %~dp0..\mo\utils\find_ie_version.py
IF %errorlevel% EQU 0 goto ie_search_end
echo [ WARNING ] The installed OpenVINO ^(TM^) toolkit version %mo_release_version% does not work as expected. Uninstalling...
pip3 uninstall -y openvino
echo [ WARNING ] Consider building the Inference Engine Python API from sources
goto ie_search_end
:install_last_ov
set errorlevel=
pip3 install openvino
IF %errorlevel% NEQ 0 (
echo [ WARNING ] Could not find OpenVINO ^(TM^) toolkit version available in pip for installation
echo [ WARNING ] Consider building the Inference Engine Python API from sources
goto ie_search_end
)
set errorlevel=
python %~dp0..\mo\utils\find_ie_version.py
IF %errorlevel% EQU 0 goto ie_search_end
echo [ WARNING ] The installed highest OpenVINO ^(TM^) toolkit version doesn't work as expected. Uninstalling...
pip3 uninstall -y openvino
echo [ WARNING ] Consider building the Inference Engine Python API from sources
goto ie_search_end
:ie_search_end
echo *****************************************************************************************
echo Optional: To speed up model conversion process, install protobuf-*.egg located in the
echo "model-optimizer\install_prerequisites" folder or building protobuf library from sources.
@@ -1,6 +1,6 @@
#!/bin/bash
# Copyright (C) 2018-2020 Intel Corporation
# Copyright (C) 2018-2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@ error() {
echo "Error on or near line $1; exiting with status ${code}"
fi
exit "${code}"
}
}
trap 'error ${LINENO}' ERR
@@ -83,18 +83,129 @@ elif [[ "$OSTYPE" == "darwin"* ]]; then
python3 -m pip install --upgrade pip
fi
install_latest_ov() {
if $2; then
sudo -E "$1" -m pip install openvino
else
"$1" -m pip install openvino
fi
}
install_ov() {
if $2; then
sudo -E "$1" -m pip install openvino=="$3"
else
"$1" -m pip install openvino=="$3"
fi
}
uninstall_ov() {
if $2; then
sudo -E "$1" -m pip uninstall -y openvino
else
"$1" -m pip uninstall -y openvino
fi
}
check_ie() {
$1 "$SCRIPTDIR/../mo/utils/find_ie_version.py"
}
check_ov_package() {
if $2; then
sudo -E "$1" -m pip show openvino
else
"$1" -m pip show openvino
fi
}
print_warning() {
YELLOW='\033[1;33m'
NC='\033[0m'
printf "${YELLOW}[ WARNING ] %s ${NC}\n" "$1"
}
find_ie_bindings() {
python_executable="$1"
requires_sudo="$2"
mo_release_version="$("$python_executable" "$SCRIPTDIR"/../mo/utils/extract_release_version.py)"
if [[ $mo_release_version == "None.None" ]]; then
mo_is_custom=true
else
mo_is_custom=false
fi
if ! check_ie "$python_executable"; then
# Check if OpenVINO version was installed using pip
if check_ov_package "$python_executable" "$requires_sudo"; then
if $mo_is_custom; then
print_warning "OpenVINO (TM) Toolkit version installed in pip is incompatible with the Model Optimizer."
print_warning "For the custom Model Optimizer version consider building Inference Engine Python API from sources (preferable) or install the highest OpenVINO (TM) toolkit version using \"pip install openvino\""
else
print_warning "OpenVINO (TM) Toolkit version installed in pip is incompatible with the Model Optimizer."
print_warning "For the release version of the Model Optimizer, which is $mo_release_version, install the OpenVINO (TM) toolkit using \"pip install openvino==$mo_release_version\" or build the Inference Engine Python API from sources."
fi
return 0
fi
print_warning "Could not find the Inference Engine Python API. Installing OpenVINO (TM) toolkit using pip."
if $mo_is_custom; then
print_warning "Detected a custom Model Optimizer version."
print_warning "The desired version of the Inference Engine can be installed only for the release Model Optimizer version."
print_warning "The highest OpenVINO (TM) toolkit version will be installed, which might be incompatible with the current Model Optimizer version."
print_warning "It is recommended to build the Inference Engine from sources even if the current installation is successful."
elif install_ov "$python_executable" "$requires_sudo" "$mo_release_version"; then
if check_ie "$python_executable"; then
return 0
fi
print_warning "The installed OpenVINO (TM) toolkit version $mo_release_version does not work as expected. Uninstalling..."
uninstall_ov "$python_executable" "$requires_sudo"
print_warning "Consider building the Inference Engine Python API from sources."
return 0
else
print_warning "Could not find the OpenVINO (TM) toolkit version $mo_release_version in pip."
print_warning "The highest OpenVINO (TM) toolkit version will be installed, which might be incompatible with the current Model Optimizer version."
print_warning "It is recommended to build the Inference Engine from sources even if the current installation is successful."
fi
# Install the highest OpenVINO pip version
if install_latest_ov "$python_executable" "$requires_sudo"; then
if check_ie "$python_executable"; then
return 0
else
print_warning "The installed highest OpenVINO (TM) toolkit version doesn't work as expected. Uninstalling..."
uninstall_ov "$python_executable" "$requires_sudo"
print_warning "Consider building the Inference Engine Python API from sources."
return 0
fi
else
print_warning "Could not find OpenVINO (TM) toolkit version available in pip for installation."
print_warning "Consider building the Inference Engine Python API from sources."
return 0
fi
fi
return 0
}
if [[ $V_ENV -eq 1 ]]; then
"$python_binary" -m venv "$SCRIPTDIR/../venv${postfix}"
source "$SCRIPTDIR/../venv${postfix}/bin/activate"
"$SCRIPTDIR/../venv${postfix}/bin/$python_binary" -m pip install -r "$SCRIPTDIR/../requirements${postfix}.txt"
venv_python_binary="$SCRIPTDIR/../venv${postfix}/bin/$python_binary"
$venv_python_binary -m pip install -r "$SCRIPTDIR/../requirements${postfix}.txt"
find_ie_bindings "$venv_python_binary" false
echo
echo "Before running the Model Optimizer, please activate virtualenv environment by running \"source ${SCRIPTDIR}/../venv${postfix}/bin/activate\""
else
if [[ "$OSTYPE" == "darwin"* ]]; then
python3 -m pip install -r "$SCRIPTDIR/../requirements${postfix}.txt"
find_ie_bindings python3 false
else
sudo -E $python_binary -m pip install -r "$SCRIPTDIR/../requirements${postfix}.txt"
find_ie_bindings $python_binary true
fi
echo "[WARNING] All Model Optimizer dependencies are installed globally."
echo "[WARNING] If you want to keep Model Optimizer in separate sandbox"
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
"""
Copyright (C) 2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
if __name__ == "__main__":
try:
from openvino.inference_engine import IECore # pylint: disable=import-error
from openvino.offline_transformations import ApplyMOCTransformations, CheckAPI # pylint: disable=import-error
except Exception as e:
print("[ WARNING ] {}".format(e))
exit(1)
CheckAPI()
+33 -4
View File
@@ -19,6 +19,7 @@ import datetime
import logging as log
import os
import sys
import subprocess
import traceback
from collections import OrderedDict
@@ -42,6 +43,7 @@ from mo.utils.model_analysis import AnalysisResults
from mo.utils.utils import refer_to_faq_msg
from mo.utils.version import get_version
from mo.utils.versions_checker import check_requirements
from mo.utils.find_ie_version import find_ie_version
def replace_ext(name: str, old: str, new: str):
@@ -90,7 +92,6 @@ def print_argv(argv: argparse.Namespace, is_caffe: bool, is_tf: bool, is_mxnet:
lines.append('\t{}: \t{}'.format(desc, 'Default'))
continue
lines.append('\t{}: \t{}'.format(desc, getattr(argv, op, 'NONE')))
lines.append('Model Optimizer version: \t{}'.format(get_version()))
print('\n'.join(lines), flush=True)
@@ -147,6 +148,19 @@ def prepare_ir(argv: argparse.Namespace):
if not argv.silent:
print_argv(argv, is_caffe, is_tf, is_mxnet, is_kaldi, is_onnx, argv.model_name)
# This try-except is additional reinsurance that the IE
# dependency search does not break the MO pipeline
try:
if not find_ie_version(silent=argv.silent) and not argv.silent:
print("[ WARNING ] Could not find the Inference Engine Python API. At this moment, the Inference Engine dependency is not required, but will be required in future releases.")
print("[ WARNING ] Consider building the Inference Engine Python API from sources or try to install OpenVINO (TM) Toolkit using \"install_prerequisites.{}\"".format(
"bat" if sys.platform == "windows" else "sh"))
# If the IE was not found, it will not print the MO version, so we have to print it manually
print("{}: \t{}".format("Model Optimizer version", get_version()))
except Exception as e:
# TODO: send exception message
pass
ret_code = check_requirements(framework=argv.framework)
if ret_code:
raise Error('check_requirements exit with return code {}'.format(ret_code))
@@ -254,9 +268,24 @@ def emit_ir(graph: Graph, argv: argparse.Namespace):
if not (argv.framework == 'tf' and argv.tensorflow_custom_operations_config_update):
output_dir = argv.output_dir if argv.output_dir != '.' else os.getcwd()
print('\n[ SUCCESS ] Generated IR version {} model.'.format(get_ir_version(argv)))
print('[ SUCCESS ] XML file: {}.xml'.format(os.path.join(output_dir, argv.model_name)))
print('[ SUCCESS ] BIN file: {}.bin'.format(os.path.join(output_dir, argv.model_name)))
orig_model_name = os.path.normpath(os.path.join(output_dir, argv.model_name))
# This try-except is additional reinsurance that the IE
# dependency search does not break the MO pipeline
try:
if find_ie_version(silent=True):
path_to_offline_transformations = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'back',
'offline_transformations.py')
status = subprocess.run([sys.executable, path_to_offline_transformations, orig_model_name], env=os.environ, timeout=100)
if status.returncode != 0 and not argv.silent:
print("[ WARNING ] offline_transformations return code {}".format(status.returncode))
except Exception as e:
# TODO: send error message
pass
print('[ SUCCESS ] Generated IR version {} model.'.format(get_ir_version(argv)))
print('[ SUCCESS ] XML file: {}.xml'.format(orig_model_name))
print('[ SUCCESS ] BIN file: {}.bin'.format(orig_model_name))
return 0
@@ -0,0 +1,76 @@
#!/usr/bin/env python3
"""
Copyright (C) 2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import re
import argparse
try:
# needed by find_ie_version.py which call check_ie_bindings.py as python script
import version # pylint: disable=import-error
except ImportError:
import mo.utils.version
from extract_release_version import extract_release_version
def import_core_modules(silent: bool, path_to_module: str):
try:
from openvino.inference_engine import IECore, get_version # pylint: disable=import-error
from openvino.offline_transformations import ApplyMOCTransformations, CheckAPI # pylint: disable=import-error
import openvino # pylint: disable=import-error
ie_version = str(get_version())
mo_version = str(version.get_version()) # pylint: disable=no-member
if not silent:
print("\t- {}: \t{}".format("Inference Engine found in", os.path.dirname(openvino.__file__)))
print("{}: \t{}".format("Inference Engine version", ie_version))
print("{}: \t {}".format("Model Optimizer version", mo_version))
# MO and IE version have a small difference in the beginning of version because
# IE version also includes API version. For example:
# Inference Engine version: 2.1.custom_HEAD_4c8eae0ee2d403f8f5ae15b2c9ad19cfa5a9e1f9
# Model Optimizer version: custom_HEAD_4c8eae0ee2d403f8f5ae15b2c9ad19cfa5a9e1f9
# So to match this versions we skip IE API version.
if not re.match(r"^([0-9]+).([0-9]+).{}$".format(mo_version), ie_version):
extracted_release_version = extract_release_version()
is_custom_mo_version = extracted_release_version == (None, None)
if not silent:
print("[ WARNING ] Model Optimizer and Inference Engine versions do no match.")
print("[ WARNING ] Consider building the Inference Engine Python API from sources or reinstall OpenVINO (TM) toolkit using \"pip install openvino{}\" {}".format(
"", "(may be incompatible with the current Model Optimizer version)" if is_custom_mo_version else "=={}.{}".format(*extracted_release_version), ""))
return True
except Exception as e:
# Do not print a warning if module wasn't found or silent mode is on
if "No module named 'openvino'" not in str(e) and not silent:
print("[ WARNING ] Failed to import Inference Engine Python API in: {}".format(path_to_module))
print("[ WARNING ] {}".format(e))
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--silent", action="store_true")
parser.add_argument("--path_to_module")
args = parser.parse_args()
if not import_core_modules(args.silent, args.path_to_module):
exit(1)
@@ -0,0 +1,42 @@
"""
Copyright (C) 2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import re
try:
# needed by install_prerequisites which call extract_release_version as python script
from version import get_version
except ImportError:
from mo.utils.version import get_version
def extract_release_version():
version = get_version()
patterns = [
# captures release version set by CI for example: '2021.1.0-1028-55e4d5673a8'
r"^([0-9]+).([0-9]+)*",
# captures release version generated by MO from release branch, for example: 'custom_releases/2021/1_55e4d567'
r"_releases/([0-9]+)/([0-9]+)_*"
]
for pattern in patterns:
m = re.search(pattern, version)
if m and len(m.groups()) == 2:
return m.group(1), m.group(2)
return None, None
if __name__ == "__main__":
print("{}.{}".format(*extract_release_version()))
+149
View File
@@ -0,0 +1,149 @@
#!/usr/bin/env python3
"""
Copyright (C) 2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import sys
import platform
import subprocess
lib_env_key = "PATH" if platform.system() == "Windows" else "LD_LIBRARY_PATH"
if lib_env_key not in os.environ:
os.environ[lib_env_key] = ""
python_path_key = "PYTHONPATH"
if python_path_key not in os.environ:
os.environ[python_path_key] = ""
lib_path_orig = os.environ[lib_env_key]
python_path_orig = os.environ[python_path_key]
def setup_env(module="", libs=[]):
"""
Update os.environ variables with given values.
:param module: path to python module
:param libs: list with paths to libraries
"""
os.environ[python_path_key] = os.pathsep.join([os.environ[python_path_key], module])
os.environ[lib_env_key] = os.pathsep.join([os.environ[lib_env_key], *libs])
def reset_env():
"""
Reset os.environ variables to default values
"""
os.environ[python_path_key] = python_path_orig
os.environ[lib_env_key] = lib_path_orig
def try_to_import_ie(module="", libs=[], silent=False):
"""
Check if Inference Engine Python API modules exists and in case of success
environment will be set with given values.
:param module: path to python module
:param libs: list with paths to libraries
:param silent: hide all output
"""
path_to_script = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'check_ie_bindings.py')
# We need to execute python modules checker in subprocess to avoid issue with environment
# in case if previous import was unsuccessful it can fail further imports even if sys.path
# will be restored to initial default values.
# To pass environment to sub-process PATH/LD_LIBRARY_PATH and PYTHONPATH are used from
# os.environ that is set after setup_env()
setup_env(module=module, libs=libs)
cmd_args = [sys.executable, path_to_script, "--path_to_module", "PYTHONPATH" if module == "" else module]
if silent:
cmd_args.append("--silent")
status = subprocess.run(cmd_args, env=os.environ)
if status.returncode == 0:
return True
else:
reset_env()
return False
def find_ie_version(silent=False):
"""
Tries to import Inference Engine Python API bindings. In case of successful import
PATH/LD_LIBRARY_PATH and PYTHONPATH environment variables will be set
This variables must be passed to subprocess in order to execute IE python bindings.
Example:
if find_ie_version():
subprocess.run([sys.executable, path_to_script], env=os.environ)
"""
if try_to_import_ie(silent=silent):
return True
python_version = 'python{}.{}'.format(sys.version_info[0], sys.version_info[1])
script_path = os.path.realpath(os.path.dirname(__file__))
bindings_paths = [
# Windows
{
"module": os.path.join(script_path, '../../../../python/', python_version),
"libs": [
os.path.join(script_path, '../../../inference_engine/bin/intel64/Release'),
os.path.join(script_path, '../../../inference_engine/external/tbb/bin'),
os.path.join(script_path, '../../../ngraph/lib'),
]
},
# Linux / Darwin
{
"module": os.path.join(script_path, '../../../../python/', python_version),
"libs": [
os.path.join(script_path, '../../../inference_engine/lib/intel64'),
os.path.join(script_path, '../../../inference_engine/external/tbb/lib'),
os.path.join(script_path, '../../../ngraph/lib'),
]
},
# Local builds
{
"module": os.path.join(script_path, '../../../bin/intel64/Release/lib/python_api/', python_version),
"libs": [
os.path.join(script_path, '../../../bin/intel64/Release/lib'),
]
},
{
"module": os.path.join(script_path, '../../../bin/intel64/RelWithDebInfo/lib/python_api/', python_version),
"libs": [
os.path.join(script_path, '../../../bin/intel64/RelWithDebInfo/lib'),
]
},
{
"module": os.path.join(script_path, '../../../bin/intel64/Debug/lib/python_api/', python_version),
"libs": [
os.path.join(script_path, '../../../bin/intel64/Debug/lib'),
]
}
]
for item in bindings_paths:
module = item['module']
if not os.path.exists(module):
continue
if try_to_import_ie(module=os.path.normpath(module), libs=item['libs'] if 'libs' in item else [], silent=silent):
return True
return False
if __name__ == "__main__":
if not find_ie_version():
exit(1)
+21 -3
View File
@@ -1,5 +1,5 @@
"""
Copyright (C) 2018-2020 Intel Corporation
Copyright (C) 2018-2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -14,12 +14,30 @@
limitations under the License.
"""
import os
import subprocess
def get_version_file_path():
return os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir, "version.txt")
def generate_mo_version():
"""
Function generates version like in cmake
custom_{branch_name}_{commit_hash}
"""
try:
branch_name = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip().decode()
commit_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip().decode()
return "custom_{}_{}".format(branch_name, commit_hash)
except Exception as e:
return "unknown version"
def get_version():
version_txt = os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir, "version.txt")
version_txt = get_version_file_path()
if not os.path.isfile(version_txt):
return "unknown version"
return generate_mo_version()
with open(version_txt) as f:
version = f.readline().replace('\n', '')
return version
+22
View File
@@ -20,6 +20,7 @@ from unittest.mock import mock_open
from unittest.mock import patch
from mo.utils.version import get_version
from mo.utils.extract_release_version import extract_release_version
class TestingVersion(unittest.TestCase):
@@ -29,3 +30,24 @@ class TestingVersion(unittest.TestCase):
mock_isfile.return_value = True
mock_open.return_value.__enter__ = mock_open
self.assertEqual(get_version(), '2021.1.0-1028-55e4d5673a8')
@patch('os.path.isfile')
@mock.patch('builtins.open', new_callable=mock_open, create=True, read_data='2021.1.0-1028-55e4d5673a8')
def test_release_version_extractor(self, mock_open, mock_isfile):
mock_isfile.return_value = True
mock_open.return_value.__enter__ = mock_open
self.assertEqual(extract_release_version(), ('2021', '1'))
@patch('os.path.isfile')
@mock.patch('builtins.open', new_callable=mock_open, create=True, read_data='custom_releases/2021/1_55e4d5673a8')
def test_custom_release_version_extractor(self, mock_open, mock_isfile):
mock_isfile.return_value = True
mock_open.return_value.__enter__ = mock_open
self.assertEqual(extract_release_version(), ('2021', '1'))
@patch('os.path.isfile')
@mock.patch('builtins.open', new_callable=mock_open, create=True, read_data='custom_my_branch/fix_55e4d5673a8')
def test_release_version_extractor_neg(self, mock_open, mock_isfile):
mock_isfile.return_value = True
mock_open.return_value.__enter__ = mock_open
self.assertEqual(extract_release_version(), (None, None))