尝试使用 tensorflow 2 预测 SavedModel 时出错

Error while trying to predict on SavedModel using tensorflow 2

我正在尝试使用以下代码对保存的模型进行预测

 features = np.ones((20, 40, 3), dtype=np.float32)
 features = tf.convert_to_tensor(value, dtype=tf.float32)
 imported_model = tf.saved_model.load(export_dir=os.path.join(os.path.join(model_path, directory)))
 import_fn = imported_model.signatures["serving_default"]
 import_fn(features)

我在 运行 使用 Tensorflow 2 时出现以下错误。当我使用 saved_model_cli.

时,模型预测工作正常
tensorflow.python.framework.errors_impl.InvalidArgumentError:  In[0] is not a matrix. Instead it has shape [20,40,3]
     [[node dense/BiasAdd (defined at model_manager.py:54) ]] [Op:__inference_pruned_318590]

保存的cli命令如下

saved_model_cli run --dir ./model_dir --tag_set serve --signature_def serving_default --input_exprs 'input=np.ones((20, 40, 3), dtype=np.float32)'

InvalidArgumentError 通常是由输入中的数据类型不匹配引起的。

根据您的错误“In[0] 不是矩阵。相反,它的形状为 [20,40,3]”。
您可以尝试操纵输入数据以正确匹配模型最初训练的输入类型和形状。您还可以检查当您使用 saved_model_cliPython IDE[=25= 相比模型如何处理您的输入].因为您在使用 Python IDE 时可能缺少一些预处理步骤,而在使用 saved_model_cli.

时正在完成这些步骤

您可以在此 link 中阅读有关使用 Saved_Model 格式 的更多用法。