Tensorflow服务:在服务时改变张量尺寸

问题描述 投票:1回答:1

Problem

我是Tensorflow的新人(事实上这是我服务的第一个模特!)所以如果答案显而易见,我道歉!

我正在使用这个image在docker上托管Tensorflow模型。问题是每次我尝试发送数据时服务器都会发送以下错误,以便服务模型可以进行预测。

W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 4. But input(1) is a vector of size 3

Background info

  1. 使用saved_model_cli,模型可以显示为
signature_def['predict']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['init_state'] tensor_info:
        dtype: DT_FLOAT
        shape: (2, 2, -1, 136)
        name: policy_estimator/lstm/Placeholder:0
    inputs['state'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 136)
        name: policy_estimator/state:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['action_probs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 260)
        name: policy_estimator/Softmax:0
    outputs['final_state'] tensor_info:
        dtype: DT_FLOAT
        shape: (2, 2, -1, 136)
        name: policy_estimator/packed:0
  Method name is: tensorflow/serving/predict
  1. 该模型在托管之前正常运行,因为我能够使用预先存在的数据训练模型。
  2. 该错误似乎与状态维度相对应。当我将数据的维度从2(即(-1,136))更改为3(使用np.expand_dims)时,错误消息将更改为
W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 5. But input(1) is a vector of size 3

当我将尺寸更改为4时,它会变为

W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 6. But input(1) is a vector of size 3

但是,当我将其设为1维时,错误消息仍为

W external/org_tensorflow/tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 4. But input(1) is a vector of size 3
  1. 服务器正常运行
{
 "model_version_status": [
  {
   "version": "1",
   "state": "AVAILABLE",
   "status": {
    "error_code": "OK",
    "error_message": ""
   }
  }
 ]
}

当我运行curl http://model:8501/v1/models/saved_model时,http://model:8501/v1/models/saved_model是托管模型的地方。

  1. 我使用python来请求模型
payload = [{"init_state":np.reshape(initial_state, (2,2,-1,136)).tolist(), "state": np.reshape(points, (-1, 136)).tolist()}]
headers = {"content-type": "application/json"}
data = json.dumps({"signature_name": "predict", "instances":payload})
r = requests.post('http://model:8501/v1/models/saved_model:predict', data=data, headers=headers)

关注this documentation。其中r是响应。在这种情况下,此r返回400的响应。

Personal conclusion

我可以从中得出的唯一结论是,模型在服务时可能会发生变化。然而,这只是猜测,因为我被困住了,并且不确定下一步。

我不再那么专业了,如果我遗漏了一些明显的东西,我道歉!请幽默我如果缺少任何信息,请通知我,因为我会尽力澄清!

tensorflow tensorflow-serving
1个回答
0
投票

试试这个:

import json
.
.
.
payload = {"init_state":np.reshape(initial_state, (2,2,-1,136)).tolist(),"state": np.reshape(points, (-1, 136)).tolist()}
headers = {"content-type": "application/json"}
data = json.dumps({"signature_name": "predict", "inputs":payload})
# add this
data = json.loads(data) # apparently json.dumps returns a json string: '{}' not a json object: {} 

r = requests.post('http://model:8501/v1/models/saved_model:predict', json=data, headers=headers)
© www.soinside.com 2019 - 2024. All rights reserved.