3

I have a trained model (CNN) deployed in Google Cloud Platform, from which I'm able to get predictions using either the Python client library, or the gcloud command.

I am now trying to use the Dot Net client v1.25 (https://github.com/google/google-api-dotnet-client/tree/v1.25.0) to get predictions, but the request is failing with {"error": "Missing "instances" field in request body."} even though the JSON I'm sending is of the form:

   {"instances": [{"image":<base64ImageData>, "key":"1"}]}

I can use the library to get a list of available models using the List() method.

Code is as follows:

using System;
using System.Text;
using Google.Apis.Auth.OAuth2;
using System.IO;
using Google.Apis.Services;
using Google.Apis.CloudMachineLearningEngine.v1beta1.Data;
using Newtonsoft.Json;

namespace GoogleCloudTesting
{
    Class Program
    {
        static void Main(string[] args)
        {
            GoogleCredential credential = GoogleCredential.GetApplicationDefaultAsync().Result;

            var service = new Google.Apis.CloudMachineLearningEngine.v1beta1.CloudMachineLearningEngineService(
                new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Testing"
                }
            );

            string jsonImagesPath = @"c:\path\to\images.json"; // {"instances": [{"image":<base64imagedata>, "key":"1"}]}
            string json = File.ReadAllText(jsonImagesPath);

            var request = new GoogleCloudMlV1beta1PredictRequest
            {
                HttpBody = new GoogleApiHttpBody { Data = json }
            };

            var predictRequest = service.Projects.Predict(request, "projects/my_project/models/my_model/versions/V1");
            var result = predictRequest.Execute();
            Console.WriteLine(result.Data); // null
        }
    }
}

Any help appreciated, thanks.

Chris
  • 183
  • 1
  • 10
  • https://cloud.google.com/ml-engine/reference/rest/v1/projects/predict suggests base64 encoded data must be replaced by a JSON object with a single attribute named 'b64'. So perhaps you need to use: { "instances": [{"image": { "b64": "" }, "key": "1" } ] } – Chris Apr 26 '17 at 16:22
  • I see a subtle difference compared to the Python version (https://cloud.google.com/ml-engine/docs/how-tos/online-predict#requesting_predictions), but I don't know the .net library well enough to fix it with certainty. The difference is that HttpBody is not used in Python. Instead, something more like service.Projects.Predict(body=json, name="projects/my_project/models/my_model/versions/V1"). Again, I can't guarantee exactly that will work, but I'm pretty sure the problem is the HttpBody. – rhaertel80 Apr 27 '17 at 11:13
  • Please follow the instructions here: https://googlecloudplatform.github.io/google-cloud-dotnet/docs/faq.html#how-can-i-trace-requests-and-responses-in-rest-based-apis to dump the HTTP request headers and body. Then, post the dump here. – Jeffrey Rennie May 01 '17 at 22:02
  • @JeffreyRennie What looks to be happening is that the request object GoogleCloudMlV1beta1PredictRequest is being serialized, resulting in the request body: {"httpBody": {"data": ...} instead of just the I specify. There is no other way to send a request apparently. – Chris May 10 '17 at 08:18
  • @Chris, I have the same problem... the property named "Data" is not a part of JSON structure of API Explorer... Do you have any solution? – Ibere Spadoto Jun 26 '17 at 18:46
  • @IbereSpadoto I had to change the method signature of `PredictRequest` from `public PredictRequest(Google.Apis.Services.IClientService service, Google.Apis.CloudMachineLearningEngine.v1beta1.Data.GoogleCloudMlV1beta1PredictRequest body, string name) : base(service)` to `public PredictRequest(Google.Apis.Services.IClientService service, object body, string name) : base(service)` and also change the `Body` property in the `PredictRequest` class to type `object` in the file google.apis.cloudmachinelearningengine.v1beta1.cs. – Chris Jun 28 '17 at 09:26
  • So I've downloaded the sorce code too... I think about this way but, I can't run the source, I got a lot of errors in Visual Studio... Could you send this changes to me, please? – Ibere Spadoto Jun 29 '17 at 12:12

1 Answers1

0

This is a known issue.

Two work-arounds are shown in github issue#1068

Chris
  • 1,685
  • 10
  • 15