Google Cloud Machine学习API Dot Net客户端 - 预测请求失败

时间:2017-04-26 15:29:30

标签: google-api google-api-dotnet-client google-cloud-ml google-cloud-ml-engine

我在Google Cloud Platform中部署了经过培训的模型(CNN),我可以使用Python客户端库或gcloud命令从中获取预测。

我现在正在尝试使用Dot Net客户端v1.25(https://github.com/google/google-api-dotnet-client/tree/v1.25.0)来获取预测,但即使我发送的JSON格式为{"error": "Missing "instances" field in request body."},请求也会失败:

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

我可以使用该库来使用List()方法获取可用模型列表。

代码如下:

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

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

这是一个已知问题。

github issue#1068

中显示了两种解决方法