tensorflow: ValueError: Could not find matching function to call loaded from the SavedModel.

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
  • OS Platform and Distribution : Windows 10
  • TensorFlow installed from: pip, tf 2.1.0, cpu
  • Python version: 3.7.4

build a simple network

import tensorflow as tf
from tensorflow.keras import layers
class Model():
    def __init__(self):
        self.build_model()

    def build_model(self):
        input1 = layers.Input(shape=(5,))
        input2 = layers.Input(shape=(5,))

        out1 = layers.Dense(1)(input1)
        out2 = layers.Dense(1)(input2)
        out = out1 - out2
        out = tf.nn.sigmoid(out)

        self.model = tf.keras.Model(inputs=[input1, input2], outputs=out)

model = Model()
s1 = "exp\\model"
model.model.save(s1)
model2 = tf.keras.models.load_model(s1)

Test Code

model = Model()
s1 = "exp\\model"
model.model.save(s1)
model2 = tf.keras.models.load_model(s1)

Error Info

Traceback (most recent call last):
  File "e:\workspace\work\Study\pytorch_demos\demo5.py", line 22, in <module>
    model2 = tf.keras.models.load_model(s1)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\keras\saving\save.py", line 150, in load_model
    return saved_model_load.load(filepath, compile)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\keras\saving\saved_model\load.py", line 89, in load
    model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\saved_model\load.py", line 552, in load_internal
    export_dir)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\keras\saving\saved_model\load.py", line 119, in __init__
    self._finalize()
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\keras\saving\saved_model\load.py", line 157, in _finalize
    created_layers={layer.name: layer for layer in node.layers})
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1903, in reconstruct_from_config
    process_node(layer, node_data)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1851, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 773, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\keras\saving\saved_model\utils.py", line 59, in return_outputs_and_add_losses
    outputs, losses = fn(inputs, *args, **kwargs)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\eager\def_function.py", line 568, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\eager\def_function.py", line 615, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\eager\def_function.py", line 497, in _initialize
    *args, **kwds))
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\eager\function.py", line 2389, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\eager\function.py", line 2703, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\eager\function.py", line 2593, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 978, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\eager\def_function.py", line 439, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "C:\Users\Administrator\Anaconda3\Lib\site-packages\tensorflow_core\python\saved_model\function_deserialization.py", line 262, in restored_function_body
    "\n\n".join(signature_descriptions)))
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (1 total):
    * Tensor("inputs:0", shape=(None, 1), dtype=float32)
  Keyword arguments: {}

Expected these arguments to match one of the following 1 option(s):

Option 1:
  Positional arguments (1 total):
    * [TensorSpec(shape=(None, 1), dtype=tf.float32, name='inputs/0')]
  Keyword arguments: {}

But removing the line will not cause the error

# Correct if not using this line
out = tf.nn.sigmoid(out)

The error is caused when the model is loaded from savedModel file. Model building process is correct.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 24 (1 by maintainers)

Most upvoted comments

Seeing similar error in 2.4.0. Not at load time, but when calling model.predict(ds) or modle.predict_step()

code that works with model just after training fails with this exception after save() and load()

Hi there , this happens due to the bugs of tf2.1.0 version. You can avoid that by upgrading to the latest tensorflow version.

I have the same bug in my code for Tensorflow 2.3 as installed with Conda. The same code for Tensorflow-gpu 2.2.0 installed with Conda produces an index error related to tf.gather_nd operations.

I’m seeing similar issue in 2.2.0 version. The difference is that if I don’t touch anything, loaded model works. But if I change input data by removing certain rows from df, then it reports this error. Shouldn’t saved model independent of input data?