首页 > 解决方案 > 如何加载和运行 TensorFlow 模型

问题描述

超级简单的问题。

我正在关注本教程: https ://github.com/tensorflow/hub/blob/master/examples/colab/tf2_image_retraining.ipynb

我按照说明(不是可选部分)训练了一个模型,并使用 tf.saved_model.save 保存了它

现在我想针对新图片运行模型。我该怎么做呢?

我尝试了以下方法:

loaded = tf.saved_model.load("/tmp/saved_flowers_model")
print(list(loaded.signatures.keys()))  # ["serving_default"]

infer = loaded.signatures["serving_default"]
print(infer.structured_outputs)

file = .../pic.jpg
output = loaded (file)

返回以下错误:

ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (3 total):
    * /Users/dansafdie/Documents/Code Repos/Image Classifier/flower_photos/daisy/19834392829_7d697871f6.jpg
    * False
    * None
  Keyword arguments: {}

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

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

Option 2:
  Positional arguments (3 total):
    * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
    * False
    * None
  Keyword arguments: {}

Option 3:
  Positional arguments (3 total):
    * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_1')
    * False
    * None
  Keyword arguments: {}

Option 4:
  Positional arguments (3 total):
    * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_1')
    * True
    * None
  Keyword arguments: {}

标签: pythontensorflowkeras

解决方案


推荐阅读