找不到匹配的函数来调用从 SavedModel 加载 [英] Could not find matching function to call loaded from the SavedModel

查看:73
本文介绍了找不到匹配的函数来调用从 SavedModel 加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的代码,它假设加载一个模型,然后对来自 MNIST 数据集的元素进行预测运行.在执行开始时,代码工作正常,我得到了我想要的预测,但突然间我确实收到了以下错误,我不确定这是否与 .predict arguments 有关.

我的代码:

# 导入库将 tensorflow 导入为 tf # 深度学习库.张量只是多维数组import gzip,sys,pickle # 数据集操作库# 导入 MNIST 数据集f = gzip.open('mnist.pkl.gz', 'rb')如果 sys.version_info <(3,):数据 = pickle.load(f)别的:data = pickle.load(f, encoding='bytes')f.close()(x_train, _), (x_test, _) = 数据打印(--------------数据集准备好---------------")# 使用 x_test/中的一个示例稍后删除# 预处理x_test = tf.keras.utils.normalize(x_test,axis=1) # 在 0 和 1 之间缩放数据# 导入模型new_model = tf.keras.models.load_model('epic_num_reader.model')打印(----------------------模型准备好了-----------------------")# 获取预测预测 = new_model.predict(x_test[0])将 numpy 导入为 np打印(----------------------预测准备好了-----------------------")打印(np.argmax(预测))

错误信息:

<块引用>

 ---------------数据集准备好-----------------------2019-10-27 00:36:58.767359: I tensorflow/core/platform/cpu_feature_guard.cc:142] 你的 CPU 支持这个 TensorFlow 二进制文件没有被编译使用的指令:AVX2-----------------------模型准备好----------------------回溯(最近一次调用最后一次):文件c:\Users\lotfi\Desktop\DigitsDetector\main1.py",第 24 行,在 <module> 中预测 = new_model.predict(x_test[0])文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py",第 909 行,在预测中use_multiprocessing=use_multiprocessing)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",第 462 行,在预测中步骤=步骤,回调=回调,**kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",第 444 行,在 _model_iterationtotal_epochs=1)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",第 123 行,在 run_one_epochbatch_outs = execution_function(迭代器)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py",第86行,在execution_function中分布式函数(input_fn))文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py",第 457 行,在 __call__ 中结果 = self._call(*args, **kwds)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py",第 503 行,在 _callself._initialize(args, kwds, add_initializers_to=initializer_map)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py",第 408 行,在 _initialize*参数,**kwds))文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py",第 1848 行,在 _get_concrete_function_internal_garbage_collected图函数,_,_ = self._maybe_define_function(args, kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py",第 2150 行,在 _maybe_define_functiongraph_function = self._create_graph_function(args, kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py",第 2041 行,在 _create_graph_function 中capture_by_value=self._capture_by_value),文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\framework\func_graph.py",第 915 行,在 func_graph_from_py_funcfunc_outputs = python_func(*func_args, **func_kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py",第358行,在wrapped_fn返回weak_wrapped_fn().__wrapped__(*args, **kwds)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py",第 73 行,在分布式函数中per_replica_function, args=(model, x, y, sample_weights))文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py",第760行,experimental_run_v2返回 self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py",第 1787 行,在 call_for_each_replica返回 self._call_for_each_replica(fn, args, kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py",第 2132 行,在 _call_for_each_replica返回 fn(*args, **kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\autograph\impl\api.py",第 292 行,在包装器中返回 func(*args, **kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py",第 162 行,在 _predict_on_batch返回 predict_on_batch(model, x)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py",第370行,在predict_on_batch中返回模型(输入)#pylint:禁用=不可调用文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py",第 847 行,在 __call__输出 = call_fn(cast_inputs, *args, **kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py",第270行,调用输出 = 层(输入,**kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py",第 847 行,在 __call__输出 = call_fn(cast_inputs, *args, **kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\utils.py",第 57 行,在 return_outputs_and_add_losses输出,损失 = fn(输入,*args,**kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py",第 457 行,在 __call__ 中结果 = self._call(*args, **kwds)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py",第 494 行,在 _call结果 = self._stateful_fn(*args, **kwds)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py",第 1822 行,在 __call__ 中graph_function, args, kwargs = self._maybe_define_function(args, kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py",第 2150 行,在 _maybe_define_functiongraph_function = self._create_graph_function(args, kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py",第 2041 行,在 _create_graph_function 中capture_by_value=self._capture_by_value),文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\framework\func_graph.py",第 915 行,在 func_graph_from_py_funcfunc_outputs = python_func(*func_args, **func_kwargs)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py",第358行,在wrapped_fn返回weak_wrapped_fn().__wrapped__(*args, **kwds)文件C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\saved_model\function_deserialization.py",第262行,在restored_function_body"\n\n".join(signature_descriptions)))

错误消息继续:

<块引用>

 ValueError: 找不到匹配的函数来调用从 SavedModel 加载.得到了:位置参数(共 1 个):*张量(输入:0",形状=(无,28),dtype=float32)关键字参数:{}预期这些参数与以下 1 个选项之一匹配:选项1:位置参数(共 1 个):* TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='inputs')关键字参数:{}

解决方案

遇到了同样的问题.试试:

 pip install tf-nightly

解决方案来自这里:https://github.com/tensorflow/tensorflow/issues/35446 - 评论来自奥努什.

但是如果你有张量板,这可能会中断对张量板的调用.对此的分步解决方案如下:

 pip卸载tensorflowpip卸载张量板pip install -q tf-nightlypip install --ignore-installed tf-nightly

如果从这里得到:https://github.com/tensorflow/tensorboard/issues/2226 - 来自 mmehedin 的评论,2019 年 6 月 29 日.

I wrote below code and it suppose to load a model followed by a predictive run of an element from MNIST dataset. At the beginning of the execution the code works fine and I get my desired prediction, but then suddenly I did get the below error following error and I'm not sure if this could be related to .predict arguments.

My code:

# importing libraries
import tensorflow as tf  # deep learning library. Tensors are just multi-dimensional arrays
import gzip,sys,pickle # dataset manipulation library

# importing MNIST dataset
f = gzip.open('mnist.pkl.gz', 'rb')
if sys.version_info < (3,):
    data = pickle.load(f)
else:
    data = pickle.load(f, encoding='bytes')
f.close()
(x_train, _), (x_test, _) = data

print("-----------------------dataset ready-----------------------")
# using an expample from x_test / to remove later
# preprocessing
x_test = tf.keras.utils.normalize(x_test, axis=1)  # scales data between 0 and 1

# importing model
new_model = tf.keras.models.load_model('epic_num_reader.model')
print("-----------------------model ready-----------------------")

# getting prediction
predictions = new_model.predict(x_test[0])
import numpy as np
print("-----------------------predection ready-----------------------")
print(np.argmax(predictions))

The error message:

    -----------------------dataset ready-----------------------
    2019-10-27 00:36:58.767359: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
    -----------------------model ready-----------------------
    Traceback (most recent call last):
      File "c:\Users\lotfi\Desktop\DigitsDetector\main1.py", line 24, in <module>
        predictions = new_model.predict(x_test[0])
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 909, in predict
        use_multiprocessing=use_multiprocessing)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 462, in predict
        steps=steps, callbacks=callbacks, **kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 444, in _model_iteration
        total_epochs=1)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 123, in run_one_epoch
        batch_outs = execution_function(iterator)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 86, in execution_function
        distributed_function(input_fn))
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 457, in __call__
        result = self._call(*args, **kwds)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 503, in _call
        self._initialize(args, kwds, add_initializers_to=initializer_map)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 408, in _initialize
        *args, **kwds))
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 1848, in _get_concrete_function_internal_garbage_collected
        graph_function, _, _ = self._maybe_define_function(args, kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 2150, in _maybe_define_function
        graph_function = self._create_graph_function(args, kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 2041, in _create_graph_function
        capture_by_value=self._capture_by_value),
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 915, in func_graph_from_py_func
        func_outputs = python_func(*func_args, **func_kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 358, in wrapped_fn
        return weak_wrapped_fn().__wrapped__(*args, **kwds)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 73, in distributed_function
        per_replica_function, args=(model, x, y, sample_weights))
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py", line 760, in experimental_run_v2
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py", line 1787, in call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py", line 2132, in _call_for_each_replica
        return fn(*args, **kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\autograph\impl\api.py", line 292, in wrapper
        return func(*args, **kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 162, in _predict_on_batch
        return predict_on_batch(model, x)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 370, in predict_on_batch
        return model(inputs)  # pylint: disable=not-callable
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 847, in __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py", line 270, in call
        outputs = layer(inputs, **kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 847, in __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\utils.py", line 57, in return_outputs_and_add_losses
        outputs, losses = fn(inputs, *args, **kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 457, in __call__
        result = self._call(*args, **kwds)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 494, in _call
        results = self._stateful_fn(*args, **kwds)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 1822, in __call__
        graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 2150, in _maybe_define_function
        graph_function = self._create_graph_function(args, kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 2041, in _create_graph_function
        capture_by_value=self._capture_by_value),
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 915, in func_graph_from_py_func
        func_outputs = python_func(*func_args, **func_kwargs)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 358, in wrapped_fn
        return weak_wrapped_fn().__wrapped__(*args, **kwds)
      File "C:\Users\lotfi\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\saved_model\function_deserialization.py", line 262, in restored_function_body
        "\n\n".join(signature_descriptions)))

Error message continued:

    ValueError: Could not find matching function to call loaded from the SavedModel. Got:
      Positional arguments (1 total):
        * Tensor("inputs:0", shape=(None, 28), 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, 28, 28), dtype=tf.float32, name='inputs')
      Keyword arguments: {}

解决方案

Had the same issue. Try:

    pip install tf-nightly

Solution is from here: https://github.com/tensorflow/tensorflow/issues/35446 - comment from oanush.

But this may break calling of tensorboards if you have one. Step by step solution for this is below:

    pip uninstall tensorflow
    pip uninstall tensorboard
    pip install -q tf-nightly
    pip install --ignore-installed tf-nightly

Got if from here: https://github.com/tensorflow/tensorboard/issues/2226 - comment from mmehedin, 29 Jun 2019.

这篇关于找不到匹配的函数来调用从 SavedModel 加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆