我已经训练了一个模型来使用 TF 和 TFX 对叶子进行分类。我是 TFServing 在本地环境中部署模型。虽然我能够部署模型但是当我向模型发出 POST 请求时,它返回
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://localhost:8501/v1/models/leaf_classification_native_keras_8/versions/1:predict
下面是部署模型的命令
docker run -p 8501:8501 --name tfserving_leaf_classification --mount type=bind,source=/home/bhargavpatel/Desktop/Image_Classification_tfx/Image_Classification/tfx/pipelines/leaf_classification_native_keras_8/Pusher/pushed_model,target=/models/leaf_classification_native_keras_8 -e MODEL_NAME=leaf_classification_native_keras_8 -t tensorflow/serving &
发帖请求的Python代码
import base64
import io
import json
import numpy as np
from PIL import Image
import requests
import tensorflow as tf
import cv2
import pprint
SERVER_URL = "http://localhost:8501/v1/models/leaf_classification_native_keras_8:predict"
IMAGE_PATH = ( "/home/bhargavpatel/Desktop/Image_Classification_tfx/Image_Classification/raw_data/test/healthy/healthy_test.0.jpg"
)
def main():
headers = {"content-type": "serving_default"}
img = cv2.imread(IMAGE_PATH, 1)
img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_AREA)
img = tf.keras.applications.mobilenet.preprocess_input(img)
image_content = img.astype("uint8").tolist()
data = json.dumps({"signature_name": "serving_default", "instances": image_content})
for _ in range(3):
response = requests.post(SERVER_URL, json=data, headers=headers)
pprint.pprint(response)
total_time = 0
num_requests = 10
for _ in range(num_requests):
response = requests.post(SERVER_URL, json=data, headers=headers)
response.raise_for_status()
total_time += response.elapsed.total_seconds()
prediction = response.json()["predictions"][0]
print("Prediction class: {}, avg latency: {} ms".format(np.argmax(prediction), (total_time * 1000) / num_requests))
if __name__ == "__main__":
main()
我使用下面提到的命令检查了是否可以访问 docker 容器
curl http://localhost:8501/v1/models/leaf_classification_native_keras_8
收到以下回复
{
"model_version_status": [
{
"version": "215",
"state": "AVAILABLE",
"status": {
"error_code": "OK",
"error_message": ""
}
}
]
}
但是当我发布预测请求时,它会给出错误 400 响应。
您能否尝试将标头更改为
headers = {"content-type": "application/json"}
以定义 JSON 数据有效负载。 json数据中的“signature_name”是可选的,可以忽略。
img_content
需要更新如下。
image_content = img.astype('float32').to_list()