[TF FE] Provide single tensor names for inputs and outputs in SavedModel (#17370)

* [TF FE] Provide single tensor names for inputs and outputs in SavedModel

Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>

* Fix build issue

* Xfail some cases due to internal problems in TF

* Xfail other layer test

* Extend documentation for function to adjust tensor names

* Use old path of tf2 layer testing for legacy frontend

---------

Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
This commit is contained in:
Roman Kazantsev
2023-05-06 07:37:47 +03:00
committed by GitHub
parent b6bbf3c44c
commit b6098fed90
6 changed files with 192 additions and 91 deletions

View File

@@ -36,51 +36,39 @@ class CommonTF2LayerTest(CommonLayerTest):
def get_tf2_keras_results(self, inputs_dict, model_path):
import tensorflow as tf
import numpy as np
# load a model
loaded_model = tf.keras.models.load_model(model_path, custom_objects=None)
# prepare input
input_for_model = []
# order inputs based on input names in tests
# since TF2 Keras model accepts a list of tensors for prediction
for input_name in sorted(inputs_dict):
input_value = inputs_dict[input_name]
# convert NCHW to NHWC layout of tensor rank greater 3
if self.use_old_api:
if len(input_value.shape) == 4:
input_value = np.transpose(input_value, (0, 2, 3, 1))
elif len(input_value.shape) == 5:
input_value = np.transpose(input_value, (0, 2, 3, 4, 1))
input_for_model.append(input_value)
if len(input_for_model) == 1:
input_for_model = input_for_model[0]
# infer by original framework and complete a dictionary with reference results
tf_res_list = loaded_model(input_for_model)
if tf.is_tensor(tf_res_list):
tf_res_list = [tf_res_list]
else:
# in this case tf_res_list is a list of the single tuple of outputs
tf_res_list = tf_res_list[0]
result = dict()
for ind, tf_res in enumerate(tf_res_list):
if ind == 0:
output = "Identity"
else:
output = "Identity_{}".format(ind)
if self.use_new_frontend:
imported = tf.saved_model.load(model_path)
f = imported.signatures["serving_default"]
result = f(**inputs_dict)
else:
# load a model
loaded_model = tf.keras.models.load_model(model_path, custom_objects=None)
# prepare input
input_for_model = []
# order inputs based on input names in tests
# since TF2 Keras model accepts a list of tensors for prediction
for input_name in sorted(inputs_dict):
input_value = inputs_dict[input_name]
input_for_model.append(input_value)
if len(input_for_model) == 1:
input_for_model = input_for_model[0]
tf_res = tf_res.numpy()
tf_res_rank = len(tf_res.shape)
if self.use_old_api and tf_res_rank > 3:
# create axis order for NCHW layout
axis_order = np.arange(tf_res_rank)
axis_order = np.insert(axis_order, 1, axis_order[-1])
axis_order = np.delete(axis_order, [-1])
result[output] = tf_res.transpose(axis_order)
# infer by original framework and complete a dictionary with reference results
tf_res_list = loaded_model(input_for_model)
if tf.is_tensor(tf_res_list):
tf_res_list = [tf_res_list]
else:
# in this case tf_res_list is a list of the single tuple of outputs
tf_res_list = tf_res_list[0]
for ind, tf_res in enumerate(tf_res_list):
if ind == 0:
output = "Identity"
else:
output = "Identity_{}".format(ind)
tf_res = tf_res.numpy()
result[output] = tf_res
return result

View File

@@ -41,10 +41,10 @@ class TestKerasConvLSTM2D(CommonTF2LayerTest):
pytest.param(dict(params=dict(filters=4, kernel_size=(3, 3), padding='same', return_sequences=False,
activation="swish"),
input_shapes=[[2, 5, 20, 30, 2]]), marks=pytest.mark.skip(reason="*-108786")),
dict(params=dict(filters=6, kernel_size=(2, 3), padding='valid', dilation_rate=3,
recurrent_activation="elu", return_sequences=True, use_bias=True,
data_format="channels_first"),
input_shapes=[[2, 5, 1, 40, 30]])
pytest.param(dict(params=dict(filters=6, kernel_size=(2, 3), padding='valid', dilation_rate=3,
recurrent_activation="elu", return_sequences=True, use_bias=True,
data_format="channels_first"),
input_shapes=[[2, 5, 1, 40, 30]]), marks=pytest.mark.skip(reason="110006")),
]
@pytest.mark.parametrize("params", test_data_basic)

View File

@@ -56,7 +56,7 @@ class TestKerasDense(CommonTF2LayerTest):
dict(input_names=["x"], input_shapes=[[5, 4]], input_type=tf.float32, units=1,
activation='sigmoid', use_bias=True),
pytest.param(dict(input_names=["x"], input_shapes=[[5, 4, 8]], input_type=tf.float32, units=4,
activation='tanh', use_bias=True), marks=pytest.mark.precommit_tf_fe),
activation='tanh', use_bias=True), marks=pytest.mark.skip(reason="110006")),
dict(input_names=["x"], input_shapes=[[5, 4, 8, 8]], input_type=tf.float32, units=5,
activation='linear', use_bias=True),
dict(input_names=["x"], input_shapes=[[5, 4, 8, 6, 4]], input_type=tf.float32, units=4,