TensorFlow Lite模型推理报错求助:输入参数不匹配问题
解决TFLite签名推理的参数错误问题
首先,你遇到的问题核心是错误传入多余参数,且使用了模型签名中不存在的输入参数名。以下是具体解决步骤:
查看模型签名的输入输出名称
在调用签名运行器前,先打印模型的签名列表,确认实际的输入参数名(不一定是x):interpreter = tf.lite.Interpreter('tflite_model') print(interpreter.get_signature_list())输出示例可能类似:
{'serving_default': {'inputs': ['input_1'], 'outputs': ['predictions']}}这里的
input_1就是你需要传入的参数名,predictions是输出键名。使用正确的参数名调用签名运行器
不需要传入shape和dtype参数(签名已预先定义输入的形状和数据类型),只需传入对应名称的输入张量:my_signature = interpreter.get_signature_runner() # 替换成你从签名列表中得到的输入名称 output = my_signature(input_1=x) # 替换成对应的输出名称 print(output['predictions'])
错误原因说明
- 最初传入
x=x, shape=x.shape, dtype=tf.float32属于多传了2个参数,而模型签名仅定义了1个输入,因此触发expected 1 vs provided 3的错误。 - 用
my_signature(x=x)提示参数无效,是因为你的TFLite模型签名中并没有名为x的输入参数,输入名由原Keras模型的输入层名称决定(比如默认的input_1)。
内容的提问来源于stack exchange,提问作者Olli
相关产品推荐
相关产品推荐

