[MO][TF FE] Remove WA for supporting different TF formats (#18078)
* [MO][TF FE] Remove WA for supporting different TF formats After providing direct support of all TensorFlow formats, we are ready to remove WA that converts all unsupported formats to frozen protobuf using native TensorFlow Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com> * Debug failure for LSTM multicell * Reset default graph before entering session * Remove temporal debug info --------- Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
This commit is contained in:
1 parent
26c4ed7ba8
commit
3c623e890d
3 files changed
+7
-224
No files matched your search
@@ -390,18 +390,12 @@ def prepare_ir(argv: argparse.Namespace):
|
||||
if moc_front_end:
|
||||
fallback_reasons = check_fallback(argv)
|
||||
if len(fallback_reasons) == 0:
|
||||
path_to_aux_pb = None
|
||||
orig_argv_values = {"input_model": argv.input_model, "model_name": argv.model_name}
|
||||
if not argv.use_legacy_frontend and is_tf:
|
||||
if tf_frontend_with_python_bindings_installed and 'tf' in available_moc_front_ends and \
|
||||
type_supported_by_tf_fe(argv.input_model):
|
||||
argv.input_model = create_tf_graph_iterator(argv.input_model,
|
||||
argv.placeholder_shapes,
|
||||
argv.placeholder_data_types,
|
||||
getattr(argv, "example_input", None))
|
||||
else:
|
||||
from openvino.tools.mo.front.tf.loader import convert_to_pb
|
||||
path_to_aux_pb = convert_to_pb(argv)
|
||||
if is_tf and tf_frontend_with_python_bindings_installed and \
|
||||
type_supported_by_tf_fe(argv.input_model):
|
||||
argv.input_model = create_tf_graph_iterator(argv.input_model,
|
||||
argv.placeholder_shapes,
|
||||
argv.placeholder_data_types,
|
||||
getattr(argv, "example_input", None))
|
||||
try:
|
||||
t.send_event("mo", "conversion_method", moc_front_end.get_name() + "_frontend")
|
||||
moc_front_end.add_extension(TelemetryExtension("mo", t.send_event, t.send_error, t.send_stack_trace))
|
||||
@@ -424,14 +418,6 @@ def prepare_ir(argv: argparse.Namespace):
|
||||
# re-throw exception for all frontends except TensorFlow FE
|
||||
# and in case unexpected conversion failures
|
||||
raise
|
||||
finally:
|
||||
# TODO: remove this workaround once new TensorFlow frontend supports non-frozen formats: checkpoint, MetaGraph, and SavedModel
|
||||
# Now it converts all TensorFlow formats to the frozen .pb format in case new TensorFlow frontend
|
||||
if is_tf and path_to_aux_pb is not None:
|
||||
argv.input_model = orig_argv_values["input_model"]
|
||||
argv.model_name = orig_argv_values["model_name"]
|
||||
if path_to_aux_pb is not None and os.path.exists(path_to_aux_pb):
|
||||
os.remove(path_to_aux_pb)
|
||||
|
||||
if len(fallback_reasons) > 0:
|
||||
reasons_message = ", ".join(fallback_reasons)
|
||||
|
||||
@@ -248,7 +248,6 @@ def saved_model_load(imported, env_setup):
|
||||
def load_tf_graph_def(graph_file_name: str = "", is_binary: bool = True, checkpoint: str = "",
|
||||
model_dir: str = "", saved_model_tags: list = [], meta_graph_file: str = "",
|
||||
user_output_node_names_list: list = []):
|
||||
|
||||
if not isinstance(graph_file_name, str) and graph_file_name is not None:
|
||||
return prepare_graph_def(graph_file_name)
|
||||
# As a provisional solution, use a native TF methods to load a model protobuf
|
||||
@@ -291,6 +290,7 @@ def load_tf_graph_def(graph_file_name: str = "", is_binary: bool = True, checkpo
|
||||
for node in input_meta_graph_def.graph_def.node:
|
||||
if '_output_shapes' in node.attr:
|
||||
del node.attr['_output_shapes']
|
||||
tf_v1.reset_default_graph()
|
||||
# pylint: disable=no-member
|
||||
with tf_v1.Session() as sess:
|
||||
restorer = tf_v1.train.import_meta_graph(input_meta_graph_def)
|
||||
@@ -326,46 +326,6 @@ def load_tf_graph_def(graph_file_name: str = "", is_binary: bool = True, checkpo
|
||||
raise Error("Unknown configuration of input model parameters")
|
||||
|
||||
|
||||
def convert_to_pb(argv: argparse.Namespace):
|
||||
from openvino.tools.mo.utils.cli_parser import get_model_name
|
||||
if argv.input_model is not None and not isinstance(argv.input_model, (str, Path)):
|
||||
return None
|
||||
env_setup = get_environment_setup("tf")
|
||||
if "tensorflow" in env_setup and env_setup["tensorflow"] >= LooseVersion("2.0.0"):
|
||||
tf.keras.backend.clear_session()
|
||||
|
||||
# any model format on disk is accepted by TensorFlow Frontend
|
||||
# only model from memory requires temporal saving on a disk
|
||||
if (argv.input_model and isinstance(argv.input_model, str)) or argv.saved_model_dir or argv.input_meta_graph:
|
||||
return None
|
||||
|
||||
user_output_node_names_list = argv.output if argv.output else None
|
||||
if user_output_node_names_list is not None and not isinstance(user_output_node_names_list, list):
|
||||
user_output_node_names_list = user_output_node_names_list.split(',')
|
||||
graph_def, _, _, _ = load_tf_graph_def(
|
||||
graph_file_name=argv.input_model,
|
||||
is_binary=not argv.input_model_is_text,
|
||||
checkpoint=argv.input_checkpoint,
|
||||
user_output_node_names_list=user_output_node_names_list,
|
||||
model_dir=argv.saved_model_dir,
|
||||
meta_graph_file=argv.input_meta_graph,
|
||||
saved_model_tags=argv.saved_model_tags)
|
||||
if argv.model_name:
|
||||
model_name = argv.model_name
|
||||
elif argv.input_model:
|
||||
model_name = get_model_name(argv.input_model)
|
||||
elif argv.saved_model_dir:
|
||||
model_name = "saved_model"
|
||||
elif argv.input_meta_graph:
|
||||
model_name = get_model_name(argv.input_meta_graph)
|
||||
argv.model_name = model_name
|
||||
tf_v1.io.write_graph(graph_def, argv.output_dir if argv.output_dir != '.' else os.getcwd(),
|
||||
model_name + "_tmp.pb", as_text=False)
|
||||
path_to_pb = os.path.normpath(os.path.join(argv.output_dir, model_name + "_tmp.pb"))
|
||||
argv.input_model = path_to_pb
|
||||
return path_to_pb
|
||||
|
||||
|
||||
def protobuf_attrs(pb: tf_v1.NodeDef):
|
||||
return {'pb': pb}
|
||||
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
# Copyright (C) 2018-2023 Intel Corporation
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from openvino.tools.mo.front.tf.loader import convert_to_pb
|
||||
|
||||
|
||||
class ConvertToPBTests(unittest.TestCase):
|
||||
test_directory = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
def setUp(self):
|
||||
self.argv = argparse.Namespace(input_model=None, input_model_is_text=False, input_checkpoint=None, output=None,
|
||||
saved_model_dir=None, input_meta_graph=None, saved_model_tags=None,
|
||||
model_name='model', output_dir=None)
|
||||
|
||||
@unittest.skip("Ticket: 106651")
|
||||
def test_saved_model(self):
|
||||
import tensorflow as tf
|
||||
with tempfile.TemporaryDirectory(dir=self.test_directory) as tmp_dir:
|
||||
inputs = tf.keras.Input(shape=(3,))
|
||||
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
|
||||
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
|
||||
model = tf.keras.Model(inputs=inputs, outputs=outputs)
|
||||
model.save(tmp_dir)
|
||||
self.argv.saved_model_dir = tmp_dir
|
||||
self.argv.output_dir = tmp_dir
|
||||
path_to_pb = convert_to_pb(self.argv)
|
||||
self.assertTrue(os.path.exists(path_to_pb), "The auxiliary .pb is not generated")
|
||||
self.assertTrue(os.path.getsize(path_to_pb) != 0, "The auxiliary .pb is empty")
|
||||
|
||||
def test_meta_format(self):
|
||||
try:
|
||||
import tensorflow.compat.v1 as tf_v1
|
||||
except ImportError:
|
||||
import tensorflow as tf_v1
|
||||
from tensorflow.python.eager.context import graph_mode
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=self.test_directory) as tmp_dir:
|
||||
with graph_mode():
|
||||
a = tf_v1.get_variable("A", initializer=tf_v1.constant(3, shape=[2]))
|
||||
b = tf_v1.get_variable("B", initializer=tf_v1.constant(5, shape=[2]))
|
||||
tf_v1.add(a, b, name='Add')
|
||||
init_op = tf_v1.global_variables_initializer()
|
||||
saver = tf_v1.train.Saver()
|
||||
with tf_v1.Session() as sess:
|
||||
sess.run(init_op)
|
||||
saver.save(sess, os.path.join(tmp_dir, 'model'))
|
||||
|
||||
self.argv.input_meta_graph = os.path.join(tmp_dir, 'model.meta')
|
||||
self.argv.output_dir = tmp_dir
|
||||
path_to_pb = convert_to_pb(self.argv)
|
||||
self.assertTrue(path_to_pb is None, "Auxiliary .pb must not be generated for .meta")
|
||||
|
||||
def test_text_frozen_format(self):
|
||||
try:
|
||||
import tensorflow.compat.v1 as tf_v1
|
||||
except ImportError:
|
||||
import tensorflow as tf_v1
|
||||
tf_v1.reset_default_graph()
|
||||
|
||||
# Create the graph and model
|
||||
with tempfile.TemporaryDirectory(dir=self.test_directory) as tmp_dir:
|
||||
with tf_v1.Session() as sess:
|
||||
x = tf_v1.placeholder(tf_v1.float32, [2, 3], 'x')
|
||||
y = tf_v1.placeholder(tf_v1.float32, [2, 3], 'y')
|
||||
tf_v1.add(x, y, name="add")
|
||||
|
||||
tf_v1.global_variables_initializer()
|
||||
tf_v1.io.write_graph(sess.graph, tmp_dir, 'model.pbtxt', as_text=True)
|
||||
|
||||
# initialize test case and check
|
||||
self.argv.input_model = os.path.join(tmp_dir, 'model.pbtxt')
|
||||
self.argv.input_model_is_text = True
|
||||
self.argv.output_dir = tmp_dir
|
||||
self.assertTrue(os.path.exists(self.argv.input_model),
|
||||
"The test model in frozen text format must exist")
|
||||
# test convert_to_pb
|
||||
path_to_pb = convert_to_pb(self.argv)
|
||||
self.assertTrue(path_to_pb is None, "Auxiliary .pb must not be generated for .pbtxt")
|
||||
|
||||
def test_binary_frozen_format(self):
|
||||
try:
|
||||
import tensorflow.compat.v1 as tf_v1
|
||||
except ImportError:
|
||||
import tensorflow as tf_v1
|
||||
tf_v1.reset_default_graph()
|
||||
|
||||
# Create the graph and model
|
||||
with tempfile.TemporaryDirectory(dir=self.test_directory) as tmp_dir:
|
||||
with tf_v1.Session() as sess:
|
||||
x = tf_v1.placeholder(tf_v1.float32, [2, 3], 'x')
|
||||
y = tf_v1.placeholder(tf_v1.float32, [2, 3], 'y')
|
||||
tf_v1.add(x, y, name="add")
|
||||
|
||||
tf_v1.global_variables_initializer()
|
||||
tf_v1.io.write_graph(sess.graph, tmp_dir, 'model.pb', as_text=False)
|
||||
|
||||
# initialize test case and check
|
||||
self.argv.input_model = os.path.join(tmp_dir, 'model.pb')
|
||||
self.argv.input_model_is_text = False
|
||||
self.argv.output_dir = tmp_dir
|
||||
self.assertTrue(os.path.exists(self.argv.input_model),
|
||||
"The test model in frozen binary format must exist")
|
||||
# test convert_to_pb - expect no auxiliary model created
|
||||
self.assertIsNone(convert_to_pb(self.argv))
|
||||
|
||||
def test_meta_format_session_clearing(self):
|
||||
try:
|
||||
import tensorflow.compat.v1 as tf_v1
|
||||
except ImportError:
|
||||
import tensorflow as tf_v1
|
||||
|
||||
from openvino.tools.mo.utils.versions_checker import get_environment_setup
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
env_setup = get_environment_setup("tf")
|
||||
use_tf2 = False
|
||||
if "tensorflow" in env_setup and env_setup["tensorflow"] >= LooseVersion("2.0.0"):
|
||||
use_tf2 = True
|
||||
|
||||
from tensorflow.python.eager.context import graph_mode
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=self.test_directory) as tmp_dir:
|
||||
with graph_mode():
|
||||
a = tf_v1.get_variable("A", initializer=tf_v1.constant(3, shape=[2]))
|
||||
b = tf_v1.get_variable("B", initializer=tf_v1.constant(5, shape=[2]))
|
||||
tf_v1.add(a, b, name='Add')
|
||||
init_op = tf_v1.global_variables_initializer()
|
||||
saver = tf_v1.train.Saver()
|
||||
with tf_v1.Session() as sess:
|
||||
sess.run(init_op)
|
||||
saver.save(sess, os.path.join(tmp_dir, 'model1'))
|
||||
if use_tf2:
|
||||
import tensorflow as tf
|
||||
tf.keras.backend.clear_session()
|
||||
|
||||
with graph_mode():
|
||||
c = tf_v1.get_variable("C", initializer=tf_v1.constant(3, shape=[2]))
|
||||
d = tf_v1.get_variable("D", initializer=tf_v1.constant(5, shape=[2]))
|
||||
tf_v1.add(c, d, name='Add1')
|
||||
init_op = tf_v1.global_variables_initializer()
|
||||
saver = tf_v1.train.Saver()
|
||||
with tf_v1.Session() as sess:
|
||||
sess.run(init_op)
|
||||
saver.save(sess, os.path.join(tmp_dir, 'model2'))
|
||||
if use_tf2:
|
||||
import tensorflow as tf
|
||||
tf.keras.backend.clear_session()
|
||||
|
||||
self.argv.input_meta_graph = os.path.join(tmp_dir, 'model1.meta')
|
||||
self.argv.output_dir = tmp_dir
|
||||
path_to_pb = convert_to_pb(self.argv)
|
||||
self.assertTrue(path_to_pb is None, "Auxiliary .pb must not be generated for .meta")
|
||||
|
||||
self.argv.input_meta_graph = os.path.join(tmp_dir, 'model2.meta')
|
||||
self.argv.output_dir = tmp_dir
|
||||
self.argv.input_model = None
|
||||
path_to_pb = convert_to_pb(self.argv)
|
||||
self.assertTrue(path_to_pb is None, "Auxiliary .pb must not be generated for .meta")
|
||||
Reference in new issue
Block a user