繁体   English   中英

TensorFlow 使用已保存的 model 进行推理

[英]TensorFlow inference using saved model

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

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 输入来解决此错误?

错误信息

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')}

表明 function 期待关键字 arguments 并且没有位置 arguments。 字典表明关键字是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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM