【问题标题】:Unknown Error Sending Data to Google Cloud ML Custom Prediction Routine将数据发送到 Google Cloud ML 自定义预测例程时出现未知错误
【发布时间】:2019-10-29 11:01:24
【问题描述】:

我正在尝试在 AI Platform 上编写自定义 ML 预测例程,以从客户端获取文本数据,进行一些自定义预处理,将其传递到模型中,然后运行模型。我能够成功地在谷歌云上打包和部署这段代码。但是,每次我尝试从 node.js 向它发送请求时,我都会返回 data: { error: 'Prediction failed: unknown error.' },

这是我的相关自定义预测例程代码。请注意,我在客户端中将instances 设置为我的文本,然后在自定义预测例程中对其进行标记和预处理。

def __init__(self, model, session, saver, dictionary):
    self.model = model
    self.sess = session

@classmethod
def from_path(cls, model_dir):
    m = Model(learning_rate=0.1)
    session = tf.Session()
    session.run(tf.global_variables_initializer())
    session.run(tf.local_variables_initializer())
    saver = tf.train.Saver(max_to_keep=0)
    saver.restore(session, (os.path.join(model_dir, 'model.ckpt')))
    return cls(m, session)

def predict(self, instances, **kwargs):
    utterance = nltk.word_tokenize(instances)
    utterance = self.preprocess_utterance(utterance)

    preds = self.sess.run([self.model['preds'], feed_dict={'input_data': utterance)
    return preds

这是我的 Node.js 代码:

   text_string = "Hello how are you?"
   google.auth.getApplicationDefault(function (err, authClient, projectId) {
        if (err) {
            console.log('Authentication failed because of ', err);
            return;
        }
        if (authClient.createScopedRequired && authClient.createScopedRequired()) {
            var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
            authClient = authClient.createScoped(scopes);
        }
        var request = {
            name: "projects/" + projectId + "/models/classifier",
            resource: {"instances": [message_string]},

            // This is a "request-level" option
            auth: authClient
        };

        machinelearning.projects.predict(request, function (err, result) {

            console.log(result)

            if (err) {
                console.log(err);
            } else {
                console.log(result);
                res.status(200).send('Hello, world! This is the prediction: ' + JSON.stringify(result)).end();
            }
        });
    });

在这段代码中,我只是将文本发送到谷歌云模型。请求正文是: body: '{"instances":["Hello how are you?"]}',

有人知道为什么会失败吗?

如果没有,那么有人知道我该如何调试吗?一条未知的错误消息根本没有用。

编辑:

这是saved_model_cli 的输出,带有--all 选项。

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['length_input'] tensor_info:
        dtype: DT_INT32
        shape: ()
        name: Placeholder_3:0
    inputs['seqlen'] tensor_info:
        dtype: DT_INT32
        shape: (-1)
        name: Placeholder_2:0
    inputs['indicator'] tensor_info:
        dtype: DT_INT32
        shape: (-1, 2)
        name: Placeholder_1:0
    inputs['input_data'] tensor_info:
        dtype: DT_INT32
        shape: (-1, -1)
        name: Placeholder:0
    inputs['y'] tensor_info:
        dtype: DT_INT32
        shape: (-1, -1)
        name: Placeholder_4:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['preds'] tensor_info:
        dtype: DT_INT32
        shape: (-1, -1)
        name: Cast:0
  Method name is: tensorflow/serving/predict

基于此,我应该提供这个字典作为输入,但它不起作用。

{"instances": [ { "input_data": [138, 30, 66], "length_input": 1, "indicator": [[0, 0]], "seqlen": [3], "y": [138, 30, 66] } ]}

【问题讨论】:

  • 请在 savedmodel 上运行 saved_model_cli 并发布你得到的东西
  • 我不知道如何在我的模型上运行它,因为我用tf.train.write_graph(tf.get_default_graph(),...) 保存它,我收到错误Message type "tensorflow.SavedModel" has no field named "node"..。但是,我可以告诉你我的输入和输出是什么。我的输入是字段input_data, seqlen, indicator, length_input。我更改了我的实例以匹配它,但它仍然失败。 {"instances": [{"input_data": "Hello how are you doing?", "seqlen": [5], "indicator": [1], "length_input": 1}]}

标签: google-cloud-platform google-cloud-ml


【解决方案1】:

我发现了这个问题。问题不在于输入数据的格式。而是在 NLTK 中。 NLTK.word_tokenize 抛出错误,因为它没有进行标记化所需的数据。我必须将数据上传到 Google Cloud 或使用不需要任何数据文件的标记化方法来解决此问题。

我不知道为什么这个 Google Cloud 自定义预测例程软件没有告诉它的用户正在发生的错误,但是通过我的所有努力,它总是在出现问题时返回 Unknown error。如果我准确地知道错误是什么,这将很容易解决。

【讨论】:

【解决方案2】:

我认为你需要:

{instances: [
 {"input_data": "hello, how are you?"},
 {"input_data": "who is this?"}
]}

但我们可以确认是否可以查看在您的 SavedModel 文件上调用 saved_model_cli 的结果。

【讨论】:

  • 好的,我会发布并试试这个。但是,当我首先进行预处理时,为什么我需要我的实例字典具有模型输入的字典格式(在这种情况下 input_data 是模型中的输入张量,它接收文本)?我不想将文本hello, how are you? 输入到模型中,我需要先对其进行预处理。
  • 我在原帖中发布了 saved_model_cli 的输出。
  • 我在 AI Platform 中部署的模型来自 saver.save,但不是我用来生成此 saved_model_cli 输出的文件。我使用tf.saved_model.simple_save 来生成这个。
  • 我现在使用 tf.saved_model.simple_save 保存了模型并将其部署到云端。我在 Google Cloud 控制台中的原始问题末尾尝试了我粘贴的输入。我仍然收到未知错误。请注意,当我通过创建自定义预测例程类的实例然后仅将文本字符串作为“实例”传递给它时,我会得到正确的结果。我只是不明白可能出了什么问题。
猜你喜欢
  • 2020-06-25
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 2019-02-16
  • 2018-07-15
  • 1970-01-01
  • 2014-03-03
相关资源
最近更新 更多