No prediction results in response from Google ML Engine online prediction request


No prediction results in response from Google ML Engine online prediction request



I've got a model set up and running on Google ML Engine, which I can use successfully from my personal computer. Now I'm trying to set up Firebase Cloud Functions to make calls to my ML Engine model. I'm really close (I think) but I'm hitting a wall now.



My prediction requests are being received by ML Engine, but I am not getting the prediction results in the callback function.



results.predictions is always null, and when I stringified results I am seeing this:


results.predictions


results



Firebase Cloud Functions console



and Stackdriver shows the request being successfully sent:



Stackdriver logs



You can also see on the ML Engine Models dashboard, that the Last use time does reflect when I am calling the cloud function.


Last use time



My model is being called



But I can't figure out why I'm not getting the prediction results back from ML Engine.



This is what I have been following as a guide for setting up my Cloud Functions: https://github.com/GoogleCloudPlatform/ml-functions-helpdesk/blob/master/functions/index.js



This is my cloud function currently, it is passed the base64 encoded image data through data.imageData, and I have verified that it is a valid image encoding by copying the output and running the prediction request from my PC.


data.imageData


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
}
});
});
}
});



Note: I have obfuscated my_project_name and my_model_name, that is not what they actually say.


my_project_name


my_model_name



I have also tried multiple different variations of prediction parameters, such as:


name: 'projects/my_project_name/models/my_model_name',
resource: {
instances: [{"image_bytes": {"b64": data.imageData}, "key": "0"}]
}



and


name: 'projects/my_project_name/models/my_model_name',
resource: {
name: 'projects/my_project_name/models/my_model_name',
instances: [{"b64": data.imageData}]
}



but they all seem to perform the same.



Any ideas on why I'm not getting the prediction results back from the request? Thanks.




1 Answer
1



After some more digging, trial, error, etc.. I've determined that the prediction results are stored in results.data and not in results. For example, I can view my scores in result.data.predictions[0].scores and the prediction in result.data.predictions[0].prediction. It seems that I was originally tricked by the output limit of the Cloud Functions console, where the data property was being truncated so I couldn't see the prediction results.


results.data


results


result.data.predictions[0].scores


result.data.predictions[0].prediction


data






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”