c++ Tensorflow 2,cpp中的服务预测问题

9jyewag0  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(100)

我尝试使用cpp中的最终saved_model.pb发送text as input并接收predicted class id and score。但我得到了这个错误:
W tensorflow/core/framework/op_kernel.cc:1767] OP_REQUIRES failed at strided_slice_op.cc:108 : Invalid argument: slice index 0 of dimension 0 out of bounds.
我的模型是用Python训练的。现在我在TensorFlow 2.5中使用它。
使用saved_model_model的更多信息:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['classification']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: Placeholder:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['classes'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 74)
        name: head/Tile:0
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 74)
        name: head/predictions/probabilities:0
  Method name is: tensorflow/serving/classify

signature_def['predict']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['content'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: Placeholder:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['all_class_ids'] tensor_info:
        dtype: DT_INT32
        shape: (-1, 74)
        name: head/predictions/Tile:0
    outputs['all_classes'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 74)
        name: head/predictions/Tile_1:0
    outputs['class_ids'] tensor_info:
        dtype: DT_INT64
        shape: (-1, 1)
        name: head/predictions/ExpandDims:0
    outputs['classes'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: head/predictions/hash_table_Lookup/LookupTableFindV2:0
    outputs['logits'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 74)
        name: add:0
    outputs['probabilities'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 74)
        name: head/predictions/probabilities:0
  Method name is: tensorflow/serving/predict

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: Placeholder:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['classes'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 74)
        name: head/Tile:0
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 74)
        name: head/predictions/probabilities:0
  Method name is: tensorflow/serving/classify

我的源代码:

// run the model
std::string test(const char *modelPath, const char *text) {
    tensorflow::SavedModelBundle model;
    tensorflow::Status status = tensorflow::LoadSavedModel(
            tensorflow::SessionOptions(),
            tensorflow::RunOptions(),
            modelPath,
            {"serve"},
            &model);

    TF_CHECK_OK(status);

    // Provide input data.
    tensorflow::Tensor tensor(tensorflow::DT_STRING, tensorflow::TensorShape());
    tensor.scalar<tensorflow::tstring>()() = tensorflow::tstring(text);

    // Link the data with some tags so tensorflow know where to put those data entries.
    std::vector<std::pair<std::string, tensorflow::Tensor>> feedInputs = {{"Placeholder:0", tensor}};
    std::vector<std::string> fetches = {"head/Tile:0", "head/predictions/probabilities:0"};

    // We need to store the results somewhere.
    std::vector<tensorflow::Tensor> outputs;

    // Let's run the model...
    status = model.GetSession()->Run(feedInputs, fetches, {}, &outputs);
    TF_CHECK_OK(status);

    for (const auto& output : outputs) {
        // TODO::
    }

    return "";
}

我试过在cpp中更改输入和输出名称,但似乎正确的名称是源代码中使用的名称。我对tensorflow cpp API不是很熟悉。谢谢.

sxpgvts3

sxpgvts31#

问题在于

tensorflow::Tensor tensor(tensorflow::DT_STRING, tensorflow::TensorShape());

应当

tensorflow::Tensor tensor(tensorflow::DT_STRING, tensorflow::TensorShape({1}));

错误告诉你Invalid argument: slice index 0 of dimension 0 out of bounds.看起来Tensor没有1个字符串的额外维度(1-D)。

相关问题