python - 如何使用稀疏张量将 REST API 请求发送到 Tensorflow Serving 模型?
问题描述
我正在尝试使用 TF Serving 为我的 TF 模型提供服务。这是我的模型输入:
raw_feature_spec = {
'x': tf.io.VarLenFeature(tf.string),
'y': tf.io.VarLenFeature(tf.string),
'z': tf.io.FixedLenFeature([], tf.string)
}
然后我导出我的模型:
import tensorflow as tf
import tensorflow_transform as tft
tf_transform_output = tft.TFTransformOutput('saved_transform_graph_folder')
estimator = tf.keras.estimator.model_to_estimator(keras_model_path='model_folder')
estimator.export_saved_model('OUTPUT_MODEL_NAME', make_serving_input_fn(tf_transform_output))
def make_serving_input_fn(tf_transform_output):
raw_feature_spec = {
'x': tf.io.VarLenFeature(tf.string),
'y': tf.io.VarLenFeature(tf.string),
'z': tf.io.FixedLenFeature([], tf.string)
}
def serving_input_fn():
raw_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(raw_feature_spec)
raw_reatures = raw_input_fn().features
transformed_features = tf_transform_output.transform_raw_features(raw_reatures)
return tf.estimator.export.ServingInputReceiver(transformed_features, raw_reatures)
return serving_input_fn
因此,当我使用 Docker Hub 中的 TFS 映像通过此代码提供导出模型时,将 HTTP GET 请求发送到 /model/metadata 我得到:
{
"model_spec": {
"name": "newModel",
"signature_name": "",
"version": "1585581470"
},
"metadata": {
"signature_def": {
"signature_def": {
"serving_default": {
"inputs": {
"x": {
"coo_sparse": {
"values_tensor_name": "ParseExample/ParseExampleV2:3",
"indices_tensor_name": "ParseExample/ParseExampleV2:1",
"dense_shape_tensor_name": "ParseExample/ParseExampleV2:5"
},
"dtype": "DT_STRING",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "-1",
"name": ""
}
],
"unknown_rank": false
}
},
"y": {
"coo_sparse": {
"values_tensor_name": "ParseExample/ParseExampleV2:2",
"indices_tensor_name": "ParseExample/ParseExampleV2:0",
"dense_shape_tensor_name": "ParseExample/ParseExampleV2:4"
},
"dtype": "DT_STRING",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "-1",
"name": ""
}
],
"unknown_rank": false
},
"z": {
"dtype": "DT_STRING",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
}
],
"unknown_rank": false
},
"name": "ParseExample/ParseExampleV2:6"
}
},
"outputs": {
"main_output": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "20",
"name": ""
}
],
"unknown_rank": false
},
"name": "main_output/Softmax:0"
}
},
"method_name": "tensorflow/serving/predict"
}
}
}
}
但是当我向 /model:predict 发送 HTTP POST 请求时:
{
"instances":
[
{
"x": ["text","text","text","text","text","text"],
"y": ["test","test","test","test","test","test"],
"z": "str"
}
]
}
我收到一个错误
{
"error": "Tensor :0, specified in either feed_devices or fetch_devices was not found in the Graph"
}
那么我应该如何重新组织 JSON 以从 API 获得正确的响应?TF 版本:2.1,TF Serving 最新和 TF Transform - 0.21.0。
解决方案
推荐阅读
- tcp - EG getting restarted when ESB tries to fetch data from an external system over TCPIP connection for more than 59 times
- java - 需要使用正则表达式检查通配符
- php - MYSQL 两张表,每个用户在块内显示
- spring-boot - 如何将一个表中的实体链接到另一个表中?
- javascript - How to use thumbnail slick carousel in Vue.JS
- php - 在大文件上传中批量处理数据时如何避免 PHP/WordPress 内存致命错误?
- amazon-web-services - AWS RDS:虽然安全组对我的 ip 或 All 具有入站权限,但无法从我的本地访问
- c# - Assets/sucriptusu/movement/onflor.cs(23,13): error CS0120: An object reference is required for the non-static field, method, or property 'zoom.upZoom'
- python - Python: Trying to approximate 'portion_saved' in the following code
- arrays - How to unwind two arrays in Mongodb and project them distinctly?