如何处理此错误:“无法获取签名的输入Map:serving_default”
当提供给TensorFlow Serving API的输入数据格式与模型签名中定义的预期输入格式不匹配时,通常会发生此错误。要解决此问题,请执行以下步骤:
1.**检查模型签名:**确保您提供的输入数据格式与模型签名所期望的格式一致,输入格式(如数据类型、形状)应符合签名的输入要求。
1.**数据预处理:**如果模型需要对输入数据进行特定的预处理(例如,去噪、归一化),请确保在将图像发送给模型之前对图像进行相应的预处理。
1.**JSON请求格式:**确保您发送到TensorFlow Serving的JSON请求格式正确。instances
字段应包含输入列表,其中每个输入都与模型输入签名的预期格式匹配。
1.**检查模型版本:**如果您使用的是特定版本的模型,请确保在API请求(url
变量)中指定正确的版本。
以下是使用TensorFlow Serving发送图像进行预测的正确格式化代码示例:
import requests
import json
import numpy as np
import base64
import cv2
# Replace this with the actual image path you want to test
image_path = 'H_L_.jpg'
# Read and preprocess the image
image = cv2.imread(image_path)
image = cv2.resize(image, (256, 256))
image = image.astype(np.float32) / 255.0
image = np.expand_dims(image, axis=0)
# Convert the NumPy array to bytes before encoding
encoded_image = base64.b64encode(image.tobytes()).decode('utf-8')
# Prepare the JSON request with the signature name
data = {
"signature_name": "serving_default",
"instances": [{"input_1": encoded_image}] # Adjust the input key based on your model's signature
}
# Replace these labels with your actual labels
labels = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
# Send the inference request to TensorFlow Serving
url = 'http://localhost:8501/v1/models/model:predict' # Replace 'model' with the actual model name and version
headers = {"content-type": "application/json"}
response = requests.post(url, data=json.dumps(data), headers=headers)
# Process the response
if response.status_code == 200:
predictions = response.json()['predictions'][0]
predicted_class_idx = np.argmax(predictions)
predicted_label = labels[predicted_class_idx]
print("Predicted Label:", predicted_label)
print("Class Probabilities:", predictions)
else:
print("Error: Unable to get predictions. Status code:", response.status_code)
print("Response content:", response.content)
1条答案
按热度按时间c2e8gylq1#
你的模型好像没有一个叫做
signature_default
的签名,你能展示一下saved_model_cli show --dir /path/to/your/model --all
的结果吗?