如何解决TensorFlow Serving报错“Failed to get input map for signature: serving_default”
如何处理报错:“Failed to get input map for signature: serving_default”
该错误通常出现在向TensorFlow Serving API提供的输入数据格式与模型签名定义的预期输入格式不匹配时,可按以下步骤解决:
- 检查模型签名:确保输入数据的格式(如数据类型、形状)与模型签名要求的输入格式一致。
- 数据预处理:若模型对输入数据有特定预处理要求(如缩放、归一化),需在发送至模型前完成相应预处理。
- JSON请求格式:确保发送给TensorFlow Serving的JSON请求格式正确,
instances字段应包含符合模型输入签名预期格式的输入列表。 - 检查模型版本:若使用特定版本的模型,需在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)
内容的提问来源于stack exchange,提问作者Mayur Katre
相关产品推荐
相关产品推荐

