Google ML 引擎在线预测请求的响应没有预测结果
No prediction results in response from Google ML Engine online prediction request
我已经在 Google 机器学习引擎上设置了模型和 运行,我可以在我的个人电脑上成功地使用它。现在我正在尝试设置 Firebase Cloud Functions 来调用我的 ML Engine 模型。我真的很接近(我认为)但我现在碰壁了。
ML 引擎正在接收我的预测请求,但我没有在回调函数中获得预测结果。
results.predictions
始终为空,当我将 results
字符串化时,我看到了这个:
Stackdriver 显示请求已成功发送:
您还可以在 ML 引擎模型仪表板上看到,Last use time
确实 反映了我调用云函数的时间。
但我不明白为什么我没有从 ML Engine 返回预测结果。
这是我一直遵循的设置云功能的指南:https://github.com/GoogleCloudPlatform/ml-functions-helpdesk/blob/master/functions/index.js
这是我目前的云函数,它通过 data.imageData
传递了 base64 编码的图像数据,我已经通过复制输出和 运行 预测来验证它是一个有效的图像编码从我的电脑请求。
const functions = require('firebase-functions');
const {google} = require('googleapis');
exports.analyzeDetection = functions.https.onCall((data, context) => {
if (data.imageData) {
// Auth
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
console.error("getApplicationDefault err " + err);
return {
prediction: "error"
}
}
//[START ml_engine_auth]
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
// https://developers.google.com/identity/protocols/googlescopes#mlv1
authClient = authClient.createScoped(['https://www.googleapis.com/auth/cloud-platform']);
}
//Create authenticated ml engine client
var ml = google.ml({
version: 'v1',
auth: authClient
});
//[END ml_engine_auth]
// Prediction
ml.projects.predict({
name: `projects/my_project_name/models/my_model_name`,
resource: {
instances: [{"b64": data.imageData}]
}
}, (err, result) => {
// The problem is that result is always null
if (err){
console.error('ERROR ', err);
return {
prediction: "error"
}
}
return {
prediction: "add this after the prediction requests are working"//result.predictions[0].predicted
}
});
});
}
});
注意:我混淆了 my_project_name
和 my_model_name
,这不是他们真正说的。
我也尝试了多种不同的预测参数变体,例如:
name: 'projects/my_project_name/models/my_model_name',
resource: {
instances: [{"image_bytes": {"b64": data.imageData}, "key": "0"}]
}
和
name: 'projects/my_project_name/models/my_model_name',
resource: {
name: 'projects/my_project_name/models/my_model_name',
instances: [{"b64": data.imageData}]
}
但他们的表现似乎都一样。
关于为什么我没有从请求中得到预测结果的任何想法?谢谢。
经过更多的挖掘、试验、错误等。我确定预测结果存储在 results.data
而不是 results
。例如,我可以在 result.data.predictions[0].scores
中查看我的分数,在 result.data.predictions[0].prediction
中查看预测。看来我最初是被 Cloud Functions 控制台的输出限制所欺骗,其中 data
属性 被截断了,所以我看不到预测结果。
我已经在 Google 机器学习引擎上设置了模型和 运行,我可以在我的个人电脑上成功地使用它。现在我正在尝试设置 Firebase Cloud Functions 来调用我的 ML Engine 模型。我真的很接近(我认为)但我现在碰壁了。
ML 引擎正在接收我的预测请求,但我没有在回调函数中获得预测结果。
results.predictions
始终为空,当我将 results
字符串化时,我看到了这个:
Stackdriver 显示请求已成功发送:
您还可以在 ML 引擎模型仪表板上看到,Last use time
确实 反映了我调用云函数的时间。
但我不明白为什么我没有从 ML Engine 返回预测结果。
这是我一直遵循的设置云功能的指南:https://github.com/GoogleCloudPlatform/ml-functions-helpdesk/blob/master/functions/index.js
这是我目前的云函数,它通过 data.imageData
传递了 base64 编码的图像数据,我已经通过复制输出和 运行 预测来验证它是一个有效的图像编码从我的电脑请求。
const functions = require('firebase-functions');
const {google} = require('googleapis');
exports.analyzeDetection = functions.https.onCall((data, context) => {
if (data.imageData) {
// Auth
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
console.error("getApplicationDefault err " + err);
return {
prediction: "error"
}
}
//[START ml_engine_auth]
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
// https://developers.google.com/identity/protocols/googlescopes#mlv1
authClient = authClient.createScoped(['https://www.googleapis.com/auth/cloud-platform']);
}
//Create authenticated ml engine client
var ml = google.ml({
version: 'v1',
auth: authClient
});
//[END ml_engine_auth]
// Prediction
ml.projects.predict({
name: `projects/my_project_name/models/my_model_name`,
resource: {
instances: [{"b64": data.imageData}]
}
}, (err, result) => {
// The problem is that result is always null
if (err){
console.error('ERROR ', err);
return {
prediction: "error"
}
}
return {
prediction: "add this after the prediction requests are working"//result.predictions[0].predicted
}
});
});
}
});
注意:我混淆了 my_project_name
和 my_model_name
,这不是他们真正说的。
我也尝试了多种不同的预测参数变体,例如:
name: 'projects/my_project_name/models/my_model_name',
resource: {
instances: [{"image_bytes": {"b64": data.imageData}, "key": "0"}]
}
和
name: 'projects/my_project_name/models/my_model_name',
resource: {
name: 'projects/my_project_name/models/my_model_name',
instances: [{"b64": data.imageData}]
}
但他们的表现似乎都一样。
关于为什么我没有从请求中得到预测结果的任何想法?谢谢。
经过更多的挖掘、试验、错误等。我确定预测结果存储在 results.data
而不是 results
。例如,我可以在 result.data.predictions[0].scores
中查看我的分数,在 result.data.predictions[0].prediction
中查看预测。看来我最初是被 Cloud Functions 控制台的输出限制所欺骗,其中 data
属性 被截断了,所以我看不到预测结果。