convert_model() legacy extensions fix (#15742)
* Fixed legacy extensions passing to MO tool. * Added tests. * Corrected test. * Add debug print. * Moved tests to layer tests. * Added comment. * Moved legacy ext tests to separate file. Fixed tmp .pb file cleaning. * Small correction. * Run MO Python API tests directory in CI. * Small fix. * Fix for case of splitted output. * Corrected imports. * Corrected imports. * Added run of legacy extensions tests from subprocess.
This commit is contained in:
committed by
GitHub
parent
fc98454174
commit
9efdb38b96
@@ -0,0 +1,107 @@
|
||||
# Copyright (C) 2018-2023 Intel Corporation
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import numpy as np
|
||||
import openvino.runtime as ov
|
||||
import os
|
||||
import tempfile
|
||||
import tensorflow as tf
|
||||
import unittest
|
||||
|
||||
from openvino.runtime import PartialShape, Model
|
||||
from openvino.test_utils import compare_functions
|
||||
from common.utils.common_utils import generate_ir
|
||||
|
||||
|
||||
def create_tf_model():
|
||||
import tensorflow as tf
|
||||
|
||||
tf.compat.v1.reset_default_graph()
|
||||
|
||||
with tf.compat.v1.Session() as sess:
|
||||
inp1 = tf.compat.v1.placeholder(tf.float32, [1, 2, 3], 'Input')
|
||||
inp2 = tf.compat.v1.placeholder(tf.float32, [1, 2, 3], 'Input')
|
||||
relu = tf.nn.relu(inp1 + inp2, name='Relu')
|
||||
|
||||
output = tf.nn.sigmoid(relu, name='Sigmoid')
|
||||
|
||||
tf.compat.v1.global_variables_initializer()
|
||||
tf_net = sess.graph_def
|
||||
return tf_net
|
||||
|
||||
|
||||
def create_ref_model_1():
|
||||
shape = [1, 2, 3]
|
||||
shape = PartialShape(shape)
|
||||
param1 = ov.opset10.parameter(shape)
|
||||
param2 = ov.opset10.parameter(shape)
|
||||
add = ov.opset10.add(param1, param2)
|
||||
relu = ov.opset10.relu(add)
|
||||
sin = ov.opset10.sin(relu)
|
||||
sigm = ov.opset10.sigmoid(sin)
|
||||
parameter_list = [param1, param2]
|
||||
ref_model = Model([sigm], parameter_list, "test")
|
||||
return ref_model
|
||||
|
||||
|
||||
def create_ref_model_2():
|
||||
shape = [1, 2, 3]
|
||||
shape = PartialShape(shape)
|
||||
param1 = ov.opset10.parameter(shape)
|
||||
param2 = ov.opset10.parameter(shape)
|
||||
add = ov.opset10.add(param1, param2)
|
||||
relu = ov.opset10.relu(add)
|
||||
sin = ov.opset10.sin(relu)
|
||||
sigm = ov.opset10.sigmoid(sin)
|
||||
tanh = ov.opset10.tanh(sigm)
|
||||
parameter_list = [param1, param2]
|
||||
ref_model = Model([tanh], parameter_list, "test")
|
||||
return ref_model
|
||||
|
||||
|
||||
class LegacyExtTest(unittest.TestCase):
|
||||
test_directory = os.path.dirname(os.path.realpath(__file__))
|
||||
def test_legacy_extensions(self):
|
||||
from openvino.tools.mo import convert_model
|
||||
with tempfile.TemporaryDirectory(dir=self.test_directory) as tmpdir:
|
||||
ext_path1 = os.path.join(os.path.dirname(__file__), "test_legacy_exts/test_exts_dir1")
|
||||
ext_path2 = os.path.join(os.path.dirname(__file__), "test_legacy_exts/test_exts_dir2")
|
||||
model = create_tf_model()
|
||||
out_xml = os.path.join(tmpdir, "model.xml")
|
||||
|
||||
# tests for convert_model()
|
||||
ov_model = convert_model(model, extensions=ext_path1)
|
||||
flag, msg = compare_functions(ov_model, create_ref_model_1(), False)
|
||||
assert flag, msg
|
||||
|
||||
ov_model = convert_model(model, extensions=[ext_path1, ext_path2])
|
||||
flag, msg = compare_functions(ov_model, create_ref_model_2(), False)
|
||||
assert flag, msg
|
||||
|
||||
ov_model = convert_model(model, extensions=','.join([ext_path1, ext_path2]))
|
||||
flag, msg = compare_functions(ov_model, create_ref_model_2(), False)
|
||||
assert flag, msg
|
||||
|
||||
tf.io.write_graph(model, tmpdir, 'model.pb', False)
|
||||
inp_model = os.path.join(tmpdir, 'model.pb')
|
||||
from openvino.runtime import Core
|
||||
core = Core()
|
||||
|
||||
# tests for MO cli tool
|
||||
exit_code, stderr = generate_ir(coverage=False, **{"input_model": inp_model,
|
||||
"extensions": ext_path1,
|
||||
"output_dir": tmpdir})
|
||||
assert not exit_code
|
||||
|
||||
ov_model = core.read_model(os.path.join(tmpdir, "model.xml"))
|
||||
flag, msg = compare_functions(ov_model, create_ref_model_1(), False)
|
||||
assert flag, msg
|
||||
|
||||
exit_code, stderr = generate_ir(coverage=False, **{"input_model": inp_model,
|
||||
"extensions": ','.join([ext_path1, ext_path2]),
|
||||
"output_dir": tmpdir})
|
||||
assert not exit_code
|
||||
|
||||
ov_model = core.read_model(os.path.join(tmpdir, "model.xml"))
|
||||
flag, msg = compare_functions(ov_model, create_ref_model_2(), False)
|
||||
assert flag, msg
|
||||
@@ -0,0 +1,15 @@
|
||||
# Copyright (C) 2018-2023 Intel Corporation
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from openvino.tools.mo.front.common.replacement import FrontReplacementPattern
|
||||
from openvino.tools.mo.graph.graph import Graph
|
||||
from openvino.tools.mo.ops.activation_ops import Sin
|
||||
|
||||
|
||||
class DummyExt1(FrontReplacementPattern):
|
||||
enabled = True
|
||||
|
||||
def find_and_replace_pattern(self, graph: Graph):
|
||||
for node in graph.get_op_nodes(op='ReLU'):
|
||||
new_node = Sin(graph, {'name': node.soft_get('name') + '/sin'}).create_node()
|
||||
node.out_port(0).get_connection().insert_node(new_node)
|
||||
@@ -0,0 +1,15 @@
|
||||
# Copyright (C) 2018-2023 Intel Corporation
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from openvino.tools.mo.front.common.replacement import FrontReplacementPattern
|
||||
from openvino.tools.mo.graph.graph import Graph
|
||||
from openvino.tools.mo.ops.activation_ops import Tanh
|
||||
|
||||
|
||||
class DummyExt2(FrontReplacementPattern):
|
||||
enabled = True
|
||||
|
||||
def find_and_replace_pattern(self, graph: Graph):
|
||||
for node in graph.get_op_nodes(op='Sigmoid'):
|
||||
new_node = Tanh(graph, {'name': node.soft_get('name') + '/tanh'}).create_node()
|
||||
node.out_port(0).get_connection().insert_node(new_node)
|
||||
@@ -6,7 +6,6 @@ import numpy as np
|
||||
|
||||
from common.mo_convert_test_class import CommonMOConvertTest
|
||||
from common.onnx_layer_test_class import save_to_onnx
|
||||
from unit_tests.utils.graph import build_graph
|
||||
|
||||
import openvino.runtime as ov
|
||||
from openvino.runtime import PartialShape, Model
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# Copyright (C) 2018-2023 Intel Corporation
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
|
||||
class TestSubprocessMoConvert(unittest.TestCase):
|
||||
def test_mo_convert(self):
|
||||
args = [sys.executable, '-m', 'pytest',
|
||||
os.path.join(os.path.dirname(__file__), 'mo_convert_legacy_extensions_test_actual.py'), '-s']
|
||||
|
||||
status = subprocess.run(args, env=os.environ)
|
||||
assert not status.returncode
|
||||
Reference in New Issue
Block a user