【问题标题】:TensorFlow inference using saved model使用已保存模型的 TensorFlow 推理
【发布时间】:2020-12-16 22:48:35
【问题描述】:

我是 TensorFlow 2.3.1 的新手,并试图弄清楚推理是如何完成的。加载保存的模型后,我想传递一个只有张量的张量,以确保模型输出我们期望的结果。比如……

import tensorflow as tf

resnet18_tf = tf.saved_model.load("resnet18.tf")
x_tf = tf.ones((1,3,224,224), tf.float32)

resnet18_tf(x_tf)

但是,上面的代码导致以下错误...

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-34-33fa05a7412b> in <module>
      4 x_tf = tf.ones((1,3,224,224), tf.float32)
      5 
----> 6 resnet18_tf(x_tf)

ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (1 total):
    * Tensor("None_0:0", shape=(1, 3, 224, 224), dtype=float32)
  Keyword arguments: {}

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

Option 1:
  Positional arguments (0 total):
    * 
  Keyword arguments: {'input': TensorSpec(shape=(1, 3, 224, 224), dtype=tf.float32, name='input')}

我很确定形状是正确的,但我很难解释这个错误信息。如何进行 TensorSpec 输入来解决此错误?

【问题讨论】:

  • 也许尝试将其作为关键字参数传递? resnet18_tf(input=x_tf)
  • 哇,成功了……谢谢!

标签: python tensorflow


【解决方案1】:

错误信息

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

Option 1:
  Positional arguments (0 total):
    * 
  Keyword arguments: {'input': TensorSpec(shape=(1, 3, 224, 224), dtype=tf.float32, name='input')}

表明该函数需要关键字参数并且没有位置参数。字典表明关键字是input

import tensorflow as tf

resnet18_tf = tf.saved_model.load("resnet18.tf")
x_tf = tf.ones((1,3,224,224), tf.float32)

resnet18_tf(input=x_tf)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 2020-10-08
    • 2017-11-09
    相关资源
    最近更新 更多